最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringCloud中NacosNamingService的作用詳解

 更新時(shí)間:2023年11月15日 08:54:23   作者:阿孟呀  
這篇文章主要介紹了SpringCloud中NacosNamingService的作用詳解,NacosNamingService類(lèi)完成服務(wù)實(shí)例注冊(cè),撤銷(xiāo)與獲取服務(wù)實(shí)例操作,NacosNamingService初始化采用單例模式,使用反射生成,需要的朋友可以參考下

NacosNamingService初始化

NacosNamingService類(lèi)完成服務(wù)實(shí)例注冊(cè),撤銷(xiāo)與獲取服務(wù)實(shí)例操作。

生成方式:NacosNamingService初始化采用單例模式,使用反射生成。

NacosServiceRegistry bean初始化時(shí),在構(gòu)造中,根據(jù)nacos的屬性配置文件,通過(guò)反射,初始化了NacosNamingService。

NacosServiceRegistry

public NacosServiceRegistry(NacosDiscoveryProperties nacosDiscoveryProperties) {
		this.nacosDiscoveryProperties = nacosDiscoveryProperties;
		this.namingService = nacosDiscoveryProperties.namingServiceInstance();
	}
NacosDiscoveryProperties
public NamingService namingServiceInstance() {
 
		if (null != namingService) {
			return namingService;
		}
 
		try {
			namingService = NacosFactory.createNamingService(getNacosProperties());
		}
		catch (Exception e) {
			log.error("create naming service error!properties={},e=,", this, e);
			return null;
		}
		return namingService;
	}

NamingFactory

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

NacosNamingService初始化過(guò)程

NacosNamingService初始化時(shí),主要是進(jìn)行了以下操作:

  1. 初始化命名空間,默認(rèn)為public
  2. 初始化服務(wù)端地址.xxx:8848
  3. 初始化naocs 服務(wù)端兩個(gè)webcontext,"/nacos//v1/ns"和"/nacos//v1/ns//instance"
  4. 初始化服務(wù)實(shí)例信息緩存目錄,默認(rèn)為用戶(hù)目錄下"/nacos/naming/public"
  5. 初始化日志
  6. 初始化事件分發(fā)器
  7. 初始化NamingProxy,像服務(wù)端發(fā)起請(qǐng)求的代理
  8. 初始化心跳信息,使用ScheduledThreadPoolExecutor初始化守護(hù)線(xiàn)程池,nacos默認(rèn)5秒像服務(wù)端發(fā)起一次心跳,
  9. 初始化主機(jī)信息,默認(rèn)不使用本地緩存。
public NacosNamingService(Properties properties) {
        init(properties);
    }
 
    private void init(Properties properties) {
        namespace = InitUtils.initNamespaceForNaming(properties);
        initServerAddr(properties);
        InitUtils.initWebRootContext();
        initCacheDir();
        initLogName(properties);
 
        eventDispatcher = new EventDispatcher();
        serverProxy = new NamingProxy(namespace, endpoint, serverList);
        serverProxy.setProperties(properties);
        beatReactor = new BeatReactor(serverProxy, initClientBeatThreadCount(properties));
        hostReactor = new HostReactor(eventDispatcher, serverProxy, cacheDir, isLoadCacheAtStart(properties), initPollingThreadCount(properties));
    }

NacosNamingService心跳啟動(dòng)

客戶(hù)端向服務(wù)端發(fā)起請(qǐng)求時(shí),會(huì)先通過(guò)定時(shí)任務(wù)啟動(dòng)心跳,默認(rèn)心跳為5秒一次

public void registerInstance(String serviceName, String groupName, Instance instance) throws NacosException {
 
        if (instance.isEphemeral()) {
            BeatInfo beatInfo = new BeatInfo();
            beatInfo.setServiceName(NamingUtils.getGroupedName(serviceName, groupName));
            beatInfo.setIp(instance.getIp());
            beatInfo.setPort(instance.getPort());
            beatInfo.setCluster(instance.getClusterName());
            beatInfo.setWeight(instance.getWeight());
            beatInfo.setMetadata(instance.getMetadata());
            beatInfo.setScheduled(false);
            long instanceInterval = instance.getInstanceHeartBeatInterval();
            beatInfo.setPeriod(instanceInterval == 0 ? DEFAULT_HEART_BEAT_INTERVAL : instanceInterval);
               //啟動(dòng)心跳任務(wù)
            beatReactor.addBeatInfo(NamingUtils.getGroupedName(serviceName, groupName), beatInfo);
        }
 
        serverProxy.registerService(NamingUtils.getGroupedName(serviceName, groupName), groupName, instance);
    }
public void addBeatInfo(String serviceName, BeatInfo beatInfo) {
        NAMING_LOGGER.info("[BEAT] adding beat: {} to beat map.", beatInfo);
        String key = buildKey(serviceName, beatInfo.getIp(), beatInfo.getPort());
        BeatInfo existBeat = null;
        //fix #1733
        if ((existBeat = dom2Beat.remove(key)) != null) {
            existBeat.setStopped(true);
        }
        dom2Beat.put(key, beatInfo);//開(kāi)啟定時(shí)心跳任務(wù)
        executorService.schedule(new BeatTask(beatInfo), beatInfo.getPeriod(), TimeUnit.MILLISECONDS);
        MetricsMonitor.getDom2BeatSizeMonitor().set(dom2Beat.size());
    }

獲取服務(wù)實(shí)例

