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>
  1. 将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) {
  1. 将下面的四个文件全部拷贝到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() {}
	}

  1. 修改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;

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ucountonme

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值