Sentinel Dashboard(基於1.8.1)流控規則持久化到Nacos——涉及部分Sentinel Dashboard源碼改造

前言

  之前雖然也一直在使用sentinel實現限流熔斷功能,但卻沒有好好整理之前看的源碼與資料,今天有時間將之前自己整理過的資料寫成一篇博文,或者是是一篇關於Sentinel(基於目前最近版本1.8,如果沒有特殊說明,都指最新1.8版本)持久化Nacos的指南,因爲我發現網上的一些博文雖然有參考價值但卻沒有好好完善好細節,一知半解,或者版本比較老不具備參考價值。比如說爲什麼要做這一步,這一步需要完成什麼具體工作等等。所以盡我所能,詳細介紹下手把手整合Sentinel與Nacos,實現Sentinel Dashboard控制檯到Nacos配置中心的流控規則通信並下發規則到具體應用

  前提是要對Sentinel Dashboard跟Nacos有一定的瞭解,具體可以查看官方wiki參考資料也是來源於此,再加上對sentinel-dashboard源碼參考改造

 


 

一、準備工作

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 class="glyphicon glyphicon-filter"></i>&nbsp;&nbsp;流控規則</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 class="glyphicon glyphicon-filter"></i>&nbsp;&nbsp;流控規則</a>
          </li>-->
          <!-- 修改爲flow,直接調用FlowControllerV2 -->
          <li ui-sref-active="active" ng-if="!entry.isGateway">
            <a ui-sref="dashboard.flow({app: entry.app})">
              <i class="glyphicon glyphicon-filter"></i>&nbsp;&nbsp;流控規則</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

  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章