Spring Cloud Building MicroServices Architecture (iii) Circuit breakers

Source: Internet
Author: User

In a distributed architecture, the role of the circuit breaker pattern is similar, when a service unit fails (similar to a short-circuit with electrical appliances), through the fault monitoring of the circuit breaker (similar to a fuse fuse), to the caller returned an error response, rather than a long wait. This does not cause the thread to fail to be released for a long time because of the invocation of the fault service, thus avoiding the spread of the fault in the distributed system.

Netflix Hystrix

The hystrix is used in spring cloud to realize the function of the circuit breaker. Hystrix is one of Netflix's open source MicroServices framework packages designed to provide greater fault tolerance for latency and failures by controlling the nodes that access remote systems, services, and third-party libraries. The Hystrix features thread and signal isolation with fallback mechanism and breaker functionality, request caching and request packaging, and monitoring and configuration.

Let's take a look at how to use Hystrix.

Preparatory work

Before starting to join the circuit breaker, we will first build two micro-services as the basis for the following operations, mainly using the following several projects

    • Eureka-server Project: Service registration Center, Port 1111
    • Compute-service Project: Service Unit, Port 2222
    • Eureka-ribbon: Service units implemented through the Ribbon, dependent on compute-service services, Port 3333
    • Eureka-feign: Service unit implemented through feign, dependent on Compute-service service, Port 3333
    • Introduction of Hystrix in Ribbon
        • Start Eureka-server, Compute-service, Eureka-ribbon project in turn
        • Access http://localhost:1111/to see the status of the registry
        • Access Http://localhost:3333/add, call Eureka-ribbon's service, the service will go to invoke Compute-service service, calculate the value of 10+20, page display 30
        • Close the Compute-service service, Access Http://localhost:3333/add, we get the following error message
       for  This  21:16:59 CST There is anunexpected error (type=internal Server error, status=500). I for "http://COMPUTE-SERVICE/add?a=10&b=20": Connection refused:connect; Nested exception is java.net.ConnectException:Connection refused:connect

        • pom.xmlRelies on Hystrix dependency in the import
      <dependency><groupId>org.springframework.cloud</groupId><artifactId> Spring-cloud-starter-hystrix</artifactid></dependency>

        • RibbonApplicationuse @EnableCircuitBreaker Annotations to turn on circuit breaker functionality in the main class of Eureka-ribbon:
      @SpringBootApplication @enablediscoveryclient@enablecircuitbreaker  Public class ribbonapplication {@Bean @loadbalancedresttemplate resttemplate () {returnnew   Public Static void Main (string[] args) {Springapplication.run (ribbonapplication. class , args);} }

        • Transform the original service consumption method, add a ComputeService class, add annotations to the function that uses the Ribbon consumer service @HystrixCommand to specify the callback method.
      @Service  Public class  = "Addservicefallback") public String AddService () {return Resttemplate.getforentity ("Http://COMPUTE-SERVICE/add?a=10&b=20", String.  Class public String addservicefallback () {return "error";}}

        • The controller that provides the rest interface changes to call Computeservice's AddService
      @RestController  Public class Consumercontroller {@Autowiredprivate= "/add", method = requestmethod.get)  Public String Add () {return  computeservice.addservice ();}}

        • Verify the callback for the circuit breaker
          • Start Eureka-server, Compute-service, Eureka-ribbon project in turn
          • Access http://localhost:1111/to see the status of the registry
          • Visit Http://localhost:3333/add, page display: 30
          • After you close the Compute-service service and then visit Http://localhost:3333/add, the page displays: Error

      For more information on the use of Hystrix, refer to

      Feign using Hystrix

      Note that this is said to be "use", there is no mistake, we do not need to introduce hystix,feign in the Feigh project has relied on hystrix, we can do before any modification, try the following your operation:

        • Start Eureka-server, Compute-service, Eureka-feign project in turn
        • Access http://localhost:1111/to see the status of the registry
        • Access Http://localhost:3333/add, call Eureka-feign's service, the service will go to invoke Compute-service service, calculate the value of 10+20, page display 30
        • Close the Compute-service service, Access Http://localhost:3333/add, we get the following error message
       for  This  22:10:05 CST There is anunexpected error (type=internal Server error, status=500). Add timed -out and no fallback available.

      If you are careful enough, you will find that the error in the Ribbon is different, see add timed-out and no fallback available this sentence, perhaps you have guessed what, see our console, you can see the error message from hystrix-core-1.5.2.jar , so in this project, we have to learn how to use feign integrated hystrix.

        • Use @FeignClient the Fallback property in annotations to specify the callback class
      @FeignClient (value = "Compute-service", fallback = Computeclienthystrix.  Class)publicinterface= requestmethod.get, value = "/add"= "a") Integer A, @RequestParam (value = "B") integer b); }

        • Create the callback class ComputeClientHystrix , implemented @FeignClient by the interface, at this time the implementation of the method is the corresponding @FeignClient interface map of the fallback function.
      @Component  Public class Implements computeclient {@Override Public integer Add (@RequestParam (value = "a") integer A, @RequestParam (value = "B") Integer b) {return -9999;}}

        • Then verify with the previous method that the page returns 9999 if the Compute-service service is unavailable.
        • For more information on how to use feign, refer to: Feign

Spring Cloud Building MicroServices Architecture (iii) Circuit breakers

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.