Spring Cloud Alibaba整合Sentinel的實(shí)現(xiàn)步驟
實(shí)現(xiàn)一個(gè)簡(jiǎn)單的 整合 sentinel,不涉及sentinel的用法
二、實(shí)現(xiàn)步驟1、下載 sentinel dashboard
https://github.com/alibaba/Sentinel/releases 注意:
默認(rèn)會(huì)啟動(dòng)8080端口,如果端口沖突,可以在啟動(dòng)命令上加入 -Dserver.port=新端口
默認(rèn)用戶名和密碼[sentinel/sentinel]啟動(dòng)控制臺(tái)可用的配置項(xiàng)
2、服務(wù)提供者和消費(fèi)者引入sentinel依賴
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency>
注意: 在這個(gè)里面查找 sentinel-core的版本號(hào),可以確定我們需要下載那個(gè)版本的 sentinel dashboard
3、配置控制臺(tái)信息
spring: sentinel: transport: # 控制臺(tái)的地址 dashboard: localhost:8080 # 與控制臺(tái)通訊的端口,默認(rèn)是8719,不可用會(huì)一直+1,知道找到一個(gè)可用的 port: 8719 # 和控制臺(tái)保持心跳的ip地址 client-ip: 127.0.0.1 # 發(fā)送心跳的周期,默認(rèn)是10s heartbeat-interval-ms: 10000
4、一個(gè)簡(jiǎn)答的整合就整合完了,訪問資源,查看控制臺(tái)。
三、整合Feigen1、配置文件中加入 feign.sentinel.enabled=true 即可。2、加入依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId></dependency>
3、對(duì) Feign接口的降級(jí)或限流等操作時(shí),資源名稱的格式為:httpmethod:protocol://requesturl
四、整合 RestTemplate1、在RestTemplate上加入 @SentinelRestTemplate注解。2、配置文件中加入 resttemplate.sentinel.enabled: true,默認(rèn)就是 true3、降級(jí)或限流時(shí)的處理
抄sentinel的官網(wǎng)描述,文本給出鏈接
@SentinelRestTemplate 注解的屬性支持限流(blockHandler, blockHandlerClass)和降級(jí)(fallback, fallbackClass)的處理。其中 blockHandler 或 fallback 屬性對(duì)應(yīng)的方法必須是對(duì)應(yīng) blockHandlerClass 或 fallbackClass 屬性中的靜態(tài)方法。該方法的參數(shù)跟返回值跟 org.springframework.http.client.ClientHttpRequestInterceptor#interceptor 方法一致,其中參數(shù)多出了一個(gè) BlockException 參數(shù)用于獲取 Sentinel 捕獲的異常。比如上述 @SentinelRestTemplate 注解中 ExceptionUtil 的 handleException 屬性對(duì)應(yīng)的方法聲明如下:public class ExceptionUtil { public static ClientHttpResponse handleException(HttpRequest request, byte[] body, ClientHttpRequestExecution execution, BlockException exception) { ... }}五、@SentinelResource的用法
1、@SentinelResource 注解用來標(biāo)識(shí)資源是否被限流、降級(jí)。2、一般推薦將 @SentinelResource 注解加到服務(wù)實(shí)現(xiàn)上3、可以指定 blockHandler 或 fallback ,在發(fā)生異常時(shí)的處理。
六、代碼路徑https://gitee.com/huan1993/spring-cloud-alibaba-parent/tree/master/sentinel
七、參考文檔Sentinel WikeSentinel 注解支持
到此這篇關(guān)于Spring Cloud Alibaba整合Sentinel的實(shí)現(xiàn)步驟的文章就介紹到這了,更多相關(guān)Spring Cloud Alibaba整合Sentinel內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 利用CSS3新特性創(chuàng)建透明邊框三角2. html清除浮動(dòng)的6種方法示例3. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)4. vue實(shí)現(xiàn)將自己網(wǎng)站(h5鏈接)分享到微信中形成小卡片的超詳細(xì)教程5. 不要在HTML中濫用div6. 使用css實(shí)現(xiàn)全兼容tooltip提示框7. 詳解CSS偽元素的妙用單標(biāo)簽之美8. JavaScript數(shù)據(jù)類型對(duì)函數(shù)式編程的影響示例解析9. CSS代碼檢查工具stylelint的使用方法詳解10. Vue3使用JSX的方法實(shí)例(筆記自用)
