美文网首页
Nacos 监听 配置

Nacos 监听 配置

作者: 大黑跟小白的日常 | 来源:发表于2020-09-26 20:02 被阅读0次
static ThreadPoolExecutor threadPoolExecutor =
            new ThreadPoolExecutor(2,4,1,TimeUnit.SECONDS,
                    new LinkedBlockingDeque<>(100), new ThreadPoolExecutor.CallerRunsPolicy());
    public static void main(String[] args) {

        // 监听 nacos 对应的一个 dataId 的数据变动
        Listener listenerJsonTest = new Listener() {
            @Override
            public Executor getExecutor() {
                return threadPoolExecutor;
            }
            @Override
            public void receiveConfigInfo(String s) {
                // 收到变动
                System.out.println(s);
            }
        };
        Listener listenerJson2= new Listener() {
            @Override
            public Executor getExecutor() {
                return threadPoolExecutor;
            }
            @Override
            public void receiveConfigInfo(String s) {

            }
        };

        Properties properties = new Properties();
        properties.put(PropertyKeyConst.SERVER_ADDR,"localhost:8848");
        properties.put(PropertyKeyConst.NAMESPACE, "dev");

        ConfigService configService = null;
        try {
            configService = NacosFactory.createConfigService(properties);
        } catch (NacosException e) {
            e.printStackTrace();
        }
        try {
            String jsonTestStr = configService.getConfig("json-test", "demo", 1000);
            System.out.println(jsonTestStr);
            configService.addListener("json-test", "demo", listenerJsonTest);
        } catch (NacosException e) {
            e.printStackTrace();
        }
        while (true) {

        }

    }

相关文章

网友评论

      本文标题:Nacos 监听 配置

      本文链接:https://www.haomeiwen.com/subject/celhuktx.html