1, Installation
- download
Sentinel download address - install
java -jar sentinel-dashboard-1.7.0.jar
3. Visit localhost:8080
2, Disposition
1. Construction project
- Introducing maven dependency
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-nacos-discovery</artifactId> </dependency> <!--Persistence --> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
- Configure yml
server: port: 8401 spring: application: #Service instance name name: cloudalbaba-sentinel-service cloud: nacos: discovery: #nacos service address server-addr: 192.168.252.101:1111 sentinel: transport: # Configure sentinel dashboard address dashboard: localhost:8080 # The default port is 8719. If it is occupied, it will automatically start a + 1 scan from 8719 until an unoccupied port is found port: 8719
- Add annotation to startup class
@SpringBootApplication
@EnableDiscoveryClient - Request any interface to refresh sentinel dashboard
2. Flow control
- Flow control rules
Click the flow control rule on the sentinel dashboard page, add a new flow control rule, and set the threshold type and threshold
Threshold type:
QPS: the number of requests per second, when the threshold time limit is reached
Number of threads: the number of threads processing requests. When the thread calling the api reaches the threshold time limit stream-
Flow control mode
- Direct: when the interface reaches the current limiting condition, the current limiting is turned on
- Association: when the associated resources meet the current limit conditions, the current limit is enabled, which is suitable for application concessions. For example, add associated flow control for a query interface, and the associated resource is an updated interface. When the updated interface reaches the threshold, turn on the flow limit of the query interface and give way to server resources for the updated interface.
- Link: when the resource from an interface reaches the current limit condition, the current limit is enabled
-
Flow control effect
- Rapid failure
- Warm Up
When the system is at a low water level for a long time and the flow increases suddenly, directly raising the system to a high water level may crush the system instantly. The flow increases slowly through preheating and gradually increases to the threshold within a certain time to avoid system collapse.- Formula: divide the threshold value by coldFactor (cold loading factor, default value 3), and the threshold value will not be reached until it is preheated for a long time
-
Queue up
Strictly control the time interval of request passing, and let the request pass at a uniform speed, corresponding to the leaky bucket algorithm. Used to handle interval burst traffic
-
- Degradation rule (no half open state)
- RT (average response time in seconds)
When the average response time exceeds the threshold and QPS > = 5, the degradation is triggered after the two conditions are met at the same time, and the circuit breaker is closed after the window period
RT Max 4900 (larger setting - DCSP. Sentinel. Statistical. Max.rt = XXX)
Abnormal proportion (second level)
When QPS > = 5 and the abnormal proportion (second level statistics) exceeds the threshold, degradation is triggered; When the time window period ends, turn off the downgrade
Different constant (minute level)
If the abnormal constant (minute Statistics) exceeds the threshold, the degradation is triggered; When the time window period ends, the degradation is closed. When setting, the time window is greater than 60 seconds
- RT (average response time in seconds)
- Hotspot rule
Request the specified parameters of the api name specified by SentinelResource, open the circuit breaker when the QPS is reached, and close the circuit breaker after the time window period- Parameter exceptions
When the parameter is of the specified type, the rule of exception configuration is used
- Parameter exceptions
- System rules (entry level, coarse granularity)
- LOAD
- RT
- Number of threads
- Entrance QPS
- CUP utilization
3.@SentinelResource
private method is not supported
value: name
fallback handles running exceptions, and blockHandler handles flow control configuration
blockHandler: demote method
blockHandlerClass: demote class
blockHandlerClass,blockHandler: Specifies the demotion method in the demotion class
exceptisToIgnore: ignoring multiple exceptions
Integration with openFeign
Add configuration
#Activate Sentinel support for feign feign: sentinel: enabled: true
4. Persistence
Persist flow restriction rules to nacos
- Add maven dependency
<dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> </dependency>
2. Configure yml
spring: application: #Service instance name name: cloudalibaba-sentinel-service cloud: nacos: discovery: #nacos service address server-addr: 192.168.252.101:1111 sentinel: transport: # Configure sentinel dashboard address dashboard: localhost:8080 # The default port is 8719. If it is occupied, it will automatically start a + 1 scan from 8719 until an unoccupied port is found port: 8719 datasource: dsl: nacos: server-addr: 192.168.252.101:1111 dataId: cloudalibaba-sentinel-service data-type: json rule-type: flow
- nacos configuration flow control rules