最新文章

Spring Cloud Ribbon 详解

Spring Cloud Ribbon 详解

客户端负载均衡, Ribbon的核心概念是命名的客户端.

使用

引入Ribbon依赖和配置

加入spring-cloud-starter-netflix-ribbon依赖

代码中使用RibbonClient注解

@Configuration 
@RibbonClient(name = "foo", configuration = FooConfiguration.class) public class TestConfiguration {}
@Configuration protected static class FooConfiguration {

    @Bean 
    public ZonePreferenceServerListFilter serverListFilter() {
        ZonePreferenceServerListFilter filter = new ZonePreferenceServerListFilter();
        filter.setZone("myTestZone");
        return filter; 
    }

    @Bean 
    public IPing ribbonPing() { 
        return new PingUrl(); 
    }

}

Ribbon客户端的配置, 如果不指定会使用默认的实现:

  • IClientConfig 客户端相关配置
  • IRule 定义负载均衡策略
  • IPing 定义如何ping目标服务实例来判断是否存活, ribbon使用单独的线程每隔一段时间(默认10s)对本地缓存的ServerList做一次检查
  • ServerList 定义如何获取服务实例列表. 两种实现基于配置的ConfigurationBasedServerList和基于Eureka服务发现的DiscoveryEnabledNIWSServerList
  • ServerListFilter 用来使用期望的特征过滤静态配置动态获得的候选服务实例列表. 若未提供, 默认使用ZoneAffinityServerListFilter
  • ILoadBalancer 定义了软负载均衡器的操作的接口. 一个典型的负载均衡器至少需要一组用来做负载均衡的服务实例, 一个标记某个服务实例不在旋转中的方法, 和对应的方法调用从实例列表中选出某一个服务实例.
  • ServerListUpdater DynamicServerListLoadBalancer用来更新实例列表的策略(推EurekaNotificationServerListUpdater/拉PollingServerListUpdater, 默认是拉)

About Me

张晓辉

英文名 Addo。 资深程序员,LF APAC 开源布道师,CNCF Ambassador,云原生社区管委会成员,公众号“云原生指北”作者,微软 Azure MVP。 曾任职于汇丰软件、唯品会、数人云、小鹏汽车,有多年的微服务和基础架构实践经验,主要工作涉及微服务、容 …

进一步了解