1、什么是Spring Cloud LoadBalancer
Spring Cloud LoadBalancer是Spring Cloud官方自己提供的客户端负载均衡器,用来替代Ribbon。(Ribbon:也进入了维护状态,停止更新了,但是Spring官方还在使用)
Spring官方提供了两种负载均衡的客户端:
????????第一种:RestTemplate:
RestTemplate默认依赖jdk的HTTP连接工具,提供了多种便捷访问远程Http服务的方法,是Spring提供的用于访问Rest服务的客户端;
????????第二种:WebClient:
从Spring WebFlux5.0开始提供的一个非阻塞的基于响应式编程的进行Http请求的客户端工具。它的响应式编程基于Reactor。WebClient中提供了标准Http请求方式对应的get、post、put、delete等方法,可以用来发起相应请求。
2、RestTemplate整合LoadBalancer
2.1引入依赖
<dependencies>
<!--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服务注册与发现,移除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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
nacos-discovery中引入了ribbon,需要移除ribbon的包,如果不移除,也可以在yml中配置不使用ribbon
server:
port: 10001
spring:
application:
name: service-consumer
cloud:
nacos:
discovery:
server-addr: http://101.132.143.77:8848
# 不使用ribbon
loadbalancer:
ribbon:
enabled: false
原理:默认情况下,如果同时拥有RibbonLoadBalancerClient和BlockingLoadBalancerClient,为了保持向后兼容性,将使用RibbonLoadBalancerClient。要覆盖它,可以设置spring.cloud.loadbalancer.ribbon.enabled属性等于false;
?
?2.2使用@LaodBalanced注解配置RestTemplate
@Configuration
public class CommonConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
?2.3使用
@RestController
@RequestMapping("/provide/")
public class ProvideController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("hello")
public String test(){
return "hello,provider";
}
@GetMapping("consume")
public String consume(){
String url = "http://service-consumer/consume/hello";
return restTemplate.getForObject(url,String.class);
}
}
3、WebClient整合LoadBalancer
3.1引入依赖
<!--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服务注册与发现,移除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>
3.2配置WebClient作为负载均衡器client
@Configuration
public class CommonConfig {
@LoadBalanced
@Bean
WebClient.Builder webClientBuilder(){
return WebClient.builder();
}
@Bean
WebClient webClient(){
return webClientBuilder().build();
}
}
3.3使用?
@RestController
@RequestMapping("/consume/")
public class ConsumeController {
@Autowired
private WebClient webClient;
@GetMapping("hello")
public String test(){
return "hello,consumer";
}
@GetMapping("provide")
public Mono<String> provide(){
String url = "http://service-provider/provide/hello";
Mono<String> result = webClient.get().uri(url).retrieve().bodyToMono(String.class);
return result;
}
}
原理:底层会使用ReactiveLoadBalancer
引入webFlux
@Autowired
private ReactorLoadBalancerExchangeFilterFunction lbFunction;
@GetMapping("provide2")
public Mono<String> provide2(){
String url = "http://service-provider/provide/hello";
Mono<String> result = WebClient.builder()
.filter(lbFunction)
.build()
.get()
.uri(url)
.retrieve()
.bodyToMono(String.class);
return result;
}
?
|