SpringCloud第七篇 Hystrix(断路器)-feign

1.新建一个项目 hystrix-feign

2.启动eureka和两个feign client

3.pom.xml


    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0
    
        com.star.guo
        stage2
        0.0.1-SNAPSHOT
        
    

    
    hystrix-feign
    
    jar
    hystrix-feign
    Demo project for Spring Boot

    
        1.8
        Greenwich.M3
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        

        
            org.springframework.cloud
            spring-cloud-starter-netflix-hystrix
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            

        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            

        

    

    
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/milestone
            
                false
            

        

    



4.添加注解

@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrix

5.添加和修改配置文件

    serviceUrl:
      defaultZone: http://localhost:8000/eureka/
server:
  port: 8006
spring:
  application:
    name: hystrix-feign
feign:
  hystrix:
    enabled: true

6.新建一个Service 接口,一个fallback实现类和一个controlller

@FeignClient(value = "EUREKA-CLIENT",fallback=FeignHystrixFallbackImpl.class)
public interface FeignService {

    /**
     * hello方法
     * 
     * 本例子未使用其他的路径,如果使用的有配置就行如@GetMapping(value = "/hello")
     * 
     * @param user 用户名称
     * @return String
     */
    @GetMapping
    String hello(@RequestParam(value = "user") String user);
}
 

@Service
public class FeignHystrixFallbackImpl implements FeignService{

    @Override
    public String hello(String user) {
        // TODO Auto-generated method stub
        return "user 传入"+user+"时发生了异常 By Feign";
    }

}

 

/**
 * 消息Controller,使用feign测试集群效果
 * 
 * @author Star.Guo
 *
 */
@RestController
public class MessageController {

    @Autowired
    FeignService feignService;

    /**
     * Hello方法
     * 
     * @param user 用户名
     * @return String
     */
    @GetMapping
    public String hello(String user) {
        return feignService.hello(user);
    }

}

7.启动访问和测试

http://localhost:8006/?user=star

刷新切换正常,停掉一个之后出现下面内容则正常

user 传入star时发生了异常 By Feign

 

你可能感兴趣的:(----SpringBoot,----SpringCloud)