SpringCloud视频教程:
https://ke.qq.com/course/2805647?tuin=a3e3fb1&from_uin=171851697&from=1000201007
个人博客纯净版
http://www.51ufo.cn/%E5%BE%AE%E6%9C%8D%E5%8A%A1/%E5%88%86%E5%B8%83%E5%BC%8F/2020/07/10/SpringCloud%E5%85%A5%E9%97%A89-fegin%E4%BD%BF%E7%94%A8%E6%96%AD%E8%B7%AF%E5%99%A8-Hystrix.html
本文代码git地址 https://gitee.com/xmingtx/springcloud-lesson.git
Feign是自带断路器的,在D版本的Spring Cloud之后,它没有默认打开。需要在配置文件中配置打开它,在application.yml配置文件添加以下配置:
feign:
hystrix:
enabled: true
基于consumer-feign工程进行改造,只需要在HelloFeginClient接口的FeignClient注解中加上fallback的指定类就行了:
@FeignClient(value = "eureka-client-provider", fallback = HelloFeginFallback.class)
public interface HelloFeginClient {
@GetMapping("/hello")
String hello(@RequestParam(value = "name") String name);
}
HelloFeginFallback需要实现HelloFeginClient 接口,并注入到Ioc容器中,代码如下:
@Component
public class HelloFeginFallback implements HelloFeginClient {
@Override
public String hello(String name) {
return "hello," + name + ",sorry,error!";
}
}
启动四consumer-feign工程,浏览器打开http://localhost:8091/hello?name=xiaoming,注意此时eureka-client-provider工程没有启动,网页显示:
通过fegin客户端调用结果为:hello,xiaoming,sorry,error!
启动eureka-client-provider服务,再次访问,浏览器显示:
通过fegin客户端调用结果为:hello, xiaoming, 我是服务提供者:端口为:8080
这证明断路器起到作用了。