Sentinel Dashboard限流规则保存方法有哪些

发布时间:2021-06-25 17:43:53 作者:chen
来源:亿速云 阅读:312

这篇文章主要讲解了“Sentinel Dashboard限流规则保存方法有哪些”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“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程序流程

 Sentinel Dashboard限流规则保存方法有哪些

Gateway网关程序流程

Sentinel Dashboard限流规则保存方法有哪些

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;

然后启动之后就可以测试了

感谢各位的阅读,以上就是“Sentinel Dashboard限流规则保存方法有哪些”的内容了,经过本文的学习后,相信大家对Sentinel Dashboard限流规则保存方法有哪些这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. Sentinel的使用方法
  2. Spring Cloud中Sentinel架构的使用方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

sentinel dashboard

上一篇:java中方法重写的本质是什么

下一篇:python中怎么封装特性

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》