Sentinel Dashboard(基于1.8.1)流控规则持久化到Nacos——涉及部分Sentinel Dashboard源码改造

129 阅读10分钟

本文由 简悦 SimpRead 转码, 原文地址 www.cnblogs.com

前言

  之前虽然也一直在使用 sentinel 实现限流熔断功能,但却没有好好整理之前看的源码与资料,今天有时间将之前自己整理过的资料写成一篇博文,或者是是一篇关于 Sentinel(基于目前最近版本 1.8,如果没有特殊说明,都指最新 1.8 版本)持久化 Nacos 的指南,因为我发现网上的一些博文虽然有参考价值但却没有好好完善好细节,一知半解,或者版本比较老不具备参考价值。比如说为什么要做这一步,这一步需要完成什么具体工作等等。所以尽我所能,详细介绍下手把手整合 Sentinel 与 Nacos,实现 Sentinel Dashboard 控制台到 Nacos 配置中心的流控规则通信并下发规则到具体应用

  前提是要对 Sentinel Dashboard 跟 Nacos 有一定的了解,具体可以查看官方 wiki参考资料也是来源于此,再加上对 sentinel-dashboard 源码参考改造

博文中源码已上传至 github(github.com/Jian0110/le…),欢迎小伙伴们 star...

一、准备工作

1、Sentinel Dashboard 持久化

  我们首先需要知道:在 Sentinel Dashboard 中配置规则之后重启应用就会丢失,所以实际生产环境中需要配置规则的持久化实现,Sentinel 提供多种不同的数据源来持久化规则配置,包括 file,redis、nacos、zk。

  这就需要涉及到 Sentinel Dashboard 的规则管理及推送功能:集中管理和推送规则sentinel-core 提供 API 和扩展接口来接收信息。开发者需要根据自己的环境,选取一个可靠的推送规则方式;同时,规则最好在控制台中集中管理。

  而规则管理推送主要有以下三种模式:

      

                (以上部分文字跟截图来源自官方 wiki)

  很明显,我们需要的是第三种 Push 模式,即 Sentinel Dashboard 统一管理配置(有良好的 UI 界面,为什么不能统一管理呢,明显比 Nacos 编写 json 要专业),然后将规则统一推送到 Nacos 并持久化(生成配置文件),最后客户端监听 Nacos(这一部了解使用过 Nacos 的话应该很熟,采用 ConfigService.getConfg() 方法获取配置文件),下发配置生成 Rule。如下图(虚线部分不推荐):

        

  换句话说就是实现 Sentinel Dashboard 与 Nacos 之间的相互通信:

  • Sentinel Dashboard 界面配置流控规则 --- 发布 / 推送 --->Nacos 生成配置文件并持久化;
  • 通过 Nacos 配置文件修改流控规则 --- 拉取 --->Sentinel Dashboard 界面显示最新的流控规则。

  需要注意的是:

  • 在 Nacos 控制台上修改流控制,虽然可以同步到 Sentinel Dashboard,但是 Nacos 此时应该作为一个流控规则的持久化平台,所以正常操作过程应该是开发者在 Sentinel Dashboard 上修改流控规则后同步到 Nacos,遗憾的是目前 Sentinel Dashboard 不支持该功能
  • 试想下,如果公司没有统一在 Sentinel Dashboard 或 Nacos 中二选一进行配置,而是一会在 Sentinel Dashboard 配置,一会在 Nacos 配置。那么就会出现很严重的问题(流控规则达不到预期,配置数据不一致),所以推荐使用 Sentinel Dashboard 统一界面进行配置管理流控规则

正因为 Sentinel Dashboard 当前版本(截至目前为止是 1.8.1-SNAPSHOT)暂不支持,但是可以通过改造部分源码实现此功能,具体请看下面介绍。

2、Sentinel Dashboard 流控规则源码改造须知

首先通过 git 拉取下载源码,导入 idea 工程,解析 maven 后观察 sentinel-dashboard 模块目录结构

git clone https://github.com/alibaba/Sentinel.git

github 可能会很慢,如果只是研究源码了解的话,有需要源码打包的话,可以评论或私信发给你压缩包。

改造前,我们所要了解实现 Sentinel Dashboard 与 Nacos 相互通信需要经历哪些流程或者说是缺少哪些流程,我们才好对症下药,根据我的理解我归纳总结出一下几点

