Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Springcloud (4): fuse Hystrix

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Talking about the springcloud circuit breaker reminds me of the circuit breaker in the stock market last year. I have learned many times that the impact of a random circuit breaker on the whole system is disastrous. OK, let's get down to business. Friends who are willing to understand the source code directly ask for communication and sharing technology: 2147775633

Avalanche effect

In micro-service architecture, there are usually multiple service layer invocations, and the failure of basic services may lead to cascade failures, resulting in the unavailability of the whole system, which is called service avalanche effect. The service avalanche effect is a process in which the unavailability of the "service provider" leads to the unavailability of the "service consumer" and gradually magnifies the unavailability.

As shown in the following figure: an is the service provider, B is the service consumer of A, and C and D are the service consumers of B. The unavailability of A causes the unavailability of B, and when the unavailability is enlarged to C and D like a snowball, the avalanche effect is formed.

Fuse (CircuitBreaker)

The principle of a fuse is simple, like an electric overload protector. It can achieve rapid failure, and if it detects many similar errors in a period of time, it will force multiple subsequent calls to fail quickly and no longer access the remote server, thus preventing the application from constantly trying to perform operations that may fail, making the application continue to execute without waiting for the error to be corrected, or wasting CPU time waiting for a long timeout to occur. The fuse also enables the application to diagnose whether the error has been corrected, and if so, the application will try to invoke the operation again.

Fuse mode is like an agent for operations that can easily lead to errors. This agent can record the number of errors that occurred in the most recent call and then decide to allow the operation to continue or return an error immediately. The logic of fuse switch conversion is shown in the following figure:

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

Hystrix characteristics

1. Circuit breaker mechanism

The circuit breaker is easy to understand. When the number of Hystrix Command requests for back-end service failures exceeds a certain percentage (default 50%), the circuit breaker will switch to the open state (Open). At this point, all requests will fail directly and will not be sent to the back-end service. After keeping the circuit breaker in the open state for a period of time (default 5 seconds), the circuit breaker automatically switches to the semi-open state (HALF-OPEN). At this time, the return of the next request will be judged, and if the request is successful, the circuit breaker will switch back to the closed-circuit state (CLOSED), otherwise it will switch back to the open-circuit state (OPEN). Hystrix circuit breaker is like a fuse in our home circuit. Once the back-end service is not available, the circuit breaker will directly cut off the request chain to avoid sending a large number of invalid requests to affect the system throughput, and the circuit breaker has the ability to self-detect and recover.

2.Fallback

Fallback is equivalent to a downgrade operation. For query operations, we can implement a fallback method. When an exception occurs in the request back-end service, we can use the value returned by the fallback method. The return value of the fallback method is usually the default value set or from the cache.

3. Resource isolation

In Hystrix, resource isolation is mainly achieved through thread pool. Usually when we use it, we divide multiple thread pools according to the remote service invoked. For example, the Command that calls the product service is put into the A thread pool, and the Command that invokes the account service is put into the B thread pool. The main advantage of this is that the operating environment is isolated. In this way, even if the code that invokes the service exists bug or other reasons cause the thread pool to be exhausted, it will not affect other services of the system. But the cost is that maintaining multiple thread pools will bring additional performance overhead to the system. If you have strict performance requirements and are sure that the client code that invokes the service will not have a problem, you can use Hystrix's signal mode (Semaphores) to isolate resources. Sources of data and source

The technical architecture diagram is as follows:

The overall code structure is as follows:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report