Spring Cloud Alibaba - 10 Ribbon 自定义负载均衡策略(权重算法)

本文详细介绍了如何在Spring Cloud Alibaba的Ribbon框架中,通过代码实现全局配置,并开发自定义策略,以根据服务性能权重进行负载均衡。通过Nacos配置权重,实现代理请求的优化分配。
摘要由CSDN通过智能技术生成

在这里插入图片描述


Pre

我们看下Nacos Server上的服务详情中有个权重

在这里插入图片描述

Spring Cloud Alibaba - 07 Ribbon 应用篇及内置的负载均衡算法

在这里插入图片描述

没有根据权重访问的策略, 自己写个行不 ?

假设我们一个微服务部署了三台服务器A,B,C.其中A,B,C三台服务的性能不一,A的性能最牛逼,B次之,C最差.那么我们设置权重比例 为5 : 3:2 那就说明 10次请求到A上理论是5次,B服务上理论是3次,B服务理论是2次.


工程

artisan-cloud-customcfg-ribbon-order (修改)

artisan-cloud-customcfg-ribbon-pay (无修改)

在这里插入图片描述

首先屏蔽细粒度配置

#自定义Ribbon的细粒度配置 (推荐)
#artisan-pay-center:
#  ribbon:
#    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule
#
#artisan-product-center:
#  ribbon:
#    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

# ribbon 饥饿加载 解决第一次耗时多的问题

然后通过代码设置一个全局配置 指定 GlobalRibbonConfig

package com.artisan.config;

import com.globalconfig.GlobalRibbonConfig;
import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.context.annotation.Configuration;

/**
 * @author 小工匠
 * @version 1.0
 * @description: Ribbon 全局配置,通过代码实现
 * @date 2022/2/3 0:05
 * @mark: show me the code , change the world
 */


@Configuration
@RibbonClients(defaultConfiguration = GlobalRibbonConfig.class)
public class CustomRibbonConfig2 {
}

GlobalRibbonConfig 设置负载均衡策略

package com.globalconfig;

import com.artisan.customrules.ArtisanWeightedRule;
import com.netflix.loadbalancer.IRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 小工匠
 * @version 1.0
 * @description: 全局负载均衡策略
 * @date 2022/2/3 0:06
 * @mark: show me the code , change the world
 */

@Configuration
public class GlobalRibbonConfig {

    @Bean
    public IRule globalConfig() {
        // 根据权重的规则
        return new ArtisanWeightedRule();
    }
}

开发自定义策略 (权重访问)

package com.artisan.customrules;

import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.cloud.nacos.ribbon.NacosServer;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.BaseLoadBalancer;
import com.netflix.loadbalancer.Server;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author 小工匠
 * @version 1.0
 * @description: 自定义权重策略
 * @date 2022/2/3 0:08
 * @mark: show me the code , change the world
 */
@Slf4j
public class ArtisanWeightedRule extends AbstractLoadBalancerRule {


    @Autowired
    private NacosDiscoveryProperties discoveryProperties;

    @Override
    public void initWithNiwsConfig(IClientConfig iClientConfig) {
        //读取配置文件并且初始化,ribbon内部的, 几乎用不上
    }

    @Override
    public Server choose(Object key) {
        try {
            log.info("key:{}", key);
            BaseLoadBalancer baseLoadBalancer = (BaseLoadBalancer) this.getLoadBalancer();
            log.info("baseLoadBalancer--->:{}", baseLoadBalancer);

            //获取微服务的名称
            String serviceName = baseLoadBalancer.getName();

            //获取Nacos服务发现的相关组件API
            NamingService namingService = discoveryProperties.namingServiceInstance();

            //获取 一个基于nacos client 实现权重的负载均衡算法
            Instance instance = namingService.selectOneHealthyInstance(serviceName);

            //返回一个server
            return new NacosServer(instance);
        } catch (NacosException e) {
            log.error("自定义负载均衡算法错误");
        }
        return null;
    }
}
    

可以看到,这里的权重访问主要是依赖Nacos提供的功能


验证

在这里插入图片描述

权重的取值 0 ~ 1 , 修改两个节点的访问权重 0.9 和 0.1

在这里插入图片描述

访问10次
在这里插入图片描述

观察请求日志
在这里插入图片描述

当调整为 0.5:0.5 , 再此请求10次

在这里插入图片描述


源码

https://github.com/yangshangwei/SpringCloudAlibabMaster

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小小工匠

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

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

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

打赏作者

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

抵扣说明:

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

余额充值