Nacos - NacosNamingService初始化

大軍發表於2020-12-31

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

相關文章