This article mainly introduces the sharing configuration and service instances of native Java connection to Nacos and operating Nacos.
1. Introduce dependencies
<dependencies> <dependency> <groupId></groupId> <artifactId>nacos-client</artifactId> <version>2.5.0</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>2.0.16</version> </dependency> </dependencies>
Note: Be sure to introduce slf4j
2. Operation configuration
package code; import ; import ; import ; import ; import ; import ; import ; public class OperConfig { public static void main(String[] args) throws NacosException, InterruptedException { String serverAddr = "localhost"; String dataId = "hello"; String group = "DEFAULT_GROUP"; Properties properties = new Properties(); (PropertyKeyConst.SERVER_ADDR, serverAddr); (,"nacos"); //In the previous article, the authentication of nacos was enabled and the password was set to nacos (,"nacos"); ConfigService configService = (properties); String content = (dataId, group, 5000); //Get the corresponding configuration value through dataId and group; 5000 is the timeout time, unit ms (content); (dataId, group, new Listener() { @Override public void receiveConfigInfo(String configInfo) { ("receive:" + configInfo); } @Override public Executor getExecutor() { return null; } }); boolean isPublishOk = (dataId, group, "content"); //Release configuration (isPublishOk); (3000); content = (dataId, group, 5000); (content); boolean isRemoveOk = (dataId, group); //Delete the configuration (isRemoveOk); (3000); content = (dataId, group, 5000); (content); (3000); } }
3. Operation service examples
public class OperService { public static void main(String[] args) throws NacosException, InterruptedException { String serverAddr = "localhost"; Properties properties = new Properties(); (PropertyKeyConst.SERVER_ADDR, serverAddr); (,"nacos"); (,"nacos"); NamingService naming = (properties); ("test", "11.11.11.11", 8888, "DEFAULT"); //Register test instance, grouped as DEFAULT (2000); ("test", "127.0.0.1", 9999, "DEFAULT"); //The second time I registered the test instance, the same grouped as DEFAULT (2000); (("test")); //Only get 127.0.0.1 here, because the second registration will be overwritten for the first time (2000); ("test", new EventListener() { @Override public void onEvent(Event event) { (((NamingEvent)event).getServiceName()); (((NamingEvent)event).getInstances()); } }); ("test", "127.0.0.1", 9999, "DEFAULT"); //Login service instance (2000); ("dereg 127.0.0.1"); (("test")); } }
The official documentation clearly states that only one instance can be registered with one service for the same Nacos Client instance; if the same Nacos Client instance registers an instance with the same service multiple times, the subsequently registered instance will overwrite the first registered instance. If you are sure that multiple instances need to be registered simultaneously, add service instances in batches.
Friends who use it should note: According to the blogger's test, the getAllInstances() function will occasionally fail when obtaining the instance, but no exception is thrown.
4. Add service instances in batches
package code; import ; import ; import ; import ; import ; import ; import ; import ; public class BatchOperService { public static void main(String[] args) throws NacosException { String serverAddr = "localhost"; Properties properties = new Properties(); (PropertyKeyConst.SERVER_ADDR, serverAddr); (,"nacos"); (,"nacos"); NamingService naming = (properties); Instance instance1 = new Instance(); ("127.0.0.1"); (8848); ("DEFAULT"); Instance instance2 = new Instance(); ("127.0.0.1"); (9848); ("DEFAULT"); List<Instance> instances = new ArrayList<>(2); (instance1); (instance2); ("", "DEFAULT_GROUP", instances); //Batch registration instance (("")); } }
This article ends here. For configuration and instances, listening configuration, obtaining healthy instances, etc., you can check the official website documentation.(Click to enter). In the next article, we will learn about the process of springboot integrating nacos together:)