Mastering Sentinel: From Setup to Advanced Flow Control in Spring Cloud
This comprehensive guide walks you through installing Sentinel, configuring its dashboard, integrating it with Spring Cloud microservices, and mastering flow control, circuit breaking, hotspot parameter limiting, system adaptive protection, custom block handling, rule persistence with Nacos, and cluster-wide rate limiting.
1. Introduction
This article is the fifth in the "Spring Cloud Advanced" series and introduces Alibaba's open‑source traffic‑guard Sentinel, a mature solution tested during ten years of Double‑11 traffic.
2. What is Sentinel?
Sentinel (literally "sentinel") monitors traffic, provides flow control, circuit breaking, and system load protection to keep services stable.
3. Features
Rich scenarios from Alibaba's Double‑11 traffic (spike control, message smoothing, cluster flow control, real‑time circuit breaking).
Real‑time monitoring of single‑machine and cluster metrics.
Integration with Spring Cloud, Dubbo, gRPC, Quarkus, and multi‑language support.
Extensible SPI for custom logic.
4. Version selection
When using the Spring Cloud Alibaba dependencies version 2.2.1.RELEASE, Sentinel 1.7.1 is recommended.
5. Installing the Sentinel dashboard
Download the appropriate sentinel-dashboard-1.7.1.jar and run it with:
java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.7.1.jarDefault credentials are sentinel/sentinel.
6. Integrating a microservice with the dashboard
Register the service with Nacos, add the Sentinel starter dependency, and configure the dashboard address in application.yml:
spring:
cloud:
sentinel:
transport:
dashboard: localhost:8080Define a test controller and verify that the service appears in the dashboard.
7. Flow control configuration
Sentinel supports QPS and concurrent‑thread limits. A FlowRule consists of resource, count, grade, limitApp, strategy, and controlBehavior. Three control behaviors are available: fast‑fail, warm‑up, and queue‑waiting.
7.1 Fast‑fail
Requests exceeding the threshold are rejected immediately.
7.2 Warm‑up
Gradually increases traffic after a cold start using a token‑bucket algorithm.
7.3 Queue‑waiting
Uses a leaky‑bucket algorithm to pace requests.
8. Degrade (circuit‑breaker) rules
Sentinel provides three degrade strategies: average response time, exception ratio, and exception count. Rules are defined with DegradeRule and can be visualised in the dashboard.
9. Hot‑parameter flow control
Hot‑parameter limits protect frequently accessed parameters (e.g., product IDs in a flash‑sale). Rules are defined with ParamFlowRule and support parameter‑specific thresholds and exception items.
10. System adaptive flow control
Sentinel can protect the whole system based on load, CPU usage, average RT, entry QPS, or concurrent threads.
11. Custom block messages
Use @SentinelResource with blockHandler (or blockHandlerClass) to return custom responses when a request is blocked.
12. Exception fallback
Define fallback (or defaultFallback) methods to handle business exceptions separately from block exceptions.
13. Black‑/white‑list control
Configure AuthorityRule to allow or deny requests from specific origins (e.g., IP addresses).
14. Rule persistence
Sentinel rules are stored in memory by default. Using the Push mode with Nacos, rules can be persisted and synchronised. Example application.yml configuration is provided.
15. Pushing dashboard changes to Nacos
Modify Sentinel Dashboard source to use Nacos datasource for both pull and push, enabling rule changes made in the UI to be written back to Nacos.
16. Cluster flow control
Sentinel supports embedded or standalone token servers for cluster‑wide flow control. Configure one instance as the token server and others as clients, then add cluster flow rules via the dashboard or Nacos.
17. Gateway flow control
Gateway integration follows the same principles and can be configured in the dashboard.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Su San Talks Tech
Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.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.
