为了账号安全,请及时绑定邮箱和手机立即绑定

跟我学Spring Cloud(Finchley版)-08-Ribbon深入

标签:
Spring Cloud

上一节讲了Ribbon的入门姿势,本节深入探讨Ribbon的高级特性。

内置负载均衡规则

负载均衡规则是Ribbon的核心,下面来看一下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所在区域的性能和Server的可用性选择Server;

如需自定义负载均衡规则,只需实现IRule 接口或继承AbstractLoadBalancerRule、PredicateBasedRule即可 ,读者可参考RandomRuleRoundRobinRuleZoneAvoidanceRule 等内置Rule编写自己的负载均衡规则。

Ribbon配置自定义【细粒度配置】

Ribbon可实现精确到目标服务的细粒度配置。例如A服务调用服务B,A服务调用C,可以针对B服务一套配置,针对C服务另一套配置。

方式1、代码配置方式

在Spring Cloud中,Ribbon的默认配置如下(格式是BeanType beanName: ClassName):

  • IClientConfig ribbonClientConfig: DefaultClientConfigImpl

  • IRule ribbonRule: ZoneAvoidanceRule

  • IPing ribbonPing: NoOpPing

  • ServerList<Server> ribbonServerList: ConfigurationBasedServerList

  • ServerListFilter<Server> ribbonServerListFilter: ZonePreferenceServerListFilter

  • ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer

  • ServerListUpdater ribbonServerListUpdater: PollingServerListUpdater

代码示例

  • 创建一个空类,并在其上添加@Configuration 注解和@RibbonClient 注解。

    /**
     * 使用RibbonClient,为特定的目标服务自定义配置。
     * 使用@RibbonClient的configuration属性,指定Ribbon的配置类。
     * 可参考的示例:
     * http://spring.io/guides/gs/client-side-load-balancing/
     * @author 周立
     */@Configuration@RibbonClient(name = "microservice-provider-user", configuration = RibbonConfiguration.class)public class TestConfiguration {
    }

    由代码可知,使用@RibbonClient 注解的configuration属性,即可自定义指定名称Ribbon客户端的配置。

  • 创建Ribbon的配置类。

    /**
     * 该类为Ribbon的配置类
     * 注意:该类不能放在主应用程序上下文@ComponentScan所扫描的包中,否则配置将会被所有Ribbon Client共享。
     * @author 周立
     */@Configurationpublic class RibbonConfiguration {  @Bean
      public IRule ribbonRule() {    // 负载均衡规则,改为随机
        return new RandomRule();
      }
    }

配套代码

GitHub:https://github.com/eacdy/spring-cloud-study/tree/master/2018-Finchley/microservice-consumer-movie-ribbon-config-java

Gitee:https://gitee.com/itmuch/spring-cloud-study/tree/master/2018-Finchley/microservice-consumer-movie-ribbon-config-java

方式2、属性配置方式【推荐】

<clientName>.ribbon. 如下属性

  • NFLoadBalancerClassName: should implement ILoadBalancer

  • NFLoadBalancerRuleClassName: should implement IRule

  • NFLoadBalancerPingClassName: should implement IPing

  • NIWSServerListClassName: should implement ServerList

  • NIWSServerListFilterClassName should implement ServerListFilter

代码示例

user:  ribbon:    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

TIPS

属性配置的优先级高于代码配置。

配套代码

GitHub:https://github.com/eacdy/spring-cloud-study/tree/master/2018-Finchley/microservice-consumer-movie-ribbon-config-properties

Gitee:https://gitee.com/itmuch/spring-cloud-study/tree/master/2018-Finchley/microservice-consumer-movie-ribbon-config-properties

Ribbon配置自定义【全局配置】

方式1、代码配置方式

@RibbonClients(defaultConfiguration = DefaultRibbonConfig.class)public class RibbonClientDefaultConfigurationTestsConfig {
}@Configurationclass DefaultRibbonConfig {  @Bean
  public IRule ribbonRule() {    return new RandomRule();
  }
}

方法2、属性配置方式【推荐】

和上文细粒度配置类似,只需将目标服务名称前缀去掉即可。

ribbon:  NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

Ribbon Eager加载

默认情况下Ribbon是懒加载的——首次请求Ribbon相关类才会初始化,这会导致首次请求过慢的问题,你可以配置饥饿加载,让Ribbon在应用启动时就初始化。

ribbon:
  eager-load:
    enabled: true
    # 多个用,分隔
    clients: microservice-provider-user




点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消