3步让Dubbo项目快速集成Sentinel

  |   0 评论   |   0 浏览

在微服务系统中,缓存、限流、熔断是保证系统高可用的三板斧。本文通过3个步骤,让Dubbo项目快速集成使用Sentinel实现系统限流。

本文接着《5小步快速集成使用sentinel限流》,继续介绍Dubbo项目如何快速集成使用Sentinel。

1、环境和资源准备

环境和资源准备,参看《5小步快速集成使用sentinel限流》

2、启动sentinel-dashboard

下载sentinel-dashboard,然后执行命令启动:java -jar sentinel-dashboard-1.8.0.jar

启动完毕后,通过http://localhost:8080/#/dashboard访问dashboard,出现如下界面:

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相关依赖排除掉。不排除的情况下,可能会出现代码配置流控规则不生效的问题。

<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、配置需要被限流的资源

文中我依旧使用代码方式配置流控规则,在控制台中也可以直接配置流控规则,为什么不使用控制台方式呢?主要是应用服务器的地址会变化,详细见上一篇文章《5小步快速集成使用sentinel限流》

流控规则一般会有如下几个:

  • 资源限流规则FlowRule
  • 异常熔断降级规则DegradeRule
  • 系统过载保护规则SystemRule
  • 访问黑白名单规则AuthorityRule

本文继续使用资源限流规则做示例。

注意,Dubbo配置被限流资源时,不能使用打注解@SentinelResource的方式。要使用类名:方法名(参数类名)的方式设置资源名。比如这样下面代码里的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实现系统限流。整体步骤和上一篇文章《5小步快速集成使用sentinel限流》类似。只不过有2点需要注意:

  • 老的sentinel依赖要排除掉,采用新的依赖,不排除的话,可能会导致代码里配置的流控规则不生效。
  • 代码设置资源时flowRule.setResource(RES_KEY);,资源定义的格式类名:方法名(参数类名),注意类名和参数类名都需要带上完整路径。不能直接在接口或者实现类的方法上打注解@SentinelResource

本篇完结!感谢你的阅读,欢迎点赞 关注 收藏 私信!!!

原文链接: http://www.mangod.top/articles/2023/09/18/1695000997906.htmlhttps://mp.weixin.qq.com/s/RY00MV5nICMUQuTIF8cOeg


标题:3步让Dubbo项目快速集成Sentinel
作者:不焦躁程序员
地址:http://www.mangod.top/articles/2023/09/22/1695364279992.html