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

SpringCloud如何根據(jù)服務(wù)名獲取服務(wù)運(yùn)行實(shí)例并進(jìn)行負(fù)載均衡

 更新時(shí)間:2025年01月20日 17:28:08   作者:DanceDonkey  
文章介紹了SpringCloud中使用Nacos作為注冊(cè)中心時(shí),服務(wù)注冊(cè)和發(fā)現(xiàn)的過(guò)程,以及如何通過(guò)DiscoveryClient接口和LoadBalancerClient類進(jìn)行服務(wù)的負(fù)載均衡,感興趣的朋友跟隨小編一起看看吧

Nacos注冊(cè)中心

每個(gè)服務(wù)啟動(dòng)之后都要向注冊(cè)中心發(fā)送服務(wù)注冊(cè)請(qǐng)求,注冊(cè)中心可以和各個(gè)注冊(cè)客戶端自定義協(xié)議實(shí)現(xiàn)服務(wù)注冊(cè)和發(fā)現(xiàn)。

pom.xml

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

啟動(dòng)服務(wù)

獲取服務(wù)實(shí)例測(cè)試

@EnableDiscoveryClient
@SpringBootApplication
public class Main83
{
    public static void main(String[] args)
    {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(Main83.class, args);
        DiscoveryClient discoveryClient = applicationContext.getBean(DiscoveryClient.class);
        List<ServiceInstance> instances = discoveryClient.getInstances("nacos-payment-provider");
        for (ServiceInstance instance : instances) {
            System.out.println("instance.getHost() = " + instance.getHost());
            System.out.println("instance.getPort() = " + instance.getPort());
        }
        NacosDiscoveryClient nacosDiscoveryClient = applicationContext.getBean(NacosDiscoveryClient.class);
        List<ServiceInstance> serviceInstances = nacosDiscoveryClient.getInstances("nacos-payment-provider");
        for (ServiceInstance instance : serviceInstances) {
            System.out.println("Nacos instance.getHost() = " + instance.getHost());
            System.out.println("instance.getPort() = " + instance.getPort());
        }
        LoadBalancerClient balancerClient = applicationContext.getBean(LoadBalancerClient.class);
        System.out.println("balancerClient.choose(\"nacos-payment-provider\").getPort() = " + balancerClient.choose("nacos-payment-provider").getPort());
        System.out.println("balancerClient.choose(\"nacos-payment-provider\").getPort() = " + balancerClient.choose("nacos-payment-provider").getPort());
        System.out.println("balancerClient.choose(\"nacos-payment-provider\").getPort() = " + balancerClient.choose("nacos-payment-provider").getPort());
        System.out.println("balancerClient.choose(\"nacos-payment-provider\").getPort() = " + balancerClient.choose("nacos-payment-provider").getPort());
        System.out.println("balancerClient.choose(\"nacos-payment-provider\").getPort() = " + balancerClient.choose("nacos-payment-provider").getPort());
        System.out.println("balancerClient.choose(\"nacos-payment-provider\").getPort() = " + balancerClient.choose("nacos-payment-provider").getPort());
    }
}

在引入注冊(cè)中心相關(guān)的依賴后,注冊(cè)中心的相關(guān)API會(huì)實(shí)現(xiàn)SpringCloud規(guī)范,自動(dòng)給容器中存入DiscoveryClient對(duì)象,引入了Nacos,就是NacosDiscoveryClient。通過(guò)DiscoveryClient接口提供的能力可以從注冊(cè)中心實(shí)時(shí)拉取服務(wù)列表。

  • 負(fù)載均衡實(shí)現(xiàn)

微服務(wù)在自動(dòng)進(jìn)行服務(wù)發(fā)現(xiàn)后,進(jìn)行的是客戶端負(fù)載均衡,也就是客戶端自己維護(hù)了一套負(fù)載均衡算法,每次請(qǐng)求選擇某一臺(tái)服務(wù)器進(jìn)行請(qǐng)求。

pom.xml

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>
LoadBalancerClient balancerClient = applicationContext.getBean(LoadBalancerClient.class);
        System.out.println("balancerClient.choose(\"nacos-payment-provider\").getPort() = " + balancerClient.choose("nacos-payment-provider").getPort());
        System.out.println("balancerClient.choose(\"nacos-payment-provider\").getPort() = " + balancerClient.choose("nacos-payment-provider").getPort());
        System.out.println("balancerClient.choose(\"nacos-payment-provider\").getPort() = " + balancerClient.choose("nacos-payment-provider").getPort());
        System.out.println("balancerClient.choose(\"nacos-payment-provider\").getPort() = " + balancerClient.choose("nacos-payment-provider").getPort());
        System.out.println("balancerClient.choose(\"nacos-payment-provider\").getPort() = " + balancerClient.choose("nacos-payment-provider").getPort());
        System.out.println("balancerClient.choose(\"nacos-payment-provider\").getPort() = " + balancerClient.choose("nacos-payment-provider").getPort());

