1. 添加依赖
com.alibaba.nacos nacos-client1.4.1
2. 获取配置方法
public String getConfig(String dataId, String group, long timeoutMs) throws NacosException
3. 获取配置示例
try { String serverAddr = "{serverAddr}"; String dataId = "{dataId}"; String group = "{group}"; Properties properties = new Properties(); properties.put("serverAddr", serverAddr); ConfigService configService = NacosFactory.createConfigService(properties); String content = configService.getConfig(dataId, group, 5000); System.out.println(content); } catch (NacosException e) { // TODO Auto-generated catch block e.printStackTrace(); }
4. 添加监听方法
public void addListener(String dataId, String group, Listener listener)
当监听的配置发送变更时,nacos会自动进行通知。
5. 监听示例
String serverAddr = "{serverAddr}"; String dataId = "{dataId}"; String group = "{group}"; Properties properties = new Properties(); properties.put("serverAddr", serverAddr); ConfigService configService = NacosFactory.createConfigService(properties); String content = configService.getConfig(dataId, group, 5000); System.out.println(content); configService.addListener(dataId, group, new Listener() { @Override public void receiveConfigInfo(String configInfo) { System.out.println("recieve1:" + configInfo); } @Override public Executor getExecutor() { return null; } });
6. 删除监听
public void removeListener(String dataId, String group, Listener listener) String serverAddr = "{serverAddr}"; String dataId = "{dataId}"; String group = "{group}"; Properties properties = new Properties(); properties.put("serverAddr", serverAddr); ConfigService configService = NacosFactory.createConfigService(properties); configService.removeListener(dataId, group, yourListener);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)