作者:Grey
原文地址:
部落格園:使用 Spring Cloud Loadbalancer 實現使用者端負載均衡
CSDN:使用 Spring Cloud Loadbalancer 實現使用者端負載均衡
在Spring Cloud G 版釋出時提到,
Spring Cloud Netflix 那套元件很多都進入了維護期,如下表所示
同時,針對一些元件,Spring 官方給出了一些替代方案
針對 spring-cloud-ribbon 這個負載均衡元件,Spring 官方提出的替換解決方案是 Spring Cloud Loadbalancer。本文主要通過一個範例介紹了 Spring Cloud Loadbalancer 的基礎使用。
JDK 1.8+
Maven 3.5+
Spring Boot 版本:2.7.5
Spring Cloud 版本:2021.0.5
整個過程如下範例圖
注:Spring Cloud Loadbalancer 是在使用者端實現負載均衡策略。
伺服器端主要暴露一個服務,未做特殊設定
@GetMapping("/greeting")
public String greet() {
log.info("Access /greeting");
int randomNum = rand.nextInt(greetings.size());
return greetings.get(randomNum);
}
使用者端的核心設定如下,主要是針對負載均衡設定:
package git.snippet.client.config;
import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;
import java.util.Arrays;
import java.util.List;
@Configuration
@LoadBalancerClient(name = "server")
public class WebClientConfig {
@LoadBalanced
@Bean
WebClient.Builder webClientBuilder() {
return WebClient.builder();
}
@Bean
@Primary
ServiceInstanceListSupplier serviceInstanceListSupplier() {
return new ServiceInstanceListSupplier() {
@Override
public String getServiceId() {
return "server";
}
@Override
public Flux<List<ServiceInstance>> get() {
return Flux.just(Arrays.asList(
new DefaultServiceInstance(getServiceId() + "1", getServiceId(), "localhost", 8090, false),
new DefaultServiceInstance(getServiceId() + "2", getServiceId(), "localhost", 9092, false),
new DefaultServiceInstance(getServiceId() + "3", getServiceId(), "localhost", 9999, false)));
}
};
}
}
其中 @LoadBalancerClient(name = "server")
指定了 伺服器端的名稱;
getServiceId()
指定了伺服器端的服務 ID;
serviceInstanceListSupplier()
方法中列出了三個伺服器端範例的地址;
new DefaultServiceInstance(getServiceId() + "1", getServiceId(), "localhost", 8090, false)
new DefaultServiceInstance(getServiceId() + "2", getServiceId(), "localhost", 9092, false)
new DefaultServiceInstance(getServiceId() + "3", getServiceId(), "localhost", 9999, false)
有了這個設定,在使用者端的 Controller 中,做如下注入即可
package git.snippet.client.controller;
import org.springframework.cloud.client.loadbalancer.reactive.ReactorLoadBalancerExchangeFilterFunction;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
@RestController
public class HiController {
private final WebClient.Builder loadBalancedWebClientBuilder;
private final ReactorLoadBalancerExchangeFilterFunction lbFunction;
public HiController(WebClient.Builder loadBalancedWebClientBuilder, ReactorLoadBalancerExchangeFilterFunction lbFunction) {
this.loadBalancedWebClientBuilder = loadBalancedWebClientBuilder;
this.lbFunction = lbFunction;
}
@RequestMapping("/hi")
public Mono<String> hi(@RequestParam(value = "name", defaultValue = "Mary") String name) {
return loadBalancedWebClientBuilder.build().get().uri("http://server/greeting").retrieve().bodyToMono(String.class).map(greeting -> String.format("%s, %s!", greeting, name));
}
@RequestMapping("/hello")
public Mono<String> hello(@RequestParam(value = "name", defaultValue = "John") String name) {
return WebClient.builder().filter(lbFunction).build().get().uri("http://server/greeting").retrieve().bodyToMono(String.class).map(greeting -> String.format("%s, %s!", greeting, name));
}
}
首先啟動 server,注意:
啟動 server 的時候,需要啟動多範例,且每個範例要定義 VM options
範例一定義 VM options 為 -Dserver.port=8090
;
範例二定義 VM options 為 -Dserver.port=9092
;
範例三定義 VM options 為 -Dserver.port=9999
。
如果使用 IDEA Intellij,設定方式如下
三個 Server 啟動後,接下來啟動 client,執行 ClientApplication,啟動完畢後,多次存取: http://localhost:8888/hi
可以通過每個 server 範例的執行紀錄檔,檢視到每個範例都輪流獲取到了請求,實現了負載均衡。
見:spring-cloud-loadbalancer-usage
Spring Tips: Spring Cloud Loadbalancer
本文來自部落格園,作者:Grey Zeng,轉載請註明原文連結:https://www.cnblogs.com/greyzeng/p/16892071.html