springCloud ribbon-13 ribbon 请求重试

问题:

使用ribbon 负载均衡,无论是使用轮询,权重,还是随机策略。如果负载均衡后面有一个服务宕机,当负载均衡到该宕机的服务时,请求就会出错。

解决方法:

使用spring 的重试组件,对ribbon 进行重试配置。

    ConnectTimeout: 250 # Ribbon的连接超时时间
    ReadTimeout: 1000 # Ribbon的数据读取超时时间
    OkToRetryOnAllOperations: true # 是否对所有操作都进行重试
    MaxAutoRetriesNextServer: 1 # 切换实例的重试次数
    MaxAutoRetries: 1 # 对当前实例的重试次数

一,在服务消费者配置文件去掉负载均衡策略

server:
  port: 9002
spring:
  application:
    name: com.zjk.product.service-order
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springclouddemo?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
    username: root
    password: root
  jpa:
    database: MySQL
    show-sql: true
    open-in-view: true
eureka:
  client:
    service-url:
      defaultZone: http://localhost:9003/eureka/,http://localhost:9004/eureka/
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port} #向注册中心中展示注册服务id
    lease-expiration-duration-in-seconds: 10 #eureka client 发送心跳给server端后,续约到期时间(默认为90秒)。
    lease-renewal-interval-in-seconds: 5 # 发送心跳续约间隔(每一个心跳的间隔)

#修改ribbon的负载均衡策略 服务名-ribbon-NFLoadBalancer
service-product:
  ribbon:
    # NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

二,在消息消费者配置文件加上ribbon 重试配置

server:
  port: 9002
spring:
  application:
    name: com.zjk.product.service-order
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springclouddemo?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
    username: root
    password: root
  jpa:
    database: MySQL
    show-sql: true
    open-in-view: true
eureka:
  client:
    service-url:
      defaultZone: http://localhost:9003/eureka/,http://localhost:9004/eureka/
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port} #向注册中心中展示注册服务id
    lease-expiration-duration-in-seconds: 10 #eureka client 发送心跳给server端后,续约到期时间(默认为90秒)。
    lease-renewal-interval-in-seconds: 5 # 发送心跳续约间隔(每一个心跳的间隔)

#修改ribbon的负载均衡策略 服务名-ribbon-NFLoadBalancer
service-product:
  ribbon:
    # NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
    ConnectTimeout: 250 # Ribbon的连接超时时间
    ReadTimeout: 1000 # Ribbon的数据读取超时时间
    OkToRetryOnAllOperations: true # 是否对所有操作都进行重试
    MaxAutoRetriesNextServer: 1 # 切换实例的重试次数
    MaxAutoRetries: 1 # 对当前实例的重试次数

三,为了方便查看日志,可以在消息消费者配置文件上加上日志打印显示

server:
  port: 9002
spring:
  application:
    name: com.zjk.product.service-order
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springclouddemo?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
    username: root
    password: root
  jpa:
    database: MySQL
    show-sql: true
    open-in-view: true
eureka:
  client:
    service-url:
      defaultZone: http://localhost:9003/eureka/,http://localhost:9004/eureka/
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port} #向注册中心中展示注册服务id
    lease-expiration-duration-in-seconds: 10 #eureka client 发送心跳给server端后,续约到期时间(默认为90秒)。
    lease-renewal-interval-in-seconds: 5 # 发送心跳续约间隔(每一个心跳的间隔)
#修改ribbon的负载均衡策略 服务名-ribbon-NFLoadBalancer
service-product:
  ribbon:
    # NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
    ConnectTimeout: 250 # Ribbon的连接超时时间
    ReadTimeout: 1000 # Ribbon的数据读取超时时间
    OkToRetryOnAllOperations: true # 是否对所有操作都进行重试
    MaxAutoRetriesNextServer: 1 # 切换实例的重试次数
    MaxAutoRetries: 1 # 对当前实例的重试次数
logging:
  level:
    root: debug
logging:
  level:
    root: debug

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vegetari

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值