Comprehensive Guide to Alibaba Sentinel: Installation, Configuration, Flow Control, Degrade Rules, Hotspot Parameters, Persistence, Cluster Flow Control and More
This article provides an in‑depth tutorial on Alibaba Sentinel, covering its core concepts, installation of the dashboard, version selection, integration with Spring Boot microservices, detailed flow‑control and degrade rule configurations, hotspot parameter limiting, system adaptive protection, custom block handlers, blacklist/whitelist settings, rule persistence with Nacos, push‑mode synchronization, cluster flow control, gateway integration and OpenFeign fault‑tolerance, all illustrated with practical code examples.
1. Introduction
This article introduces Alibaba's open‑source traffic‑guard Sentinel, a mature project refined through nearly ten years of Double‑11 traffic peaks.
2. What is Sentinel?
Sentinel, literally "sentinel", is a traffic‑defense guard for microservices. It protects service stability from traffic control, circuit breaking, and system load perspectives.
Key features include:
Rich application scenarios : handles flash sales, message throttling, cluster traffic control, real‑time circuit breaking, etc.
Real‑time monitoring : per‑machine second‑level metrics and cluster‑wide summaries.
Broad open‑source ecosystem : integrates with Spring Cloud, Dubbo, gRPC, Quarkus, and supports Java/Go/C++.
Complete SPI extension mechanism : custom rule management, dynamic data source adaptation.
3. Sentinel vs. Hystrix
Sentinel outperforms Hystrix in many aspects; the article provides a visual comparison.
4. Selecting Sentinel Version
When using Spring Cloud Alibaba, the version aligns with spring-cloud-alibaba-dependencies 2.2.1.RELEASE, so Sentinel 1.7.1 is recommended.
5. Installing the Sentinel Dashboard
Download the appropriate JAR from the GitHub releases page and run:
java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.7.1.jarKey parameters:
-Dserver.port : dashboard port (default 8080).
-Dproject.name : service name.
-Dcsp.sentinel.dashboard.server : dashboard address for registration.
Default login: sentinel/sentinel . JDK >= 1.8 is required.
6. Integrating Microservices with Sentinel
Register the service with Nacos, add the Sentinel starter dependency, and configure the dashboard address:
spring:
cloud:
sentinel:
transport:
dashboard: localhost:8080Create a test controller:
@RestController
@RequestMapping("/sentinel")
public class FlowLimitController {
@GetMapping("/test")
public String test() {
return "Received a message--------";
}
}Start the microservice (e.g., port 9008) and verify it appears in the dashboard.
7. Configuring Flow Control
Flow control monitors QPS or concurrent threads and limits traffic when thresholds are reached.
Each rule consists of:
resource : target name.
count : threshold.
grade : 1 for QPS, 0 for threads.
limitApp : caller origin (default default ).
strategy : 0 direct, 1 associated, 2 chain.
controlBehavior : 0 fast‑fail, 1 warm‑up, 2 queue‑waiting.
Three flow‑control effects:
Fast Fail
Requests exceeding the threshold are immediately rejected with a FlowException .
Warm‑Up
Gradually ramps up traffic using a token‑bucket algorithm; implemented by WarmUpController .
Queue‑Waiting
Uses a leaky‑bucket algorithm to queue requests; implemented by RateLimiterController .
8. Degrade (Circuit‑Breaker) Rules
Three degrade strategies:
Average response time (RT) – triggers when the average RT exceeds count ms within 1 s.
Exception ratio – triggers when the exception ratio exceeds count (0.0‑1.0) after at least 5 QPS.
Exception count – triggers when the number of exceptions in the last minute exceeds count .
Example: a 3‑second sleep endpoint exceeds a 200 ms RT threshold and is circuit‑broken.
9. Hotspot Parameter Limiting
Limits frequently accessed parameter values (e.g., product IDs) using LRU + token bucket. Only the specified parameter index is subject to limiting.
10. System Adaptive Flow Control
Protects the whole system based on load, CPU usage, average RT, entry QPS, or concurrent threads. Example shown for entry‑QPS.
11. Custom Block Handler
Use @SentinelResource with blockHandler (or blockHandlerClass ) to return custom messages when a rule blocks a request.
12. Exception Fallback (Degrade) Handling
Define fallback , fallbackClass , or defaultFallback in @SentinelResource to handle business exceptions and degrade gracefully.
13. Black/White List
Configure AuthorityRule to allow or deny requests based on origin (e.g., IP). Implement RequestOriginParser to extract the origin.
14. Rule Persistence
Sentinel stores rules in memory by default. Use Nacos as a configuration center with the sentinel-datasource-nacos dependency. Example application.yml snippet:
spring:
cloud:
sentinel:
datasource:
ds-flow:
nacos:
server-addr: 127.0.0.1:8848
dataId: ${spring.application.name}-flow
groupId: DEFAULT_GROUP
data-type: json
rule-type: flow
ds-degrade:
nacos:
server-addr: 127.0.0.1:8848
dataId: ${spring.application.name}-degrade
groupId: DEFAULT_GROUP
data-type: json
rule-type: degradeJSON formats for flow, degrade, hotspot, system, and authority rules are provided in the article.
15. Push Mode Synchronization
Push mode automatically pushes Nacos changes to the dashboard. To push dashboard changes back to Nacos, modify Sentinel’s dashboard source code (replace test‑scope dependencies, copy Nacos rule provider/publisher to main, adjust @Qualifier beans, and configure NacosConfigUtil ).
16. Cluster Flow Control
Two roles: Token Client and Token Server. Modes: Alone (stand‑alone server) and Embedded (server inside each instance). Example: three instances (ports 9009, 9011, 9013) with one designated as token server via the dashboard.
17. Gateway Flow Control
Configuration details are omitted; refer to Sentinel’s official gateway documentation.
18. OpenFeign Integration for Fault Tolerance
See the previous OpenFeign article for detailed steps.
Conclusion
If you find this tutorial helpful, please like, share, comment, and bookmark. The full source code is available on GitHub; reply with keyword 9528 to the public account "码猿技术专栏" to obtain it.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.