SpringCloudAlibaba - 整合 Ribbon 实现负载均衡

前言

Ribbon是一个Netflix开源的客户端侧负载均衡器,其提供了丰富的负载均衡算法,服务消费者集成Ribbon后,Ribbon会自动从Nacos Server获取想要调用的服务的地址列表,通过负载均衡算法计算出一个实例交给RestTemplate调用


注:Spring Cloud 2020 已移除对 Ribbon 的支持,相应的,Spring Cloud 2021 版本 Nacos 中 也删除了Ribbon 的 jar 包,实现时需要注意版本依赖关系


环境

Spring Cloud Hoxton.SR9 + Spring Cloud Alibaba 2.2.6.RELEASE


简单示例

  • spring-cloud-starter-alibaba-nacos-discovery中默认包含Ribbon,所以不需要添加依赖

在这里插入图片描述


这里的示例代码是内容中心(content-center)通过调用用户中心(user-center)的两个实例看是否实现负载均衡


user-center

  • TestController.java
@RestController
@Slf4j
public class TestController {

    @GetMapping("/test/{name}")
    public String test(@PathVariable String name) {
    	log.info("请求...");
        return "hello " + name;
    }

}
  • 用户中心以80818082端口启动

在这里插入图片描述


在这里插入图片描述



content-center

  • 启动类
/**
 * @LoadBalanced 为RestTemplate 整合Ribbon
 */
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
    return new RestTemplate();
}
  • TestController.java
@RestController
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TestController {

	private final RestTemplate restTemplate;

    /**
     * 整合Ribbon
     * @return
     */
    @GetMapping("test3")
    public String test3() {
        /**
         * RestTemplate 请求时,ribbon会把user-center转换成用户中心在Nacos上的地址,
         * 进行负载均衡算法计算出一个实例去请求
         */
        return restTemplate.getForObject(
                "http://user-center/test/{name}",
                String.class,
                "Coisini"
        );
    }
}

测试

  • 访问接口测试,可以发现Ribbon以轮询的方式实现负载均衡

在这里插入图片描述
在这里插入图片描述


Ribbon 的组成和默认的负载均衡规则

Ribbon 的组成

  • Ribbon为所有的组件提供了接口,如果对默认值不满意,可以实现这些接口进行二次开发
接口作用默认值
IClientConfig读取配置DefaultClientConfigImpl
IRule负载均衡规则,选择实例ZoneAvoidanceRule
IPing筛选掉ping不通的实例DummyPing
ServerList< Server>交给Ribbon的实例列表Ribbon:ConfigurationBasedServerList
Spring Cloud Alibaba: NacosServerList
ServerListFilter< Server>过滤掉不符合条件的实例ZonePreferenceServerListFilter
ILoadBalancerRibbon的入口ZoneAwareLoadBalancer
ServerListUpdater更新交给Ribbon的List的策略PollingServerListUpdater

Ribbon 默认的负载均衡规则

  • Ribbon默认的负载均衡规则是ZoneAvoidanceRule,在没有zone的情况下默认是轮询
  • Ribbon内置的负载均衡规则如下
规则名称特点
AvailabilityFilteringRule过滤掉一直连接失败的被标记为circuit tripped的后端Server,并过滤掉那些高并发的后端Server或者使用一个AvailabilityPredicate来包含过滤server的逻辑,其实就是检查status里记录的各个Server的运行状态
BestAvailableRule选择一个最小的并发请求的Server,逐个考察Server,如果Server被tripped了,则跳过
RandomRule随机选择一个Server
ResponseTimeWeightedRule已废弃,作用同WeightedResponseTimeRule
RetryRule对选定的负载均衡策略机上重试机制,在同一个配置时间段内当选择Server不成功,则一直尝试使用subRule的方式选择一个可用的Server
RoundRobinRule轮询选择,轮询index,选择index对应位置的Server
WeightedResponseTimeRule根据响应时间加权,响应时间越长,权重越小,被选中的可能性越低
ZoneAvoidanceRule复合判断Server所在Zone的性能和Server的可用性选择Server,在没有Zone的环境下,类似于轮询(RoundRobinRule)

Ribbon 的自定义配置

实现目的:通过自定义配置实现随机选择


Java 代码配置方式

  • UserCenterRibbonConfiguration.java
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Configuration;
import ribbonconfiguration.RibbonConfiguration;

/**
 * @Description 用户中心配置类
 */
