限流組件Sentinel
- Sentinel是把流量作為切入點,從流量控制碉克、熔斷降級凌唬、系統(tǒng)負載保護等多個維度保護服務(wù)的穩(wěn)定性。
- 默認支持 Servlet漏麦、Feign客税、RestTemplate、Dubbo 和 RocketMQ 限流降級功能的接入撕贞,可以在運行時通過控制臺實時修改限流降級規(guī)則更耻,還支持查看限流降級 Metrics 監(jiān)控。
- 自帶控臺動態(tài)修改限流策略捏膨。但是每次服務(wù)重啟后就丟失了秧均。所以它也支持ReadableDataSource 目前支持file, nacos, zk, apollo 這4種類型
接入Sentinel
創(chuàng)建項目cloud-sentinel
- 1 引入 Sentinel starter
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
- 2 application.properties配置如下
server.port=18084
spring.application.name=service-sentinel
#Sentinel 控制臺地址
spring.cloud.sentinel.transport.dashboard=localhost:8080
#取消Sentinel控制臺懶加載
spring.cloud.sentinel.eager=true
接入限流埋點
Sentinel 默認為所有的 HTTP 服務(wù)提供了限流埋點。引入依賴后自動完成所有埋點号涯。只需要在控制配置限流規(guī)則即可
- 注解埋點
如果需要對某個特定的方法進行限流或降級目胡,可以通過 @SentinelResource 注解來完成限流的埋點
@SentinelResource("resource")
@RequestMapping("/sentinel/hello")
public Map<String,Object> hello(){
Map<String,Object> map=new HashMap<>(2);
map.put("appName",appName);
map.put("method","hello");
return map;
}
部署Sentinel控制臺
安裝
啟動控制臺
執(zhí)行 Java 命令 java -jar sentinel-dashboard.jar
默認的監(jiān)聽端口為 8080
訪問
打開http://localhost:8080 即可看到控制臺界面
說明cloud-sentinel已經(jīng)成功和Sentinel完成率通訊
配置限流規(guī)則
如果控制臺沒有找到自己的應(yīng)用,可以先調(diào)用一下進行了 Sentinel 埋點的 URL 或方法或著禁用Sentinel 的賴加載spring.cloud.sentinel.eager=true
配置 URL 限流規(guī)則
控制器隨便添加一個普通的http方法
/**
* 通過控制臺配置URL 限流
* @return
*/
@RequestMapping("/sentinel/test")
public Map<String,Object> test(){
Map<String,Object> map=new HashMap<>(2);
map.put("appName",appName);
map.put("method","test");
return map;
}
點擊新增流控規(guī)則诚隙。為了方便測試閥值設(shè)為 1
瀏覽器重復(fù)請求 http://localhost:18084/sentinel/test 如果超過閥值就會出現(xiàn)如下界面
整個URL限流就完成了讶隐。但是返回的提示不夠友好。
配置自定義限流規(guī)則(@SentinelResource埋點)
自定義限流規(guī)則就不是添加方法的訪問路徑久又。 配置的是@SentinelResource注解中value的值。比如@SentinelResource("resource")
就是配置路徑為resource
- 訪問:http://localhost:18084/sentinel/hello
- 通過
@SentinelResource
注解埋點配置的限流規(guī)則如果沒有自定義處理限流邏輯效五,當(dāng)請求到達限流的閥值時就返回404頁面
image
自定義限流處理邏輯
@SentinelResource 注解包含以下屬性:
- value: 資源名稱地消,必需項(不能為空)
- entryType: 入口類型,可選項(默認為 EntryType.OUT)
- blockHandler:blockHandlerClass中對應(yīng)的異常處理方法名畏妖。參數(shù)類型和返回值必須和原方法一致
- blockHandlerClass:自定義限流邏輯處理類
//通過注解限流并自定義限流邏輯
@SentinelResource(value = "resource2", blockHandler = "handleException", blockHandlerClass = {ExceptionUtil.class})
@RequestMapping("/sentinel/test2")
public Map<String,Object> test2() {
Map<String,Object> map=new HashMap<>();
map.put("method","test2");
map.put("msg","自定義限流邏輯處理");
return map;
}
public class ExceptionUtil {
public static Map<String,Object> handleException(BlockException ex) {
Map<String,Object> map=new HashMap<>();
System.out.println("Oops: " + ex.getClass().getCanonicalName());
map.put("Oops",ex.getClass().getCanonicalName());
map.put("msg","通過@SentinelResource注解配置限流埋點并自定義處理限流后的邏輯");
return map;
}
}
控制臺新增resource2的限流規(guī)則并設(shè)置閥值為1脉执。訪問http://localhost:18084/sentinel/test2 請求到達閥值時機會返回自定義的異常消息
基本的限流處理就完成了。 但是每次服務(wù)重啟后 之前配置的限流規(guī)則就會被清空因為是內(nèi)存態(tài)的規(guī)則對象.所以下面就要用到Sentinel一個特性ReadableDataSource 獲取文件戒劫、數(shù)據(jù)庫或者配置中心是限流規(guī)則
讀取文件的實現(xiàn)限流規(guī)則
一條限流規(guī)則主要由下面幾個因素組成:
- resource:資源名半夷,即限流規(guī)則的作用對象
- count: 限流閾值
- grade: 限流閾值類型(QPS 或并發(fā)線程數(shù))
- limitApp: 流控針對的調(diào)用來源,若為 default 則不區(qū)分調(diào)用來源
- strategy: 調(diào)用關(guān)系限流策略
- controlBehavior: 流量控制效果(直接拒絕迅细、Warm Up巫橄、勻速排隊)
SpringCloud alibaba集成Sentinel后只需要在配置文件中進行相關(guān)配置,即可在 Spring 容器中自動注冊 DataSource茵典,這點很方便湘换。配置文件添加如下配置
#通過文件讀取限流規(guī)則
spring.cloud.sentinel.datasource.ds1.file.file=classpath: flowrule.json
spring.cloud.sentinel.datasource.ds1.file.data-type=json
spring.cloud.sentinel.datasource.ds1.file.rule-type=flow
在resources新建一個文件 比如flowrule.json 添加限流規(guī)則
[
{
"resource": "resource",
"controlBehavior": 0,
"count": 1,
"grade": 1,
"limitApp": "default",
"strategy": 0
},
{
"resource": "resource3",
"controlBehavior": 0,
"count": 1,
"grade": 1,
"limitApp": "default",
"strategy": 0
}
]
重新啟動項目。出現(xiàn)如下日志說明文件讀取成功
[Sentinel Starter] DataSource ds1-sentinel-file-datasource start to loadConfig
[Sentinel Starter] DataSource ds1-sentinel-file-datasource load 2 FlowRule
刷新Sentinel 控制臺 限流規(guī)則就會自動添加進去
Sentinel的基本配置
spring.cloud.sentinel.enabled #Sentinel自動化配置是否生效
spring.cloud.sentinel.eager #取消Sentinel控制臺懶加載
spring.cloud.sentinel.transport.dashboard #Sentinel 控制臺地址
spring.cloud.sentinel.transport.heartbeatIntervalMs #應(yīng)用與Sentinel控制臺的心跳間隔時間
spring.cloud.sentinel.log.dir #Sentinel 日志文件所在的目錄