配置的发布与订阅
我们先来看看如何使用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可以定位一个配置文件。

深入了解配置发布
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()));
可以看到发布的配置首先会
本文介绍了Nacos配置中心的发布与订阅机制。深入探讨了配置发布的流程,包括配置信息的持久化及变更通知。同时,详细阐述了配置订阅的初始化过程,当配置变更时如何触发fireEvent的LocalDataChangeEvent事件。文章总结了配置中心的基本实现,指出实际使用中还有更多细节待探索。
最低0.47元/天 解锁文章

372

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



