feign整合hystrix:
生活随笔
收集整理的這篇文章主要介紹了
feign整合hystrix:
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
feign 默認是支持hystrix的, 但是在Spring - cloud Dalston 版本之后就默認關閉了, 因為不一定業務需求要用的到,
所以現在要使用首先得打開他,在yml文件加上如下配置:
feign:hystrix:enabled: true加上配置之后降級方法怎么寫呢?
@FeignClient(value = "SERVER-POWER",fallback = PowerServiceFallBack.class) public interface PowerServiceClient {@RequestMapping("/power.do")public Object power(@RequestParam("name") String name);}在feign客戶端的注解上 有個屬性叫fallback 然后指向一個類
PowerServiceFallBack 類:
@Component public class PowerServiceFallBack implements PowerServiceClient {@Overridepublic Object power(String name) {return R.error("測試降級");} }這樣子,方法降級就寫好了
當然 可能你有這種需求, 需要拿到具體的錯誤信息, 那么可以這樣寫:
@Component public class PowerServiceClientFallBackFactory implements FallbackFactory<PowerServiceClient> {@Overridepublic PowerServiceClient create(Throwable throwable) {return new PowerServiceClient() {@Overridepublic Object power(String name) {String message = throwable.getMessage();return R.error("feign降級");}};} }客戶端指定一個fallbackFactory就好了
@FeignClient(value = "SERVER-POWER",fallbackFactory = PowerServiceClientFallBackFactory.class) public interface PowerServiceClient {@RequestMapping("/power.do")public Object power(@RequestParam("name") String name);}這個message 就是拿到的錯誤信息
至此, 就完成了feign與hystrix的整合
?
總結
以上是生活随笔為你收集整理的feign整合hystrix:的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 熔断,限流
- 下一篇: hystrix相关配置