项目说明

本项目演示如何使用 Sentinel starter 完成 Dubbo 应用的限流管理。

Sentinel 是阿里巴巴开源的分布式系统的流量防卫组件,Sentinel 把流量作为切入点,从流量控制,熔断降级,系统负载保护等多个维度保护服务的稳定性。

Dubbo是一款高性能Java RPC框架,有对应的SpringBoot工程。

本项目专注于Sentinel与Dubbo的整合,关于Sentinel的更多特性可以查看sentinel-core-example。

示例

如何接入

在启动示例进行演示之前,我们先了解一下 Dubbo 如何接入 Sentinel。
注意 本章节只是为了便于您理解接入方式,本示例代码中已经完成接入工作,您无需再进行修改。

  1. 首先,修改 pom.xml 文件,引入 Sentinel starter 和 Dubbo starter。

    1. <dependency>
    2. <groupId>com.alibaba.cloud</groupId>
    3. <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    4. </dependency>
    5. <dependency>
    6. <groupId>com.alibaba.boot</groupId>
    7. <artifactId>dubbo-spring-boot-starter</artifactId>
    8. </dependency>
  1. 配置限流规则
    Sentinel提供了sentinel-dubbo-adapter模块用来支持Dubbo服务调用的限流降级。sentinel-starter默认也集成了该功能。
    sentinel-dubbo-adapter内部的Dubbo Filter会根据资源名进行限流降级处理。只需要配置规则即可:
    1. FlowRule flowRule = new FlowRule();
    2. flowRule.setResource("dubboResource");
    3. flowRule.setCount(10);
    4. flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
    5. flowRule.setLimitApp("default");
    6. FlowRuleManager.loadRules(Collections.singletonList(flowRule));

服务定义及发布

Provider端在application.properties文件中定义dubbo相关的配置,比如协议,注册中心:

  1. spring.application.name = dubbo-provider-demo
  2. foo.service.version = 1.0.0
  3. dubbo.scan.basePackages = com.alibaba.cloud.examples
  4. dubbo.application.id = dubbo-provider-demo
  5. dubbo.application.name = dubbo-provider-demo
  6. dubbo.protocol.id = dubbo
  7. dubbo.protocol.name = dubbo
  8. dubbo.protocol.port = 12345
  9. dubbo.protocol.status = server
  10. dubbo.registry.id = my-registry
  11. dubbo.registry.address = N/A

sentinel-dubbo-api模块中定义了FooService服务,内容如下:

  1. package com.alibaba.cloud.examples;
  2. public interface FooService {
  3. String hello(String name);
  4. }

定义具体的服务:

  1. @Service(
  2. version = "${foo.service.version}",
  3. application = "${dubbo.application.id}",
  4. protocol = "${dubbo.protocol.id}",
  5. registry = "${dubbo.registry.id}"
  6. )
  7. public class FooServiceImpl implements FooService {
  8. @Override
  9. public String hello(String name) {
  10. return "hello, " + name;
  11. }
  12. }

服务调用

Consumer端在服务调用之前,先定义限流规则。

sentinel-dubbo-api模块中定义了FooService服务,内容如下:

  1. package com.alibaba.cloud.examples;
  2. public interface FooService {
  3. String hello(String name);
  4. }

该服务在Sentinel下对应的资源名是 com.alibaba.cloud.examples.FooService:hello(java.lang.String) 。

定义该资源名对应的限流规则:

  1. FlowRule flowRule = new FlowRule();
  2. flowRule.setResource("com.alibaba.cloud.examples.FooService:hello(java.lang.String)");
  3. flowRule.setCount(10);
  4. flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
  5. flowRule.setLimitApp("default");
  6. FlowRuleManager.loadRules(Collections.singletonList(flowRule));

根据Provider端中发布的定义,使用Dubbo的@Reference注解注入服务对应的Bean:

  1. @Reference(version = "${foo.service.version}", application = "${dubbo.application.id}",
  2. path = "dubbo://localhost:12345", timeout = 30000)
  3. private FooService fooService;

由于设置的qps是10。调用15次查看是否被限流:

  1. FooServiceConsumer service = applicationContext.getBean(FooServiceConsumer.class);
  2. for (int i = 0; i < 15; i++) {
  3. try {
  4. String message = service.hello("Jim");
  5. System.out.println((i + 1) + " -> Success: " + message);
  6. }
  7. catch (SentinelRpcException ex) {
  8. System.out.println("Blocked");
  9. }
  10. catch (Exception ex) {
  11. ex.printStackTrace();
  12. }
  13. }

应用启动

支持 IDE 直接启动和编译打包后启动。

Provider端:

  1. IDE直接启动:找到主类 SentinelDubboProviderApp,执行 main 方法启动应用。
  2. 打包编译后启动:首先执行 mvn clean package 将工程编译打包,然后执行 java -jar sentinel-dubbo-provider-example.jar启动应用。

Consumer端:

  1. IDE直接启动:找到主类 SentinelDubboConsumerApp,执行 main 方法启动应用。
  2. 打包编译后启动:首先执行 mvn clean package 将工程编译打包,然后执行 java -jar sentinel-dubbo-consumer-example.jar启动应用。