(1)流控规则 Controller 入口

  Sentinel Dashboard 的流控规则下的所有操作,都会调用 Sentinel-Dashboard 源码中的 FlowControllerV1 类,这个类中包含流控规则本地化的 CRUD 操作;

                

 在 com.alibaba.csp.sentinel.dashboard.controller.v2 包下存在一个 FlowControllerV2;类,这个类同样提供流控规则的 CURD,与 V1 不同的是,它可以实现指定数据源的规则拉取和发布

        

  官方说明:

从 Sentinel 1.4.0 开始,我们抽取出了接口用于向远程配置中心推送规则以及拉取规则:

  • DynamicRuleProvider<T>: 拉取规则
  • DynamicRulePublisher<T>: 推送规则

以 Nacos 为例,若希望使用 Nacos 作为动态规则配置中心,用户可以提取出相关的类,然后只需在 FlowControllerV2 中指定对应的 bean 即可开启 Nacos 适配

@Autowired
@Qualifier("flowRuleNacosProvider")
private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
@Autowired
@Qualifier("flowRuleNacosPublisher")
private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;

所以根据官网说明,我们知道,FlowControllerV2 依赖两个非常重要的类

  • DynamicRuleProvider:动态规则的拉取,从指定数据源中获取控制后在 Sentinel Dashboard 中展示。
  • DynamicRulePublisher:动态规则发布,将在 Sentinel Dashboard 中修改的规则同步到指定数据源中。

只需要扩展这两个类,然后集成 Nacos 来实现 Sentinel Dashboard 规则同步

(2)Sentinel Dashboard 前端 sidebar.html 页面入口

在目录 resources/app/scripts/directives/sidebar 找到 sidebar.html,里面有关于 V1 版本的请求入口:

<li ui-sref-active="active" ng-if="!entry.isGateway">
    <a ui-sref="dashboard.flowV1({app: entry.app})">
      <i></i>  流控规则</a>
</li>

 对应的 JS 请求如下,可以看到请求就是 V1 版本的 Controller,那么之后的改造需要重新对应 V2 版本的 Controller

.state('dashboard.flowV1', {
        templateUrl: 'app/views/flow_v1.html',
        url: '/flow/:app',
        controller: 'FlowControllerV1',
        resolve: {
          loadMyFiles: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'sentinelDashboardApp',
              files: [
                'app/scripts/controllers/flow_v1.js',
              ]
            });
          }]
        }
      })

(3)Sentinel Dashboard 缺少 Nacos 配置

在源码中虽然官方提供了 test 示例(即 test 目录)下关于 Nacos 等持久化示例,但是具体的实现还需要一些细节,比如在 Sentinel Dashboard 配置 Nacos 的 serverAddr、namespace、groupId,并且通过 Nacos 获取配置文件获取服务列表等。

                

例如:NacosConfig 中 ConfigFactory.createConfigService("localhost") 并没有实现创建具体的 nacos config service,而是默认 localhost

@Configuration
public class NacosConfig {

    @Bean
    public Converter<List<FlowRuleEntity>, String> flowRuleEntityEncoder() {
        return JSON::toJSONString;
    }

    @Bean
    public Converter<String, List<FlowRuleEntity>> flowRuleEntityDecoder() {
        return s -> JSON.parseArray(s, FlowRuleEntity.class);
    }

    @Bean
    public ConfigService nacosConfigService() throws Exception {
        return ConfigFactory.createConfigService("localhost");
    }
}

application.properties 文件中也没有 Nacos 的相关配置

#spring settings
spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true

#cookie name setting
server.servlet.session.cookie.name=sentinel_dashboard_cookie

#logging settings
logging.level.org.springframework.web=INFO
logging.file=C:\\Users\\Administrator/logs/csp/sentinel-dashboard.log
logging.pattern.file= %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
#logging.pattern.console= %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n

#auth settings
auth.filter.exclude-urls=/,/auth/login,/auth/logout,/registry/machine,/version
auth.filter.exclude-url-suffixes=htm,html,js,css,map,ico,ttf,woff,png
# If auth.enabled=false, Sentinel console disable login
auth.username=sentinel
auth.password=sentinel

# Inject the dashboard version. It's required to enable
# filtering in pom.xml for this resource file.
sentinel.dashboard.version=1.8.1-SNAPSHOT

