继续细化一下后续步骤中的 熔断与限流(Sentinel): 在网关或服务层配置QPS限流, 定义熔断规则,防止服务雪崩。希望能给大家带来帮助。
下载 Sentinel Dashboard:
wget https://github.com/alibaba/Sentinel/releases/download/1.8.6/sentinel-dashboard-1.8.6.jar
启动控制台:
java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -jar sentinel-dashboard-1.8.6.jar
http://localhost:8080
,默认账号密码:sentinel/sentinel
。
<!-- 网关模块的 pom.xml -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
# application.yml
spring:
cloud:
sentinel:
transport:
dashboard: localhost:8080 # Sentinel控制台地址
eager: true # 立即向控制台发送心跳
# 网关限流配置
scg:
enabled: true
动态规则(通过代码初始化):
@PostConstruct
public void initGatewayRules() {
Set<GatewayFlowRule> rules = new HashSet<>();
// 对 /api/user/** 路径限流,QPS=2
rules.add(new GatewayFlowRule("user_service_route")
.setResourceMode(SentinelGatewayConstants.RESOURCE_MODE_ROUTE_ID)
.setCount(2)
.setIntervalSec(1));
GatewayRuleManager.loadRules(rules);
}
动态规则(通过控制台配置):
user_service_route
)。
<!-- 用户服务模块的 pom.xml -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
# application.yml
spring:
cloud:
sentinel:
transport:
dashboard: localhost:8080
eager: true
@Service
public class UserService {
// 标记需要保护的资源
@SentinelResource(
value = "getUserById",
blockHandler = "handleBlock", // 限流/降级处理
fallback = "handleFallback" // 异常处理
)
public User getUserById(Long id) {
// 模拟数据库调用(实际业务中可能抛出异常)
if (id < 0) throw new RuntimeException("Invalid User ID");
return userMapper.selectById(id);
}
// 限流/降级处理逻辑
public User handleBlock(Long id, BlockException ex) {
return new User(-1L, "blocked_user", "请求过于频繁,请稍后重试!");
}
// 异常处理逻辑
public User handleFallback(Long id, Throwable t) {
return new User(-1L, "fallback_user", "服务暂时不可用:" + t.getMessage());
}
}
getUserById
测试工具:使用 JMeter
或 curl
连续发送请求:
# 连续发送5次请求
for i in {1..5}; do curl http://localhost:8888/api/user/info; done
预期结果:
429 Too Many Requests
。
触发异常:
curl http://localhost:8081/user/-1 # 传入非法ID触发异常
观察熔断:
handleFallback
逻辑。
// 网关模块添加自定义限流处理器
@Component
public class SentinelBlockHandler implements BlockRequestHandler {
@Override
public Mono<ServerResponse> handleRequest(ServerWebExchange exchange, Throwable t) {
String msg = "请求被限流,请稍后重试";
if (t instanceof DegradeException) {
msg = "服务降级中,请稍后重试";
}
return ServerResponse.status(429)
.contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromValue(Response.fail(429, msg)));
}
}
添加依赖:
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
配置Nacos数据源:
spring:
cloud:
sentinel:
datasource:
ds:
nacos:
server-addr: localhost:8848
dataId: sentinel-rules
groupId: DEFAULT_GROUP
rule-type: flow # 规则类型(flow/degrade)
通过以上步骤,您的脚手架已集成 熔断与限流 能力,能够有效应对高并发和依赖故障场景。
本篇的分享就到这里了,感谢观看,如果对你有帮助,别忘了点赞+收藏+关注。 后续将继续把后续步骤继续完善,有什么遗漏、或者对什么功能有缺失的可以评论区,指出来,共同成长共同进步。