Sentinel的簡單使用


Sentinel簡介:

隨着微服務的流行,服務和服務之間的穩定性變得越來越重要。Sentinel 以流量為切入點,從流量控制、熔斷降級、系統負載保護等多個維度保護服務的穩定性。

Sentinel 具有以下特征:

豐富的應用場景:Sentinel 承接了阿里巴巴近 10 年的雙十一大促流量的核心場景,例如秒殺(即突發流量控制在系統容量可以承受的范圍)、消息削峰填谷、集群流量控制、實時熔斷下游不可用應用等。

完備的實時監控:Sentinel 同時提供實時的監控功能。您可以在控制台中看到接入應用的單台機器秒級數據,甚至 500 台以下規模的集群的匯總運行情況。

廣泛的開源生態:Sentinel 提供開箱即用的與其它開源框架/庫的整合模塊,例如與 Spring Cloud、Dubbo、gRPC 的整合。您只需要引入相應的依賴並進行簡單的配置即可快速地接入 Sentinel。

完善的 SPI 擴展點:Sentinel 提供簡單易用、完善的 SPI 擴展接口。您可以通過實現擴展接口來快速地定制邏輯。例如定制規則管理、適配動態數據源等。

總而言之,Sentinel和Hystrix一樣,是為了房子雪崩效應引起系統故障而產生的。

搭建Sentinel控制台:

1.下載並運行sentinel的jar包:

可以去https://github.com/alibaba/Sentinel下載Sentinel的jar包。

然后使用java -jar xxxxx.jar命令運行Sentinel。

運行成功后訪問8080端口 結果如圖:

 

 

 初始賬號和密碼均為sentinel.

這樣,sentinel就配置好了。

將微服務注冊到Sentinel:

1.配置pom.xml:

<!--SpringCloud ailibaba sentinel -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency>

2.寫yml:
spring:
application:
name: sentinel-service-5555
cloud:
sentinel:
transport:
dashboard: localhost:8080 #sentineldashboard地址
port: 8719 #如果被占用會增加1
3.寫方法:
@RestController
public class RateLimitController {
@GetMapping("/resource")
@SentinelResource(value = "resource")
public CommonResult resource(){
return new CommonResult(200,"test",new Payment(2020l,"001"));
}
public CommonResult handler(BlockException blockException){
return new CommonResult(444,blockException.getClass().getName());
}
}
使用@SentinelResource(value = "resource") 將該服務命名。
啟動微服務,並訪問該服務的url,能看到:

 

 
        

 這樣能夠通過控制台對服務進行配置。

配置Sentinel對該微服務的限制規則:

 流控模式:

 

 QPS為每秒請求數,如果大於閾值,會觸發流控

而線程數是設置了調用該api的最大線程數,如果大於閾值,也會觸發流控。

流控模式:

關聯:關聯的資源達到閾值,限流自己。

 

 例如:當支付接口出現問題時,限流訂單接口。關聯資源達到閾值,限流資源名的接口。

鏈路:只記錄指定鏈路上的流量(指定資源從入口資源進來的流量,如果達到閾值,就可以限流)

流控效果:

預熱:根據codeFactor(冷加載因子,默認為3)的值,即請求 QPS 從 threshold / 3 開始,經預熱時長逐漸升至設定的 QPS 閾值。

例:

 

初始閾值為10/3,經過5秒的預熱,最終閾值為10 

排隊等待:如字面意思,每個請求在超時時間內排隊等待進行處理。

降級規則:

1.慢調用比例:最大RT(平均響應時間) 需要設置的閾值,超過該值則為慢應用。

比例閾值 慢調用占所有的調用的比率,范圍:[0~1]

熔斷時長 在這段時間內發生熔斷、拒絕所有請求

最小請求數 即允許通過的最小請求數,在該數量內不發生熔斷

2.異常比例: 異常比例閾值:發生異常請求數/總請求數

3.異常數:請求發生異常的數量

熱點規則限流:何為熱點?熱點即經常訪問的數據。很多時候我們希望統計某個熱點數據中訪問頻次最高的 Top K 數據,並對其訪問進行限制。

 

