This is the third day of my participation in the August More text Challenge. For details, see: August More Text Challenge
Distributed traffic limiting framework Sentinel basic reference
Basic reference to Sentinel
The core library of Sentinel is used to realize flow limiting, which is divided into the following steps:
- Define the resources
- Define flow limiting rules
- Check whether the rule takes effect
Sentinel implements flow limiting
Introduce Sentinel core library
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
<version>1.8.2</version>
</dependency>
Copy the code
Define common business methods
private static void doLimiting(a){
try(Entry limiting = SphU.entry("doLimiting")){
log.info("{}-doLimiting",System.currentTimeMillis());
}catch (BlockException e){
// Handle flow-controlled logic}}Copy the code
In the doLimiting method, the flow control logic is implemented by defining a resource using Sentinel’s sphu.entry (“doLimiting”), which means that when a request is made to enter the doLimiting method, a flow limiting judgment is required. If a BlockException is thrown, DoLimiting can be defined as a method name, interface name, or other unique identifier
Define flow limiting rules for protected resources
private static void initFlowRules(a){
final List<FlowRule> flowRules = new ArrayList<>();
final FlowRule flowRule = new FlowRule();
flowRule.setResource("doLimiting");
flowRule.setCount(20);
flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
flowRules.add(flowRule);
FlowRuleManager.loadRules(flowRules);
}
Copy the code
For doLimiting resources, run initFlowRules to set flow limiting rules. The meanings of some parameters are as follows:
-
Grade: indicates the flow limiting threshold type. Number of concurrent threads (0) or QPS (1)
-
Resource: Set the name of the resource to be protected. The resource name must be the same as the one used in sphu.entry
-
Count: indicates the traffic limiting threshold
For the doLimiting method, a maximum of 20 requests per second are allowed, which is a QPS of 20
Verify that the doLImiting method limits the flow
public static void main(String[] args) {
initFlowRules();
while(true){ doLimiting(); }}Copy the code
Start the main method and view the following logs in {USER_HOME}\logs\ CSP \{package name – class name}– metric.log.YYYy-MM-dd
1627357800000|2021-07-27 11:50:00|doLimiting|20|6740|20|0|0|0|0|0
1627357801000|2021-07-27 11:50:01|doLimiting|20|7372|20|0|0|0|0|0
1627357802000|2021-07-27 11:50:02|doLimiting|20|7093|20|0|0|0|0|0
1627357803000|2021-07-27 11:50:03|doLimiting|20|6714|20|0|0|0|0|0
1627357804000|2021-07-27 11:50:04|doLimiting|20|6412|20|0|0|0|0|0
1627357805000|2021-07-27 11:50:05|doLimiting|20|7296|20|0|0|0|0|0
1627357806000|2021-07-27 11:50:06|doLimiting|20|7606|20|0|0|0|0|0
1627357807000|2021-07-27 11:50:07|doLimiting|20|7033|20|0|0|0|0|0
1627357808000|2021-07-27 11:50:08|doLimiting|20|6489|20|0|0|0|0|0
Copy the code
Steady output is 20 times per second, consistent with the value set in the rule, and the maximum number of rejected requests is 7606 times per second
Specific meanings of corresponding fields in the log
timestamp|yyyy-MM-dd HH:mm:ss|resource|passQPS|blockQPS|successQPS|exceptionQPS|rt|occupiedPassQPS | concurrency | classification passQPS by the number of requests blockQPS is prevented the number of requests successQPS successfully completes the number of requests exceptionQPS user custom exception rt the average response time OccupiedPassQPS Indicates the request that passes preferentially. Classification Resource typeCopy the code