小程序开发定制Spring Cloud Alibaba - 10 Ribbon 自定义负载均衡策略(权重算法)

文章目录


Pre

我们看下 Server小程序开发定制上的服务详情中有个权重

小程序开发定制没有根据权重访问的策略, 小程序开发定制自己写个行不 ?

假设我们一个微服务部署了三台服务器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 饥饿加载 解决第一次耗时多的问题
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

然后通过代码设置一个全局配置 指定 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 {}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

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 */@Configurationpublic class GlobalRibbonConfig {    @Bean    public IRule globalConfig() {        // 根据权重的规则        return new ArtisanWeightedRule();    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

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

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 */@Slf4jpublic 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;    }}    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

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


验证

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

访问10次

观察请求日志

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


源码

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发