package com.water.step.service.user.nacos; import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.DynamicServerListLoadBalancer;
import com.netflix.loadbalancer.Server;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.alibaba.nacos.NacosDiscoveryProperties;
import org.springframework.cloud.alibaba.nacos.ribbon.NacosServer; /**
* 基于Nacos权重的负载均衡
*
* @Author Administrator
* @create 2020/10/14 20:54
*/
@Slf4j
public class NacosWeightLoadBalancerRule extends AbstractLoadBalancerRule { @Autowired
private NacosDiscoveryProperties nacosDiscoveryProperties; @Override
public void initWithNiwsConfig(IClientConfig iClientConfig) {
// 读取配置文件,并初始化NacosWeightLoadBalancerRule
} @Override
public Server choose(Object o) {
DynamicServerListLoadBalancer loadBalancer = (DynamicServerListLoadBalancer) getLoadBalancer();
// 请求的微服务名称
String applicationName = loadBalancer.getName();
try {
// nacos 通过基于权重的负载均衡算法,算出一个健康的服务实例以供调用
Instance instance = nacosDiscoveryProperties.namingServiceInstance().selectOneHealthyInstance(applicationName);
return new NacosServer(instance);
} catch (NacosException e) {
log.error("获取服务实例异常:{}", e.getMessage());
}
return null;
}
}
局部配置:调用指定服务提供的服务时,使用基于Nacos权重的负载均衡算法
下面以调用为step-service-order的微服务请求为例:
在application.yaml文件中新增以下代码:
# 被调用的微服务名
step-service-order:
ribbon:
# 指定使用Nacos提供的基于权重的负载均衡
NFLoadBalancerRuleClassName: com.water.step.service.user.nacos.NacosWeightLoadBalancerRule
全局配置:调用其他微服务,一律使用基于Nacos权重的负载均衡算法
package com.water.step.service.user.nacos; import com.netflix.loadbalancer.IRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class RibbonConfiguration { @Bean
public IRule ribbonRule() {
return new NacosWeightLoadBalancerRule();
} }