Speaking of springcloud circuit breaking reminds me of the circuit breaking in the stock market last year. I have realized many painful times. The impact of random circuit breaking on the entire system is disastrous. Well, let’s talk about the serious matters next.
Fuse
Avalanche effect
In microservice architectures, there are usually multiple service layers to call. The failure of the underlying service may lead to cascade failures, which in turn causes the entire system to be unavailable. This phenomenon is called the 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 consumers" and gradually amplifies the unavailability.
If the figure below shows: A 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 unavailability of B and when the unavailability is enlarged to C and D like a snowball, the avalanche effect is formed.
CircuitBreaker
The principle of a fuse is very simple, like a power overload protector. It can achieve fast failure. If it detects many similar errors over a period of time, it will force its subsequent calls to fail quickly and no longer access the remote server, thus preventing the application from constantly trying to perform possible failure operations, causing the application to continue to execute without waiting for correction of the error, or wasting CPU time to wait until a long timeout occurs. The fuse can also enable the application to diagnose whether the error has been fixed, and if it has been fixed, the application will try to call the operation again.
The fuse mode is like a proxy that can easily lead to wrong operations. This proxy can record the number of errors that occurred in the most recent call, and then decide to use the allow operation to continue, or return the error immediately.
The logic of the mutual conversion of fuse switches is as follows:
Fuses are the last line of defense to protect high availability of services.
Hystrix Features
1. Circuit Breaker Mechanism
The circuit breaker is easy to understand. When the Hystrix Command requests back-end service failures exceed a certain proportion (default 50%), the circuit breaker will switch to the open state (Open). At this time, all requests will fail directly without sending to the back-end service. After the circuit breaker remains in the open state for a period of time (default 5 seconds), it will automatically switch to the half-open state (HALF-OPEN). At this time, the return status of the next request will be judged. If the request is successful, the circuit breaker will switch back to the closed circuit state (CLOSED), otherwise it will switch to the open state (OPEN). Hystrix's circuit breaker is like a fuse in our home circuit. Once the back-end service is unavailable, the circuit breaker will directly cut off the request link to avoid sending a large number of invalid requests to affect the system throughput. The circuit breaker has the ability to self-detection 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 a requested back service, the value returned by the fallback method can be used. The return value of the fallback method is generally set by the default value or comes from the cache.
3. Resource Isolation
In Hystrix, resource isolation is mainly achieved through thread pools. Usually when using it, we divide multiple thread pools according to the remote service we call. For example, the command that calls product services is placed into A thread pool, and the command that calls account services is placed into B thread pool. The main advantage of this is that the running environment is isolated. In this way, even if the code that calls service is bugged or the thread pool that is consumed due to other reasons, it will not affect other services of the system. However, the cost is that maintaining multiple thread pools will bring additional performance overhead to the system. If you have strict performance requirements and you are sure that there will be no problems with the client code that calls service, you can use Hystrix's signal mode (Semaphores) to isolate resources.
Feign Hystrix
Because circuit breaker only works on the service call end, we only need to change the relevant code of the spring-cloud-consumer project based on the example code in the previous article. Because Feign already depends on Hystrix, there is no need to make any changes to the maven configuration.
1. Configuration file
Add this to application.properties:
feign.hystrix.enabled=true
2. Create a callback class
Create HelloRemoteHystrix class inheritance and HelloRemote to implement callbacks
@Componentpublic class HelloRemoteHystrix implements HelloRemote{ @Override public String hello(@RequestParam(value = "name") String name) { return "hello" +name+", this message send failed "; }}3. Add fallback attribute
Add the specified fallback class to the HelloRemote class, and return the contents in the fallback class when the service is circuited.
@FeignClient(name= "spring-cloud-producer",fallback = HelloRemoteHystrix.class)public interface HelloRemote { @RequestMapping(value = "/hello") public String hello(@RequestParam(value = "name") String name);}This is the point of change, it's very simple.
4. Test
Then let's test it to see the effect.
Start the three projects spring-cloud-eureka, spring-cloud-producer, and spring-cloud-consumer.
Enter in the browser: http://localhost:9001/hello/neo
Return: hello neo, this is first message
It means that after adding circuit breaker-related information, it will not affect normal access. Next, we manually stop the spring-cloud-producer project and test it again:
Enter in the browser: http://localhost:9001/hello/neo
Return: hello neo, this message send failed
According to the return result, the circuit breaker was successfully indicated.
Sample code
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.