对于所有内容,我添加了 spring-boot-starter-actuator
以添加 /health
端点。
在 Zuul/Ribbon 配置中我添加了:
zuul:
ignoredServices: "*"
routes:
home-service:
path: /service/**
serviceId: home-service
retryable: true
home-service:
ribbon:
listOfServers: localhost:8080,localhost:8081
eureka.enabled: false
ServerListRefreshInterval: 1
这样,每次客户端调用GET http://localhost:7070/service/home
时,负载均衡器都会选择在 8080 或 8081 端口上运行的两个 HomeService 之一,并调用其端点/home
。
但是,当其中一个 HomeService 关闭时,负载均衡器似乎并不知道(尽管有 ServerListRefreshInterval
配置),并且如果发生以下情况,将会失败并显示 error=500
尝试调用关闭实例。
我该如何解决这个问题?
最佳答案
我已收到并测试了来自 spring-cloud team 的解决方案.
解决方案是 here in github
总结一下:
- 我已将
org.springframework.retry.spring-retry
添加到我的 zuul 类路径 - 我已将
@EnableRetry
添加到我的 zuul 应用程序 - 我已将以下属性放入我的 zuul 配置中
application.yml
server:
port: ${PORT:7070}
spring:
application:
name: gateway
endpoints:
health:
enabled: true
sensitive: true
restart:
enabled: true
shutdown:
enabled: true
zuul:
ignoredServices: "*"
routes:
home-service:
path: /service/**
serviceId: home-service
retryable: true
retryable: true
home-service:
ribbon:
listOfServers: localhost:8080,localhost:8081
eureka.enabled: false
ServerListRefreshInterval: 100
retryableStatusCodes: 500
MaxAutoRetries: 2
MaxAutoRetriesNextServer: 1
OkToRetryOnAllOperations: true
ReadTimeout: 10000
ConnectTimeout: 10000
EnablePrimeConnections: true
ribbon:
eureka:
enabled: false
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 30000
关于spring - 当服务器关闭时负载平衡失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44168155/