一、GateWay网关规则持久化
1、介绍:Sentinel官方文档 | 动态规则扩展 | 推荐链接
2、下载源码Sentinel-1.8.0.zip,并解压
3、sentinel-dashboard项目注释pom.xml依赖(sentinel-datasource-nacos)作用域配置(<scope>test</scope>)

4、复制NacosConfig和NacosConfigUtil文件

5、修改NacosConfig的配置在这里插入图片描述


/**
* 网关API
*
* @return
* @throws Exception
*/
@Bean
public Converter<List<ApiDefinitionEntity>, String> apiDefinitionEntityEncoder() {
return JSON::toJSONString;
}
@Bean
public Converter<String, List<ApiDefinitionEntity>> apiDefinitionEntityDecoder() {
return s -> JSON.parseArray(s, ApiDefinitionEntity.class);
}
/**
* 网关flowRule
*
* @return
* @throws Exception
*/
@Bean
public Converter<List<GatewayFlowRuleEntity>, String> gatewayFlowRuleEntityEncoder() {
return JSON::toJSONString;
}
@Bean
public Converter<String, List<GatewayFlowRuleEntity>> gatewayFlowRuleEntityDecoder() {
return s -> JSON.parseArray(s, GatewayFlowRuleEntity.class);
}
@Bean
public ConfigService nacosConfigService() throws Exception {
Properties properties = new Properties();
// nacos 地址
properties.put(PropertyKeyConst.SERVER_ADDR, "101.101.101.101:7001");
// 命令空间
properties.put(PropertyKeyConst.NAMESPACE, "dev");
// nacos 账号
properties.put(PropertyKeyConst.USERNAME, "nacos");
// nacos 账号密码
properties.put(PropertyKeyConst.PASSWORD, "nacos");
return ConfigFactory.createConfigService(properties);
}
6、NacosConfigUtil文件中添加配置

public static final String GETWAY_FLOW_DATA_ID_POSTFIX = "-sentinel-gateway-flow-rules";
public static final String GETWAY_API_DATA_ID_POSTFIX = "-sentinel-gateway-api-rules";
7、创建GateWayFlowRulesNacosProvider、GateWayFlowRulesNacosPunlisher、GetWayApiNacosProvider、GetWayApiNacosPublisher文件。

package com

最低0.47元/天 解锁文章
1903

被折叠的 条评论
为什么被折叠?



