NacosSupport for service registration and discovery is the foundation of every service governance, and other modules are implemented on the basis of service registration and discovery.

Example [nacos-example]; This module has three demo classes in the Develop branch.

Java, configexample. Java, namingexample. Java

The namingexample.java class has the following code:

Public class NamingExample {public static void main(String[] args) throws NacosException {/* Set attribute, serverAddr: Connect the NACOS IP address to the port namespace: */ Properties Properties = new Properties(); */ Properties Properties = new Properties(); properties.setProperty("serverAddr", System.getProperty("serverAddr")); properties.setProperty("namespace", System.getProperty("namespace")); / * * this is a factory method, is used to create NamingService implementation class * / NamingService naming = NamingFactory. CreateNamingService (properties); Naming. RegisterInstance ("nacos.test.3", "11.11.11.11", 8888, "TEST1"); Naming. RegisterInstance (" nacos. Test. 3 ", "2.2.2.2", 9999, "DEFAULT"); System.out.println(naming.getallinstances ("nacos.test.3")); /* * Obtain information about all instances of the nacos.test.3 service */ system.out.println (naming.getallinstances ("nacos.test.3")); / * *, in contrast to the registerInstance, this is cancel service instance. * / naming deregisterInstance (" nacos. Test. 3 ", "2.2.2.2", 9999, "DEFAULT"); System.out.println(naming.getAllInstances("nacos.test.3")); /* * Subscription service */ naming. Subscribe ("nacos.test.3", new EventListener() { @Override public void onEvent(Event event) { System.out.println(((NamingEvent) event).getServiceName()); System.out.println(((NamingEvent) event).getInstances()); }}); }}

Now let’s look at one method at a time

NamingFactory is a static factory class for creating services. It provides two methods that instantiate the NacosNamingService class using reflection. The methods take the parameters of the constructor.

public class NamingFactory {
    
    /**
     * Create a new naming service.  
     *
     * @param serverList server list
     * @return new naming service
     * @throws NacosException nacos exception
     */
    public static NamingService createNamingService(String serverList) throws NacosException {
        try {
            Class<?> driverImplClass = Class.forName("com.alibaba.nacos.client.naming.NacosNamingService");
            Constructor constructor = driverImplClass.getConstructor(String.class);
            NamingService vendorImpl = (NamingService) constructor.newInstance(serverList);
            return vendorImpl;
        } catch (Throwable e) {
            throw new NacosException(NacosException.CLIENT_INVALID_PARAM, e);
        }
    }
    
    /**
     * Create a new naming service.
     *
     * @param properties naming service properties
     * @return new naming service
     * @throws NacosException nacos exception
     */
    public static NamingService createNamingService(Properties properties) throws NacosException {
        try {
            Class<?> driverImplClass = Class.forName("com.alibaba.nacos.client.naming.NacosNamingService");
            Constructor constructor = driverImplClass.getConstructor(Properties.class);
            NamingService vendorImpl = (NamingService) constructor.newInstance(properties);
            return vendorImpl;
        } catch (Throwable e) {
            throw new NacosException(NacosException.CLIENT_INVALID_PARAM, e);
        }
    }
}

Take a look at the constructor used for reflection instantiation of the NamingFactory class above

// This constructor assigns the serversAddr address to 'Properties' as well, but it is not as flexible as the second one. It can assign more Properties. public NacosNamingService(String serverList) throws NacosException { Properties properties = new Properties(); properties.setProperty(PropertyKeyConst.SERVER_ADDR, serverList); init(properties); } public NacosNamingService(Properties properties) throws NacosException { init(properties); Private void init(Properties Properties) throws NacosException { ValidatorUtils.checkInitParam(properties); this.namespace = InitUtils.initNamespaceForNaming(properties); InitUtils.initSerialization(); initServerAddr(properties); InitUtils.initWebRootContext(); initCacheDir(); initLogName(properties); this.eventDispatcher = new EventDispatcher(); this.serverProxy = new NamingProxy(this.namespace, this.endpoint, this.serverList, properties); this.beatReactor = new BeatReactor(this.serverProxy, initClientBeatThreadCount(properties)); this.hostReactor = new HostReactor(this.eventDispatcher, this.serverProxy, beatReactor, this.cacheDir, isLoadCacheAtStart(properties), initPollingThreadCount(properties)); }

Next, look at the source code of the registered instance. Registering an instance provides several overload methods for customers to register an instance in different dimensions,

@Override public void registerInstance(String serviceName, String ip, int port) throws NacosException { registerInstance(serviceName, ip, port, Constants.DEFAULT_CLUSTER_NAME); } @Override public void registerInstance(String serviceName, String groupName, String ip, int port) throws NacosException { registerInstance(serviceName, groupName, ip, port, Constants.DEFAULT_CLUSTER_NAME); } @Override public void registerInstance(String serviceName, String ip, int port, String clusterName) throws NacosException { registerInstance(serviceName, Constants.DEFAULT_GROUP, ip, port, clusterName); } @Override public void registerInstance(String serviceName, String groupName, String ip, int port, String clusterName) throws NacosException { Instance instance = new Instance(); instance.setIp(ip); instance.setPort(port); The instance. SetWeight (1.0); instance.setClusterName(clusterName); registerInstance(serviceName, groupName, instance); } @Override public void registerInstance(String serviceName, Instance instance) throws NacosException { registerInstance(serviceName, Constants.DEFAULT_GROUP, instance); } // This is where the most important thing is. @Override public void registerInstance(String serviceName, String groupName, Instance Instance) throws NacosException {/ / get the name of the group String groupedServiceName = NamingUtils. GetGroupedName (serviceName,  groupName); if (instance.isEphemeral()) { BeatInfo beatInfo = beatReactor.buildBeatInfo(groupedServiceName, instance); beatReactor.addBeatInfo(groupedServiceName, beatInfo); } serverProxy.registerService(groupedServiceName, groupName, instance); }