spring-cloud-netflix: Ribbon not loading a list of servers during spring boot startup or initialisation

Bug:

I want to use the listofservers which are defined in a configuration file during spring boot startup (i.e. in a @Postconstruct annotation) to connect to an aws s3 instance.

  ribbon:
    listOfServers: localhost:8080
    ServerListRefreshInterval: 15000

Eureka is disabled.

This is being set using the

@RibbonClient(name = "test", configuration = TestConfiguration.class).

Am autowiring the loadbalacer client

    @Autowired
    private LoadBalancerClient loadBalancer;
eg: @PostConstruct
loadBalancer.choose("test").getUri().toString()

& loading the servers in a config file as

   @Bean
    public ServerList<Server> ribbonServerList(IClientConfig config) {
        ConfigurationBasedServerList serverList = new ConfigurationBasedServerList();
        serverList.initWithNiwsConfig(config);
        return serverList;
    }.

But the list of servers during initialization is always empty, so i get back an exception that riboon client cannot find the list of servers during springboot startup.

: No up servers available from load balancer: DynamicServerListLoadBalancer:{NFLoadBalancer:name=s3,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:com.netflix.loadbalancer.ConfigurationBasedServerList@3efedc6f
WARN 19548 --- [main] c.netflix.loadbalancer.BaseLoadBalancer  : LoadBalancer [s3]:  Error choosing server for key default

even though the serverList is defined in the application.yml file, if i don’t read the serverList from @postcontruct annotation it is working fine.

I looked up online but could not find examples of using this in a @postcontruct annotation, but i need this to be used there is there any other alternative?

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 22 (7 by maintainers)

Most upvoted comments

Hi Rohit, Try deleting @Bean public IRule ribbonRule(IClientConfig config) { return new AvailabilityFilteringRule(); } from your SayHelloConfiguration.java

eager-load can’t be under say-hello:. It needs to be:

ribbon:
  eureka:
     enabled: false
  eager-load:
    enabled: true
    clients: say-hello
say-hello:
  ribbon:
    listOfServers: localhost:8090,localhost:9092,localhost:9999
    ServerListRefreshInterval: 15000

@spencergibb or use RestTemplate.getForObject(…)

I have the same problem, I remove the ribbonRule and works fine. thanks @ArtemZubenko . Why? Is correct not use ribbonRule?

@spencergibb I have the same issue like @RohitGupta31. I added the following lines in application.yaml:

ribbon:
  eager-load:
    enabled: true
    clients: merchant-security
  eureka:
    enabled: false
merchant-security:
  ribbon:
    listOfServers: localhost:8081, localhost:8082
    ServerListRefreshInterval: 15000

I used @FeignClient: @FeignClient(name = "merchantSecurityPermissionClient", serviceId = "merchant-security")

In the method “public void configure(HttpSecurity http) throws Exception” of the Configuration class (this class extends ResourceServerConfigurerAdapter.java). I called @FeignClient and it return an error: Caused by: com.netflix.client.ClientException: Load balancer does not have available server for client: merchant-security

However, after Spring Boot StartUp, I call @FeignClient again and it works fine.

Please learn how to format code on GitHub.

I would imagine that your @PostConstruct code is running before Ribbon is fully initialized. I am not sure what we could do about that.