Sentinel Dashboard限流規則保存方式

Sentinel Dashboard限流規則保存

sentinel在限流規則配置方面提供瞭可視化頁面 sentinel dashboard,源碼可從github下載,請自行搜索,此處不提供下載鏈接。

規則持久化後首先觸發GatewayFlowRuleController(源碼似乎沒有,請參考普通規則改造)的/new.json(或)請求,方法會調用publishRules()將本次編輯規則組裝後通過遠程調用請求gateway/updateRules更新遠程服務內存中限流規則,該接口由遠程服務UpdateGatewayRuleCommandHandler提供。

UpdateGatewayRuleCommandHandler接收到請求通過調用handle方法,方法通過

Set<GatewayFlowRule> flowRules = (Set)JSON.parseObject(data, new TypeReference<Set<GatewayFlowRule>>() {
            }, new Feature[0]);
            GatewayRuleManager.loadRules(flowRules);

將規則持久化到內存。

具體請參考以下流程圖

Sentinel DashBoard程序流程

 在這裡插入圖片描述

Gateway網關程序流程

在這裡插入圖片描述

sentinel dashboard 限流規則持久化到nacos

1、將webapp/resources/app/scripts/directives/sidebar/sidebar.html中的

<li ui-sref-active="active">
<a ui-sref="dashboard.flowV1({app: entry.app})">
<i class="glyphicon glyphicon-filter"></i>&nbsp;&nbsp;流控規則
</a>
</li>

改為:

<li ui-sref-active="active">
<a ui-sref="dashboard.flow({app: entry.app})">
<i class="glyphicon glyphicon-filter"></i>&nbsp;&nbsp;流控規則
</a>
</li>

2、將webapp\resources\app\scripts\controllers\identity.js中的(主要是將FlowServiceV1改為FlowServiceV2)

app.controller('IdentityCtl', ['$scope', '$stateParams', 'IdentityService',
  'ngDialog', 'FlowServiceV1', 'DegradeService', 'AuthorityRuleService', 'ParamFlowService', 'MachineService',
  '$interval', '$location', '$timeout',
  function ($scope, $stateParams, IdentityService, ngDialog,
    FlowService, DegradeService, AuthorityRuleService, ParamFlowService, MachineService, $interval, $location, $timeout) {

改為:

app.controller('IdentityCtl', ['$scope', '$stateParams', 'IdentityService',
  'ngDialog', 'FlowServiceV2', 'DegradeService', 'AuthorityRuleService', 'ParamFlowService', 'MachineService',
  '$interval', '$location', '$timeout',
  function ($scope, $stateParams, IdentityService, ngDialog,
    FlowService, DegradeService, AuthorityRuleService, ParamFlowService, MachineService, $interval, $location, $timeout) {

3、將下面的四個文件全部拷貝到src/main/java的com.alibaba.csp.sentinel.dashboard.rule包下

FlowRuleNacosProvider.java (從nacos讀取配置)

 package com.alibaba.csp.sentinel.dashboard.rule;
 import java.util.ArrayList;
 import java.util.List;
 import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
 import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider;
 import com.alibaba.csp.sentinel.datasource.Converter;
 import com.alibaba.csp.sentinel.util.StringUtil;
 import com.alibaba.nacos.api.config.ConfigService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 @Component("flowRuleNacosProvider")
 public class FlowRuleNacosProvider implements DynamicRuleProvider<List<FlowRuleEntity>> {
  @Autowired
  private ConfigService configService;
  @Autowired
  private Converter<String, List<FlowRuleEntity>> converter;
  @Override
  public List<FlowRuleEntity> getRules(String appName) throws Exception {
   // app端如果需要讀取在此處設置好的配置需要設置的GROUP和dataId 需要和這裡保持一致
   String group = NacosConfigUtil.GROUP_ID;
   String dataId = appName + NacosConfigUtil.FLOW_DATA_ID_POSTFIX;
   String rules = configService.getConfig(dataId, group, 3000);
   if (StringUtil.isEmpty(rules)) {
    return new ArrayList<>();
   }
   return converter.convert(rules);
  }
 }

FlowRuleNacosPublisher.java (將修改後的配置同步到nacos)

 package com.alibaba.csp.sentinel.dashboard.rule; 
 import java.util.List;
 import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
 import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
 import com.alibaba.csp.sentinel.datasource.Converter;
 import com.alibaba.csp.sentinel.util.AssertUtil;
 import com.alibaba.nacos.api.config.ConfigService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 @Component("flowRuleNacosPublisher")
 public class FlowRuleNacosPublisher implements DynamicRulePublisher<List<FlowRuleEntity>> {
 
   @Autowired
      private ConfigService configService;
      @Autowired
      private Converter<List<FlowRuleEntity>, String> converter;
 
      @Override
      public void publish(String app, List<FlowRuleEntity> rules) throws Exception {
          AssertUtil.notEmpty(app, "app name cannot be empty");
          if (rules == null) {
              return;
          }
          //需要和FlowRuleNacosProvider保持一致
          String group = NacosConfigUtil.GROUP_ID;
    String dataId = appName + NacosConfigUtil.FLOW_DATA_ID_POSTFIX;
          configService.publishConfig(dataId , group , converter.convert(rules));
      }
    
 }

NacosConfig.java(初始化nacos的nacosConfigService)

 package com.alibaba.csp.sentinel.dashboard.rule; 
 import java.util.List;
 import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity;
 import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
 import com.alibaba.csp.sentinel.datasource.Converter;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.nacos.api.config.ConfigFactory;
 import com.alibaba.nacos.api.config.ConfigService;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 @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 Converter<List<DegradeRuleEntity>, String> degradeRuleEntityEncoder() {
         return JSON::toJSONString;
     }
     @Bean
     public Converter<String, List<DegradeRuleEntity>> degradeRuleEntityDecoder() {
         return s -> JSON.parseArray(s, DegradeRuleEntity.class);
     }
     @Bean
     public ConfigService nacosConfigService() throws Exception {
      //在此處設置nacos服務器的地址
         return ConfigFactory.createConfigService("localhost:8848");
     }
 }

NacosConfigUtil.java(nacos配置的一些常量)

package com.alibaba.csp.sentinel.dashboard.rule;
 public final class NacosConfigUtil { 
     public static final String GROUP_ID = "SENTINEL_GROUP";     
     public static final String FLOW_DATA_ID_POSTFIX = "-flow-rules";
     public static final String PARAM_FLOW_DATA_ID_POSTFIX = "-param-rules";
     public static final String CLUSTER_MAP_DATA_ID_POSTFIX = "-cluster-map";
     public static final String DEGRADE_DATA_ID_POSTFIX = "-degrade-rules";
     /**
      * cc for `cluster-client`
      */
     public static final String CLIENT_CONFIG_DATA_ID_POSTFIX = "-cc-config";
     /**
      * cs for `cluster-server`
      */
     public static final String SERVER_TRANSPORT_CONFIG_DATA_ID_POSTFIX = "-cs-transport-config";
     public static final String SERVER_FLOW_CONFIG_DATA_ID_POSTFIX = "-cs-flow-config";
     public static final String SERVER_NAMESPACE_SET_DATA_ID_POSTFIX = "-cs-namespace-set";
 
     private NacosConfigUtil() {}
 }

4、修改com.alibaba.csp.sentinel.dashboard.controller.v2.FlowControllerV2

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

改為:

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

然後啟動之後就可以測試瞭

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: