In a reactive microservice I'm registering to Eureka and using a @LoadBalanced
WebClient to get a response from an instance. Registering in Eureka alone works, but once I add the @LoadBalanced
WebClient I get following error.
2021-05-22 14:31:14.835 INFO 1852 --- [ main] com.netflix.discovery.DiscoveryClient : Getting all instance registry info from the eureka server
2021-05-22 14:31:14.985 ERROR 1852 --- [ main] scoveryClientServiceInstanceListSupplier : Exception occurred while retrieving instances for service 127.0.0.1
org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'scopedTarget.eurekaClient': Requested bean is currently in creation: Is there an unresolvable circular reference?
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:274) ~[spring-beans-5.3.6.jar:5.3.6]
I assume it is related to the configuration eureka.client.webclient.enabled=true
.
That's my application and the crucial parts of its configuration.
application.yml
eureka:
client:
serviceUrl:
defaultZone: ${vcap.services.eureka-service.credentials.uri:http://127.0.0.1:8761}/eureka/
webclient:
enabled: true
ConsumerApplication.java
@SpringBootApplication
public class ConsumerApplication {
public Mono<ServerResponse> handler(ServerRequest request) {
return webClientBuilder()
.baseUrl("http://producer")
.build()
.get()
.retrieve()
.bodyToMono(String.class)
.onErrorResume(e -> Mono.just("Error " + e.getMessage()))
.flatMap(r -> ok().bodyValue(Map.of("Producer says", r)));
}
@Bean
@LoadBalanced
public WebClient.Builder webClientBuilder(){
return WebClient.builder();
}
@Bean
RouterFunction<ServerResponse> routes() {
return route()
.GET("", this::handler)
.build();
}
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
If instead eureka.client.webclient.enabled=false
is used, everything works perfectly fine. However, I don't think this should be the solution.
DiscoveryClientOptionalArgsConfiguration : Eureka HTTP Client uses RestTemplate.
How would I go about using a @LoadBalanced
WebClient together with eureka.client.webclient.enabled=true
?
Had the same problem, this is how I fixed it.
@Bean
@LoadBalanced
public WebClient.Builder lbWebClient() {
return WebClient.builder();
}
@Bean
@Primary
public WebClient.Builder webClient() {
return WebClient.builder();
}
Use @Qualifier later on to used the loadbalanced one.