SpringCloud-Fuse Hystrix (two)

Hands

1 ) Introduce dependencies
In Consumer - Demo consumer end system pom.xml add the following dependencies:
2 ) Turn on the fuse
Add a comment on the startup class ConsumerApplication : @EnableCircuitBreaker
As you can see, there are more and more annotations on our classes. In microservices, the above three annotations are often introduced, so Spring provides a combined annotation
解: @SpringCloudApplication
Therefore, we can use this combined annotation to replace the previous 3 annotations.
3 ) Write downgrade logic
When the call to the target service fails, we hope to fail quickly and give the user a friendly prompt. Therefore, it is necessary to prepare the downgrade processing logic in case of failure in advance.
Edit, to use HystrixCommand to complete.
Transform consumer - demo\src\main\java\com\itheima\consumer\controller\ConsumerController.java processor
The classes are as follows:
 
       Pay attention; because the degraded logic method of fusing must be guaranteed with the normal logic method: the same parameter list and return value declaration . It doesn't make much sense to return the User object in the failure logic, and generally a friendly prompt will be returned. So the queryById method is transformed to return String , which is also Json data anyway . It is more convenient to return an error description in the failure logic.
Description:
@HystrixCommand(fallbackMethod = "queryByIdFallBack") : Method used to declare a downgrade logic
test:
When the user - service provides services normally, the access is the same as before. But when the user - service is shut down, you will find that the page returns to the downgrade process
information:
4 ) The default Fallback
Just now I wrote fallback on a certain business method. If there are many such methods, wouldn’t it be necessary to write a lot. So you can add the Fallback configuration to the class
Above , realize the default fallback ;
Transform consumer-demo\src\main\java\com\itheima\consumer\controller\ConsumerController.java again
@DefaultProperties(defaultFallback = "defaultFallBack") : Specify a unified failure downgrade method on the class; all methods in the class
The return type should be consistent with the return type of the method that handled the failure.
5 ) Timeout setting
In the previous case, the request will return an error message after more than 1 second. This is because the default timeout period of Hystrix is 1 , and we can modify it through configuration
Change this value; modify consumer -demo\src\main\resources\application.yml and add the following configuration:
This configuration will affect all methods globally. To facilitate copying to the yml configuration file, you can copy hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=2000 to the yml file and it will be automatically formatted and then modified.
In order to trigger the timeout, you can sleep for 2 seconds in the method of user - service\src\main\java\com\itheima\user\service\UserService.java ;
It can be found that the requested duration has reached 2s+ , which proves that the configuration has taken effect. If you modify the modification time to less than 2 seconds, you can access it normally again.
 

Guess you like

Origin blog.csdn.net/SSbandianH/article/details/109443147