(4)流控规则配置文件约束

在 NacosConfigutils 已经指定了默认的流控规则配置文件的 groupId 等,但是如果需要指定的话这里也需要修改

public final class NacosConfigUtil {


    /**
     * 流控规则配置文件默认在SENTINEL_GROUP组、DATA_ID以-flow-rules结尾
     */
    public static final String FLOW_DATA_ID_POSTFIX = "-flow-rules";
    public static final String GROUP_ID = "SENTINEL_GROUP";
    // 省略

}

这样我们在 Sentinel 客户端就可以这么配置指定流控规则配置文件约束了

spring.cloud.sentinel.datasource.flow.nacos.server-addr=127.0.0.1:8848
spring.cloud.sentinel.datasource.flow.nacos.data-id=${spring.application.name}-flow-rules
spring.cloud.sentinel.datasource.flow.nacos.group-id=SENTINEL_GROUP
spring.cloud.sentinel.datasource.flow.nacos.data-type=json
spring.cloud.sentinel.datasource.flow.nacos.rule-type=flow

二、改造 Sentinel Dashboard 源码实现 Nacos 持久化

有了以上的须知以及改造前准备工作之后,我们可以开始进行改造源码,其中需要修改部分都会做相关注释

1、在 pom.xml 文件中去掉 test scope 注释

这是因为官方提供的 Nacos 持久化用例都是在 test 目录下,所以 scope 需要去除 test,需要 sentinel-datasource-nacos 包的支持。之后将修改好的源码放在源码主目录下,而不是继续在 test 目录下。

<!-- for Nacos rule publisher sample -->
       <dependency>
           <groupId>com.alibaba.csp</groupId>
           <artifactId>sentinel-datasource-nacos</artifactId>
           <!--<scope>test</scope>-->
       </dependency>

2、修改前端路由配置(sidebar.html)

找到 resources/app/scripts/directives/sidebar/sidebar.html 文件修改,修改 flowV1 为 flow,去掉 V1,这样的话会调用 FlowControllerV2 接口

 <!--<li ui-sref-active="active" ng-if="!entry.isGateway">
           <a ui-sref="dashboard.flowV1({app: entry.app})">
             <i></i>  流控规则</a>
         </li>-->
         <!-- 修改为flow,直接调用FlowControllerV2 -->
         <li ui-sref-active="active" ng-if="!entry.isGateway">
           <a ui-sref="dashboard.flow({app: entry.app})">
             <i></i>  流控规则</a>
         </li>

这样就可以通过 js 跳转至 FlowControllerV2 了

     .state('dashboard.flow', {
         templateUrl: 'app/views/flow_v2.html',
         url: '/v2/flow/:app',
         controller: 'FlowControllerV2',
         resolve: {
             loadMyFiles: ['$ocLazyLoad', function ($ocLazyLoad) {
                 return $ocLazyLoad.load({
                     name: 'sentinelDashboardApp',
                     files: [
                         'app/scripts/controllers/flow_v2.js',
                     ]
                 });
             }]
         }
     })

3、创建 nacos 配置

(1)流控配置文件约束

我们采用官方的约束,即 默认 Nacos 适配的 dataId 和 groupId 约定如下:

  • groupId: SENTINEL_GROUP
  • 流控规则 dataId: {appName}-flow-rules,比如应用名为 appA,则 dataId 为 appA-flow-rules

所以不需要修改 NacosConfigUtil.java 了,但这是展示是为了步骤的完整性。

(2)创建读取 nacos 配置的 NacosPropertiesConfiguration 文件并且 application.properties 指定配置

package com.alibaba.csp.sentinel.dashboard.rule.nacos;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "sentinel.nacos")
public class NacosPropertiesConfiguration {
    private String serverAddr;
    private String dataId;
    private String groupId = "SENTINEL_GROUP"; // 默认分组
    private String namespace;
   // 省略 getter/setter  
}

然后配置 sentinel-dashboar/resources/application.properties 中配置 nacos 配置,以为 sentinel.nacos 为前缀:

# nacos config server
sentinel.nacos.serverAddr=127.0.0.1:8848
sentinel.nacos.namespace=
sentinel.nacos.group-id=SENTINEL-GROUP

(3) 改造 NacosConfig,创建 NacosConfigService

