关于nacos:Nacos-NacosNamingService初始化

4次阅读

共计 2685 个字符,预计需要花费 7 分钟才能阅读完成。

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