2020年前SpringCloud是采用Ribbon作为负载均衡实现,但是在2020后采用了LoadBalancer替代
LoadBalancer默认提供了两种负载均衡策略(只能通过配置类来修改负载均衡策略)
1.RandomLoadBalancer-随机分配策略
2.RoundRobinLoadBalancer-轮询分配策略(默认)
添加一个自定义的负载均衡策略CustomLoadBalancerConfiguration 配置类,可以直接复制官网
package com.test.order.config;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.loadbalancer.core.RandomLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;public class CustomLoadBalancerConfiguration {@BeanReactorLoadBalancer<ServiceInstance> randomLoadBalancer(Environment environment,LoadBalancerClientFactory loadBalancerClientFactory) {String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);return new RandomLoadBalancer(loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class),name);}
}
注意如果负载均衡策略配置类使用spring注解@ComponentScan或者@Configuration,就要注意如果被SpringBootApplication的ComponentScan扫描到(默认扫描SpringBootApplication目录下的全部包),就会变成全局的负载均衡策略,如果需要局部负载均衡最好不用注解,或者设置不被默认的ComponentScan扫描到
在启动类配置那个微服务使用那种负载均衡策略(如果有Ribbon负载均衡需要在yml负载均衡去掉,一般在2020版本后无需配置去掉,因为默认没有Ribbon)
@SpringBootApplication
//@LoadBalancerClients(defaultConfiguration={CustomLoadBalancerConfiguration.class})
//value的值表示需要负载均衡的微服务,调用指定的负载均衡策略
//@LoadBalancerClient(value = "stock-nacos",configuration = {CustomLoadBalancerConfiguration.class})
//@LoadBalancerClients({@LoadBalancerClient(value = "stock-nacos", configuration = {CustomLoadBalancerConfiguration.class}),@LoadBalancerClient(value = "stock-nacos2", configuration = {CustomLoadBalancerConfiguration2.class})})
@LoadBalancerClients({@LoadBalancerClient(value = "stock-nacos", configuration = {CustomLoadBalancerConfiguration.class})})
public class OrderLoadbalancerApplication {public static void main(String[] args) {SpringApplication.run(OrderLoadbalancerApplication.class, args);}@Bean@LoadBalancedpublic RestTemplate restTemplate(RestTemplateBuilder builder) {RestTemplate restTemplate = builder.build();return restTemplate;}
}
pom.xml配置(因为spring-cloud-starter-alibaba-nacos-discovery依赖中的spring-cloud-starter-loadbalancer的true,所以不会继承,需要收到继承),如下
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-loadbalancer</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency></dependencies>
父pom.xml配置
<dependencyManagement><!--Spring Cloud alibaba的版本管理,通过dependency完成继承--><dependencies><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>2021.0.4.0</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>2021.0.4</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>