@EnableConfigurationProperties(NacosPropertiesConfiguration.class)
@Configuration
public class NacosConfig {

    @Bean
    public Converter<List<FlowRuleEntity>, String> flowRuleEntityEncoder() {
        return JSON::toJSONString;
    }

    @Bean
    public Converter<String, List<FlowRuleEntity>> flowRuleEntityDecoder() {
        return s -> JSON.parseArray(s, FlowRuleEntity.class);
    }

    @Bean
    public ConfigService nacosConfigService(NacosPropertiesConfiguration nacosPropertiesConfiguration) throws Exception {
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.SERVER_ADDR, nacosPropertiesConfiguration.getServerAddr());
        properties.put(PropertyKeyConst.NAMESPACE, nacosPropertiesConfiguration.getNamespace());
        return ConfigFactory.createConfigService(properties);
//        return ConfigFactory.createConfigService("localhost");
    }
}

NacosConfig 主要做两件事:

1) 注入 Convert 转换器,将 FlowRuleEntity 转化成 FlowRule,以及反向转化

2) 注入 Nacos 配置服务 ConfigService

4、动态实现从 Nacos 配置中心获取流控规则——重写 FlowRuleNacosProvider 与 FlowRuleNacosPublisher 类

重写 FlowRuleNacosProvider 类

@Service("flowRuleNacosProvider")
public class FlowRuleNacosProvider implements DynamicRuleProvider<List<FlowRuleEntity>> {
    public static final Logger log = LoggerFactory.getLogger(FlowRuleNacosProvider.class);

    @Autowired
    private ConfigService configService;
    @Autowired
    private Converter<String, List<FlowRuleEntity>> converter;


    /**
     * 1)通过ConfigService的getConfig()方法从Nacos Config Server读取指定配置信息
     * 2)通过转为converter转化为FlowRule规则
     * @param appName
     * @return
     * @throws Exception
     */
    @Override
    public List<FlowRuleEntity> getRules(String appName) throws Exception {
        String rules = configService.getConfig(appName + NacosConfigUtil.FLOW_DATA_ID_POSTFIX,
            NacosConfigUtil.GROUP_ID, 3000);
        log.info("obtain flow rules from nacos config:{}", rules);
        if (StringUtil.isEmpty(rules)) {
            return new ArrayList<>();
        }
        return converter.convert(rules);
    }
}

重写 FlowRuleNacosPublisher 类:

@Service("flowRuleNacosPublisher")
public class FlowRuleNacosPublisher implements DynamicRulePublisher<List<FlowRuleEntity>> {
    public static final Logger log = LoggerFactory.getLogger(FlowRuleNacosPublisher.class);
    @Autowired
    private ConfigService configService;
    @Autowired
    private Converter<List<FlowRuleEntity>, String> converter;


    /**
     * 通过configService的publishConfig()方法将rules发布到nacos
     * @param app app name
     * @param rules list of rules to push
     * @throws Exception
     */
    @Override
    public void publish(String app, List<FlowRuleEntity> rules) throws Exception {
        AssertUtil.notEmpty(app, "app name cannot be empty");
        if (rules == null) {
            return;
        }
        log.info("sentinel dashboard push rules: {}", rules);
        configService.publishConfig(app + NacosConfigUtil.FLOW_DATA_ID_POSTFIX,
            NacosConfigUtil.GROUP_ID, converter.convert(rules));
    }
}

5、复制到源码主目录下

  之后需要将上述文件(com.alibaba.csp.sentinel.dashboard.test.rule.nacos)复制到 com.alibaba.csp.sentinel.dashboard.rule.nacos 目录下,即去掉 test 目录,这样是以内源码中 Nacos 等持久化的配置都是在 test 中,打包 jar 的时候并不会打包进去,所以需要 copy 到主源码目录下。

              

6、修改 FlowControllerV2 类,使用 @Qulifier 将上面配置的两个类注入进来

@RestController
@RequestMapping(value = "/v2/flow")
public class FlowControllerV2 {

    private final Logger logger = LoggerFactory.getLogger(FlowControllerV2.class);

    @Autowired
    private InMemoryRuleRepositoryAdapter<FlowRuleEntity> repository;

