如何在Spring Cloud Ribbon中覆盖ribbon.serverListRefreshInterval默认值?

6 人关注

我写了一个简单的Spring Cloud Ribbon应用程序,用来调用一个在Eureka中注册的REST服务。

但如何重写 ribbon.serverListRefreshInterval ?默认值是30秒,我想缩短时间间隔。

spring
cloud
spring-cloud
spring-cloud-netflix
netflix-ribbon
Anson Wang
Anson Wang
发布于 2016-09-19
2 个回答
Anson Wang
Anson Wang
发布于 2021-12-07
0 人赞同

@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缓存没有被刷新。

codependent
codependent
发布于 2021-12-07
已采纳
0 人赞同
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));