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);
将规则持久化到内存。
本文来源gao*daima.com搞@代#码&网6具体请参考以下流程图
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> 流控规则 </a> </li>
改为:
<li ui-sref-active="active"> <a ui-sref="dashboard.flow({app: entry.app})"> <i class="glyphicon glyphicon-filter"></i> 流控规则 </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); } }