亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring負載均衡LoadBalancer使用詳解

 更新時間:2023年11月18日 08:55:43   作者:morris131  
這篇文章主要介紹了Spring負載均衡LoadBalancer使用詳解,Spring Cloud LoadBalancer是Spring Cloud官方自己提供的客戶端負載均衡器, 用來替代Ribbon,Spring官方提供了兩種客戶端都可以使用loadbalancer,需要的朋友可以參考下

LoadBalancer

Spring Cloud LoadBalancer是Spring Cloud官方自己提供的客戶端負載均衡器, 用來替代Ribbon。

Spring官方提供了兩種客戶端都可以使用loadbalancer:

  • RestTemplate:Spring提供的用于訪問Rest服務的客戶端,RestTemplate提供了多種便捷訪問遠程Http服務的方法,能夠大大提高客戶端的編寫效率。默認情況下,RestTemplate默認依賴jdk的HTTP連接工具。
  • WebClient:從Spring WebFlux 5.0版本開始提供的一個非阻塞的基于響應式編程的進行Http請求的客戶端工具。它的響應式編程的基于Reactor的。WebClient中提供了標準Http請求方式對應的get、post、put、delete等方法,可以用來發(fā)起相應的請求。

RestTemplate整合LoadBalancer

引入LoadBalancer的依賴

<!-- LoadBalancer -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>

<!-- 提供了RestTemplate支持 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- nacos服務注冊與發(fā)現(xiàn)  移除ribbon支持-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </exclusion>
    </exclusions>
</dependency>

注意:nacos-discovery中引入了ribbon,需要移除ribbon的包,如果不移除,也可以在yml中配置不使用ribbon。

默認情況下,如果同時擁有RibbonLoadBalancerClient和BlockingLoadBalancerClient,為了保持向后兼容性,將使用RibbonLoadBalancerClient。

要覆蓋它,可以設置spring.cloud.loadbalancer.ribbon.enabled屬性為false。

spring:
  application:
    name: user-service

  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
    # 不使用ribbon,使用loadbalancer
    loadbalancer:
      ribbon:
        enabled: false

使用@LoadBalanced注解修飾RestTemplate,開啟客戶端負載均衡功能

package com.morris.user.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestConfig {

    /**
     * 默認的RestTemplate,不帶負載均衡
     * @return
     */
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    /**
     * 有負責均衡能力的RestTemplate
     * @return
     */
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate2() {
        return new RestTemplate();
    }
}

RestTemplate的使用:

package com.morris.user.controller;

import com.morris.user.entity.Order;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("user")
public class RestTemplateController {

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private RestTemplate restTemplate2;

    @GetMapping("findOrderByUserId")
    public List<Order> findOrderByUserId(Long userId) {
        Order[] orders = restTemplate.getForObject("http://127.0.0.1:8020/order/findOrderByUserId?userId=", Order[].class, userId);
        return Arrays.asList(orders);
    }

    @GetMapping("findOrderByUserId2")
    public List<Order> findOrderByUserId2(Long userId) {
        Order[] orders = restTemplate2.getForObject("http://order-service/order/findOrderByUserId?userId=", Order[].class, userId);
        return Arrays.asList(orders);
    }
}

WebClient整合LoadBalancer

引入依賴webflux,WebClient位于webflux內(nèi):

<!-- LoadBalancer -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>

<!-- webflux -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

<!-- nacos服務注冊與發(fā)現(xiàn) -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </exclusion>
    </exclusions>
</dependency>

同樣需要在配置文件中禁用ribbon:

spring:
  application:
    name: user-service

  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
    # 不使用ribbon,使用loadbalancer
    loadbalancer:
      ribbon:
        enabled: false

使用@LoadBalanced注解修飾WebClient.Builder,開啟客戶端負載均衡功能

package com.morris.user.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;

@Configuration
public class WebClientConfig {

    @Bean
    WebClient.Builder webClientBuilder() {
        return WebClient.builder();
    }

    @Bean
    @LoadBalanced
    WebClient.Builder webClientBuilder2() {
        return WebClient.builder();
    }

}

WebClient負載均衡的使用:

package com.morris.user.controller;

import com.morris.user.entity.Order;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("user2")
public class WebClientController {

    @Resource
    private WebClient.Builder webClientBuilder;

    @Resource
    private WebClient.Builder webClientBuilder2;


