欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

Nacos注冊中心

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

pom.xml

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

啟動服務(wù)

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

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

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

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

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

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類會自動使用容器中的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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java反射使用的詳細(xì)介紹(最新推薦)

    Java反射使用的詳細(xì)介紹(最新推薦)

    這篇文章主要介紹了Java反射使用的詳細(xì)介紹,反射的第一步都是先得到編譯后的Class類對象,然后就可以得到Class的全部成分,本文結(jié)合實(shí)例代碼詳細(xì)講解,需要的朋友可以參考下
    2023-02-02
  • Redis6搭建集群并在SpringBoot中使用RedisTemplate的實(shí)現(xiàn)

    Redis6搭建集群并在SpringBoot中使用RedisTemplate的實(shí)現(xiàn)

    本文主要介紹了Redis6搭建集群并在SpringBoot中使用RedisTemplate,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Springboot中Jackson用法詳解

    Springboot中Jackson用法詳解

    Springboot自帶默認(rèn)json解析Jackson,可以在不引入其他json解析包情況下,解析json字段,下面我們就來聊聊Springboot中Jackson的用法吧
    2025-01-01
  • 詳解JAVA 抽象類

    詳解JAVA 抽象類

    這篇文章主要介紹了JAVA 抽象類的相關(guān)資料,文中講解非常細(xì)致,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • idea如何配置springboot熱部署

    idea如何配置springboot熱部署

    這篇文章主要介紹了idea如何配置springboot熱部署問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Springboot加載所有Bean之后運(yùn)行方式

    Springboot加載所有Bean之后運(yùn)行方式

    這篇文章主要介紹了Springboot加載所有Bean之后運(yùn)行方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • Java實(shí)現(xiàn)LRU緩存的代碼詳解

    Java實(shí)現(xiàn)LRU緩存的代碼詳解

    LRU緩存是一種緩存替換策略,當(dāng)緩存容量達(dá)到上限時,LRU 會淘汰掉最近最少使用的緩存項(xiàng),在 Java 中,我們可以使用 LinkedHashMap 來實(shí)現(xiàn)一個簡單的 LRU 緩存,所以本文給大家介紹了Java實(shí)現(xiàn)LRU緩存的方法,需要的朋友可以參考下
    2025-03-03
  • JVM的類加載過程詳細(xì)說明

    JVM的類加載過程詳細(xì)說明

    近來讀了《深入理解JVM虛擬機(jī)》的部分內(nèi)容,對JVM也慢慢有個整體的認(rèn)識,今天就來分享一下我對JVM類加載過程的學(xué)習(xí)和理解,需要的朋友可以參考下
    2021-06-06
  • 配置DispatcherServlet的方法介紹

    配置DispatcherServlet的方法介紹

    今天小編就為大家分享一篇關(guān)于配置DispatcherServlet的方法介紹,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • Maven deploy配置方法詳解

    Maven deploy配置方法詳解

    這篇文章主要介紹了Maven deploy配置方法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07

最新評論