Unlocking Sentinel: Deep Dive into Flow Control, Rules, and Core Mechanics
This article introduces Alibaba's Sentinel library, explains its core concepts of resources and rules, provides step‑by‑step Java demos—including basic usage and Spring Boot integration—then dissects the internal processor‑slot architecture that powers flow control, circuit breaking, and system protection.
Sentinel Overview
Sentinel is an open‑source flow‑control component from Alibaba for distributed, multi‑language service architectures. It provides traffic routing, flow control, shaping, circuit breaking, system overload protection, hotspot traffic protection, and other features to help developers ensure microservice stability.
Core Concepts
Understanding a new technology starts with its core concepts.
Resource
A resource is the object protected by Sentinel, such as a piece of code, an HTTP interface, or an RPC interface. Each resource has a name, typically the request path for an HTTP interface.
Rule
Rules define the conditions under which a resource is limited. Sentinel supports flow‑control rules, circuit‑breaker rules, and system‑protection rules.
Demo
Below is a basic demo showing how to add Sentinel dependencies, define a flow rule for the resource sayHello , and invoke the resource with SphU.entry("sayHello") . The demo prints a message when the resource is accessed and catches BlockException when the QPS exceeds 2.
<code><dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
<version>1.8.6</version>
</dependency>
</code> <code>public class SentinelSimpleDemo {
public static void main(String[] args) {
// Load flow rule
initFlowRules();
for (int i = 0; i < 5; i++) {
Entry entry = null;
try {
entry = SphU.entry("sayHello");
// Protected logic
System.out.println("访问sayHello资源");
} catch (BlockException ex) {
System.out.println("被流量控制了,可以进行降级处理");
} finally {
if (entry != null) {
entry.exit();
}
}
}
}
private static void initFlowRules() {
List<FlowRule> rules = new ArrayList<>();
// Create a flow rule
FlowRule rule = new FlowRule();
// Limit sayHello resource
rule.setResource("sayHello");
// QPS based limiting
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
// Max QPS = 2
rule.setCount(2);
rules.add(rule);
// Load rules
FlowRuleManager.loadRules(rules);
}
}
</code>Explanation of the demo code:
initFlowRules loads a flow rule for the resource sayHello with a QPS limit of 2.
SphU.entry("sayHello") checks the rule and may throw BlockException .
When blocked, you can perform fallback logic.
Test results show that after two successful accesses the QPS reaches 2 and subsequent calls are blocked.
Spring Integration
In a Spring Boot project, add the Sentinel starter dependencies and expose an endpoint /sayHello . Configure the dashboard address in application.yml to enable dynamic rule management.
<code><dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
</code> <code>@RestController
public class SentinelDemoController {
@GetMapping("/sayHello")
public String sayHello() throws InterruptedException {
return "hello";
}
}
</code> <code>spring:
cloud:
sentinel:
transport:
# Specify dashboard IP and port
dashboard: localhost:8080
</code>After starting the application, the resource /sayHello appears in the Sentinel dashboard where you can adjust rules.
Core Principles
Sentinel creates a processing chain (responsibility chain) for each resource. The chain consists of several ProcessorSlot implementations that are loaded via SPI and executed in a fixed order.
NodeSelectorSlot – selects the statistics node for the current entry.
ClusterBuilderSlot – builds cluster and origin nodes.
LogSlot – logs exceptions.
StatisticSlot – updates statistics (QPS, threads, etc.).
AuthoritySlot – performs black‑/white‑list authorization.
SystemSlot – limits based on system metrics (CPU, load, etc.).
FlowSlot – enforces flow‑control rules.
DegradeSlot – handles circuit‑breaker logic.
Each slot plays a specific role, from preparing data to enforcing limits and finally performing degradation.
NodeSelectorSlot
Sets the statistics Node for the current resource entry, allowing separate statistics for different entry points.
ClusterBuilderSlot
Creates two nodes: a ClusterNode aggregating all entry data and an OriginNode for a specific caller (origin).
LogSlot
Logs errors when exceptions occur.
StatisticSlot
Updates the selected node with real‑time metrics such as request count and QPS.
AuthoritySlot
Applies black‑list or white‑list rules to control which callers may access a resource.
SystemSlot
Limits access based on system‑wide metrics (QPS, threads, response time, CPU usage, load).
FlowSlot
Evaluates flow‑control rules against the current statistics and may throw BlockException or apply fast‑fail, warm‑up, or queue‑waiting strategies.
DegradeSlot
Implements circuit‑breaker strategies (slow‑call ratio, exception ratio, exception count) with OPEN, HALF_OPEN, and CLOSED states.
Conclusion
Sentinel’s core consists of statistical nodes and a chain of processor slots that together provide flexible flow control and circuit‑breaker capabilities. The source code is concise and well‑structured, making it easy to extend.
https://github.com/sanyou3/sentinel.git
https://github.com/sanyou3/sentinel-demo.git
Sanyou's Java Diary
Passionate about technology, though not great at solving problems; eager to share, never tire of learning!
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.