From 595f1d40fafae952a8fd29d6e77460e74abb82cc Mon Sep 17 00:00:00 2001 From: Wuzhengyu97 <49513890+Wuzhengyu97@users.noreply.github.com> Date: Mon, 25 Sep 2023 15:35:53 +0800 Subject: [PATCH] [ISSUE #11094] Enhancements for example Module: Introduce README.md and Optimize Code. (#11116) --- example/README.md | 8 ++++++ .../java/com/alibaba/nacos/example/App.java | 8 ++++-- .../alibaba/nacos/example/ConfigExample.java | 10 +++---- .../alibaba/nacos/example/NamingExample.java | 26 ++++++++++++------- 4 files changed, 36 insertions(+), 16 deletions(-) create mode 100644 example/README.md diff --git a/example/README.md b/example/README.md new file mode 100644 index 000000000..1a484e2ca --- /dev/null +++ b/example/README.md @@ -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. \ No newline at end of file diff --git a/example/src/main/java/com/alibaba/nacos/example/App.java b/example/src/main/java/com/alibaba/nacos/example/App.java index e81ca79f0..df46234aa 100644 --- a/example/src/main/java/com/alibaba/nacos/example/App.java +++ b/example/src/main/java/com/alibaba/nacos/example/App.java @@ -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"))); } } diff --git a/example/src/main/java/com/alibaba/nacos/example/ConfigExample.java b/example/src/main/java/com/alibaba/nacos/example/ConfigExample.java index 5bd38f9fb..4020b597c 100644 --- a/example/src/main/java/com/alibaba/nacos/example/ConfigExample.java +++ b/example/src/main/java/com/alibaba/nacos/example/ConfigExample.java @@ -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); } diff --git a/example/src/main/java/com/alibaba/nacos/example/NamingExample.java b/example/src/main/java/com/alibaba/nacos/example/NamingExample.java index c195c1c06..db5d885a6 100644 --- a/example/src/main/java/com/alibaba/nacos/example/NamingExample.java +++ b/example/src/main/java/com/alibaba/nacos/example/NamingExample.java @@ -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 -> { @@ -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); }