Sentinel Dashboard限流規(guī)則保存方式
Sentinel Dashboard限流規(guī)則保存
sentinel在限流規(guī)則配置方面提供了可視化頁(yè)面 sentinel dashboard,源碼可從github下載,請(qǐng)自行搜索,此處不提供下載鏈接。
規(guī)則持久化后首先觸發(fā)GatewayFlowRuleController(源碼似乎沒有,請(qǐng)參考普通規(guī)則改造)的/new.json(或)請(qǐng)求,方法會(huì)調(diào)用publishRules()將本次編輯規(guī)則組裝后通過遠(yuǎn)程調(diào)用請(qǐng)求gateway/updateRules更新遠(yuǎn)程服務(wù)內(nèi)存中限流規(guī)則,該接口由遠(yuǎn)程服務(wù)UpdateGatewayRuleCommandHandler提供。
UpdateGatewayRuleCommandHandler接收到請(qǐng)求通過調(diào)用handle方法,方法通過
Set<GatewayFlowRule> flowRules = (Set)JSON.parseObject(data, new TypeReference<Set<GatewayFlowRule>>() { }, new Feature[0]); GatewayRuleManager.loadRules(flowRules);
將規(guī)則持久化到內(nèi)存。
具體請(qǐng)參考以下流程圖
Sentinel DashBoard程序流程
Gateway網(wǎng)關(guān)程序流程
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、將下面的四個(gè)文件全部拷貝到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端如果需要讀取在此處設(shè)置好的配置需要設(shè)置的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 { //在此處設(shè)置nacos服務(wù)器的地址 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;
然后啟動(dòng)之后就可以測(cè)試了
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- sentinel?整合spring?cloud限流的過程解析
- SpringCloud中使用Sentinel實(shí)現(xiàn)限流的實(shí)戰(zhàn)
- Spring Cloud Alibaba之Sentinel實(shí)現(xiàn)熔斷限流功能
- 詳解Springboot集成sentinel實(shí)現(xiàn)接口限流入門
- SpringBoot2.0+阿里巴巴Sentinel動(dòng)態(tài)限流實(shí)戰(zhàn)(附源碼)
- SpringBoot基于Sentinel在服務(wù)上實(shí)現(xiàn)接口限流
- spring cloud gateway整合sentinel實(shí)現(xiàn)網(wǎng)關(guān)限流
- Spring Cloud Alibaba使用Sentinel實(shí)現(xiàn)接口限流
- Sentinel熱門詞匯限流的實(shí)現(xiàn)詳解
相關(guān)文章
SSH框架網(wǎng)上商城項(xiàng)目第30戰(zhàn)之項(xiàng)目總結(jié)(附源碼下載地址)
這篇文章主要介紹了SSH框架網(wǎng)上商城項(xiàng)目第30戰(zhàn)之項(xiàng)目總結(jié),并附源碼下載地址,感興趣的小伙伴們可以參考一下2016-06-06Java 如何使用JDBC連接數(shù)據(jù)庫(kù)
這篇文章主要介紹了Java 如何使用JDBC連接數(shù)據(jù)庫(kù),幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下2021-02-02Java中Process類的使用與注意事項(xiàng)說明
這篇文章主要介紹了Java中Process類的使用與注意事項(xiàng)說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12Spring Security 構(gòu)建rest服務(wù)實(shí)現(xiàn)rememberme 記住我功能
這篇文章主要介紹了Spring Security 構(gòu)建rest服務(wù)實(shí)現(xiàn)rememberme 記住我功能,需要的朋友可以參考下2018-03-03java實(shí)現(xiàn)波雷費(fèi)密碼算法示例代碼
這篇文章主要介紹了java實(shí)現(xiàn)波雷費(fèi)密碼算法示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01Spring源碼解析之BeanPostProcessor知識(shí)總結(jié)
今天給大家?guī)淼奈恼率荢pring的相關(guān)知識(shí),文章圍繞著BeanPostProcessor的使用展開,文中有非常詳細(xì)的介紹,需要的朋友可以參考下2021-06-06