详解springcloud Feign的Hystrix支持
本文介绍了springcloudFeign的Hystrix支持,分享给大家,具体如下:
一、Feignclient中加入Hystrix的fallback
@FeignClient(name="springboot-h2",fallback=HystrixClientFallback.class)//在fallback属性中指定断路器的fallback publicinterfaceUserFeignClient{ //@GetMapping("/user/{id}") @RequestMapping(value="/user/{id}",method=RequestMethod.GET) UserfindById(@PathVariable("id")Longid); @RequestMapping(value="/users",method=RequestMethod.GET) ListfindAll(); @RequestMapping(value="/post/user",method=RequestMethod.POST) Usersave(@RequestBodyUseruser); }
二、编写HystrixClientFallback类
@Component//加入springbean中 publicclassHystrixClientFallbackimplementsUserFeignClient{ @Override publicUserfindById(Longid){ Useru=newUser(); u.setName("临时名"); u.setUsername("匿名"); returnu; } @Override publicListfindAll(){ returnnull; } @Override publicUsersave(Useruser){ returnnull; } }
三、加入Hystrix支持
@EnableCircuitBreaker
四、测试
不启动底层依赖的服务,直接启动服务,然后测试,发现浏览器中的结果为:
{"id":null,"username":"匿名","name":"临时名","age":null,"balance":null}
并没有像想象中的那样报异常,而是进入了HystrixClientFallback类中的findById方法中。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。