1. The concept
1.1 what is
Sentinel colloquially refers to it as the upgraded Web version of Hystrix. Through the way of visualization, solve the service in practice encountered various problems.
1.2 Download and Installation
Download the latest stable JAR file and start it.
window
Background Startup Modejavaw -jar xxx.jar
,cmd
Window to startjava -jar xxx.jar
The default boot port is 8080. If port 8080 is used, you can customize the boot portjava -jar xxx.jar --server.port=8888
linux
Background Startup Modenohup java -jar xxx.jar >log.log &
Specify log file location and background suspend startup.
Address to access after successful startup:http://ip:port
The default user name and password aresentinel
.
1.3 Core function modules
- Service avalanche
- Service degradation
- Service fusing
- Service current limiting
1.4 Used in the project
Add the following configuration to the service configuration file
spring:
cloud:
nacos:
discovery:
server-addr: 192.16810.120.: 8848 The nacOS registry is used here. The actual configuration is based on the registry of your own project
sentinel:
transport:
dashboard: 192.168101.120.: 8080 # start address above
Copy the code
2. Actual application and configuration
2.1 Panel terminology
The name of the | introduce | The value that |
---|---|---|
Resource name | unique | The default is the request path |
In view of the resources | Sentinel You can limit the flow for the caller and fill in the service name |
The default isdefault |
The threshold type QPS |
Number of requests per second | When the changeapi theQPS When the threshold is reached, traffic limiting is triggered |
Threshold Type Number of threads | When the changeapi When the number of request threads reaches the threshold, the traffic limiting operation is triggered |
|
Flow control mode direct | whenapi Reach the current limiting condition, direct current limiting |
|
Flow control mode association | When the associated resource reaches the threshold, traffic limiting is performed | |
Link in flow control mode | When the resource from an interface reaches the traffic limiting condition, traffic limiting is enabled | |
Flow control fails quickly | Fail directly and throw an exception | |
Effect of flow controlWarm Up |
According to theCodeFactor (Cold loading factor, default is 3) value from threshold /codeFactor , the QPS threshold was reached after the preheating time |
|
Flow control effect queue up | Queue evenly, let requests pass at an even rate,The threshold type must be set to QPS, otherwise it is invalid |
2.2 Configuration Instructions
2.2.1 Threshold Type QPS Flow control mode Direct
Description of related parameters and methods
Sentinel provides this capability, which allows us to define another method to return data instead of the restricted or exception service, namely fallback and blockHandler
fallback
: Fails to be invoked. If unknown exceptions occur on the interface, the interface is invokedfallback
Specifies the interface.blockHandler
:sentinel
Called if the access is restricted or the service is degradedblockHandler
Specifies the interface.
- The test of
service
layer
/** * Threshold type QPS flow control mode direct */
String hello(String limit);
Copy the code
service
Implementation layer
@SentinelResource(value = "limit",defaultFallback = "defaultFallback",blockHandler = "handlerException",blockHandlerClass = {BlockException.class})
@Override
public String hello(String limit) {
return "Threshold type QPS Flow control mode direct";
}
// Customize simple service limiting or downgrade callback methods
public String defaultFallback(a){
return "Too crowded ~ please try again later";
}
public String handlerException(a){
return "Tests whether the portion that exceeds the traffic limit enters the blockHandler method.";
}
Copy the code
controller
test
@Autowired
private SentinelService sentinelService;
@GetMapping("/test1")
public String testA(a){
return "this is testA -----------";
}
@GetMapping("/test2")
public String testB(a){
return "this is testB -----------";
}
@GetMapping("/limit")
public String limit (a){
return sentinelService.hello("Test QPS current limiting");
}
Copy the code
- Call the above three test interfaces in turn, because
Sentinel
Service monitoring is lazily loaded. Only when the service interface is called, it is captured by the monitoring center. After the call, the following data can be seen in the panel
- Configure flow control for interfaces of related services as follows: (If the current interface attempts more than three times within 1s, the flow limiting operation is triggered directly and the callback method fails.)
- Test, get use in browser
postman
Quick click interfacehttp://localhost:8041/limit
, when more than three times within 1s, enter the preset callback method and return the parameter as
2.2.2 Threshold Type Number of threads and flow control mode Direct
service
layer
String threadTest(String thread);
Copy the code
service
Implementation layer
@SentinelResource(value = "thread",defaultFallback = "defaultFallback",blockHandler = "handlerException",blockHandlerClass = {BlockException.class})
@Override
public String threadTest(String thread) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "Threshold type thread count, flow control mode direct";
}
Copy the code
controller
layer
@GetMapping("/thread")
public String thread(a){
return sentinelService.threadTest("Thread test");
}
Copy the code
- use
Apache jmeter
Run multithreaded tests
Create 5 threads and test normally. Each thread results return normally, add thread limit flow limiting configuration
- Set the thread threshold to 4, create 5 threads to call the interface, from the return result can be seen, the first four threads return normal results, there is a thread return is restricted results
2.2.3 Association between threshold Type QPS and Flow Control Mode
- Based on
test1
andtest2
The interface remains unchanged. Associate the two interfaces. - Simulation scenario: Payment interface and order interface. When the payment interface reaches the threshold, the generation of order interface will be limited. Relieve pressure on services. The specific process is as follows
3. Test interface, separate fast and slow testtest1
andtest2
Interface. The returned result is normal. 4postman
andJmeter
Test two interfaces to create multiple thread runstest2
At the same timepostman
calltest1
To trigger traffic limiting and return an exception log. whentest2
After limiting the current,test1
The interface returns normally
2.2.4 Flow control effect
-
Fail fast
Fail directly and enter exception handling, or enter the specified callback and do no additional processing.
-
Warm Up Enters a buffering phase from the start to the maximum threshold. The threshold is 1/3 of the maximum threshold at the beginning, and then increases slowly until the maximum threshold is reached. It is applicable to the sudden increase of traffic and changes to a slow increase, avoiding the server downtime caused by sudden large traffic.
Note: For example, if the threshold is set to 10, the initial maximum allowed number of requests is 10/3=3. When a large number of requests occur again, the threshold is automatically increased from 3 to 10 within 5 seconds
- Waiting in line
Set the threshold type to QPS. Set the single-node threshold to 2 and the flow control effect to queuing. Implementation effect for 1 s, the current interface to request more than 2, then the rest of the not directly in abnormal, into the queue, waiting in front of the interface call completed will continue to call the back of the interface, also can set the timeout, if didn’t call out more than the current time, the interface directly behind the discarded into the exception
Create 10 thread test interfaces with JMeter. Set the thread interval to 0.4s and the timeout time to 2000ms. Only a small part of the request will be limited and the rest will execute slowly.
2.2.5 Fuse downgrade
Reference books Jane at https://www.jianshu.com/p/35ba0d96450d