Nacos - NacosNamingService初始化

Nacos - 啓動提到了NacosWatch#start會獲取NamingService,他託管NacosServiceManager來完成這件事。segmentfault

NacosServiceManager#getNamingService

爲空的時候,去建立一個NamingService緩存

public NamingService getNamingService(Properties properties) {
    // 爲空的時候,去建立一個NamingService
    if (Objects.isNull(this.namingService)) {
        buildNamingService(properties);
    }
    // 返回namingService
    return namingService;
}

NacosServiceManager#buildNamingService

加鎖保證只能有一個namingServiceui

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對象。this

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對象。spa

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));
}
相關文章
相關標籤/搜索