java入门中心_Java教程:nacos入门系列之配置中心

配置的发布与订阅我们先来看看如何使用nacos提供的api来实现配置的发布与订阅发布配置:publicclassConfigPub{publicstaticvoidmain(String[]args)throwsNacosException{finalStringdataId="test";finalStringgroup="DEFAULT_GROUP";ConfigSer...
摘要由CSDN通过智能技术生成

配置的发布与订阅我们先来看看如何使用nacos提供的api来实现配置的发布与订阅发布配置:public class ConfigPub{

public static void main(String[] args) throws NacosException{

final String dataId="test";

final String group="DEFAULT_GROUP";

ConfigService configService= NacosFactory.createConfigService("localhost:8848");

configService.publishConfig(dataId,group,"test config body");

}

}订阅配置:public static void main(String[] args) throws NacosException, InterruptedException{

final String dataId="test";

final String group="DEFAULT_GROUP";

ConfigService configService= NacosFactory.createConfigService("localhost:8848");

configService.addListener(dataId, group, new Listener() {

@Override

public Executor getExecutor(){

return null;

}

@Override

public void receiveConfigInfo(String configInfo){

System.out.println("receiveConfigInfo:"+configInfo);

}

});

Thread.sleep(Integer.MAX_VALUE);

}

}根据上面的demo可以看到通过dataId和group可以定位一个配置文件。

194829245_1_2020070705365968

深入了解配置发布

1-发布的配置信息会通过http请求调用具体的服务agent.httpPost(url, headers, params, encode, POST_TIMEOUT);服务类为 ConfigController:处理配置相关的http请求persistService

.insertOrUpdateTag(configInfo, tag, srcIp, srcUser, time, false);

EventDispatcher.fireEvent(

new ConfigDataChangeEvent(false, dataId, group, tenant, tag,

time.getTime()));

可以看到发布的配置首先会进行持久化,然后会触发变更通知。

持久化这里就不做分析,我们来看看fireEvent这个方法:EventDispatcher.fireEvent:

static public void fireEvent(Event event) {

if (null == event) {

throw new IllegalArgumentException("event is null");

}

for (AbstractEventListener listener : getEntry

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要取得Nacos配置中心中的List类型配置,需要使用Nacos提供的Java SDK,具体步骤如下: 1. 引入Nacos Java SDK的依赖,可以在Maven中添加如下依赖: ```xml <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> <version>${nacos.version}</version> </dependency> ``` 2. 在代码中使用Nacos SDK获取配置,示例代码如下: ```java import com.alibaba.nacos.api.NacosFactory; import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.config.listener.Listener; import com.alibaba.nacos.api.exception.NacosException; import java.util.List; import java.util.Properties; import java.util.concurrent.Executor; public class NacosConfigExample { public static void main(String[] args) throws NacosException { String serverAddr = "localhost:8848"; // Nacos服务地址 String dataId = "example"; // 配置ID String group = "DEFAULT_GROUP"; // 配置分组 Properties properties = new Properties(); properties.put("serverAddr", serverAddr); ConfigService configService = NacosFactory.createConfigService(properties); String content = configService.getConfig(dataId, group, 5000); // 获取配置内容,超时时间为5000ms // 将List类型的配置转换为List对象 List<String> list = Arrays.asList(content.split(",")); // 监听配置变化 configService.addListener(dataId, group, new Listener() { @Override public void receiveConfigInfo(String configInfo) { // 配置变化后的处理逻辑 List<String> list = Arrays.asList(configInfo.split(",")); // ... } @Override public Executor getExecutor() { return null; } }); } } ``` 在获取配置之后,可以将List类型的配置内容转换为List对象进行处理。同时,可以使用addListener方法监听配置的变化,当配置发生变化时,会触发Listener中的回调函数,从而进行相应的处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值