Spring Cloud Alibaba教程之Sentinel的使用
什么是Sentinel
Sentinel,中文翻譯為哨兵,是為微服務(wù)提供流量控制、熔斷降級的功能,它和Hystrix提供的功能一樣,可以有效的解決微服務(wù)調(diào)用產(chǎn)生的“雪崩”效應(yīng),為微服務(wù)系統(tǒng)提供了穩(wěn)定性的解決方案。隨著Hytrxi進入了維護期,不再提供新功能,Sentinel是一個不錯的替代方案。通常情況,Hystrix采用線程池對服務(wù)的調(diào)用進行隔離,Sentinel才用了用戶線程對接口進行隔離,二者相比,Hystrxi是服務(wù)級別的隔離,Sentinel提供了接口級別的隔離,Sentinel隔離級別更加精細,另外Sentinel直接使用用戶線程進行限制,相比Hystrix的線程池隔離,減少了線程切換的開銷。另外Sentinel的DashBoard提供了在線更改限流規(guī)則的配置,也更加的優(yōu)化。
從官方文檔的介紹,Sentinel 具有以下特征:
- 豐富的應(yīng)用場景: Sentinel 承接了阿里巴巴近 10 年的雙十一大促流量的核心場景,例如秒殺(即突發(fā)流量控制在系統(tǒng)容量可以承受的范圍)、消息削峰填谷、實時熔斷下游不可用應(yīng)用等。
- 完備的實時監(jiān)控: Sentinel 同時提供實時的監(jiān)控功能。您可以在控制臺中看到接入應(yīng)用的單臺機器秒級數(shù)據(jù),甚至 500 臺以下規(guī)模的集群的匯總運行情況。
- 廣泛的開源生態(tài): Sentinel 提供開箱即用的與其它開源框架/庫的整合模塊,例如與 Spring Cloud、Dubbo、gRPC 的整合。您只需要引入相應(yīng)的依賴并進行簡單的配置即可快速地接入 Sentinel。
- 完善的 SPI 擴展點: Sentinel 提供簡單易用、完善的 SPI 擴展點。您可以通過實現(xiàn)擴展點,快速的定制邏輯。例如定制規(guī)則管理、適配數(shù)據(jù)源等。
如何在Spring Cloud中使用Sentinel
Sentinel作為Spring Cloud Alibaba的組件之一,在Spring Cloud項目中使用它非常的簡單?,F(xiàn)在以案例的形式來講解如何在Spring Cloud項目中使用Sentinel。本項目是在之前nacos教程的案例基礎(chǔ)上進行改造。在工程的pom文件加上sentinel的Spring Cloud起步依賴,代碼如下:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> <version>0.9.0.RELEASE</version> </dependency>
在工程的配置文件application.yml文件中配置,需要新增2個配置:
- spring.cloud.sentinel.transport.port: 8719 ,這個端口配置會在應(yīng)用對應(yīng)的機器上啟動一個 Http Server,該 Server 會與 Sentinel 控制臺做交互。比如 Sentinel 控制臺添加了1個限流規(guī)則,會把規(guī)則數(shù)據(jù) push 給這個 Http Server 接收,Http Server 再將規(guī)則注冊到 Sentinel 中。
- spring.cloud.sentinel.transport.dashboard: 8080,這個是Sentinel DashBoard的地址。
server: port: 8762 spring: application: name: nacos-provider cloud: nacos: discovery: server-addr: 127.0.0.1:8848 sentinel: transport: port: 8719 dashboard: localhost:8080
寫一個RestController,在接口上加上SentinelResource注解就可以了。
@RestController public class ProviderController { @GetMapping("/hi") @SentinelResource(value="hi") public String hi(@RequestParam(value = "name",defaultValue = "forezp",required = false)String name){ return "hi "+name; } }
關(guān)于@SentinelResource 注解,有以下的屬性:
- value:資源名稱,必需項(不能為空)
- entryType:entry 類型,可選項(默認為 EntryType.OUT)
- blockHandler / blockHandlerClass: blockHandler 對應(yīng)處理 BlockException 的函數(shù)名稱,可選項
- fallback:fallback 函數(shù)名稱,可選項,用于在拋出異常的時候提供 fallback 處理邏輯。
啟動Nacos,并啟動nacos-provider項目。文末有源碼下載鏈接。
Sentinel DashBoard
Sentinel 控制臺提供一個輕量級的控制臺,它提供機器發(fā)現(xiàn)、單機資源實時監(jiān)控、集群資源匯總,以及規(guī)則管理的功能.
Sentinel DashBoard下載地址:https://github.com/alibaba/Sentinel/releases
下載完成后,以以下的命令啟動
java -jar sentinel-dashboard-1.6.1.jar
默認啟動端口為8080,可以-Dserver.port=8081的形式改變默認端口。啟動成功后,在瀏覽器上訪問localhost:8080,就可以顯示Sentinel的登陸界面,登陸名為sentinel,密碼為sentinel。
登陸sentinel dashboard成功后,并多次訪問nacos-provider的localhost:8080/hi接口,在nacos訪問信息如下:
sentinel dashboard顯示了nacos-provider的接口資源信息。
在/hi資源處設(shè)置接口的限流功能,在“+流控”按鈕點擊開設(shè)置界面如下,設(shè)置閾值類型為 qps,單機閾值為2。
設(shè)置成功后可以在流控規(guī)則這一欄進行查看,如圖所示:
測試
多次快速訪問nacos-provider的接口資源http://localhost:8762/hi,可以發(fā)現(xiàn)偶爾出現(xiàn)以下的信息:
Blocked by Sentinel (flow limiting)
正常的返回邏輯為
hi forezp
由以上可只,接口資源/hi的限流規(guī)則起到了作用。
在FeignClient中使用Sentinel
Hystrix默認集成在Spring Cloud 的Feign Client組件中,Sentinel也可以提供這樣的功能。現(xiàn)以案例的形式來講解如何在FeignClient中使用Sentinel,z本案例是在之前的nacos教程案例的nacos-consumer工程上進行改造,除了引入spring-cloud-starter-alibaba-sentinel,還需要引入spring-cloud-starter-openfeign,代碼如下:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> <version>0.9.0.RELEASE</version> </dependency>
在配置文件中需要加上sentinel.transport. dashboard配置外,還需要加上feign.sentinel.enabled的配置,代碼如下:
server: port: 8763 spring: application: name: nacos-consumer cloud: nacos: discovery: server-addr: 127.0.0.1:8848 sentinel: transport: port: 8719 dashboard: localhost:8080 feign.sentinel.enabled: true
寫一個FeignClient,調(diào)用nacos-provider的/hi接口:
@FeignClient("nacos-provider") public interface ProviderClient { @GetMapping("/hi") String hi(@RequestParam(value = "name", defaultValue = "forezp", required = false) String name); }
寫一個RestController調(diào)用ProviderClient,代碼如下:
@RestController public class ConsumerController { @Autowired ProviderClient providerClient; @GetMapping("/hi-feign") public String hiFeign(){ return providerClient.hi("feign"); } }
在FeignClient中,Sentinel為Feign調(diào)用生成了資源名策略定義,定義規(guī)則為httpmethod:protocol://requesturl。啟動nacos-consumer工程,在Sentinel DashBoard生成了如下的資源信息:
添加流控,QPS為2,在瀏覽器上快速多次點擊訪問http://localhost:8763/hi-feign,瀏覽器在正常情況下是能夠正常返回如下的信息:
hi feign
在被限流的時候返回錯誤信息。
需要注意的是,被限流的時候FeignClient并不會調(diào)用nacos-provider的接口,而是在nacos-consumer工程里直接報錯。
源碼下載
參考資料
https://github.com/alibaba/Sentinel/releases
https://github.com/alibaba/Sentinel/tree/master/sentinel-dashboard
https://github.com/spring-cloud-incubator/spring-cloud-alibaba/wiki/Sentinel
https://github.com/alibaba/Sentinel/wiki/控制臺
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
- Spring Cloud Alibaba之Sentinel實現(xiàn)熔斷限流功能
- Spring Cloud Alibaba 使用 Feign+Sentinel 完成熔斷的示例
- SpringCloud-Alibaba-Sentinel-配置持久化策略詳解
- SpringCloud-Alibaba-Sentinel服務(wù)降級,熱點限流,服務(wù)熔斷
- Spring Cloud Alibaba整合Sentinel的實現(xiàn)步驟
- Spring Cloud Alibaba使用Sentinel實現(xiàn)接口限流
- 淺談如何在項目中使用Spring Cloud Alibaba Sentinel組件
相關(guān)文章
SpringBoot Security實現(xiàn)單點登出并清除所有token
Spring Security是一個功能強大且高度可定制的身份驗證和訪問控制框架。提供了完善的認證機制和方法級的授權(quán)功能。是一款非常優(yōu)秀的權(quán)限管理框架。它的核心是一組過濾器鏈,不同的功能經(jīng)由不同的過濾器2023-01-01Java使用icepdf將pdf文件按頁轉(zhuǎn)成圖片
這篇文章主要為大家詳細介紹了Java使用icepdf將pdf文件按頁轉(zhuǎn)成圖片,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12Java8使用Supplier啟動ScheduledThread代碼實例
這篇文章主要介紹了Java8使用Supplier啟動ScheduledThread詳解,項目開啟立即啟動定時任務(wù)是很多項目都會遇到的一個需求,如何利用Java提供的函數(shù)優(yōu)雅的寫出來十分考驗一個人的功底,需要的朋友可以參考下2024-01-01關(guān)于mybatis if else if 條件判斷SQL片段表達式取值和拼接問題
這篇文章主要介紹了mybatis if else if 條件判斷SQL片段表達式取值和拼接,文章通過自己真實使用的例子給大家詳細介紹,需要的朋友可以參考下2021-09-09解決Mybatis出現(xiàn)報錯Error querying database.Cause: j
這篇文章主要介紹了解決Mybatis出現(xiàn)報錯Error querying database.Cause: java.lang.IndexOutOfBoundsException: Index 9 out of,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05Spring使用AspectJ注解和XML配置實現(xiàn)AOP
這篇文章主要介紹了Spring使用AspectJ注解和XML配置實現(xiàn)AOP的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10Java中for(;;)和while(true)的區(qū)別
這篇文章主要介紹了 Java中for(;;)和while(true)的區(qū)別,文章圍繞for(;;)和while(true)的相關(guān)自來哦展開詳細內(nèi)容,需要的小伙伴可以參考一下,希望對大家有所幫助2021-11-11