Explain the hystrix circuit breaker of spring cloud microservice architecture in detail
1: What is hystrix
In a distributed environment, some of many service dependencies will inevitably fail. Hystrix is a library that helps you control the interaction between these distributed services by adding delay tolerance and fault-tolerant logic. Hystrix improves the overall resilience of the system by isolating access points between services, stopping cascading failures and providing fallback options.
Hystrix is designed to:
1: Protects against dependencies accessed through third-party client libraries (usually over the network) and controls delays and failures.
2: Isolate cascading faults in complex distributed systems.
3: Quickly find the fault and recover as soon as possible.
4: Step back and downgrade as gracefully as possible.
5: Enable near real-time monitoring, alarm and operation control.
2: Why need hystrix?
In large-scale distributed systems, a client or service depends on external services. If a service goes down, the corresponding time will be affected because we set the service invocation system timeout. In the case of high concurrency, the thread pool of most servers will be blocked, affecting the stability of the whole online service.
(official picture)
When everything is healthy, requests can look like this
When one of many back-end service systems goes down, the entire user requests:
When multiple clients call the same exception service, the following occurs:
3: What problems does hystrix solve?
Applications in distributed architecture have dozens of dependencies, and each dependency will inevitably have exceptions at some time. If the application is not isolated from these external faults, thread pool blocking may occur, causing system avalanche.
For example, for applications that rely on 30 services, the uptime of each service is 99.99%. You can:
99.99% power 30 = 99.7% uptime
0.3% of 1 billion requests = 3000000 failures
2 + hours downtime / month, even if all dependencies are uptime.
When fusing with hystrix, each dependency is isolated from each other, limiting blocking when delays occur.
4: Use of hystrix with feign
Create a project Eureka_ feign_ hystrix_ client
pom. XML file content
Create startup file
FeignHystrixApplication
Userclient class
Create class userclienthystrix
When the network is abnormal, or jump directly to the implementation class here
Create action class
UserController
Start first: Eureka_ register_ Service project
Then run our feignhystrixapplication
At this time, we obviously find that the biz-service-0 service is not running, so we open it http://127.0.0.1:8005/getuserinfostr
appear
Userclienthystrix getuserinfostr() is fallback service is unavailable..
This is the return result of our custom fuse
If you don't use the fuse, this page will appear
Code address: https://github.com/zhp8341/SpringCloudDemo