@GetMapping("/testHotKey")
@SentinelResource(value = "testHotKey",blockHandler = "dealProblem")
//blockhandler配置了當該方法違背熱點規則時,調用的方法。當違背熱點規則時,調用dealProblem方法。
public String HotKEY(@RequestParam(value = "p1",required = false) String p1,@RequestParam(value = "p2",required = false) String p2){
return "testHotKey";
}
public String dealProblem(String p1, String p2, BlockException exception){
return "22222";
}
配置熱點規則:

 

 這意味着,調用被熱點規則限制的api時,如果包含參數p1並超過QPS閾值,則會調用dealProbelm方法,無法正常訪問,不含p1則無QPS限制。如果參數索引改成1,則限制p2。

參數例外項:

 

 當熱點參數的參數類型為設置的參數類型的設置參數值,限流閾值為設置的閾值。

 系統規則:對整個系統進行保護設置。

 自定義Sentinel處理:

當限流或異常時,我們想要跳轉到自定義的頁面或自定義處理方法,這就需要我們使用@SentinelResource注解

在上文的熱點限流中,我們也已經使用過了@SentinelResource,現在我們再看看@SentinelResource的用法。

blockHandler:為服務設置觸發sentinel限流,降級時調用的方法,

@SentinelResource(value = "testHotKey",blockHandler = "dealProblem")
public String HotKEY(@RequestParam(value = "p1",required = false) String p1,@RequestParam(value = "p2",required = false) String p2){
   return "testHotKey";
}
   public String  dealProblem(String p1, String p2, BlockException exception){
    return "22222";
   }
當該api觸發sentinel設置的限流 降級規則時,將調用blokcHandler的dealProblem方法。
blockHandler 函數訪問范圍需要是 public,返回類型需要與原方法相匹配,

 

blockHandlerClass:

blockHandler默認調用方法在當前類,blockHandlerClass 設置調用方法所在的類。但是調用的函數必須為static函數。

例如:

@GetMapping("/customerBlockHandler")
@SentinelResource(value = "customerBlockHandler",blockHandlerClass= MyCustomerBlockHandler.class,blockHandler = "globalHandler")
public CommonResult customerBlockHandler(){
return new CommonResult(200,"test2",new Payment(2020l,"002"));
}
public class MyCustomerBlockHandler {

public static CommonResult globalHandler(BlockException blockException){
return new CommonResult(444,"test3");
}
}
當觸發sentinel設置的規則后,將調用MyCustomerBlockHandler.class類中的globalHandler方法進行處理。

fallback:

當產生異常,而不是觸發sentinel配置時,將調用設置的兜底方法。類似於hystrix中的fallbackMethod。不太清楚hystrix的朋友可以去看看我寫的hystrix博客。

 

fallbackClass:

作用和blockHandlerClass類似。

 

exceptionsToIgnore

 

指定哪些異常被排除,不進入 fallback 邏輯中,原樣拋出。

注:當同時被限流和降級,會調用blockHandler的方法,Sentinel熔斷OpenFeign和hystrix整合OpenFeign類似,唯一的區別是,把feign對hystrix的支持改為對sentinel的支持feign sentinel:

feign:
sentinel:
enabled: true


sentinel的規則持久化
配置的規則,在微服務重啟后消失,需要對其進行持久化。可以將持久化信息存入nacos中
1.引入依賴
<!--SpringCloud ailibaba sentinel-datasource-nacos 后續做持久化用到-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
配置yml:
spring:
application:
name: cloudalibaba-sentinel-service
cloud:
nacos:
discovery:
server-addr: localhost:8848 #Nacos服務注冊中心地址
sentinel:
transport:
dashboard: localhost:8080 #配置Sentinel dashboard地址
port: 8719
datasource:
ds1:
nacos:
server-addr: localhost:8848
dataId: cloudalibaba-sentinel-service
groupId: DEFAULT_GROUP
data-type: json
rule-type: flow
nacos在對應的命名空間創建配置文件,信息與配置一致,內容為:
[
    {
        "resource": "demo_test",
        "count": 1,
        "grade": 1,
        "limitApp": "default",
        "strategy": 0,
        "controlBehavior": 0
    }
]




 
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM