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

Ribbon负载均衡

在这里插入图片描述

Ribbon在工作时分成两步:

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

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

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

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

修改8002/8003各自YML:

server:
port: 8002
mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml  #mybatis所在路径
type-aliases-package: com.atguigu.springcloud.entities #entity别名类
mapper-locations:
  - classpath:mybatis/mapper/**/*.xml #mapper映射文件
spring:
application:
name: microservicecloud-dept 
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/cloudDB02
username: root
password: 123456
dbcp2:
min-idle: 5
initial-size: 5
max-total: 5
max-wait-millis: 200
eureka:
client: #客户端注册进eureka服务列表内
service-url: 
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
instance:
instance-id: microservicecloud-dept8002   #自定义服务名称信息
prefer-ip-address: true #访问路径可以显示IP地址
info:
app.name: atguigu-microservicecloud
company.name: www.atguigu.com
build.artifactId: $project.artifactId$
build.version: $project.version$
server:
port: 8003
mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml  #mybatis所在路径
type-aliases-package: com.atguigu.springcloud.entities #entity别名类
mapper-locations:
  - classpath:mybatis/mapper/**/*.xml #mapper映射文件
spring:
application:
name: microservicecloud-dept 
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/cloudDB03
username: root
password: 123456
dbcp2:
min-idle: 5
initial-size: 5
max-total: 5
max-wait-millis: 200
eureka:
client: #客户端注册进eureka服务列表内
service-url: 
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
instance:
instance-id: microservicecloud-dept8003   #自定义服务名称信息
prefer-ip-address: true #访问路径可以显示IP地址
info:
app.name: atguigu-microservicecloud
company.name: www.atguigu.com
build.artifactId: $project.artifactId$
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配置类,从而使配置生效,

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

在这里插入图片描述

发布了779 篇原创文章 · 获赞 2146 · 访问量 27万+

猜你喜欢

转载自blog.csdn.net/cold___play/article/details/104435528