    /*@Autowired
    @Qualifier("flowRuleDefaultProvider")
    private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
    @Autowired
    @Qualifier("flowRuleDefaultPublisher")
    private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;*/
    /**
     * 修改默认publisher/provider为Nacos
     * 使用@Qualifier指定bean
     */
    @Autowired
    @Qualifier("flowRuleNacosProvider")
    private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
    @Autowired
    @Qualifier("flowRuleNacosPublisher")
    private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;

    // 省略      
}

7、mvn clean package 打包

先 install sentinel-parent 保证依赖包已经 install 到本地 repository

之后打包 sentinel-dashboard 模块,执行 mvn clean package 命令,打包成 jar 包

如果以上步骤嫌麻烦,或者中间过程哪里有问题,可以私信我直接要 jar 包。

三、流控规则持久化测试

1、编写 Sentinel 客户端

(1)创建 springboot 应用,编写 pom 文件如下:

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <sentinel.version>1.8.1-SNAPSHOT</sentinel.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--  sentinel核心库 -->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-core</artifactId>
            <version>${sentinel.version}</version>
        </dependency>
        <!-- 通过nacos持久化流控规则 -->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
            <version>${sentinel.version}</version>
        </dependency>
        <!--  sentinel AspectJ 的扩展用于自动定义资源 -->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-annotation-aspectj</artifactId>
            <version>${sentinel.version}</version>
        </dependency>
        <!-- sentinel 整合spring cloud alibaba -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>

        <!-- sentinel客户端与dashboard通信依赖 -->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-transport-simple-http</artifactId>
            <version>${sentinel.version}</version>
        </dependency>
    </dependencies>

(2)配置 nacos,配置 sentinel dashboard datasource 信息:

  1)bootstrap.properties 中配置 Nacos Config Server

spring.cloud.nacos.config.server-addr=127.0.0.1:8848

  2)application.properties 配置 sentinel dashboard datasource

server.port=6003
spring.application.name=sentinel
management.endpoints.web.exposure.include=*
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.sentinel.transport.dashboard=127.0.0.1:6005
#指定csp.sentinel.api.port时需要配置,否则默认8719
#spring.cloud.sentinel.transport.port=6007

# sentinel nacos配置
spring.cloud.sentinel.datasource.flow.nacos.server-addr=127.0.0.1:8848
spring.cloud.sentinel.datasource.flow.nacos.data-id=${spring.application.name}-flow-rules
spring.cloud.sentinel.datasource.flow.nacos.group-id=SENTINEL_GROUP
spring.cloud.sentinel.datasource.flow.nacos.data-type=json
spring.cloud.sentinel.datasource.flow.nacos.rule-type=flow

(3)编写 SayHelloController,指定 / hello 资源节点

package com.cloud.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SayHelloController {

    @RequestMapping("/hello")
    public String sayHello(){
        return "hello Jian";
    }
}

2、启动 Nacos

我使用的是 win10 下启动 nacos,之后登录 Nacos

查看起初是没有 ${spring.application.name}-flow-rules 配置文件,也没有 SENTINEL-GROUP 分组;

但是服务列表会 sentinel 客户端实例:

分配的虚拟 IP 与 port

3、启动 sentinel dashboard 控制台

(1)启动 sentinel dashboard

 找到 target/sentinel-dashboard.jar,执行命令:

java -Dserver.port=6005 -Dcsp.sentinel.dashboard.server=localhost:6006 -Dproject.name=sentinel-dashboard -Dcsp.sentinel.api.port=6007 -jar target/sentinel-dashboard.jar

具体的启动参数介绍:

  -Dserver.port=6005 控制台端口,sentinel 控制台是一个 spring boot 程序。客户端配置文件需要填对应的配置,如:spring.cloud.sentinel.transport.dashboard=192.168.1.102:8718
-Dcsp.sentinel.dashboard.server=localhost:6007 控制台的地址,指定控制台后客户端会自动向该地址发送心跳包。
-Dproject.name=sentinel-dashboard  指定 Sentinel 控制台程序的名称
-Dcsp.sentinel.api.port=8719 可选项,客户端提供给 Dashboard 访问或者查看 Sentinel 的运行访问的参数,默认 8719

其它启动配置项,具体查看官方 wiki

(2)登录 sentinel dashboard 配置流控规则

1)输入 localhost:6005 访问 sentinel dashboard 控制台(登录用户 / 密码默认 sentinel)

