Sentinel Dashboard限流规则保存方式

 更新时间:2021年06月25日 11:51:53   作者:y&m  
这篇文章主要介绍了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;

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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Java对List进行排序的方法总结

    Java对List进行排序的方法总结

    在Java中,对List进行排序是一项常见的任务,Java提供了多种方法来对List中的元素进行排序,本文将详细介绍如何使用Java来实现List的排序操作,涵盖了常用的排序方法和技巧,需要的朋友可以参考下
    2024-07-07
  • SpringBoot集成PageHelper及使用方法详解

    SpringBoot集成PageHelper及使用方法详解

    这篇文章主要介绍了SpringBoot集成PageHelper及使用方法详解,PageHelper 是一个开源的 Java 分页插件,它可以帮助开发者简化分页操作,本文提供部分相关代码,需要的朋友可以参考下
    2023-10-10
  • Java的MyBatis框架中实现多表连接查询和查询结果分页

    Java的MyBatis框架中实现多表连接查询和查询结果分页

    这篇文章主要介绍了Java的MyBatis框架中实现多表连接查询和查询结果分页,借助MyBatis框架中带有的动态SQL查询功能可以比普通SQL查询做到更多,需要的朋友可以参考下
    2016-04-04
  • 浅析Java中的GC垃圾回收器的意义及与GC的交互

    浅析Java中的GC垃圾回收器的意义及与GC的交互

    这篇文章主要介绍了Java中的GC垃圾回收器的意义及与其的交互,GC是Java虚拟机JVM的一项重要特性,需要的朋友可以参考下
    2015-12-12
  • Java Stream流知识总结

    Java Stream流知识总结

    这篇文章主要介绍了Java Stream流的相关知识,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-06-06
  • 浅析Java中为什么要设计包装类

    浅析Java中为什么要设计包装类

    我们知道Java是一个面相对象的编程语言,基本类型并不具有对象的性质,为了让基本类型也具有对象的特征,就出现了包装类型,它相当于将基本类型“包装起来”,使得它具有了对象的性质,并且为其添加了属性和方法,丰富了基本类型的操作
    2021-06-06
  • java的jdbc简单封装方法

    java的jdbc简单封装方法

    本篇文章是对java的jdbc简单封装方法进行了详细的分析介绍,需要的朋友参考下
    2015-07-07
  • 详解Java 反射和反射的应用场景

    详解Java 反射和反射的应用场景

    这篇文章主要介绍了Java 反射和反射的应用场景的相关资料,帮助大家更好的理解和学习Java反射的相关知识,感兴趣的朋友可以了解下
    2020-08-08
  • springboot结合mysql主从来实现读写分离的方法示例

    springboot结合mysql主从来实现读写分离的方法示例

    这篇文章主要介绍了springboot结合mysql主从来实现读写分离的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • 一文详解Java的饿汉和懒汉设计模式

    一文详解Java的饿汉和懒汉设计模式

    这篇文章主要为大家详细介绍了Java设计模式中的的饿汉模式和懒汉模式,文中的示例代码讲解详细,对我们学习Java有一定的帮助,需要的可以参考一下
    2022-12-12

最新评论