美文网首页微服务架构和实践
Ribbon负载均衡策略与自定义配置

Ribbon负载均衡策略与自定义配置

作者: billJiang | 来源:发表于2017-08-26 17:42 被阅读0次

    Ribbon负载均衡策略

    Ribbon负载均衡策略.png

    配置

    • 对调用的某个服务启用某种负载策略

    1)通过配置文件配置

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

    2)通过java注解配置

    @Configuration
    public class RibbonConfiguration{
          @Bean
          public IRule ribbonRule(){
              //随机负载
              return new RandomRule();
         }
    }
    

    通过注解@RibbonClient为特定的服务配置负载均衡策略

    @Configuration
    @RibbonClient(name="hello", configuration=RibbonConfiguration.class)
    public class TestRibbonConfiguration{
    }
    

    以上配置都是在服务消费者中配置。

    单独使用Ribbon

    因为往往Ribbon配合Eureka使用的,往往也有第三方服务没有注册到Eureka Server,但也部署了多个实例,也需要进行负载均衡,这时可以在服务消费者的配置文件中进行如下方式配置,实现负载均衡

    hello:
       ribbon:
          listOfServers:localhost:8010,localhost:8011
    

    相关文章

      网友评论

        本文标题:Ribbon负载均衡策略与自定义配置

        本文链接:https://www.haomeiwen.com/subject/jhosdxtx.html