java service 初始化_【Java】Nacos – NacosNamingService初始化

Nacos – 启动提到了NacosWatch#start会获取NamingService,他托管NacosServiceManager来完成这件事。

NacosServiceManager#getNamingService

为空的时候,去创建一个NamingService

public NamingService getNamingService(Properties properties) {

// 为空的时候,去创建一个NamingService

if (Objects.isNull(this.namingService)) {

buildNamingService(properties);

}

// 返回namingService

return namingService;

}

NacosServiceManager#buildNamingService

加锁保证只能有一个namingService

private NamingService buildNamingService(Properties properties) {

if (Objects.isNull(namingService)) {

// 加锁保证只能有一个namingService

synchronized (NacosServiceManager.class) {

if (Objects.isNull(namingService)) {

namingService = createNewNamingService(properties);

}

}

}

return namingService;

}

最终调用NamingFactory#createNamingService来创建NamingService对象。

private NamingService createNewNamingService(Properties properties) {

try {

return createNamingService(properties);

}

catch (NacosException e) {

throw new RuntimeException(e);

}

}

// NacosFactory中的方法

public static NamingService createNamingService(Properties properties) throws NacosException {

return NamingFactory.createNamingService(properties);

}

NamingFactory#createNamingService

通过反射的方式创建了com.alibaba.nacos.client.naming.NacosNamingService对象。

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);

}

}

NacosNamingService#init

主要是初始化namespace、序列化、注册中心服务地址、WebRootContext上下文、缓存路径、日志名称、EventDispatcher、NamingProxy、BeatReactor、HostReactor。

EventDispatcher负责处理服务监听相关。

NamingProxy负责和Nacos服务的通信,比如服务注册、服务取消注册、心跳等。

BeatReactor负责检测心跳。

HostReactor负责获取、更新并保存服务信息。

private void init(Properties properties) throws NacosException {

ValidatorUtils.checkInitParam(properties);

// namespace默认public

this.namespace = InitUtils.initNamespaceForNaming(properties);

// 序列化初始化

InitUtils.initSerialization();

// 注册中心服务地址初始化,这个从配置文件取

initServerAddr(properties);

//初始化WebRootContext上下文

InitUtils.initWebRootContext();

// 初始化缓存路径 System.getProperty("user.home") + "/nacos/naming/" + namespace

initCacheDir();

// 初始化日志名称naming.log

initLogName(properties);

// 初始化EventDispatcher

this.eventDispatcher = new EventDispatcher();

// 初始化NamingProxy

this.serverProxy = new NamingProxy(this.namespace, this.endpoint, this.serverList, properties);

// 初始化BeatReactor

this.beatReactor = new BeatReactor(this.serverProxy, initClientBeatThreadCount(properties));

// 初始化HostReactor

this.hostReactor = new HostReactor(this.eventDispatcher, this.serverProxy, beatReactor, this.cacheDir,

isLoadCacheAtStart(properties), initPollingThreadCount(properties));

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值