Spring Cloud Alibaba整合Sentinel的實現(xiàn)步驟
一、需求
實現(xiàn)一個簡單的 整合 sentinel,不涉及sentinel的用法
二、實現(xiàn)步驟
1、下載 sentinel dashboard
https://github.com/alibaba/Sentinel/releases
注意:
默認會啟動
8080端口,如果端口沖突,可以在啟動命令上加入-Dserver.port=新端口
默認用戶名和密碼[sentinel/sentinel]
啟動控制臺可用的配置項
2、服務提供者和消費者引入sentinel依賴
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency>
注意: 在這個里面查找 sentinel-core的版本號,可以確定我們需要下載那個版本的 sentinel dashboard
3、配置控制臺信息
spring: sentinel: transport: # 控制臺的地址 dashboard: localhost:8080 # 與控制臺通訊的端口,默認是8719,不可用會一直+1,知道找到一個可用的 port: 8719 # 和控制臺保持心跳的ip地址 client-ip: 127.0.0.1 # 發(fā)送心跳的周期,默認是10s heartbeat-interval-ms: 10000
4、一個簡答的整合就整合完了,訪問資源,查看控制臺。
三、整合Feigen
1、配置文件中加入 feign.sentinel.enabled=true 即可。
2、加入依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
3、對 Feign接口的降級或限流等操作時,資源名稱的格式為:httpmethod:protocol://requesturl
四、整合 RestTemplate
1、在RestTemplate上加入 @SentinelRestTemplate注解。
2、配置文件中加入 resttemplate.sentinel.enabled: true,默認就是 true
3、降級或限流時的處理
抄sentinel的官網(wǎng)描述,文本給出鏈接
@SentinelRestTemplate 注解的屬性支持限流(blockHandler, blockHandlerClass)和降級(fallback, fallbackClass)的處理。
其中 blockHandler 或 fallback 屬性對應的方法必須是對應 blockHandlerClass 或 fallbackClass 屬性中的靜態(tài)方法。
該方法的參數(shù)跟返回值跟 org.springframework.http.client.ClientHttpRequestInterceptor#interceptor 方法一致,其中參數(shù)多出了一個 BlockException 參數(shù)用于獲取 Sentinel 捕獲的異常。
比如上述 @SentinelRestTemplate 注解中 ExceptionUtil 的 handleException 屬性對應的方法聲明如下:
public class ExceptionUtil {
public static ClientHttpResponse handleException(HttpRequest request, byte[] body, ClientHttpRequestExecution execution, BlockException exception) {
...
}
}
五、@SentinelResource的用法
1、@SentinelResource 注解用來標識資源是否被限流、降級。
2、一般推薦將 @SentinelResource 注解加到服務實現(xiàn)上
3、可以指定 blockHandler 或 fallback ,在發(fā)生異常時的處理。
六、代碼路徑
https://gitee.com/huan1993/spring-cloud-alibaba-parent/tree/master/sentinel
七、參考文檔
到此這篇關(guān)于Spring Cloud Alibaba整合Sentinel的實現(xiàn)步驟的文章就介紹到這了,更多相關(guān)Spring Cloud Alibaba整合Sentinel內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot實現(xiàn)全局自定義異常的方法詳解
這篇文章主要介紹了Springboot實現(xiàn)全局自定義異常的方法詳解,SpringBoot的項目已經(jīng)對有一定的異常處理了,但是對于我們開發(fā)者而言可能就不太合適了,因此我們需要對這些異常進行統(tǒng)一的捕獲并處理,需要的朋友可以參考下2023-11-11
SpringBoot+MinIO實現(xiàn)對象存儲的示例詳解
MinIO?是一個基于Apache?License?v2.0開源協(xié)議的對象存儲服務,它是一個非常輕量的服務,可以很簡單的和其他應用的結(jié)合,所以下面我們就來看看SpringBoot如何整合MinIO實現(xiàn)對象存儲吧2023-10-10
Mybatis-Plus實現(xiàn)自定義SQL具體方法
Mybatis-Plus是Mybatis的一個增強工具,它可以優(yōu)化我們的開發(fā)效率,這篇文章主要介紹了Mybatis-Plus實現(xiàn)自定義SQL,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2023-08-08
Mybatis傳list參數(shù)調(diào)用oracle存儲過程的解決方法
怎么利用MyBatis傳List類型參數(shù)到數(shù)據(jù)庫存儲過程中實現(xiàn)批量插入數(shù)據(jù)?接下來通過本文給大家介紹Mybatis傳list參數(shù)調(diào)用oracle存儲過程,需要的朋友可以參考下2017-03-03

