三步讓Dubbo項目快速集成Sentinel
本文接著《5小步快速集成使用sentinel限流》,繼續(xù)介紹Dubbo項目如何快速集成使用Sentinel。
1、環(huán)境和資源準備
環(huán)境和資源準備,參看《5小步快速集成使用sentinel限流》。
2、啟動sentinel-dashboard
下載sentinel-dashboard,然后執(zhí)行命令啟動:java -jar sentinel-dashboard-1.8.0.jar
啟動完畢后,通過http://localhost:8080/#/dashboard訪問dashboard,出現(xiàn)如下界面:
3、項目集成sentinel
項目中集成sentinel分如下3步。
3.1、引入pom
<!-- 這是sentinel的核心依賴 -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
<version>1.8.0</version>
</dependency>
<!-- 這是將自己項目和sentinel-dashboard打通的依賴 -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
<version>1.8.0</version>
</dependency>
<!-- 這是dubbo集成setinel的核心依賴 -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-dubbo-adapter</artifactId>
<version>1.8.0</version>
</dependency>
這里有一點要注意,如果有的項目之前依賴了spring-cloud-alibaba-dependencies,那要先從這個依賴里把sentinel相關依賴排除掉。不排除的情況下,可能會出現(xiàn)代碼配置流控規(guī)則不生效的問題。
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
<exclusions>
<exclusion>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-annotation-aspectj</artifactId>
</exclusion>
<exclusion>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-cluster-client-default</artifactId>
</exclusion>
<exclusion>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-cluster-common-default</artifactId>
</exclusion>
<exclusion>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-cluster-server-default</artifactId>
</exclusion>
<exclusion>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-extension</artifactId>
</exclusion>
<exclusion>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</exclusion>
<exclusion>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-parameter-flow-control</artifactId>
</exclusion>
<exclusion>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-reactor-adapter</artifactId>
</exclusion>
<exclusion>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-webflux-webflux-adapter</artifactId>
</exclusion>
<exclusion>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-webflux-webmvc-adapter</artifactId>
</exclusion>
<exclusion>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</exclusion>
<exclusion>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-webflux-webmvc-adapter</artifactId>
</exclusion>
<exclusion>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
</exclusion>
</exclusions>
</dependency>
3.2、增加sentinel.properties配置
在application.properties同級目錄下,增加sentinel.properties文件,配置內容如下:
# 集成到sentinel的項目名稱
project.name=dubbo-sentinel-demo
# 對應的sentinel-dashboard地址
csp.sentinel.dashboard.server=localhost:8080
同時需要加載sentinel.properties配置,有兩種加載方式,選擇一種即可,如下:
3.4、配置需要被限流的資源
文中我依舊使用代碼方式配置流控規(guī)則,在控制臺中也可以直接配置流控規(guī)則,為什么不使用控制臺方式呢?主要是應用服務器的地址會變化,詳細見上一篇文章《5小步快速集成使用sentinel限流》。
流控規(guī)則一般會有如下幾個:
- 資源限流規(guī)則FlowRule
- 異常熔斷降級規(guī)則DegradeRule
- 系統(tǒng)過載保護規(guī)則SystemRule
- 訪問黑白名單規(guī)則AuthorityRule
本文繼續(xù)使用資源限流規(guī)則做示例。
注意,Dubbo配置被限流資源時,不能使用打注解@SentinelResource的方式。要使用類名:方法名(參數(shù)類名)的方式設置資源名。比如這樣下面代碼里的RES_KEY或者INTERFACE_RES_KEY:
public class FooProviderBootstrap {
// 資源名-具體方法
private static final String RES_KEY = "com.alibaba.csp.sentinel.demo.dubbo.FooService:sayHello(java.lang.String)";
// 資源名-接口
private static final String INTERFACE_RES_KEY = "com.alibaba.csp.sentinel.demo.dubbo.FooService";
public static void main(String[] args) {
initFlowRule();
System.out.println("Service provider is ready");
}
private static void initFlowRule() {
FlowRule flowRule = new FlowRule();
flowRule.setResource(RES_KEY);
flowRule.setCount(1);
flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
flowRule.setLimitApp("default");
FlowRuleManager.loadRules(Collections.singletonList(flowRule));
}
}
4、啟動測試
測試流程與上一篇文章《5小步快速集成使用sentinel限流》類似。只不過這次還需要在搭建一個dubbo-consumer調用dubbo-provider。
Dubbo項目與Web項目展示的限流資源有所不同:
- 簇點鏈路:這里Web項目只會展示被限流的資源,而Dubbo只要有接口被調用就會被調用。
- 資源名稱:在展示資源名稱時,Web項目展示的是@SentinelResource設置名稱,或者代碼里設置的資源名,而Dubbo項目只能展示完整的類名+方法名。
- 資源展示效果如下:
限流效果如下:
5、總結
本文主要介紹Dubbo項目如何快速集成Sentinel實現(xiàn)系統(tǒng)限流。整體步驟和上一篇文章《5小步快速集成使用sentinel限流》類似。只不過有2點需要注意:
- 老的sentinel依賴要排除掉,采用新的依賴,不排除的話,可能會導致代碼里配置的流控規(guī)則不生效。
- 代碼設置資源時flowRule.setResource(RES_KEY);,資源定義的格式類名:方法名(參數(shù)類名),注意類名和參數(shù)類名都需要帶上完整路徑。不能直接在接口或者實現(xiàn)類的方法上打注解@SentinelResource。