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

本文介绍了Nacos配置中心的发布与订阅机制。深入探讨了配置发布的流程,包括配置信息的持久化及变更通知。同时,详细阐述了配置订阅的初始化过程,当配置变更时如何触发fireEvent的LocalDataChangeEvent事件。文章总结了配置中心的基本实现,指出实际使用中还有更多细节待探索。
摘要由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可以定位一个配置文件。

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

 

深入了解配置发布

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()));

可以看到发布的配置首先会

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值