當前項目源碼
控制臺下載
啟動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;/*** 開啟注解*/@Beanpublic SentinelResourceAspect sentinelResourceAspect() {return new SentinelResourceAspect();}/*** 加載規則*/public void loadRules() {log.info("加載限流規則");FlowRuleManager.loadRules(rules);ParamFlowRuleManager.loadRules(params);}@Overridepublic void afterPropertiesSet() {triggerSentinelInit();loadRules();}/*** 觸發sentinel初始化。*/private static void triggerSentinelInit() {new Thread(InitExecutor::doInit).start();}}
- application.yml配置文件添加內容
sentinel:# 資源限流rules:- resource: "test"count: 1grade: 1# 參數限流params:- resource: "test"param-idx: 0count: 1grade: 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)@ResponseBodypublic 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