溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Sentinel Dashboard限流規(guī)則保存方法有哪些

發(fā)布時(shí)間:2021-06-25 17:43:53 來源:億速云 閱讀:326 作者:chen 欄目:開發(fā)技術(shù)

這篇文章主要講解了“Sentinel Dashboard限流規(guī)則保存方法有哪些”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Sentinel Dashboard限流規(guī)則保存方法有哪些”吧!

Sentinel Dashboard限流規(guī)則保存

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

規(guī)則持久化后首先觸發(fā)GatewayFlowRuleController(源碼似乎沒有,請參考普通規(guī)則改造)的/new.json(或)請求,方法會(huì)調(diào)用publishRules()將本次編輯規(guī)則組裝后通過遠(yuǎn)程調(diào)用請求gateway/updateRules更新遠(yuǎn)程服務(wù)內(nèi)存中限流規(guī)則,該接口由遠(yuǎn)程服務(wù)UpdateGatewayRuleCommandHandler提供。

UpdateGatewayRuleCommandHandler接收到請求通過調(diào)用handle方法,方法通過

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

將規(guī)則持久化到內(nèi)存。

具體請參考以下流程圖

Sentinel DashBoard程序流程

 Sentinel Dashboard限流規(guī)則保存方法有哪些

Gateway網(wǎng)關(guān)程序流程

Sentinel Dashboard限流規(guī)則保存方法有哪些

sentinel dashboard 限流規(guī)則持久化到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;流控規(guī)則
</a>
</li>

改為:

<li ui-sref-active="active">
<a ui-sref="dashboard.flow({app: entry.app})">
<i class="glyphicon glyphicon-filter"></i>&nbsp;&nbsp;流控規(guī)則
</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、將下面的四個(gè)文件全部拷貝到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端如果需要讀取在此處設(shè)置好的配置需要設(shè)置的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 {
      //在此處設(shè)置nacos服務(wù)器的地址
         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;

然后啟動(dòng)之后就可以測試了

感謝各位的閱讀,以上就是“Sentinel Dashboard限流規(guī)則保存方法有哪些”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對Sentinel Dashboard限流規(guī)則保存方法有哪些這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI