4

I'm struggling to switch from Ribbon to Spring Cloud Loadbalancer after upgrading spring cloud versions. Setting up the SimpleDiscoveryClient with Feign was easy. But the simplediscovery client is 'too simple'. I want to add at least a healthcheck so it doesn't use an instance that is potentially down & preferably also a retry mechanism. I've read the docs over & over but cannot find an easy way to set this up.

https://docs.spring.io/spring-cloud-commons/docs/current/reference/html/#instance-health-check-for-loadbalancer I found this example custom config for the health check, but it doesn't work.

public class CustomLoadBalancerConfiguration {

@Bean
public ServiceInstanceListSupplier discoveryClientServiceInstanceListSupplier(
        ConfigurableApplicationContext context) {
    return ServiceInstanceListSupplier.builder()
                .withDiscoveryClient()
                .withHealthChecks()
                .build(context);
    }
}

if I run it as is, it throws the following missing bean error: Method discoveryClientServiceInstanceListSupplier in XXX.CustomLoadBalancerConfig required a bean of type 'org.springframework.web.client.RestTemplate' that could not be found.

Can anyone give me some pointers on how to get this working or how I can replicate the ribbon behavior?

ExaSephiroth
  • 165
  • 8

2 Answers2

0

Add the below RestTemplate Bean in your configuration.

@Bean
public RestTemplate restTemplate() {
    return new RestTemplate();
}
broken98
  • 43
  • 6
0

Try adding the following to your application class

@LoadBalancerClients(defaultConfiguration = LoadBalancerClientConfiguration.class)
public class GatewayApplication {

  public static void main(String[] args) {
    SpringApplication.run(GatewayApplication.class, args);
  }
}

This will enable the creation of DiscoveryClientServiceInstanceListSupplier (note the class itself is not available in the context, so you can't just autowire it for testing).

Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265