问题:
使用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