@Override
    public List<Instance> getAllInstances(String serviceName, String groupName, List<String> clusters, boolean subscribe) throws NacosException {
 
        ServiceInfo serviceInfo;
        if (subscribe) {
            serviceInfo = hostReactor.getServiceInfo(NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ","));//本地map獲取
        } else {
            serviceInfo = hostReactor.getServiceInfoDirectlyFromServer(NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ","));//遠(yuǎn)程服務(wù)獲取
        }
        List<Instance> list;
        if (serviceInfo == null || CollectionUtils.isEmpty(list = serviceInfo.getHosts())) {
            return new ArrayList<Instance>();
        }
        return list;
    }

服務(wù)實(shí)例注銷(xiāo)

@Override
    public void deregisterInstance(String serviceName, String groupName, Instance instance) throws NacosException {
        if (instance.isEphemeral()) {
            beatReactor.removeBeatInfo(NamingUtils.getGroupedName(serviceName, groupName), instance.getIp(), instance.getPort());
        }
        serverProxy.deregisterService(NamingUtils.getGroupedName(serviceName, groupName), instance);
    }

到此這篇關(guān)于SpringCloud中NacosNamingService的作用詳解的文章就介紹到這了,更多相關(guān)NacosNamingService的作用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 解決ApplicationContext獲取不到Bean的問(wèn)題

    解決ApplicationContext獲取不到Bean的問(wèn)題

    這篇文章主要介紹了解決ApplicationContext獲取不到Bean的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • 如果你想寫(xiě)自己的Benchmark框架(推薦)

    如果你想寫(xiě)自己的Benchmark框架(推薦)

    這篇文章主要介紹了如果你想寫(xiě)自己的Benchmark框架,本文通過(guò)給大家分享八條軍規(guī),幫助大家理解,需要的朋友可以參考下
    2020-07-07
  • springboot swagger不顯示接口的問(wèn)題及解決

    springboot swagger不顯示接口的問(wèn)題及解決

    這篇文章主要介紹了springboot swagger不顯示接口的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Java獲取一個(gè)類(lèi)的隱藏屬性的幾種方法

    Java獲取一個(gè)類(lèi)的隱藏屬性的幾種方法

    這篇文章主要討論了在Java中如何訪(fǎng)問(wèn)或修改類(lèi)的私有字段,包括使用公共的getter和setter方法、反射、繼承和序列化機(jī)制,文章強(qiáng)調(diào)了尊重類(lèi)的封裝性,感興趣的小伙伴跟著小編一起來(lái)看看吧
    2025-02-02
  • 淺析Java中的動(dòng)態(tài)代理

    淺析Java中的動(dòng)態(tài)代理

    動(dòng)態(tài)代理指代理類(lèi)和目標(biāo)類(lèi)的關(guān)系在程序運(yùn)行的時(shí)候確定的,客戶(hù)通過(guò)代理類(lèi)來(lái)調(diào)用目標(biāo)對(duì)象的方法。本文將通過(guò)案例詳細(xì)講解一下Java動(dòng)態(tài)代理的原理及實(shí)現(xiàn),需要的可以參考一下
    2022-09-09
  • SpringBoot 使用Mybatis分頁(yè)插件實(shí)現(xiàn)詳解

    SpringBoot 使用Mybatis分頁(yè)插件實(shí)現(xiàn)詳解

    這篇文章主要介紹了SpringBoot 使用Mybatis分頁(yè)插件實(shí)現(xiàn)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • idea報(bào)錯(cuò):程序包org.springframework.web.bind.annotation不存在

    idea報(bào)錯(cuò):程序包org.springframework.web.bind.annotation不存在

    在用本地的maven倉(cāng)庫(kù)的時(shí)候會(huì)org.springframework.web.bind.annotation不存在的錯(cuò)誤,本文就詳細(xì)的介紹一下解決方法,感興趣的可以了解下
    2023-08-08
  • 使用Runtime 調(diào)用Process.waitfor導(dǎo)致的阻塞問(wèn)題

    使用Runtime 調(diào)用Process.waitfor導(dǎo)致的阻塞問(wèn)題

    這篇文章主要介紹了使用Runtime 調(diào)用Process.waitfor導(dǎo)致的阻塞問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • JavaFx?Tooltip懸浮提示使用及自定義代碼詳解

    JavaFx?Tooltip懸浮提示使用及自定義代碼詳解

    本篇是基于TornadoFx框架對(duì)Tooltip組件進(jìn)行講解,使用Kotlin語(yǔ)言,和傳統(tǒng)Java使用有所區(qū)別,本章節(jié)包括對(duì)tooltip的樣式定制化以及指定窗口顯示,對(duì)JavaFx?Tooltip懸浮提示使用及自定義相關(guān)知識(shí)感興趣的朋友一起看看吧
    2021-12-12
  • 適用于Java初學(xué)者的學(xué)習(xí)路線(xiàn)圖

    適用于Java初學(xué)者的學(xué)習(xí)路線(xiàn)圖

    這篇文章主要介紹了學(xué)習(xí)Java的路線(xiàn)圖的五個(gè)必經(jīng)階段,還有一些作者的想法分享給大家,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09

最新評(píng)論

荃湾区| 长寿区| 罗城| 彭阳县| 冀州市| 思茅市| 上虞市| 临沧市| 内乡县| 平泉县| 亳州市| 施甸县| 彰武县| 宁明县| 徐水县| 准格尔旗| 门头沟区| 麻栗坡县| 清流县| 崇州市| 拜城县| 黄梅县| 邵阳市| 民乐县| 临漳县| 新昌县| 通河县| 来凤县| 鹰潭市| 福建省| 钟山县| 项城市| 平湖市| 芜湖县| 阳原县| 石柱| 宝清县| 玛曲县| 青神县| 宁海县| 共和县|