springboot-nacos-ribbon负载均衡实现

负载均衡Ribbon

Ribbon是Netflx发布的开源项目,主要功能是提供客户端的软件负载均衡算法和服务调用。Ribbon客户端组件提供一系列完善的配置项如连接超时,重试等。简单的说,就是在配置文件中列出Load Balancer(简称LB)后面所有的机器,Ribbon会自动的帮助你基于某种规则(如简单轮询,随机连接等)去连接这些机器。我们很容易使用Ribbon实现自定义的负载均衡算法。

springboot加载配置

@Configuration
public class RibbonConfig {

@Primary
@Bean
public IRule getRule() {
    // 实现带有权重的负载均衡策略
    return new DefaultGrayLoadBalancerRule();
}

}

Ribbon核心组件IRule

RoundRobinRule:轮询,出厂默认使用轮询;
RandomRule:随机;
RetryRule:先按照RoundRobinRule的策略获取服务,如果获取服务失败则在指定时间内会进行重试,获取可用服务;
WeightedResponseTimeRule:对RandomRule的扩展,响应速度越快的实例选择权重越大,越容易被选择;
BestAvaliableRule:会先过滤由于多次访问故障而处于断路器跳闸状态的服务,然后选择一个并发量小的服务;
AvailabilityFilteringRule:先过滤掉故障实例,再选择并发较小的实例;
ZoneAvoidanceRule:默认规则,复合判断server所在区域的性能和server的可用性选择服务器。

实现choose方法

可以获取nacos,实现负载均衡


```java
   @Override
    public Server choose(Object key) {
        try {
            NamingService namingService = this.nacosDiscoveryProperties.namingServiceInstance();
            // 获取到所有的目标实例
            DynamicServerListLoadBalancer loadBalancer = (DynamicServerListLoadBalancer) getLoadBalancer();
            String serverName = "";
            if (key != null && !StrUtil.isEmpty((String) key)) {
                serverName = (String) key;
            } else {
                serverName = loadBalancer.getName();
            }
            List<Instance> instances = namingService.selectInstances(serverName, true);
            if (CollectionUtils.isEmpty(instances)) {
                log.warn("no instance in service {}", serverName);
                return null;
            }

            List<Instance> instancesToChoose = instances(instances);

            if (CollectionUtils.isEmpty(instancesToChoose)) {
                log.warn("no instance in service {}", serverName);
                return null;
            }
            String clusterName = this.nacosDiscoveryProperties.getClusterName();

            if (StringUtils.isNotBlank(clusterName)) {
                List<Instance> sameClusterInstances = instancesToChoose.stream()
                        .filter(instance -> Objects.equals(clusterName,
                                instance.getClusterName()))
                        .collect(Collectors.toList());
                if (!org.springframework.util.CollectionUtils.isEmpty(sameClusterInstances)) {
                    instancesToChoose = sameClusterInstances;
                } else {
                    log.warn(
                            "A test-cluster,name = {}, clusterName = {}, instance = {}",
                            serverName, clusterName, instancesToChoose);
                }
            }

            // 保留nacos默认负载均衡
            Instance instance = ExtendBalancer.getHostByRandomWeight2(instancesToChoose);
            return new NacosServer(instance);

        } catch (Exception e) {
            log.warn("Nacos error", e);
            return null;
        }
    }

Autowired自动装载

@Autowired
protected NacosDiscoveryProperties nacosDiscoveryProperties;

instances实现


    
  protected List<Instance> instances(List<Instance> instances) {
        List<Instance> target = new ArrayList<>();
        List<Instance> defaults = new ArrayList<>();
        List<Instance> result;
        String fromRegion = GrayContextHolder.getCurrentGrayContext().get("default");
        // 匹配满足region的实例
        for (Instance instance : instances) {
            Map<String, String> tarMetadata = instance.getMetadata();
            // 目标实例
                instancesResult .add(instance);
        }
        instancesResult = instancesTarget;
     return instancesResult;
    }

运行后,在nacos操作后台可以查看到主机内容。

  • 12
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

中年程序员一枚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值