This commit is contained in:
parent
719f8aed04
commit
595f1d40fa
8
example/README.md
Normal file
8
example/README.md
Normal file
@ -0,0 +1,8 @@
|
||||
# Nacos example module
|
||||
|
||||
This module contains some examples for nacos.
|
||||
|
||||
1. Run the Nacos service in standalone mode locally. By default, it uses port 8848.
|
||||
2. App.java file is a simple "Hello Nacos" program.
|
||||
3. ConfigExample.java file demonstrates how to utilize the configuration center in Nacos.
|
||||
4. NamingExample.java file demonstrates how to use Nacos for service register, deregister, and subscribe.
|
@ -21,6 +21,7 @@ import java.util.Properties;
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
import com.alibaba.nacos.api.naming.NamingFactory;
|
||||
import com.alibaba.nacos.api.naming.NamingService;
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
/**
|
||||
* Hello world.
|
||||
@ -30,11 +31,14 @@ import com.alibaba.nacos.api.naming.NamingService;
|
||||
public class App {
|
||||
public static void main(String[] args) throws NacosException {
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty("serverAddr", "21.34.53.5:8848,21.34.53.6:8848");
|
||||
properties.setProperty("serverAddr", "localhost:8848");
|
||||
properties.setProperty("namespace", "quickStart");
|
||||
NamingService naming = NamingFactory.createNamingService(properties);
|
||||
|
||||
naming.registerInstance("nacos.test.3", "11.11.11.11", 8888, "TEST1");
|
||||
System.out.println("[Instances after register] " + naming.getAllInstances("nacos.test.3", Lists.newArrayList("TEST1")));
|
||||
|
||||
naming.registerInstance("nacos.test.3", "2.2.2.2", 9999, "DEFAULT");
|
||||
System.out.println(naming.getAllInstances("nacos.test.3"));
|
||||
System.out.println("[Instances after register] " + naming.getAllInstances("nacos.test.3", Lists.newArrayList("DEFAULT")));
|
||||
}
|
||||
}
|
||||
|
@ -39,7 +39,7 @@ public class ConfigExample {
|
||||
properties.put("serverAddr", serverAddr);
|
||||
ConfigService configService = NacosFactory.createConfigService(properties);
|
||||
String content = configService.getConfig(dataId, group, 5000);
|
||||
System.out.println(content);
|
||||
System.out.println("[config content] " + content);
|
||||
configService.addListener(dataId, group, new Listener() {
|
||||
@Override
|
||||
public void receiveConfigInfo(String configInfo) {
|
||||
@ -53,18 +53,18 @@ public class ConfigExample {
|
||||
});
|
||||
|
||||
boolean isPublishOk = configService.publishConfig(dataId, group, "content");
|
||||
System.out.println(isPublishOk);
|
||||
System.out.println("[publish result] " + isPublishOk);
|
||||
|
||||
Thread.sleep(3000);
|
||||
content = configService.getConfig(dataId, group, 5000);
|
||||
System.out.println(content);
|
||||
System.out.println("[config content]: " + content);
|
||||
|
||||
boolean isRemoveOk = configService.removeConfig(dataId, group);
|
||||
System.out.println(isRemoveOk);
|
||||
System.out.println("[delete result]: " + isRemoveOk);
|
||||
Thread.sleep(3000);
|
||||
|
||||
content = configService.getConfig(dataId, group, 5000);
|
||||
System.out.println(content);
|
||||
System.out.println("[config content]: " + content);
|
||||
Thread.sleep(300000);
|
||||
|
||||
}
|
||||
|
@ -38,6 +38,14 @@ import java.util.concurrent.TimeUnit;
|
||||
*/
|
||||
public class NamingExample {
|
||||
|
||||
private static final String INSTANCE_SERVICE_NAME = "nacos.test.3";
|
||||
|
||||
private static final String INSTANCE_IP = "11.11.11.11";
|
||||
|
||||
private static final int INSTANCE_PORT = 8888;
|
||||
|
||||
private static final String INSTANCE_CLUSTER_NAME = "TEST1";
|
||||
|
||||
public static void main(String[] args) throws NacosException, InterruptedException {
|
||||
|
||||
Properties properties = new Properties();
|
||||
@ -46,9 +54,9 @@ public class NamingExample {
|
||||
|
||||
NamingService naming = NamingFactory.createNamingService(properties);
|
||||
|
||||
naming.registerInstance("nacos.test.3", "11.11.11.11", 8888, "TEST1");
|
||||
naming.registerInstance(INSTANCE_SERVICE_NAME, INSTANCE_IP, INSTANCE_PORT, INSTANCE_CLUSTER_NAME);
|
||||
|
||||
System.out.println("instances after register: " + naming.getAllInstances("nacos.test.3"));
|
||||
System.out.println("[instances after register] " + naming.getAllInstances(INSTANCE_SERVICE_NAME));
|
||||
|
||||
Executor executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(),
|
||||
runnable -> {
|
||||
@ -57,7 +65,7 @@ public class NamingExample {
|
||||
return thread;
|
||||
});
|
||||
|
||||
naming.subscribe("nacos.test.3", new AbstractEventListener() {
|
||||
naming.subscribe(INSTANCE_SERVICE_NAME, new AbstractEventListener() {
|
||||
|
||||
//EventListener onEvent is sync to handle, If process too low in onEvent, maybe block other onEvent callback.
|
||||
//So you can override getExecutor() to async handle event.
|
||||
@ -68,16 +76,16 @@ public class NamingExample {
|
||||
|
||||
@Override
|
||||
public void onEvent(Event event) {
|
||||
System.out.println("serviceName: " + ((NamingEvent) event).getServiceName());
|
||||
System.out.println("instances from event: " + ((NamingEvent) event).getInstances());
|
||||
System.out.println("[serviceName] " + ((NamingEvent) event).getServiceName());
|
||||
System.out.println("[instances from event] " + ((NamingEvent) event).getInstances());
|
||||
}
|
||||
});
|
||||
|
||||
naming.deregisterInstance("nacos.test.3", "11.11.11.11", 8888, "TEST1");
|
||||
|
||||
naming.deregisterInstance(INSTANCE_SERVICE_NAME, INSTANCE_IP, INSTANCE_PORT, INSTANCE_CLUSTER_NAME);
|
||||
|
||||
Thread.sleep(1000);
|
||||
|
||||
System.out.println("instances after deregister: " + naming.getAllInstances("nacos.test.3"));
|
||||
|
||||
System.out.println("[instances after deregister] " + naming.getAllInstances(INSTANCE_SERVICE_NAME));
|
||||
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user