    @GetMapping("findOrderByUserId1")
    public Mono<List<Order>> findOrderByUserId1(Long userId) {
        return webClientBuilder.build().get().uri("http://127.0.0.1:8020/order/findOrderByUserId?userId=" + userId)
                .retrieve().bodyToMono(Order[].class).map(t -> Arrays.asList(t));
    }

    @GetMapping("findOrderByUserId2")
    public Mono<List<Order>> findOrderByUserId2(Long userId) {
        return webClientBuilder2.build().get().uri("http://order-service/order/findOrderByUserId?userId=" + userId)
                .retrieve().bodyToMono(Order[].class).map(t -> Arrays.asList(t));
    }

}

原理:底層會使用ReactiveLoadBalancer

WebClient設置Filter實現(xiàn)負載均衡

與RestTemplate類似,@LoadBalanced注解的功能是通過SmartInitializingSingleton實現(xiàn)的。

SmartInitializingSingleton是在所有的bean都實例化完成之后才會調(diào)用的,所以在bean的實例化期間使用@LoadBalanced修飾的WebClient是不具備負載均衡作用的,比如在項目的啟動過程中要使用調(diào)用某個微服務,此時WebClient是不具備負載均衡作用的。

可以通過手動為WebClient設置Filter實現(xiàn)負載均衡能力。

@Bean
WebClient.Builder webClientBuilder3(ReactorLoadBalancerExchangeFilterFunction lbFunction) {
    return WebClient.builder().filter(lbFunction);
}

到此這篇關(guān)于Spring負載均衡LoadBalancer使用詳解的文章就介紹到這了,更多相關(guān)Spring負載均衡LoadBalancer內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java 線程相關(guān)總結(jié)

    Java 線程相關(guān)總結(jié)

    這篇文章主要介紹了Java 線程的相關(guān)資料,幫助大家更好的理解和學習使用Java,感興趣的朋友可以了解下
    2021-02-02
  • 詳解Mybatis-plus(MP)中CRUD操作保姆級筆記

    詳解Mybatis-plus(MP)中CRUD操作保姆級筆記

    本文主要介紹了Mybatis-plus(MP)中CRUD操作保姆級筆記,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • 如何構(gòu)建可重復讀取inputStream的request

    如何構(gòu)建可重復讀取inputStream的request

    這篇文章主要介紹了如何構(gòu)建可重復讀取inputStream的request,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • SpringBoot深入理解之內(nèi)置web容器及配置的總結(jié)

    SpringBoot深入理解之內(nèi)置web容器及配置的總結(jié)

    今天小編就為大家分享一篇關(guān)于SpringBoot深入理解之內(nèi)置web容器及配置的總結(jié),小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • Java使用正則表達式驗證手機號和電話號碼的方法

    Java使用正則表達式驗證手機號和電話號碼的方法

    今天小編就為大家分享一篇關(guān)于Java使用正則表達式驗證手機號和電話號碼的方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • 詳解Http協(xié)議以及post與get區(qū)別

    詳解Http協(xié)議以及post與get區(qū)別

    這篇文章主要介紹了詳解Http協(xié)議以及post與get區(qū)別,通過分別說明Http協(xié)議以及get與post各自的概念,再到兩者作比較有著詳細的說明,希望對你有所幫助
    2021-06-06
  • springboot整合redisson實現(xiàn)延時隊列(附倉庫地址)

    springboot整合redisson實現(xiàn)延時隊列(附倉庫地址)

    延時隊列用于管理需要定時執(zhí)行的任務,對于大數(shù)據(jù)量和高實時性需求,使用延時隊列比定時掃庫更高效,Redisson提供一種高效的延時隊列實現(xiàn)方式,本文就來詳細的介紹一下,感興趣都可以了解學習
    2024-10-10
  • Nacos?版本不一致報錯Request?nacos?server?failed解決

    Nacos?版本不一致報錯Request?nacos?server?failed解決

    這篇文章主要為大家介紹了Nacos?版本不一致報錯Request?nacos?server?failed的解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11
  • Java網(wǎng)絡編程TCP實現(xiàn)文件上傳功能

    Java網(wǎng)絡編程TCP實現(xiàn)文件上傳功能

    這篇文章主要為大家詳細介紹了Java網(wǎng)絡編程TCP實現(xiàn)文件上傳功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • Java基礎(chǔ)之FastJson詳解

    Java基礎(chǔ)之FastJson詳解

    今天給大家復習Java基礎(chǔ)FastJson,文中有非常詳細的代碼示例,對正在學習java的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-05-05

最新評論