Add java.md
This commit is contained in:
parent
40f5f215e0
commit
ace88ac67d
|
@ -0,0 +1,64 @@
|
|||
/*
|
||||
* Demo for Nacos
|
||||
* pom.xml
|
||||
<dependency>
|
||||
<groupId>com.alibaba.nacos</groupId>
|
||||
<artifactId>nacos-client</artifactId>
|
||||
<version>${version}</version>
|
||||
</dependency>
|
||||
*/
|
||||
package com.alibaba.nacos.example;
|
||||
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.Executor;
|
||||
import com.alibaba.nacos.api.NacosFactory;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import com.alibaba.nacos.api.config.listener.Listener;
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
|
||||
/**
|
||||
* Config service example
|
||||
*
|
||||
* @author Nacos
|
||||
*
|
||||
*/
|
||||
public class ConfigExample {
|
||||
|
||||
public static void main(String[] args) throws NacosException, InterruptedException {
|
||||
String serverAddr = "localhost";
|
||||
String dataId = "application-prod.yml";
|
||||
String group = "DEFAULT_GROUP";
|
||||
Properties properties = new Properties();
|
||||
properties.put(PropertyKeyConst.SERVER_ADDR, 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("receive:" + configInfo);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Executor getExecutor() {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
boolean isPublishOk = configService.publishConfig(dataId, group, "content");
|
||||
System.out.println(isPublishOk);
|
||||
|
||||
Thread.sleep(3000);
|
||||
content = configService.getConfig(dataId, group, 5000);
|
||||
System.out.println(content);
|
||||
|
||||
boolean isRemoveOk = configService.removeConfig(dataId, group);
|
||||
System.out.println(isRemoveOk);
|
||||
Thread.sleep(3000);
|
||||
|
||||
content = configService.getConfig(dataId, group, 5000);
|
||||
System.out.println(content);
|
||||
Thread.sleep(300000);
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue