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
這證明斷路器起到作用了。