LoadBalanced WebClient 与 Eureka WebClient 一起启用
LoadBalanced WebClient together with Eureka WebClient enabled
在反应式微服务中,我注册到 Eureka 并使用 @LoadBalanced
WebClient 从实例获取响应。单独在 Eureka 中注册有效,但是一旦我添加 @LoadBalanced
WebClient,我就会收到以下错误。
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]
我假设它与配置有关eureka.client.webclient.enabled=true
。
应用程序
这就是我的应用程序及其配置的关键部分。
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);
}
}
Eureka WebClient 已禁用
如果改为使用 eureka.client.webclient.enabled=false
,则一切正常。但是,我不认为这应该是解决方案。
DiscoveryClientOptionalArgsConfiguration : Eureka HTTP Client uses RestTemplate.
如何将 @LoadBalanced
WebClient 与 eureka.client.webclient.enabled=true
一起使用?
遇到了同样的问题,我就是这样解决的。
@Bean
@LoadBalanced
public WebClient.Builder lbWebClient() {
return WebClient.builder();
}
@Bean
@Primary
public WebClient.Builder webClient() {
return WebClient.builder();
}
稍后使用@Qualifier 来使用负载均衡器。
在反应式微服务中,我注册到 Eureka 并使用 @LoadBalanced
WebClient 从实例获取响应。单独在 Eureka 中注册有效,但是一旦我添加 @LoadBalanced
WebClient,我就会收到以下错误。
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]
我假设它与配置有关eureka.client.webclient.enabled=true
。
应用程序
这就是我的应用程序及其配置的关键部分。
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);
}
}
Eureka WebClient 已禁用
如果改为使用 eureka.client.webclient.enabled=false
,则一切正常。但是,我不认为这应该是解决方案。
DiscoveryClientOptionalArgsConfiguration : Eureka HTTP Client uses RestTemplate.
如何将 @LoadBalanced
WebClient 与 eureka.client.webclient.enabled=true
一起使用?
遇到了同样的问题,我就是这样解决的。
@Bean
@LoadBalanced
public WebClient.Builder lbWebClient() {
return WebClient.builder();
}
@Bean
@Primary
public WebClient.Builder webClient() {
return WebClient.builder();
}
稍后使用@Qualifier 来使用负载均衡器。