Continuing from the above article SpringCloud-Feign
Problem Description
To solve the above situation, SpringCloud provides Hystrix fuse component, which is like a fuse. Service degradation does not directly return an error, but provides a remedial measure and responds normally to the requester.
1. Service downgrade
Based on the borrowing management service, users and books are not enabled, that is, both are down.
Import Hystrix dependencies (this project has stopped maintaining, and SpringCloud dependencies are no longer included).
Add annotations to the startup class
to close the user and book services. If the query information is not available at this time, an alternative is provided. That is, when an exception occurs in the service, our alternative is returned.
Restart the borrow-service service.
At this time, accessing the borrowing information interface returns empty.
2.Service circuit breaker
After restarting, I kept visiting http://localhost:8201/borrow/1 and found that the method would be executed normally at first, and then I remedied it. As the number of clicks increases, only remediation is performed. Clicking again after a while will perform the normal method to remedy the situation. That is, fuse.
Then enable user services and book services. At this time, after the service is restored, the two service methods will be executed normally, and the circuit breaker mechanism will be turned off.
Summarize
3.openfeign implements downgrade
Openfeign downgrade is a service downgrade after failure to access a controller method. At this time, the user service will be hung up, but the book service will not be hung up. If the borrowing information is checked, the user service call will fail. At this time, the user service will be given an alternative.
Add configuration in the borrow-service configuration file to support circuit breaker
Close the user-service and restart the borrow-service
. There are remedial measures for user-service to hang up. The remedial measures for book-service are the same.
4.Hystrix monitoring
Create a new module,
start the Hystrix management page service, add dependencies to the monitored service,
and restart the service.
It can be accessed normally.
Start the monitoring service.
You can view the management page
. Enter the service to be monitored at the intermediate level, such as the borrowing service: http://localhost:8201/actuator/hystrix.stream
, and then click monitor stream to view the service, as shown in the figure.
The monitoring page will change after accessing the interface.