Ribbon--负载均衡、IRule、自定义Ribbon

傷城~ 2021-08-26 14:42 526阅读 0赞

Ribbon负载均衡

在这里插入图片描述

Ribbon在工作时分成两步:

  • 第一步先选择EurekaServer,它优先选择在同一个区域内负载较少的server。
  • 第二部再根据用户指定的策略,在从server取到的服务注册列表中选择一个地址。

其中Ribbon提供了多种策略:比如轮询,随机和根据相应时间加权。

参考microservicecloud-provider-dept-8001,新建两份,分别命名为8002,8003

新建8002/8003数据库,各自微服务分别连各自的数据库

修改8002/8003各自YML:

  1. server:
  2. port: 8002
  3. mybatis:
  4. config-location: classpath:mybatis/mybatis.cfg.xml #mybatis所在路径
  5. type-aliases-package: com.atguigu.springcloud.entities #entity别名类
  6. mapper-locations:
  7. - classpath:mybatis/mapper/**/*.xml #mapper映射文件
  8. spring:
  9. application:
  10. name: microservicecloud-dept
  11. datasource:
  12. type: com.alibaba.druid.pool.DruidDataSource
  13. driver-class-name: org.gjt.mm.mysql.Driver
  14. url: jdbc:mysql://localhost:3306/cloudDB02
  15. username: root
  16. password: 123456
  17. dbcp2:
  18. min-idle: 5
  19. initial-size: 5
  20. max-total: 5
  21. max-wait-millis: 200
  22. eureka:
  23. client: #客户端注册进eureka服务列表内
  24. service-url:
  25. defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  26. instance:
  27. instance-id: microservicecloud-dept8002 #自定义服务名称信息
  28. prefer-ip-address: true #访问路径可以显示IP地址
  29. info:
  30. app.name: atguigu-microservicecloud
  31. company.name: www.atguigu.com
  32. build.artifactId: $project.artifactId$
  33. build.version: $project.version$
  34. server:
  35. port: 8003
  36. mybatis:
  37. config-location: classpath:mybatis/mybatis.cfg.xml #mybatis所在路径
  38. type-aliases-package: com.atguigu.springcloud.entities #entity别名类
  39. mapper-locations:
  40. - classpath:mybatis/mapper/**/*.xml #mapper映射文件
  41. spring:
  42. application:
  43. name: microservicecloud-dept
  44. datasource:
  45. type: com.alibaba.druid.pool.DruidDataSource
  46. driver-class-name: org.gjt.mm.mysql.Driver
  47. url: jdbc:mysql://localhost:3306/cloudDB03
  48. username: root
  49. password: 123456
  50. dbcp2:
  51. min-idle: 5
  52. initial-size: 5
  53. max-total: 5
  54. max-wait-millis: 200
  55. eureka:
  56. client: #客户端注册进eureka服务列表内
  57. service-url:
  58. defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  59. instance:
  60. instance-id: microservicecloud-dept8003 #自定义服务名称信息
  61. prefer-ip-address: true #访问路径可以显示IP地址
  62. info:
  63. app.name: atguigu-microservicecloud
  64. company.name: www.atguigu.com
  65. build.artifactId: $project.artifactId$
  66. build.version: $project.version$

启动3个eureka集群配置

启动3个Dept微服务

启动microservicecloud-consumer-dept-80

客户端通过Ribbo完成负载均衡并访问上一步的Dept微服务

访问:http://localhost/consumer/dept/list

第一次:
在这里插入图片描述

第二次:
在这里插入图片描述

第三次:
在这里插入图片描述

默认采用轮询策略。

Ribbon核心组件IRule

IRule:根据特定算法中从服务列表中选取一个要访问的服务

  • RoundRobinRule :轮询
  • RandomRule :随机
  • AvailabilityFilteringRule :会先过滤掉由于多次访问故障而处于断路器跳闸状态的服务,还有并发的连接数量超过阈值的服务,然后对剩余的服务列表按照轮询策略进行访问
  • WeightedResponseTimeRule :根据平均响应时间计算所有服务的权重,响应时间越快服务权重越大被选中的概率越高。刚启动时如果统计信息不足,则使用RoundRobinRule策略,等统计信息足够,会切换到WeightedResponseTimeRule
  • RetryRule :先按照RoundRobinRule的策略获取服务,如果获取服务失败则在指定时间内会进行重试,获取可用的服务
  • BestAvailableRule :会先过滤掉由于多次访问故障而处于断路器跳闸状态的服务,然后选择一个并发量最小的服务
  • ZoneAvoidanceRule :默认规则,复合判断server所在区域的性能和server的可用性选择服务器

自定义Ribbon

修改microservicecloud-consumer-dept-80:

主启动类添加@RibbonClient:

在启动该微服务的时候就能去加载我么您的自定义Ribbon配置类,从而使配置生效,

  1. @RibbonClient(name="MICROSERVICECLOUD-DEPT", configuration=MySelfRule.class)

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,526人围观)

还没有评论,来说两句吧...

相关阅读