使用Sentinel Dashboard动态推拉数据同步到Nacos

本文详细介绍如何在Sentinel中集成Nacos作为配置中心,包括修改依赖、调整UI链接、编写扩展代码等步骤,并提供了完整的配置示例。

准备工作:

下载Sentinel Dashboard的release版本。地址:https://github.com/alibaba/Sentinel/releases

一 :修改pom.xml中的sentinel-datasource-nacos的依赖,将<scope>test</scope>注释掉,这样才能在主程序中使用。

 

<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-nacos</artifactId>
    <!--<scope>test</scope>-->
</dependency>

 

 二:找到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>

这样修改之后就会跳转到FlowControllerV2的接口。

 

三:再项目com.alibaba.csp.sentinel.dashboard中新建一个nacos包来实现扩展功能。

@Component
@ConfigurationProperties(prefix = "nacos.server")
public class NacosConfigProperties { private String ip; private String port; private String namespace; private String groupId; public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public String getPort() { return port; } public void setPort(String port) { this.port = port; } public String getNamespace() { return namespace; } public void setNamespace(String namespace) { this.namespace = namespace; } public String getGroupId() { return groupId; } public void setGroupId(String groupId) { this.groupId = groupId; } public String getServerAddr() { return this.getIp()+":"+this.getPort(); } @Override public String toString() { return "NacosConfigProperties [ip=" + ip + ", port=" + port + ", namespace=" + namespace + ", groupId=" + groupId + "]"; } }


public final class NacosConfigConstant {
  public static final String FLOW_DATA_ID_POSTFIX = "-sentinel-flow";
  public static final String GROUP_ID = "DEFAULT_GROUP";
}
 

 

@Configuration
public class NacosConfig { @Autowired private NacosConfigProperties nacosConfigProperties; /** * 非常关键 这里将FlowRuleEntity转换成FlowRule才会对客户端生效 * @return FlowRule */ @Bean public Converter<List<FlowRuleEntity>, String> flowRuleEntityEncoder() { return rules -> JSON.toJSONString(rules.stream().map(FlowRuleEntity::toRule).collect(Collectors.toList()), true); } @Bean public Converter<String, List<FlowRuleEntity>> flowRuleEntityDecoder() { return s -> JSON.parseArray(s, FlowRuleEntity.class); } @Bean public ConfigService nacosConfigService() throws Exception { Properties properties = new Properties(); properties.put(PropertyKeyConst.SERVER_ADDR, nacosConfigProperties.getServerAddr()); properties.put(PropertyKeyConst.NAMESPACE, nacosConfigProperties.getNamespace()); return ConfigFactory.createConfigService(properties); } }

 

四:编写动态推拉模式的扩展代码

@Component("flowRuleNacosProvider")
public class FlowRuleNacosProvider implements DynamicRuleProvider<List<FlowRuleEntity>> { private static Logger logger = LoggerFactory.getLogger(FlowRuleNacosProvider.class); @Autowired private NacosConfigProperties nacosConfigProperties; @Autowired private ConfigService configService; @Autowired private Converter<String, List<FlowRuleEntity>> converter; @Override public List<FlowRuleEntity> getRules(String appName) throws Exception { String rules = configService.getConfig(appName + NacosConfigConstant.FLOW_DATA_ID_POSTFIX, nacosConfigProperties.getGroupId(), 3000); logger.info("从Nacos中拉取到限流规则信息:{}",rules); if (StringUtil.isEmpty(rules)) { return new ArrayList<>(); } return converter.convert(rules); } }

 

@Component("flowRuleNacosPublisher")
public class FlowRuleNacosPublisher implements DynamicRulePublisher<List<FlowRuleEntity>> { @Autowired private NacosConfigProperties nacosConfigProperties; @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; } configService.publishConfig(app + NacosConfigConstant.FLOW_DATA_ID_POSTFIX, nacosConfigProperties.getGroupId(), converter.convert(rules)); } }

 

五:然后将FlowControllerV2中的默认DynamicRuleProviderDynamicRulePublisher修改为:

@Autowired
@Qualifier("flowRuleNacosProvider")
private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
@Autowired
@Qualifier("flowRuleNacosPublisher")
private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;
 

private void publishRules(/*@NonNull*/ String app) throws Exception {
    List<FlowRuleEntity> rules = repository.findAllByApp(app);
    rulePublisher.publish(app, rules);
    logger.info("添加限流规则成功{}", JSON.toJSONString(rules.stream().map(FlowRuleEntity::toRule).collect(Collectors.toList()), true));
}
 

六:application.properties配置文件

#nacos
nacos.server.ip=localhost
nacos.server.port=8848
nacos.server.namespace=
nacos.server.group-id=DEFAULT_GROUP

 

转载于:https://www.cnblogs.com/shiraishi/p/11232564.html

Sentinel Dashboard修改的规则同步Nacos可以按以下步骤操作: ### 配置项目 在项目(如Gateway项目)中添加相关配置,示例如下: ```properties # 你的nacos地址 nacos.server-addr=localhost:8148 # 准备把sentinel配置同步到的nacos命名空间 nacos.namespace=zixun_dev # 你的nacos用户名 nacos.username=nacos # 你的nacos密码 nacos.password=nacos ``` 此步骤是为项目指定Nacos的相关连接信息,以确保可以与Nacos进行通信 [^2]。 ### 编写Nacos扩展实现 在`com.alibaba.csp.sentinel.dashboard.rule`包下新建一个`nacos`包,用于编写针对Nacos的扩展实现。同时创建Nacos的配置类,代码如下: ```java import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity; import com.alibaba.fastjson.JSON; import com.alibaba.nacos.api.config.ConfigFactory; import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.PropertyKeyConst; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.convert.converter.Converter; import java.util.List; import java.util.Properties; @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 ConfigService nacosConfigService() throws Exception { Properties properties = new Properties(); properties.put(PropertyKeyConst.SERVER_ADDR, "localhost"); return ConfigFactory.createConfigService(properties); } } ``` 这个配置类中定义了规则实体的编码、解码转换器,以及创建了Nacos的配置服务实例,用于与Nacos进行交互 [^4]。 ### 修改注入的Bean 修改`com.alibaba.csp.sentinel.dashboard.controller.v2.FlowControllerV2`中`DynamicRuleProvider``DynamicRulePublisher`注入的Bean,改为针对Nacos的实现,以确保规则的获取发布使用Nacos相关逻辑 [^5]。 ### 参考数据源存取 nacos数据源的存取,可参考`test`模块`com.alibaba.csp.sentinel.dashboard.rule.nacos`的`FlowRuleNacosProvider``FlowRuleNacosPublisher`,了解如何从Nacos获取规则以及如何将规则发布到Nacos [^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值