本文涉及的基本概念:
幾種服務限流方式:
熔斷:拒絕流量訪問,當系統恢復正常時在關閉熔斷
服務降級:將次要服務降級,停止服務,將系統資源釋放出來給核心功能
延遲處理:在前端設置一個流程緩沖池,將所有的流程全部緩沖到這個池子不立即處理,常見隊列緩模式處理,服務端處理不及時會丟失部分請求
特權處理:優先處理需要高保障的請求,其他請求丟去或者延遲處理
Sentinel 的 能做什么?
可以通過 Sentinel 的控制台,我們可以對規則進行查詢和修改,也可以查看到實時監控,機器列表等信息
官方下載地址:https://github.com/alibaba/Sentinel/releases
啟動控制台
-Dproject.name=sentinel-dashboard 指定Sentinel控制台程序的名稱
-Dserver.port=8718 控制台端口,sentinel控制台是一個spring boot程序。
注:客戶端需向控制台提供端口,配置文件配置,如:spring.cloud.sentinel.transport.port=8718
-Dcsp.sentinel.dashboard.server=localhost:8718 控制台的地址,指定控制台后客戶端會自動向該地址發送心跳包。
-Dcsp.sentinel.api.port=8719 (默認8719)
注:客戶端提供給Dashboard訪問或者查看Sentinel的運行訪問的參數 。若啟動多個應用,則需要通過 -Dcsp.sentinel.api.port=xxxx 指定客戶端監控 API 的端口
注:
csp.sentinel.dashboard.server這個配置是用在客戶端,這里Sentinel控制台也使用是用於自己監控自己程序的api,否則無法顯示控制台的api情況,當然這個也可以根據情況不顯示。
csp.sentinel.api.port=8719 本地啟動 HTTP API Server 的端口號。是客戶端的端口,需要把客戶端設置的端口穿透防火牆,可在控制台的“機器列表”中查看到端口號,這里Sentinel控制台也使用是用於自己程序的api傳輸,由於是默認端口所以控制台也可以不設置。
控制台推送規則的日志在 :${user.home}/logs/csp/sentinel-dashboard.log 中,
客戶端接收規則日志在 ${user.home}/logs/csp/record.log 中
啟動配置wiki: https://github.com/alibaba/Sentinel/wiki/啟動配置項
spring cloud alibaba配置、整合feign、動態數據源支持 等的wiki:https://github.com/spring-cloud-incubator/spring-cloud-alibaba/wiki/Sentinel
java -Dserver.port=10090 -Dcsp.sentinel.dashboard.server=localhost:10090 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar
java -Dcsp.sentinel.api.port=10090 -Dserver.port=10090 -Dcsp.sentinel.dashboard.server=localhost:10090 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar
然后用瀏覽器訪問地址:http://localhost:10090
一:首先在項目里添加 sentinel 的 maven 依賴,如下:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> <version>0.2.1.RELEASE</version> </dependency>
可以加版本號,也可以省略版本號,具體如下:

配置控制台信息
在 application.yml 配置文件中:
spring: cloud: sentinel: transport: port: 8719 dashboard: localhost:8080
這里的 spring.cloud.sentinel.transport.port 端口配置會在應用對應的機器上啟動一個 Http Server,該 Server 會與 Sentinel 控制台做交互。比如 Sentinel 控制台添加了一個限流規則,會把規則數據 push 給這個 Http Server 接收,Http Server 再將規則注冊到 Sentinel 中。
動態數據源支持
SentinelProperties 內部提供了 TreeMap 類型的 datasource 屬性用於配置數據源信息。
在當前項目的 application.properties 中增加限流的配置
# 文件規則數據源
spring.cloud.sentinel.datasource.ds1.file.file=classpath: flowrule.json
# JSON格式的數據
spring.cloud.sentinel.datasource.ds1.file.data-type=json
# 規則類型
spring.cloud.sentinel.datasource.ds1.file.rule-type=flow
flowrule.json 的詳細內容:
[ { "resource": "hello", "controlBehavior": 0, "count": 1, "grade": 1, "limitApp": "default", "strategy": 0 } ]
@SentinelResource使用
@GetMapping("/test")
@SentinelResource(value="hello",blockHandler="handleException",blockHandlerClass=ExceptionUtil.class)
public String test() {
String result = restTemplate.getForObject("http://localhost:8087/user/name", String.class);
return result;
}
回退內容定義:
public class ExceptionUtil { public static String handleException(BlockException ex) { return "扛不住了啊...."; } }
前面我們使用注解的話都是手動配置SentinelResourceAspect類,為什么今天不需要配置SentinelResourceAspect呢?
那是因為在spring-cloud-alibaba中已經默認配置好了,代碼在org.springframework.cloud.alibaba.sentinel.custom.SentinelAutoConfiguration中,代碼如下:
@Bean @ConditionalOnMissingBean public SentinelResourceAspect sentinelResourceAspect() { return new SentinelResourceAspect(); }
