Actual combat target

1. Build practical Demo, integrate SpringCloud Gateway Gateway,Nacos registry and Hystrix fuse 2. Achieve test objectives: Access target service API through Gateway, for example: /provider-test/discovery/one when the access timeout period reaches 3000ms,Hystrix fuses. A friendly message is displayed, indicating that the server is busy. Please try again laterCopy the code

Set up process

1 Core Hystrix Maven introducedCopy the code
<! - fuse hystrix - > < the dependency > < groupId > org. Springframework. Cloud < / groupId > < artifactId > spring -- cloud - starter - netflix - hystrix < / artifactId > < version > 2.2.2. RELEASE < / version > < / dependency >Copy the code

2 Core YML configurationCopy the code
Cloud: nacOS: ## registry configuration discovery: Hystrix gateway: routes: -id: Provider-test-one URI: lb://nacos-main predicates: -path =/provider-test/** # Hystrix related filters: - name: Hystrix args: Name: fallbackcmd # If the request times out, it will be forwarded to /fallback fallbackUri: forward:/provider-test/fallback Fallbackcmd: Execution: Isolation: Thread: timeoutInMilliseconds: 3000 # Service timeoutCopy the code

3 Controller implementation step 2 configure /provider-test/** when the service request exceeds 3000ms, the system switches to /provider-test/fallback for friendly service busy notification. Write a simple demo here The controller canCopy the code

Hystrix fuse test

The test target is access. For example, /provider-test/discovery/one when the access timeout time reaches 3000ms,Hystrix fuses. A friendly message is displayed: the server is busy Thread.sleep((int) (math.random () * 10000));Copy the code

Through the Postman send the gateway API access request: http://localhost:9999/provider-test/discovery/one gateway will find the corresponding service provider in the center of the Nacos registered address, and then an API visit 1 When the API access timeout exceeds 3000ms, Hystrix processes it and switches to the target API /provider-test/fallback, as shown in the figureCopy the code

2 The API has not timed out, and the access is normalCopy the code

Actual source code Demo download

SpringCloud Gateway+Nacos+Hystrix

TODO

Hystrix source code analysis, how to control timeout fuse, the underlying source code mechanism appreciation analysisCopy the code