Master Sentinel: Full Guide to Flow Control, Circuit Breaking & Load Protection
This article introduces Sentinel, the core flow‑control component of Spring Cloud Alibaba, explains its key features, architecture, and functional modules such as traffic shaping, rate limiting, circuit breaking and load protection, and provides a step‑by‑step Spring Boot integration example with code.
Sentinel is a flow‑control component for distributed service architectures, offering traffic shaping, circuit breaking, and system‑adaptive protection to ensure microservice stability.
Sentinel has four main characteristics:
Rich application scenarios, handling high‑traffic events like flash sales, message throttling, clustering, and real‑time circuit breaking.
Comprehensive real‑time monitoring, with a dashboard that shows aggregated cluster status.
Broad open‑source ecosystem, providing integrations for Spring Cloud, Dubbo, gRPC and others via simple dependencies.
Extensible SPI points, allowing custom rule management and dynamic data‑source adaptation.
Sentinel Components
1. Core library – The core jar has no external framework dependencies and runs on Java 8+ runtimes, while also supporting Spring Cloud, Dubbo, gRPC, etc.
2. Sentinel Dashboard – A lightweight open‑source console that offers machine discovery, cluster link discovery, monitoring, and rule configuration.
Sentinel Functional Modules
1. Flow Control
Sentinel can shape incoming traffic based on resource call relationships, runtime metrics (QPS, thread pool, system load), and control effects (direct limit, cold start, queuing).
2. Rate Limiting
Two statistics methods are supported: concurrent thread count and QPS. When thresholds are exceeded, new requests are rejected.
3. Circuit Breaking & Degradation
When a downstream service becomes unstable, Sentinel can cut off calls based on metrics such as slow‑request ratio, error ratio, or error count, preventing cascade failures.
4. Shaping
Developers can combine the three dimensions (call relationship, runtime metrics, control effect) to create custom traffic‑shaping rules.
5. Load Protection
Sentinel balances inbound traffic with system processing capacity, protecting the system from overload during peak events without relying solely on raw load values.
How to Use Sentinel in a Spring Boot Application
Add the Sentinel dependency to your Maven pom:
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
<version>1.8.0</version>
</dependency>Create a controller that demonstrates a protected resource:
@Controller
public class TestController {
@RequestMapping(path = "/hello", method = RequestMethod.GET)
@ResponseBody
public String hello() {
try {
// Define a resource named "Hello"
Entry entry = SphU.entry("Hello");
System.out.println("Hello Sentinel");
return "Hello Sentinel";
} catch (BlockException e) {
System.out.println("System busy, please try later");
e.printStackTrace();
return "System busy, please try later";
}
}
/** Define flow rules programmatically (hard‑coded, not recommended for production) */
@PostConstruct
public void initFlowRule() {
// 1. Create a list to hold flow rules
List<FlowRule> rules = new ArrayList<>();
// 2. Create a flow rule
FlowRule rule = new FlowRule();
// Define the protected resource
rule.setResource("Hello");
// Use QPS as the grade
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
// Set the QPS threshold
rule.setCount(2);
// 3. Add the rule to the list
rules.add(rule);
// 4. Load the rules into Sentinel
FlowRuleManager.loadRules(rules);
}
}After starting the application, open the Sentinel dashboard to view real‑time metrics and adjust rules as needed.
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.
Mike Chen's Internet Architecture
Over ten years of BAT architecture experience, shared generously!
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.
