目标
演示 Spring Boot 应用接入 Sentinel,实现 QPS 限流 + 熔断降级 + 自定义 fallback,通过控制台实时下发规则。
完整代码
1. pom.xml
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2023.0.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2. application.yml
server:
port: 8080
spring:
application:
name: sentinel-demo
cloud:
sentinel:
transport:
dashboard: localhost:8080
port: 8719
eager: true
datasource:
flow:
nacos:
server-addr: localhost:8848
data-id: ${spring.application.name}-flow-rules
group-id: DEFAULT_GROUP
data-type: json
rule-type: flow
3. 控制器(限流 + 熔断)
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoController {
@GetMapping("/hello")
@SentinelResource(value = "hello", blockHandler = "helloBlock")
public String hello() {
return "Hello, Sentinel!";
}
public String helloBlock(BlockException ex) {
return "Too many requests! Please try later. " + ex.getClass().getSimpleName();
}
@GetMapping("/item/{id}")
@SentinelResource(value = "getItem", blockHandler = "itemBlock")
public String getItem(@PathVariable Long id) {
return "Item detail for ID: " + id;
}
public String itemBlock(@PathVariable Long id, BlockException ex) {
return "Item " + id + " is hot! Rate limited.";
}
@GetMapping("/risky")
@SentinelResource(value = "risky", fallback = "riskyFallback")
public String risky() {
if (System.currentTimeMillis() % 3 == 0) {
throw new RuntimeException("Simulated error");
}
return "OK from risky API";
}
public String riskyFallback(Throwable t) {
return "Fallback: Service degraded. " + t.getMessage();
}
@GetMapping("/heavy")
public String heavy() {
for (int i = 0; i < 1000000; i++) {
Math.sqrt(i);
}
return "Heavy computation done!";
}
}
4. 流控规则定义(Nacos 持久化)
[
{
"resource": "hello",
"grade": 1,
"count": 5,
"strategy": 0,
"controlBehavior": 0
},
{
"resource": "getItem",
"grade": 1,
"count": 10,
"strategy": 0,
"controlBehavior": 1,
"paramIdx": 0
}
]
运行步骤
java -jar sentinel-dashboard-1.8.7.jar
mvn spring-boot:run
curl http:
ab -n 100 -c 10 http:
for i in {1..20}
for i in {1..20}
预期输出
Too many requests! Please try later. FlowException
Item 42 is hot! Rate limited.
Fallback: Service degraded. RuntimeException: Simulated error
关键点
blockHandler 处理限流/熔断异常(BlockException)
fallback 处理业务异常(Throwable)
- Sentinel Dashboard 实时推送规则,无需重启
- 熔断半开状态:一段时间后尝试放行少量请求探测恢复