Detailed description of springcloud Feign's Hystrix support, springcloudhystrix
This article introduces springcloud Feign's Hystrix support and shares it with you, as follows:
1. Add fallback of Hystrix to Feign client
@ FeignClient (name = "springboot-h2", fallback = HystrixClientFallback. class) // specify the fallback public interface UserFeignClient of the breaker in the fallback attribute {// @ GetMapping ("/user/{id }") @ RequestMapping (value = "/user/{id}", method = RequestMethod. GET) User findById (@ PathVariable ("id") Long id); @ RequestMapping (value = "/users", method = RequestMethod. GET) List <User> findAll (); @ RequestMapping (value = "/post/user", method = RequestMethod. POST) User save (@ RequestBody User user );}
Ii. Compile the HystrixClientFallback class
@ Component // Add public class HystrixClientFallback implements UserFeignClient in spring bean {@ Override public User findById (Long id) {User u = new User (); u. setName ("temporary name"); u. setUsername ("anonymous"); return u ;}@ Override public List <User> findAll () {return null ;}@ Override public User save (User user) {return null ;}}
Iii. Added Hystrix support
@EnableCircuitBreaker
Iv. Test
If the underlying dependent service is not started, the service is started directly, and then tested, the result in the browser is:
{"Id": null, "username": "anonymous", "name": "temporary name", "age": null, "balance": null}
The findById method in the HystrixClientFallback class is not reported as expected.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.