This article introduces the Hystrix support of springcloud Feign and shares it with you. The details are as follows:
1. Add Hystrix's fallback to Feign client
@FeignClient(name="springboot-h2", fallback=HystrixClientFallback.class) //Specify the fallback public interface of the circuit breaker in the fallback property UserFeignClient { // @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); }2. Write the HystrixClientFallback class
@Component // Join the spring bean public class HystrixClientFallback implements UserFeignClient{ @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; } }3. Join Hystrix support
@EnableCircuitBreaker
4. Test
If you do not start the service that is dependent on the underlying layer, start the service directly, and then test it, you will find that the result in the browser is:
{"id":null,"username":"anonymous","name":"temporary name","age":null,"balance":null}
Instead of reporting an exception as imagined, it entered the findById method in the HystrixClientFallback class.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.