启动bin中的看板服务:账号密码:sentinel/sentinel
项目引入依赖
<!-- sentinel注解支持 -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-annotation-aspectj</artifactId>
<version>1.8.8</version>
</dependency>
<!-- 参数限流 -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-parameter-flow-control</artifactId>
<version>1.8.8</version>
</dependency>
<!-- 限流客户端插件,默认读取资源配置sentinel.properties -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
<version>1.8.8</version>
</dependency>
SpringBoot中自动配置
- 创建配置类
/**
* 容器启动成功后配置Sentinel规则
*/
@Data
@Slf4j
@Configuration
@ConfigurationProperties(prefix = "sentinel")
public class SentinelConfig implements InitializingBean {
private List<FlowRule> rules;
private List<ParamFlowRule> params;
/**
* 开启注解
*/
@Bean
public SentinelResourceAspect sentinelResourceAspect() {
return new SentinelResourceAspect();
}
/**
* 加载规则
*/
public void loadRules() {
log.info("加载限流规则");
FlowRuleManager.loadRules(rules);
ParamFlowRuleManager.loadRules(params);
}
@Override
public void afterPropertiesSet() {
triggerSentinelInit();
loadRules();
}
/**
* 触发sentinel初始化。
*/
private static void triggerSentinelInit() {
new Thread(InitExecutor::doInit).start();
}
}
- application.yml配置文件添加内容
sentinel:
# 资源限流
rules:
- resource: "test"
count: 1
grade: 1
# 参数限流
params:
- resource: "test"
param-idx: 0
count: 1
grade: 1
- 资源目录中添加sentinel配置sentinel.properties
- sentinel-transport-simple-http插件自动读取这个文件
project.name=sentinel-test
# 配置sentinel控制台地址
csp.sentinel.dashboard.server=localhost:8181
创建测试接口
@Slf4j
@RestController
@ControllerAdvice // 使用当前类配置异常拦截
@RequestMapping("test")
public class TestController {
@GetMapping
@SentinelResource(
value = "test", // 资源名称
blockHandler = "testBlock",// 规则异常
blockHandlerClass = TestFallbackHandler.class,
fallback = "testFallback", // 业务异常降级
fallbackClass = TestFallbackHandler.class,
exceptionsToIgnore = {CustomException.class} // 忽略异常,比如自定义异常可能在@ControllerAdvice配置
)
public String test(String value) {
if ("admin".equalsIgnoreCase(value)) {
throw new CustomException("管理员不能操作");
}
// 模拟未知异常
if (Math.random() > 0.8) {
throw new RuntimeException("其他异常情况");
}
log.info("request test path :{}", value);
return "访问成功";
}
/**
* 拦截CustomException异常
*/
@ExceptionHandler(CustomException.class)
@ResponseBody
public String handleCustomException(CustomException ex) {
return "自定义异常:" + ex.getMessage();
}
/**
* 自定义异常
*/
public static class CustomException extends RuntimeException {
public CustomException(String message) {
super(message);
}
}
/**
* 熔断降级单独类处理
*/
public static class TestFallbackHandler {
/**
* 规则熔断处理
*
* @param value 这里参数需要跟原参数对应,否则不能触发熔断方法
* @param ex 必须有异常参数
*/
public static String testBlock(String value, BlockException ex) {
return "当前访问过于频繁,请稍后再试";
}
/**
* 业务降级处理
*
* @param value 这里参数需要跟原参数对应,否则不能触发熔断方法
* @param ex 必须有异常参数
*/
public static String testFallback(String value, Throwable ex) {
log.warn("触发熔断降级,value={}, 异常类型: {}", value, ex.getClass().getName());
return "降级处理:" + ex.getClass().getName();
}
}
}
测试
访问http://localhost:8080/test 应该返回”访问成功“
访问http://localhost:8080/test?value=admin 应该返回”自定义异常:管理员不能操作“ 的异常信息
频繁访问http://localhost:8080/test 应该返回”当前访问过于频繁,请稍后再试“
访问http://localhost:8080/test?value=test 随机数大于0.8 ”降级处理:java.lang.RuntimeException“
控制台
- 启动.bat脚本
title sentinel-dashboard
java -Dserver.port=8181 ^
-Dcsp.sentinel.dashboard.server=localhost:8181 ^
-Dproject.name=sentinel-dashboard ^
-jar sentinel-dashboard-1.8.8.jar