在eureka-ribbon-client工程的基础上进行修改
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency>
@EnableHystrix
@Service public class RibbonService { @Autowired RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "hiError") public String hi(String name) { return restTemplate.getForObject("http://eureka-client/hi?name="+name,String.class); } public String hiError(String name) { return "hi,"+name+",sorry,error!"; } }
在浏览器上输入地址 http://localhost:8765/hi,如果停止eureka-client服务,则显示
由于feign起步依赖已经引入了熔断器,所以在feign中使用hystrix熔断器不需要引用任何依赖
修改配置文件 application.yml
feign: hystrix: enabled: true
修改EurekaClientFeign类,添加fallback = HiHystrix.class,如图:
添加HiHystrix类,并继承sayHiFromClientEureka接口
@Component public class HiHystrix implements EurekaClientFeign { @Override public String sayHiFromClientEureka(String name) { return "hi,"+name+",sorry,error!"; } }
在浏览器上输入地址 http://localhost:8766/hi,如果停止eureka-client服务,则显示
原文:https://www.cnblogs.com/tanouou/p/12324318.html