Sentinel is an open-source project for circuit breaker in Spring Cloud Alibaba. It mainly includes the following functions: flow control, circuit breaker, and system adaptive protection.
In this post, I will cover common uses of Spring Cloud Alibaba Sentinel. This post is based on Java 21, Spring Boot 3.2.12, Spring Cloud Alibaba 2023.0.3.3, and Sentinel 1.8.8.
Next, I will use a simple example application to show it in action.
plugins { java id("org.springframework.boot") version "3.2.12" // Use the Spring Dependency Management plugin to manage Spring Cloud dependencies. id("io.spring.dependency-management") version "1.1.7" }
spring.cloud.sentinel.transport.port: A custom port for starting an HTTP server that interacts with the Sentinel Dashboard. 在应用对应的机器上启动一个 Http Server,该 Server 会与 Sentinel 控制台做交互。比如 Sentinel 控制台添加了一个限流规则,会把规则数据 push 给这个 Http Server 接收,Http Server 再将规则注册到 Sentinel 中。
spring.cloud.sentinel.eager: Whether to trigger Sentinel initialization in advance.
public String service1Fallback(Throwable t) { if (BlockException.isBlockException(t)) { // Circuit breaker or traffic limitation. If there is a blockHandler in `@SentinelResource(blockHandler="xxx")` then it will not be executed here return"Blocked by Sentinel: " + t.getClass().getSimpleName() + newDate(); } // Service degradation (not reaching the exception threshold) return"Oops, failed: " + t.getClass().getCanonicalName(); } }
4. Start and Verification
Start Nacos and Sentinel
Before running the application, you must ensure Nacos and Sentinel are running.
In the preceding section, we have already started a Sentinel Dashboard. For running Nacos, you can refer to Running Nacos with Docker.
Starting the Application
Running the main() method in the SpringCloudAlibabaSentinelApplication to start the application.
When you visit http://localhost:8090/test you will see a fallback (degrade) response. Because there is an exception thrown in MyService#service1(), and the @SentinelResource annotation is added to the method MyService#service1().
The fallback (degrade) response:
Oops, failed: java.lang.ArithmeticException
Add flow rules and degrade rules to Sentinel
Visiting http://localhost:8081 to open the Sentinel Dashboard. After you login the Sentinel Dashboard, you can see the application spring-cloud-alibaba-sentinel-demo appears in the left sidebar. If you can’t see the application in the left side bar, you can visit http://localhost:8090/test to trigger Sentinel to work.
Add flow rules and degrade rules on the Sentinel Dashboard:
On the left sidebar, expand your application -> Click the menu “簇(cù)点链路” -> Add flow rules and circuit breaker (degrade) rules to the resource MyService#service1.
Test circuit breaker
Add the following circuit breaker (degrade) rules to the resource MyService#service1 on Sentinel Dashboard:
新增熔断规则
资源名:MyService#service1
熔断策略:异常比例
比例阈值:0.6
熔断时长:5
最小请求数:5
After adding the above circuit breaker rule, when you request http://localhost:8090/test more than 5 times, the circuit breaker is open. The response message is:
Blocked by Sentinel: DegradeException
Test flow control
Add the following flow rules to the resource MyService#service1 on the Sentinel Dashboard:
新增流控规则
资源名:MyService#service1
阈值类型:QPS
单机阈值:1
After adding the above circuit breaker rule, if the rate of requests to http://localhost:8090/test is over 1 request(s) per second, the circuit breaker is open. The response message is:
Blocked by Sentinel: DegradeException
Sentinel DataSource
The Sentinel rule configurations created by the Sentinel Dashboard are stored in memory. If you restart the application, the rule configurations will be lost.
Sentinel supports reading rules from files, databases, and configuration centers. For example: