开发者社区 > 云原生 > 中间件 > 正文

如何监听nacos,服务注册事件?

如何监听nacos,服务注册事件?

展开
收起
夹心789 2024-06-09 17:26:12 13 0
1 条回答
写回答
取消 提交回答
  • Nacos获取配置以及监听配置变更的基本使用方式如下:
    public class NacosConfigExample {

    public static void main(String[] args) throws NacosException, InterruptedException {
        String dataId = "test";
        String group = "DEFAULT_GROUP";
        Properties properties = new Properties();
        properties.put("namespace","test");
        properties.put("serverAddr", "192.168.153.101");
    
        //创建NacosConfigService的实例
        ConfigService configService = NacosFactory.createConfigService(properties);
    
        //获取配置内容
        String content = configService.getConfig(dataId, group, 5000);
        System.out.println("content:" + content);
    
        //添加监听器,监听配置的变更
        configService.addListener(dataId, group, new Listener() {
            @Override
            public void receiveConfigInfo(String configInfo) {
                //收到新的配置
                System.out.println("receive:" + configInfo);
            }
    
            @Override
            public Executor getExecutor() {
                return null;
            }
        });
    }
    

    }

    参考文档: https://blog.51cto.com/u_16099234/10521147

    2024-06-21 19:01:20
    赞同 展开评论 打赏

为企业提供高效、稳定、易扩展的中间件产品。

热门讨论

热门文章

相关电子书

更多
Nacos架构&原理 立即下载
workshop专场-微服务专场-开发者动手实践营-微服务-使用Nacos进行服务的动态发现和流量调度 立即下载
Nacos 启航,发布第一个版本, 云原生时代助力用户微服务平台建设 立即下载