Detailed description of springcloud Feign's Hystrix support, springcloudhystrix

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.