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

Ribbon负载均衡策略

Ribbon负载均衡策略.png

配置

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

1)通过配置文件配置

hello:
    ribbon:
     NFLoadBalancerRuleClassName:com.netflix.loadbalancer.RandomRule
  • 1
  • 2
  • 3

2)通过java注解配置

@Configuration
public class RibbonConfiguration{
      @Bean
      public IRule ribbonRule(){
          //随机负载
          return new RandomRule();
     }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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

@Configuration
@RibbonClient(name="hello", configuration=RibbonConfiguration.class)
public class TestRibbonConfiguration{
}
  • 1
  • 2
  • 3
  • 4

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

单独使用Ribbon

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

hello:
   ribbon:
      listOfServers:localhost:8010,localhost:8011
原文地址:https://www.cnblogs.com/tiancai/p/9618247.html