如何使用 Netflix/Eureka 执行故障转移?
Posted
技术标签:
【中文标题】如何使用 Netflix/Eureka 执行故障转移?【英文标题】:How to perform a failover with Netflix/Eureka? 【发布时间】:2016-10-20 11:56:17 【问题描述】:我使用 Eureka 作为我的服务发现和负载均衡器,当有两个服务“A”实例时它工作正常,但是当我停止其中一个实例时,Eureka 无法识别其中一个实例已关闭,并且每次负载均衡器尝试使用死实例时都会显示错误页面。
我已将 enableSelfPreservation
设置为 false
以防止这种情况发生,但 Eureka 最多需要 3 到 5 分钟才能取消注册该服务,但是我希望我的服务具有高可用性,并且我想立即执行故障转移几秒钟的事。这是否可能使用 Eureka,如果不是,我如何才能在其他人死亡时仅使用活着的实例。
我使用的是spring boot,这是我对Eureka服务器的配置。
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://$eureka.instance.hostname:$server.port/eureka/
server:
enableSelfPreservation: false
【问题讨论】:
【参考方案1】:您应该将功能区配置添加到您的 application.yml。还建议将 hystrix 隔离级别设置为 THREAD 并设置超时。
注意:这个配置应该在 client 端(这通常意味着你的网关服务器),因为 Ribbon(和一般的 Spring Cloud)是客户端负载平衡的一种形式。 p>
这是我使用的一个例子:
hystrix:
command:
default:
execution:
isolation:
strategy: THREAD
thread:
timeoutInMilliseconds: 40000 #Timeout after this time in milliseconds
ribbon:
ConnectTimeout: 5000 #try to connect to the endpoint for 5 seconds.
ReadTimeout: 5000 #try to get a response after successfull connection for 5 seconds
# Max number of retries on the same server (excluding the first try)
maxAutoRetries: 1
# Max number of next servers to retry (excluding the first server)
MaxAutoRetriesNextServer: 2
【讨论】:
以上是关于如何使用 Netflix/Eureka 执行故障转移?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用来自 Netflix/Eureka 服务的发现信息在 Netflix/Zuul 和 Netflix/Ribbon 中启用自动路由?