当前位置:   article > 正文

springcloud之Hystrix初识篇—结合Feign获取请求异常_feignhystrix 捕获异常

feignhystrix 捕获异常

1、创建一个fallbackfactory(捕获异常)类

  1. /**
  2. * FallbackFactory<T>:FeignTest1Service就是用于feign请求的接口
  3. * Created by py
  4. * 2020/3/26
  5. */
  6. @Component
  7. public class FeignTest1ServiceFallBackFactory implements FallbackFactory<FeignTest1Service> {
  8. @Override
  9. public FeignTest1Service create(Throwable throwable) {
  10. return new FeignTest1Service() {
  11. @Override
  12. public String testFeign(Map<String, String> map) {
  13. return "熔断了:{"+map.toString()+"},throwable:{"+throwable+"}";
  14. }
  15. };
  16. }
  17. }

2、feign调用接口配置fallback捕获异常参数

  1. /**
  2. * 此处使用的参数是fallbackFactory,不再是fallback
  3. **/
  4. @FeignClient(name = "test1",path ="eureka-clinet1",fallbackFactory = FeignTest1ServiceFallBackFactory.class,configuration = FeignAuthConfiguration.class)
  5. public interface FeignTest1Service {
  6. @RequestMapping(value = "/ribbonTest",method = RequestMethod.POST)
  7. public String testFeign(@RequestBody Map<String,String> map);
  8. }

3、请求接口

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/84622
推荐阅读
相关标签
  

闽ICP备14008679号