LoadBalancerClient類會(huì)自動(dòng)使用容器中的DiscoveryClient進(jìn)行服務(wù)的負(fù)載均衡。

到此這篇關(guān)于SpringCloud根據(jù)服務(wù)名獲取服務(wù)運(yùn)行實(shí)例并進(jìn)行負(fù)載均衡的文章就介紹到這了,更多相關(guān)SpringCloud服務(wù)名獲取服務(wù)運(yùn)行實(shí)例內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java使用apache.poi導(dǎo)出word文件的示例代碼

    java使用apache.poi導(dǎo)出word文件的示例代碼

    這篇文章主要介紹了java使用apache.poi導(dǎo)出word文件,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-07-07
  • java 數(shù)據(jù)結(jié)構(gòu)之堆排序(HeapSort)詳解及實(shí)例

    java 數(shù)據(jù)結(jié)構(gòu)之堆排序(HeapSort)詳解及實(shí)例

    這篇文章主要介紹了java 數(shù)據(jù)結(jié)構(gòu)之堆排序(HeapSort)詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • mybatis集成到spring的方式詳解

    mybatis集成到spring的方式詳解

    這篇文章主要介紹了mybatis是如何集成到spring的,將mybatis集成到spring之后,就可以被spring的ioc容器托管,再也不用自己創(chuàng)建SqlSessionFactory?、打開(kāi)SqlSession等操作,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • Spring Boot 項(xiàng)目開(kāi)發(fā)全流程實(shí)戰(zhàn)示例總結(jié)

    Spring Boot 項(xiàng)目開(kāi)發(fā)全流程實(shí)戰(zhàn)示例總結(jié)

    本文詳細(xì)介紹了SpringBoot項(xiàng)目的開(kāi)發(fā)流程,從實(shí)體類定義到Controller接口開(kāi)放,涵蓋了代碼注釋、核心概念圖解及高頻面試題,通過(guò)實(shí)際操作,展示了如何實(shí)現(xiàn)用戶增刪改查功能,感興趣的朋友跟隨小編一起看看吧
    2026-01-01
  • 解決spring-integration-mqtt頻繁報(bào)Lost connection錯(cuò)誤問(wèn)題

    解決spring-integration-mqtt頻繁報(bào)Lost connection錯(cuò)誤問(wèn)題

    這篇文章主要介紹了解決spring-integration-mqtt頻繁報(bào)Lost connection錯(cuò)誤問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • java Disruptor構(gòu)建高性能內(nèi)存隊(duì)列使用詳解

    java Disruptor構(gòu)建高性能內(nèi)存隊(duì)列使用詳解

    這篇文章主要為大家介紹了java Disruptor構(gòu)建高性能內(nèi)存隊(duì)列使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • Spring?Validation接口入?yún)⑿r?yàn)示例代碼

    Spring?Validation接口入?yún)⑿r?yàn)示例代碼

    Spring?Validation是一種用于實(shí)現(xiàn)數(shù)據(jù)校驗(yàn)的框架,它提供了一系列的校驗(yàn)器,針對(duì)不同的數(shù)據(jù)類型可以使用不同的校驗(yàn)器進(jìn)行校驗(yàn),下面這篇文章主要給大家介紹了關(guān)于Spring?Validation接口入?yún)⑿r?yàn)的相關(guān)資料,需要的朋友可以參考下
    2023-06-06
  • java 線程鎖詳細(xì)介紹及實(shí)例代碼

    java 線程鎖詳細(xì)介紹及實(shí)例代碼

    這篇文章主要介紹了java 線程鎖詳細(xì)介紹及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2016-12-12
  • Java異常退出條件的判斷示例代碼

    Java異常退出條件的判斷示例代碼

    這篇文章主要介紹了Java異常退出條件的判斷示例代碼,涉及常見(jiàn)異常退出條件等相關(guān)內(nèi)容,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-10-10
  • Java中printStackTrace()用法示例

    Java中printStackTrace()用法示例

    這篇文章主要給大家介紹了關(guān)于Java中printStackTrace()用法的相關(guān)資料,printStackTrace()方法一般與拋出異常搭配使用,效果是打印出異常位置,需要的朋友可以參考下
    2024-05-05

最新評(píng)論

东台市| 甘洛县| 重庆市| 响水县| 商南县| 北海市| 容城县| 广安市| 东台市| 潢川县| 珠海市| 叶城县| 会昌县| 黎平县| 霍山县| 钦州市| 上栗县| 正阳县| 文登市| 海宁市| 怀宁县| 扎兰屯市| 萨迦县| 陈巴尔虎旗| 洪泽县| 门头沟区| 澄城县| 南澳县| 孟津县| 伽师县| 东方市| 平泉县| 比如县| 玉山县| 香格里拉县| 称多县| 浏阳市| 曲麻莱县| 原阳县| 姚安县| 伊宁市|