Sentinel Dashboard限流規(guī)則保存方式
Sentinel Dashboard限流規(guī)則保存
sentinel在限流規(guī)則配置方面提供了可視化頁面 sentinel dashboard,源碼可從github下載,請自行搜索,此處不提供下載鏈接。
規(guī)則持久化后首先觸發(fā)GatewayFlowRuleController(源碼似乎沒有,請參考普通規(guī)則改造)的/new.json(或)請求,方法會調(diào)用publishRules()將本次編輯規(guī)則組裝后通過遠程調(diào)用請求gateway/updateRules更新遠程服務內(nèi)存中限流規(guī)則,該接口由遠程服務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程序流程
Gateway網(wǎng)關程序流程
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> 流控規(guī)則 </a> </li>
改為:
<li ui-sref-active="active"> <a ui-sref="dashboard.flow({app: entry.app})"> <i class="glyphicon glyphicon-filter"></i> 流控規(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、將下面的四個文件全部拷貝到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;
然后啟動之后就可以測試了
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- sentinel?整合spring?cloud限流的過程解析
- SpringCloud中使用Sentinel實現(xiàn)限流的實戰(zhàn)
- Spring Cloud Alibaba之Sentinel實現(xiàn)熔斷限流功能
- 詳解Springboot集成sentinel實現(xiàn)接口限流入門
- SpringBoot2.0+阿里巴巴Sentinel動態(tài)限流實戰(zhàn)(附源碼)
- SpringBoot基于Sentinel在服務上實現(xiàn)接口限流
- spring cloud gateway整合sentinel實現(xiàn)網(wǎng)關限流
- Spring Cloud Alibaba使用Sentinel實現(xiàn)接口限流
- Sentinel熱門詞匯限流的實現(xiàn)詳解
相關文章
SSH框架網(wǎng)上商城項目第30戰(zhàn)之項目總結(附源碼下載地址)
這篇文章主要介紹了SSH框架網(wǎng)上商城項目第30戰(zhàn)之項目總結,并附源碼下載地址,感興趣的小伙伴們可以參考一下2016-06-06Spring Security 構建rest服務實現(xiàn)rememberme 記住我功能
這篇文章主要介紹了Spring Security 構建rest服務實現(xiàn)rememberme 記住我功能,需要的朋友可以參考下2018-03-03Spring源碼解析之BeanPostProcessor知識總結
今天給大家?guī)淼奈恼率荢pring的相關知識,文章圍繞著BeanPostProcessor的使用展開,文中有非常詳細的介紹,需要的朋友可以參考下2021-06-06