Spring Cloud Nacos 2021使用LoadBal
2022-07-19 本文已影响0人
AC编程
一、微服务之间的调用方式
在Spring Cloud
中微服务调用默认是用http
请求,主要通过一下三种 API
- RestTemplate:同步 http API
- WebClient:异步响应式 http API
- 第三方封装:如 openfeign
二、LoadBalancer替代了Ribbon
Ribbon目前已经停止维护,新版SpringCloud(2021.x.x)
用LoadBalancer
替代了Ribbon
。Spring Cloud
全家桶在Spring Cloud Commons
项目中,添加了Spring cloud Loadbalancer
作为新的负载均衡器,并且做了兼容
Nacos 2021版本已经没有自带ribbon的整合,所以无法通过修改Ribbon负载均衡的模式来实现nacos提供的负载均衡模式,需要引入另一个支持的jar包loadbalancer。
三、使用nacos 2021.1版本实现负载均衡
nacos最新版 2021.1版本中
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2021.1</version>
</dependency>
这个包已经不提供ribbon支持,需要引入另一个jar包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
使用这个包来实现负载均衡,具体实现方式和先前版本一样。
@Bean
@LoadBalanced //负载均衡注解
public RestTemplate restTemplate(){
return new RestTemplate();
}
注册bean的同时,添加LoadBalanced负载均衡注解,到这一步为止,可以实现基本的负载均衡功能,负载均衡默认配置为轮询配置
四、配置负载均衡策略
4.1 Ribbon载均衡策略
Ribbon有多种负载均衡策略
- 随机 RandomRule
- 轮询 RoundRobinRule
- 重试 RetryRule
- 最低并发 BestAvailableRule
- 可用过滤 AvailabilityFilteringRule
- 响应时间加权重 ResponseTimeWeightedRule
- 区域权重 ZoneAvoidanceRule
4.2 LoadBalancer载均衡策略
LoadBalancer貌似只提供了两种负载均衡器,不指定的时候默认用的是轮询
- RandomLoadBalancer 随机
- RoundRobinLoadBalancer 轮询
五、自定义loadbalancer负载均衡
5.1 实现loadbalancer自定义负载均衡模式进行注入
一共有两种写法,可以直接在Spring配置文件中注入Bean,但是这样的话,在 LoadBalancerClients 提供的类里需要写为Spring的配置文件类
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients;
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.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.web.client.RestTemplate;
@Configuration
@LoadBalancerClients(defaultConfiguration = {SpringBeanConfiguration.class})
public class SpringBeanConfiguration {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
@Bean
ReactorLoadBalancer<ServiceInstance> randomLoadBalancer(Environment environment,
LoadBalancerClientFactory loadBalancerClientFactory) {
String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
return new NacosSameClusterWeightedRule(loadBalancerClientFactory
.getLazyProvider(name, ServiceInstanceListSupplier.class),
name);
}
}
或者新建一个类
import org.springframework.cloud.client.ServiceInstance;
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;
//这里不用写Configuration
public class NacosSameClusterConfiguration{
@Bean
ReactorLoadBalancer<ServiceInstance> randomLoadBalancer(Environment environment,
LoadBalancerClientFactory loadBalancerClientFactory) {
String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
// 返回内容为自定义负载均衡的配置类
return new NacosSameClusterWeightedRule(loadBalancerClientFactory
.getLazyProvider(name, ServiceInstanceListSupplier.class),
name);
}
}
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
// 在这里配置我们自定义的LoadBalancer策略,注:这里的类为注入Bean的类,而非负载均衡的实现类
@LoadBalancerClients(defaultConfiguration = {NacosSameClusterConfiguration.class})
public class SpringBeanConfiguration {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
5.2 实现自定义的负载均衡,基于nacos的同集群优先调用以及基于权重调用思想实现
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.utils.StringUtils;
import com.alibaba.nacos.client.naming.core.Balancer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.DefaultResponse;
import org.springframework.cloud.client.loadbalancer.EmptyResponse;
import org.springframework.cloud.client.loadbalancer.Request;
import org.springframework.cloud.client.loadbalancer.Response;
import org.springframework.cloud.loadbalancer.core.NoopServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.core.ReactorServiceInstanceLoadBalancer;
import org.springframework.cloud.loadbalancer.core.SelectedInstanceCallback;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import reactor.core.publisher.Mono;
import javax.annotation.Resource;
@Slf4j
// 自定义负载均衡实现需要实现 ReactorServiceInstanceLoadBalancer 接口 以及重写choose方法
public class NacosSameClusterWeightedRule implements ReactorServiceInstanceLoadBalancer {
// 注入当前服务的nacos的配置信息
@Resource
private NacosDiscoveryProperties nacosDiscoveryProperties;
// loadbalancer 提供的访问当前服务的名称
final String serviceId;
// loadbalancer 提供的访问的服务列表
ObjectProvider<ServiceInstanceListSupplier> serviceInstanceListSupplierProvider;
public NacosSameClusterWeightedRule(ObjectProvider<ServiceInstanceListSupplier> serviceInstanceListSupplierProvider, String serviceId) {
this.serviceId = serviceId;
this.serviceInstanceListSupplierProvider = serviceInstanceListSupplierProvider;
}
/**
* 服务器调用负载均衡时调的放啊
* 此处代码内容与 RandomLoadBalancer 一致
*/
public Mono<Response<ServiceInstance>> choose(Request request) {
ServiceInstanceListSupplier supplier = this.serviceInstanceListSupplierProvider.getIfAvailable(NoopServiceInstanceListSupplier::new);
return supplier.get(request).next().map((serviceInstances) -> {
return this.processInstanceResponse(supplier, serviceInstances);
});
}
/**
* 对负载均衡的服务进行筛选的方法
* 此处代码内容与 RandomLoadBalancer 一致
*/
private Response<ServiceInstance> processInstanceResponse(ServiceInstanceListSupplier supplier, List<ServiceInstance> serviceInstances) {
Response<ServiceInstance> serviceInstanceResponse = this.getInstanceResponse(serviceInstances);
if (supplier instanceof SelectedInstanceCallback && serviceInstanceResponse.hasServer()) {
((SelectedInstanceCallback)supplier).selectedServiceInstance((ServiceInstance)serviceInstanceResponse.getServer());
}
return serviceInstanceResponse;
}
/**
* 对负载均衡的服务进行筛选的方法
* 自定义
* 此处的 instances 实例列表 只会提供健康的实例 所以不需要担心如果实例无法访问的情况
*/
private Response<ServiceInstance> getInstanceResponse(List<ServiceInstance> instances) {
if (instances.isEmpty()) {
return new EmptyResponse();
}
// 获取当前服务所在的集群名称
String currentClusterName = nacosDiscoveryProperties.getClusterName();
// 过滤在同一集群下注册的服务 根据集群名称筛选的集合
List<ServiceInstance> sameClusterNameInstList = instances.stream().filter(i-> StringUtils.equals(i.getMetadata().get("nacos.cluster"),currentClusterName)).collect(Collectors.toList());
ServiceInstance sameClusterNameInst;
if (sameClusterNameInstList.isEmpty()) {
// 如果为空,则根据权重直接过滤所有服务列表
sameClusterNameInst = getHostByRandomWeight(instances);
} else {
// 如果不为空,则根据权重直接过滤所在集群下的服务列表
sameClusterNameInst = getHostByRandomWeight(sameClusterNameInstList);
}
return new DefaultResponse(sameClusterNameInst);
}
private ServiceInstance getHostByRandomWeight(List<ServiceInstance> sameClusterNameInstList){
List<Instance> list = new ArrayList<>();
Map<String,ServiceInstance> dataMap = new HashMap<>();
// 此处将 ServiceInstance 转化为 Instance 是为了接下来调用nacos中的权重算法,由于入参不同,所以需要转换,此处建议打断电进行参数调试,以下是我目前为止所用到的参数,转化为map是为了最终方便获取取值到的服务对象
sameClusterNameInstList.forEach(i->{
Instance ins = new Instance();
Map<String, String> metadata = i.getMetadata();
ins.setInstanceId(metadata.get("nacos.instanceId"));
ins.setWeight(new BigDecimal(metadata.get("nacos.weight")).doubleValue());
ins.setClusterName(metadata.get("nacos.cluster"));
ins.setEphemeral(Boolean.parseBoolean(metadata.get("nacos.ephemeral")));
ins.setHealthy(Boolean.parseBoolean(metadata.get("nacos.healthy")));
ins.setPort(i.getPort());
ins.setIp(i.getHost());
ins.setServiceName(i.getServiceId());
ins.setMetadata(metadata);
list.add(ins);
// key为服务ID,值为服务对象
dataMap.put(metadata.get("nacos.instanceId"),i);
});
// 调用nacos官方提供的负载均衡权重算法
Instance hostByRandomWeightCopy = ExtendBalancer.getHostByRandomWeightCopy(list);
// 根据最终ID获取需要返回的实例对象
return dataMap.get(hostByRandomWeightCopy.getInstanceId());
}
}
class ExtendBalancer extends Balancer {
/**
* 根据权重选择随机选择一个
*/
public static Instance getHostByRandomWeightCopy(List<Instance> hosts) {
return getHostByRandomWeight(hosts);
}
}