编程语言
首页 > 编程语言> > springcloud(六)-Ribbon配置自定义算法

springcloud(六)-Ribbon配置自定义算法

作者:互联网

前言

很多场景下,可能根据需要自定义Ribbon的配置,例如修改Ribbon的负载均衡规则等。Spring Cloud Edgware允许使用java代码或属性自定义Ribbon 的配置,两种方式等价。

Java代码定义

我们先说使用Java代码写配置类的方式定义负载均衡算法。

复制项目microservive-consumer-movie-ribbon,将ArtifactId修改为microservice-consumer-movie-ribbon-customizing。

1.创建Ribbon的配置类

@Configuration
public class RibbonConfiguration {
    @Bean
    public IRule ribbonRule() {
        // 负载均衡规则,改为随机
        return new RandomRule();
    }
}

这里要说一下,很关键。

这个类一定不要放到启动类同目录或同目录的子目录下,也就是@SpringBootApplication注解的子注解@ComponentScan所能扫描到的地方。否则该类中的配置信息将被所有的@RibbonClient共享。

当然了,你要是就想放到@ConponentScan能扫描的目录下,你需要手动exclude掉,告诉springboot,不扫描它。

 

 

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

@Configuration
@RibbonClient(name="microservice-provider-user",configuration=RibbonConfiguration.class)
public class TestConfiguration {

}

测试

启动microservice-discovery-eureka.

启动两个或更多microservice-provider-user.

启动microservice-consumer-movie-ribbon-customizing

多次访问http://localhost:8082/log-user-instance,看控制台信息

2019-04-15 19:14:31.268  INFO 10592 --- [nio-8082-exec-9] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8081
2019-04-15 19:14:31.436  INFO 10592 --- [nio-8082-exec-1] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8084
2019-04-15 19:14:31.607  INFO 10592 --- [nio-8082-exec-3] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8081
2019-04-15 19:14:31.776  INFO 10592 --- [nio-8082-exec-4] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8084
2019-04-15 19:14:31.923  INFO 10592 --- [nio-8082-exec-7] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8084
2019-04-15 19:14:32.081  INFO 10592 --- [nio-8082-exec-9] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8081
2019-04-15 19:14:32.269  INFO 10592 --- [nio-8082-exec-1] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8081
2019-04-15 19:14:32.437  INFO 10592 --- [nio-8082-exec-3] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8081
2019-04-15 19:14:32.592  INFO 10592 --- [nio-8082-exec-4] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8081
2019-04-15 19:14:32.776  INFO 10592 --- [nio-8082-exec-7] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8084
2019-04-15 19:14:32.955  INFO 10592 --- [nio-8082-exec-9] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8084
2019-04-15 19:14:33.138  INFO 10592 --- [nio-8082-exec-1] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8084
2019-04-15 19:14:33.312  INFO 10592 --- [nio-8082-exec-3] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8084

没毛病!

配置方式

我们再说通过配置yml文件的方式。我的观点是,能用配置的方式,尽量不用Java代码!

改个名字先:microservice-consumer-movie-ribbon-customizing-properties

和上面的方式比较:把上面那俩Java类删咯。在application.yml文件中加上一段。

server:
  port: 8082
eureka:
  client:
    healthcheck:
      enabled: true
    serviceUrl:
      defaultZone: http://localhost:8083/eureka/
  instance:
    prefer-ip-address: true
spring:
  application:
    name: microservice-consumer-movie
# 表示对这个user的Ribbon Client的负载均衡设置为随机
# 如果去掉16行,下面当然往前移两格,表示对所有的设置为随机
microservice-provider-user:
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

你看,就三行,还是配置方便,快捷。

测试

启动microservice-discovery-eureka

启动两个或多个microservice-provider-user实例

启动microservice-consumer-movie-ribbon-customizing-properties

多次访问http://localhost:8082/log-user-instance.看控制台

2019-04-15 19:27:32.866  INFO 12792 --- [nio-8082-exec-7] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8081
2019-04-15 19:27:33.044  INFO 12792 --- [nio-8082-exec-8] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8084
2019-04-15 19:27:33.370  INFO 12792 --- [nio-8082-exec-1] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8084
2019-04-15 19:27:33.558  INFO 12792 --- [nio-8082-exec-3] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8084
2019-04-15 19:27:33.746  INFO 12792 --- [nio-8082-exec-5] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8081
2019-04-15 19:27:33.945  INFO 12792 --- [nio-8082-exec-7] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8084
2019-04-15 19:27:34.129  INFO 12792 --- [nio-8082-exec-8] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8081
2019-04-15 19:27:34.329  INFO 12792 --- [nio-8082-exec-1] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8084
2019-04-15 19:27:34.521  INFO 12792 --- [nio-8082-exec-3] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8081
2019-04-15 19:27:34.711  INFO 12792 --- [nio-8082-exec-5] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8081
2019-04-15 19:27:34.895  INFO 12792 --- [nio-8082-exec-7] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8084
2019-04-15 19:27:35.103  INFO 12792 --- [nio-8082-exec-8] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8084
2019-04-15 19:27:35.289  INFO 12792 --- [nio-8082-exec-1] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8081
2019-04-15 19:27:35.504  INFO 12792 --- [nio-8082-exec-3] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8084
2019-04-15 19:27:35.705  INFO 12792 --- [nio-8082-exec-5] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8084
2019-04-15 19:27:35.895  INFO 12792 --- [nio-8082-exec-7] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8084
2019-04-15 19:27:36.081  INFO 12792 --- [nio-8082-exec-8] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8084
2019-04-15 19:27:36.279  INFO 12792 --- [nio-8082-exec-1] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8084
2019-04-15 19:27:36.481  INFO 12792 --- [nio-8082-exec-3] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8081
2019-04-15 19:27:36.650  INFO 12792 --- [nio-8082-exec-5] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8081
2019-04-15 19:27:36.863  INFO 12792 --- [nio-8082-exec-7] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8084
2019-04-15 19:27:37.057  INFO 12792 --- [nio-8082-exec-8] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8081
2019-04-15 19:27:37.250  INFO 12792 --- [nio-8082-exec-1] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8084
2019-04-15 19:27:37.409  INFO 12792 --- [nio-8082-exec-3] c.i.cloud.controller.MovieController     : microservice-provider-user:192.168.1.33:8081

还是没毛病!


 

代码下载地址:https://gitee.com/fengyuduke/my_open_resources/blob/master/ribbon-customizing&properties.zip

 

标签:8082,自定义,springcloud,192.168,2019,user,provider,microservice,Ribbon
来源: https://www.cnblogs.com/fengyuduke/p/10712569.html