Ribbon使用

Ribbon :处理客户端负载均衡和容错的解决方案

配置Ribbon的负载均衡

Rule接口: 定义客户端负载均衡的规则

  • RandomRule :随机选择
  • RoundRobinRule
  • ZoneAvoidanceRule

配置ribbon的负载均衡策略

  • 在配置文件中配置
yml 复制代码
user-center:
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
  • 在java代码中配置
java 复制代码
/**
 *
 * 不同的服务使用不同的ribbon策略
 * @RibbonClient : 表示这个配置是为xxx服务的
 *
 */
//@Configuration
//@RibbonClient(name = "user-center",configuration = RibbonConfiguration.class)
    // 全局服务设置
    @RibbonClients(defaultConfiguration = RibbonConfiguration.class)
public class UserCenterRibbonConfiguration {
}

如果是为某个微服务单独配置负载均衡策略 @RibbonClient 指定配置文件的位置,,,这个配置文件不能被springboot扫描到,,父子上下文环境,,就像spring和springmvc的环境,,不能重叠,,如果重叠,相当于会变成全局的配置

Ribbon的配置文件

java 复制代码
@Configuration
public class RibbonConfiguration {
    @Bean
    public IRule ribbonRule(){
        // 负载均衡策略为随机
//        return new RandomRule();

        // 根据权重      这个类是自己写的根据权重判断
        return new NacosWeightedRule();
    }

    /**
     * 配置 ping的规则
     * @return
     */
    @Bean
    public IPing ping(){
        return new PingUrl();
    }
}

写自己的负载均衡算法:

继承AbstractLoadBalancerRule , 实现里面的 choose()方法:

java 复制代码
/**
 *
 * nacos自带的   根据权重的负载均衡策略,,,,    需要配置在ribbon的负载均衡中
 */
@Slf4j
public class NacosWeightedRule extends AbstractLoadBalancerRule {

    @Autowired
    NacosDiscoveryProperties nacosDiscoveryProperties;


    /**
     * 初始化规则,, 获取配置信息,,以便在choose中使用
     * @param clientConfig
     */
    @Override
    public void initWithNiwsConfig(IClientConfig clientConfig) {

    }

    /**
     * 返回与该规则关联的负载均衡器,,, 负载均衡器维护了服务实例列表
     * @return
     */
    @Override
    public ILoadBalancer getLoadBalancer() {
        return super.getLoadBalancer();
    }

    /**
     * 选择一个服务实例来处理请求,,,   在这个方法中实现自己的负载均衡策略
     * @param key
     * @return
     */
    @Override
    public Server choose(Object key) {
        try {
            /**
             * BaseLoadBalancer :  基本的负载均衡策略:  比如:轮询(round robin),随机选择(Random),,,
             */
            BaseLoadBalancer loadBalancer = (BaseLoadBalancer) this.getLoadBalancer();

            // 想要请求的微服务的名称
            String name = loadBalancer.getName();

            // 负载均衡算法,,,     nacos内置了基于权重的算法
            NamingService namingService = nacosDiscoveryProperties.namingServiceInstance();

            // nacos client 自动通过基于权重的负载均衡算法,给我们一个实例
            Instance instance = namingService.selectOneHealthyInstance(name);

            log.info("port = {},instance = {}",instance.getPort(),instance);


            return new NacosServer(instance);
        } catch (NacosException e) {
            throw new RuntimeException(e);
        }
    }
}

在nacos中设置的权重就会生效:

用到的类

ILoadBalancer : 是 netflix ribbon中负责管理服务实例列表提供负载均衡策略的核心接口

  • getAllServers() 返回当前负载均衡器中维护的所有服务实例的列表
  • addServers() 向负载均衡器中添加service
  • chooseServer() : 根据负载均衡规则,选择一个实例来处理请求
相关推荐
超级无敌攻城狮36 分钟前
Agent 到底是怎么跑起来的
前端·后端·架构
Devin~Y36 分钟前
从Spring Boot到Spring AI:音视频AIGC内容社区Java大厂面试三轮连环问(含Kafka/Redis/安全/可观测性答案)
java·spring boot·redis·spring cloud·kafka·spring security·resilience4j
二妹的三爷44 分钟前
私有化部署DeepSeek并SpringBoot集成使用(附UI界面使用教程-支持语音、图片)
spring boot·后端·ui
神奇小汤圆1 小时前
程序员面试必备的Java八股文,适合所有的Java求职者
后端
覆东流1 小时前
第3天:Python print深入与格式化输出
开发语言·后端·python
StockTV1 小时前
SpringBoot对接黄金白银期货数据API
java·spring boot·后端
techdashen2 小时前
Rust 正式成立 Types Team:类型系统终于有了专属团队
开发语言·后端·rust
小谢小哥2 小时前
43-Kafka 核心原理与实战
后端·架构
金銀銅鐵2 小时前
[git] 如何找到已经“丢失”的 commit?
git·后端
消失的旧时光-19432 小时前
Spring Boot 核心机制之 @Conditional:从原理到实战(一次讲透)
java·spring boot·后端