nacos第二章
Posted 少林寺三毛
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nacos第二章相关的知识,希望对你有一定的参考价值。
Nacos
支持服务注册与发现,可以说这个功能时每一个服务治理的基础功能,其他模块都是基于服务注册与发现实现的。
开始实操练习:
在Nacos
源码中有个model是example[nacos-example],这个模块在develop分支中有三个demo类,
App.java、ConfigExample.java、NamingExample.java
这一节主要介绍下这个NamingExample.java
类,代码如下:
public class NamingExample {
public static void main(String[] args) throws NacosException {
/*
设置属性,
serverAddr:连接nacos的ip与port
namespace:命名空间如果不设置则默认是public 设置的话需要在nacos console上也创建好,否则找不到相应服务的
*/
Properties properties = new Properties();
properties.setProperty("serverAddr", System.getProperty("serverAddr"));
properties.setProperty("namespace", System.getProperty("namespace"));
/*
* 这是一个工厂方法,用来创建NamingService的实现类
*/
NamingService naming = NamingFactory.createNamingService(properties);
/*
* 向nacos注册服务
*/
naming.registerInstance("nacos.test.3", "11.11.11.11", 8888, "TEST1");
naming.registerInstance("nacos.test.3", "2.2.2.2", 9999, "DEFAULT");
/*
* 获取服务名称为nacos.test.3的所有实例信息
*/
System.out.println(naming.getAllInstances("nacos.test.3"));
/*
* 与 registerInstance 相反,这个是注销服务实例的
*/
naming.deregisterInstance("nacos.test.3", "2.2.2.2", 9999, "DEFAULT");
System.out.println(naming.getAllInstances("nacos.test.3"));
/*
* 订阅服务
*/
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());
}
});
}
}
现在逐个方法来看一边
NamingFactory
这是一个用来创建服务的静态工厂类,主要提供了两个方法,这两个方法都是通过反射来实例化NacosNamingService
类,方法参数为构造函数的参数。
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);
}
}
}
看下上面NamingFactory
类反射实例化时使用的构造函数
//这个构造函数其实也是将serversAddr地址赋值给`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);
}
//这里实例化了N个属性和实例对象
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));
}
接下来就在看下注册实例的源码。注册实例提供了几种重载方法,以供客户以不同维度注册实例,
@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);
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);
}
//其实最重要的就是这里,上面的几个方法直接或间接调用这个方法的。
@Override
public void registerInstance(String serviceName, String groupName, Instance instance) throws NacosException {
//获取分组的名称
String groupedServiceName = NamingUtils.getGroupedName(serviceName, groupName);
if (instance.isEphemeral()) {
BeatInfo beatInfo = beatReactor.buildBeatInfo(groupedServiceName, instance);
beatReactor.addBeatInfo(groupedServiceName, beatInfo);
}
serverProxy.registerService(groupedServiceName, groupName, instance);
}
以上是关于nacos第二章的主要内容,如果未能解决你的问题,请参考以下文章
SpringCloud第二季之Nacos,Sentinel,Seata以及雪花算法学习笔记