@Configuration
@RibbonClient(name = "user-center", configuration = RibbonConfiguration.class)
public class UserCenterRibbonConfiguration {
}
  • RibbonConfiguration.java
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description Ribbon的配置类
 *      Ribbon的配置类要有@Configuration注解,但不能被@ComponentScan重复扫描
 *      否则就会被所有的RibbonClient共享
 */
@Configuration
public class RibbonConfiguration {

	/**
     * 自定义负载均衡规则
     * RandomRule 随机选择
     * @return
     */
    @Bean
    public IRule ribbonRule() {
        return new RandomRule();
    }
}
  • 目录结构如下所示,Ribbon配置类RibbonConfiguration.java位置在java目录下,其作用是避免父子上下文重叠,Ribbon的配置类要有@Configuration注解,但不能被@ComponentScan重复扫描,否则就会被所有的RibbonClient共享

在这里插入图片描述

  • 接口访问测试发现随机访问已实现

在这里插入图片描述
在这里插入图片描述


yml 属性配置方式

  • 注释Java代码配置方式中的代码,在application.yml中添加user-center的负载均衡规则,也能实现相同的效果
user-center:
  ribbon:
    # Ribbon负载均衡规则的全路径
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

Ribbon 的全局配置

  • 有自定义配置就有全局配置,可以通过修改@RibbonClient注解为@RibbonClients注解实现全局配置
  • UserCenterRibbonConfiguration.java
import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.context.annotation.Configuration;
import ribbonconfiguration.RibbonConfiguration;

/**
 * @Description 用户中心配置类
 */
@Configuration
@RibbonClients(defaultConfiguration = RibbonConfiguration.class)
public class UserCenterRibbonConfiguration {
}
  • RibbonConfiguration.java
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description Ribbon的配置类
 */
@Configuration
public class RibbonConfiguration {

	/**
     * 自定义负载均衡规则
     * RandomRule 随机选择
     * @return
     */
    @Bean
    public IRule ribbonRule() {
        return new RandomRule();
    }
}

Ribbon 的饥饿加载

  • 默认情况下Ribbon是懒加载的,当RestTemplate第一次调用RibbonClient时才会创建一个client,这样会导致首次请求过慢或失败的问题,可以通过属性配置开启Ribbon的饥饿加载

  • application.yml

ribbon:
  eager-load:
    # 开启饥饿加载
    enabled: true
    # 为哪些RibbonClient开启饥饿加载,多个用 "," 隔开
    clients: user-center

项目源码



- End -
- 个人学习笔记 -
- 仅供参考 -

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Cloud Alibaba 致力于提供微服务开发的一站式解决方案。此项目包含开发分布式应用微服务的必需组件,方便开发者通过 Spring Cloud 编程模型轻松使用这些组件来开发分布式应用服务。依托 Spring Cloud Alibaba,您只需要添加一些注解和少量配置,就可以将 Spring Cloud 应用接入阿里微服务解决方案,通过阿里中间件来迅速搭建分布式应用系统。 主要功能服务限流降级:默认支持 WebServlet、 WebFlux, OpenFeign、 RestTemplate、 Spring CloudGateway, Zuul, Dubbo 和 RocketMQ 限流降级功能的接入,可以在运行时通过控制台实时修改限流降级规则,还支持查看限流降级 Metrics 监控。服务注册与发现:适配 Spring Cloud 服务注册与发现标准,默认集成了 Ribbon 的支持。分布式配置管理:支持分布式系统中的外部化配置,配置更改时自动刷新。消息驱动能力:基于 Spring Cloud Stream 为微服务应用构建消息驱动能力。分布式事务:使用 @GlobalTransactional 注解, 高效并且对业务零侵入地解决分布式事务问题。阿里云对象存储:阿里云提供的海量、安全、低成本、高可靠的云存储服务。支持在任何应用、任何时间、任何地点存储和访问任意类型的数据。分布式任务调度:提供秒级、精准、高可靠、高可用的定时(基于 Cron 表达式)任务调度服务。同时提供分布式的任务执行模型,如网格任务。网格任务支持海量子任务均匀分配到所有Worker(schedulerx-client)上执行。阿里云短信服务:覆盖全球的短信服务,友好、高效、智能的互联化通讯能力,帮助企业迅速搭建客户触达通道

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Maggieq8324

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

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

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

打赏作者

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

抵扣说明:

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

余额充值