Springcloud fuses the specific use of Hystrix, springcloudhystrix

Source: Internet
Author: User

Springcloud fuses the specific use of Hystrix, springcloudhystrix

Speaking of springcloud's overall lockdown reminds me of the lockdown in the stock market last year, and many painful understandings. The impact of the random lockdown on the entire system is catastrophic. Well, let's talk about the future.

Fuses

Avalanche effect

In a microservice architecture, multiple service layers are usually called. Faults of Basic Services may lead to cascade failures, resulting in unavailability of the entire system, this phenomenon is called the service avalanche effect. The service avalanche effect is a process in which "service consumers" are unavailable due to the unavailability of "service providers" and the unavailability is gradually amplified.

As shown in the following figure: A acts as the service provider, B is the service consumer of A, and C and D are the service consumers of B. A unavailability causes B unavailability, And the avalanche effect is formed when the unavailability is magnified to C and D like A snowball.

Circuit Breaker)

The principle of the fuse is very simple, just like the power overload protector. It can implement fast failure. If it detects many similar errors within a period of time, it will force multiple future calls to fail quickly and no longer access the remote server, this prevents the application from constantly trying to execute operations that may fail, so that the application continues to execute without waiting for correction errors, or wasting the CPU time to wait for a long timeout. The fuse can also enable the application to diagnose whether the error has been corrected. If so, the application will attempt to call the operation again.

The fuse mode is like a proxy for operations that are prone to errors. This type of proxy can record the number of recent call errors, and then decide to allow the operation to continue, or immediately return an error.

The logic of switch conversion between fuses is as follows:

Fuses are the last line of defense to protect the high availability of services.

Hystrix features

1. Circuit Breaker Mechanism

The circuit breaker is easy to understand. When Hystrix Command requests that the number of backend service failures exceeds a certain proportion (50% by default), the circuit breaker will switch to the Open state (Open ). at this time, all requests will directly fail and will not be sent to the backend service. after the circuit breaker remains OPEN for a period of time (5 seconds by default), it automatically switches to the semi-OPEN state (HALF-OPEN ). the returned status of the next request is determined. If the request succeeds, the circuit breaker switches back to the CLOSED-circuit state (CLOSED). Otherwise, the circuit breaker switches back to the OPEN state (OPEN ). hystrix circuit breaker is like a fuse in our home circuit. Once the backend service is unavailable, the circuit breaker will directly cut off the request chain to avoid the impact of sending a large number of invalid requests on system throughput, the circuit breaker has the ability to detect and restore itself.

2. Fallback

Fallback is equivalent to a downgrade operation. for query operations, we can implement a fallback method. When an exception occurs in a request to the backend service, we can use the value returned by the fallback method. the Return Value of the fallback method is generally the default value set or from the cache.

3. Resource isolation

In Hystrix, the thread pool is used to isolate resources. generally, multiple thread pools are divided based on the called remote service. for example, call the Command of the product service into the thread pool, call the Command of the Account Service into the B thread pool. the main advantage of this is that the runtime environment is isolated. in this way, even if there is a bug in the code that calls the service or the thread pool in which you are located is exhausted due to other reasons, other services in the system will not be affected. however, maintaining multiple thread pools brings additional performance overhead to the system. if you have strict performance requirements and are sure that the client code that calls the service will not go wrong, you can use Semaphores to isolate resources.

Feign Hystrix

Because the circuit breaking only applies to the service call end, we only need to modify the spring-cloud-consumer project code according to the sample code in the previous article. Because Feign is dependent on Hystrix, you do not need to make any changes to the maven configuration.

1. Configuration File

Add this entry in application. properties:

feign.hystrix.enabled=true

2. Create a callback class

Create a HelloRemoteHystrix class to inherit from the HelloRemote method to implement callback

@Componentpublic class HelloRemoteHystrix implements HelloRemote{  @Override  public String hello(@RequestParam(value = "name") String name) {    return "hello" +name+", this messge send failed ";  }}

3. Add the fallback attribute

Add the specified fallback class to the HelloRemote class and return the content in the fallback class during service fusing.

@FeignClient(name= "spring-cloud-producer",fallback = HelloRemoteHystrix.class)public interface HelloRemote {  @RequestMapping(value = "/hello")  public String hello(@RequestParam(value = "name") String name);}

It's easy to change.

4. Test

Let's test the effect.

Start three projects: spring-cloud-eureka, spring-cloud-producer, and spring-cloud-consumer.

Enter http: // localhost: 9001/hello/neo in the browser.

Return Value: hello neo, this is first messge

It indicates that normal access is not affected after information about the circuit breaking is added. Next, we will manually stop the spring-cloud-producer project and test again:

Enter http: // localhost: 9001/hello/neo in the browser.

Return Value: hello neo, this messge send failed

The circuit breaker is successful Based on the returned results.

Sample Code

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.