2)选择 sentinel 菜单 - 流控规则 - 新增流控规则 - 输入配置 - 新增

  

   如图所示,我们选择 QPS 阈值类型,并且 count 为 2,流控模式为默认的直接模式,流控效果快速失败(这些配置具体含义参看官网 wiki)  

注意:一开始可能会空白页面,这可能是由于机器时间机制导致的,此时可能还未发送心跳,加之 sentinel 控制台默认的又是懒加载模式(可去除该设置),所以最好是我们访问 sentinel 客户端的 / hello 接口然后刷新页面,即访问:http://192.168.1.156:6003/hello(ip:port 是由 Nacos 分配的虚拟地址)

4、访问 / hello 接口

不停刷新访问 / hello 接口,观察 sentinel dashboard 界面中的实时监控。看到有通过 QPS 与拒绝 QPS 的实时监控情况,说明该 sentinel 客户端已成功接入 sentinel dashboard。

5、测试 Sentinel Dashboard 流控规则到 Nacos 的持久化

(1)确认 Sentinel Dashboard 是否能正确发布流控规则到 Nacos

  在 Sentinel Dashboard 针对 sentinel 客户端的 / hello 资源节点已经配置了流控规则

  

  此时 Nacos 会对此次流控规则生成持久化配置文件,切换到 Nacos - 配置列表查看确实存在分组 SENTINEL_GROUP 下的 sentinel-flow-rules 配置文件

  

  点击查看具体内容,发现关键信息都是正确的,说明 Sentinel Dashboard 发布到 Nacos 通信已经打通

  

(2)确认 Sentinel Dashboard 从 nacos 拉取流控规则配置是否成功

修改分组 SENTINEL_GROUP 下的 sentinel-flow-rules 配置文件,修改 count(QPS 数)为 5,然后点击发布

  

  发布后切换到 Sentinel Dashboard 查看 / hello 资源点的流控规则的阈值是否发生变化

  

  明显已经发生变化,因为 Sentinel Dashboard 是懒加载模式,所以刷新后后台才有日志输出:

2020-12-15 19:40:11.499  INFO 11196 --- [nio-6005-exec-8] c.a.c.s.d.r.nacos.FlowRuleNacosProvider  : obtain flow rules from nacos config:[{"app":"sentinel","clusterConfig":{"acquireRef
useStrategy":0,"clientOfflineTime":2000,"fallbackToLocalWhenFail":true,"resourceTimeout":2000,"resourceTimeoutStrategy":0,"sampleCount":10,"strategy":0,"thresholdType":0,"windowInterva
lMs":1000},"clusterMode":false,"controlBehavior":0,"count":2.0,"gmtCreate":1608026073444,"gmtModified":1608026073444,"grade":1,"id":2,"ip":"169.254.102.85","limitApp":"default","port":
8720,"resource":"/hello","strategy":0}]
2020-12-15 19:51:22.612  INFO 11196 --- [nio-6005-exec-9] c.a.c.s.d.r.nacos.FlowRuleNacosProvider  : obtain flow rules from nacos config:[{"app":"sentinel","clusterConfig":{"acquireRef
useStrategy":0,"clientOfflineTime":2000,"fallbackToLocalWhenFail":true,"resourceTimeout":2000,"resourceTimeoutStrategy":0,"sampleCount":10,"strategy":0,"thresholdType":0,"windowInterva
lMs":1000},"clusterMode":false,"controlBehavior":0,"count":5.0,"gmtCreate":1608026073444,"gmtModified":1608026073444,"grade":1,"id":2,"ip":"169.254.102.85","limitApp":"default","port":
8720,"resource":"/hello","strategy":0}]

  这说明 Sentinel Dashboard 能从 nacos 成功拉取流控规则配置

 (3)验证流控规则是否生效

  此时我们的 QPS 阈值为 5,也就是说 1s 之间内我们需要超过访问 5 次,则会被 sentinel 限流。不断访问 / hello 资源节点,观察返回

  

  返回 Blocked By Sentinel(flow limiting) 说明限流规则已经生效。此时实时监控上也会出现通过的 QPS 数目为 5

  

EOF

本文作者:Jian
本文链接:www.cnblogs.com/jian0110/p/…
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角**【推荐】**一下。您的鼓励是博主的最大动力!