如何覆盖 Spring Cloud Ribbon 中的ribbon.serverListRefreshInterval 默认值?
Posted
技术标签:
【中文标题】如何覆盖 Spring Cloud Ribbon 中的ribbon.serverListRefreshInterval 默认值?【英文标题】:How to override the ribbon.serverListRefreshInterval default value in Spring Cloud Ribbon? 【发布时间】:2017-01-26 18:48:26 【问题描述】:我写了一个简单的 Spring Cloud Ribbon 应用程序,用来调用在 Eureka 中注册的 REST 服务。
但是如何覆盖ribbon.serverListRefreshInterval
值呢?默认值为 30 秒,我想缩短时间间隔。
提前致谢。
【问题讨论】:
【参考方案1】:尝试:
myService.ribbon.ServerListRefreshInterval=10000
myService
是目标微服务的名称。
更新:
经过一些源代码挖掘,我发现LoadBalancerBuilder
调用:
@Deprecated
public ZoneAwareLoadBalancer(IClientConfig clientConfig, IRule rule,
IPing ping, ServerList<T> serverList, ServerListFilter<T> filter)
super(clientConfig, rule, ping, serverList, filter);
谁的超级是:
@Deprecated
public DynamicServerListLoadBalancer(IClientConfig clientConfig, IRule rule, IPing ping,
ServerList<T> serverList, ServerListFilter<T> filter)
this(
clientConfig,
rule,
ping,
serverList,
filter,
new PollingServerListUpdater()
);
注意PollingServerListUpdater
构造函数:
private static int LISTOFSERVERS_CACHE_REPEAT_INTERVAL = 30 * 1000; // msecs;
public PollingServerListUpdater()
this(LISTOFSERVERS_CACHE_UPDATE_DELAY, LISTOFSERVERS_CACHE_REPEAT_INTERVAL);
public PollingServerListUpdater(IClientConfig clientConfig)
this(LISTOFSERVERS_CACHE_UPDATE_DELAY, getRefreshIntervalMs(clientConfig));
第二个允许我们覆盖默认的刷新间隔。但是它是第一个被调用的,所以它忽略了这个属性。
更新 2:
关于这个有一个未解决的问题:https://github.com/spring-cloud/spring-cloud-netflix/issues/1304
【讨论】:
您的服务在您的 Eureka 服务器中注册为Compute-Service
吗?注意大写字母。
好的,仔细检查没有错字,稍后会附上一些截图供您参考
感谢您的宝贵回答,希望能尽快修复该错误【参考方案2】:
@codependent
在application.yml中放入下面的配置后,看起来配置没有生效。
Compute-Service:
ribbon:
ServerListRefreshInterval: 1000
我观察到,Ribbon 端的实例列表已经更新(通过 eureka.client.registry-fetch-interval-seconds 参数),但是 Ribbon 仍然指出了一个死实例:
[tbeatExecutor-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_RIBBON-CONSUMER/192.168.1.101:Ribbon-Consumer:3333 - Heartbeat status: 200
[freshExecutor-0] com.netflix.discovery.DiscoveryClient : Got delta update with apps hashcode UP_2_
[freshExecutor-0] com.netflix.discovery.DiscoveryClient : Added instance 192.168.1.101:Ribbon-Consumer:3333 to the existing apps in region null
[freshExecutor-0] com.netflix.discovery.DiscoveryClient : Deleted instance 192.168.1.101:Compute-Service:2222 to the existing apps
[freshExecutor-0] com.netflix.discovery.DiscoveryClient : Added instance 192.168.1.101:Compute-Service:1111 to the existing apps in region null
[freshExecutor-0] com.netflix.discovery.DiscoveryClient : The total number of instances fetched by the delta processor : 3
[freshExecutor-0] com.netflix.discovery.DiscoveryClient : The total number of all instances in the client now is 2
[freshExecutor-0] com.netflix.discovery.DiscoveryClient : Completed cache refresh task for discovery. All Apps hash code is Local region apps hashcode: UP_2_, is fetching remote regions? false
[nio-3333-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://Compute-Service/add": Connection refused; nested exception is java.net.ConnectException: Connection refused] with root cause
192.168.1.101:Compute-Service:1111 是正确的服务实例,而 192.168.1.101:Compute-Service:2222 是死实例,显然 Ribbon 仍然指向死实例,这意味着 Ribbon ServerList 缓存没有刷新。
【讨论】:
以上是关于如何覆盖 Spring Cloud Ribbon 中的ribbon.serverListRefreshInterval 默认值?的主要内容,如果未能解决你的问题,请参考以下文章
Spring Cloud Ribbon 源码分析---负载均衡算法
2.Spring Cloud Alibaba实现负载均衡-Ribbon
Spring Cloud:如何在没有 Ribbon 的情况下使用 Feign