微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

LoadBalanced WebClient 与 Eureka WebClient 一起启用 应用程序Eureka WebClient 已禁用

如何解决LoadBalanced WebClient 与 Eureka WebClient 一起启用 应用程序Eureka WebClient 已禁用

在我的反应式微服务中,我正在向 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]

除此错误外,我还收到以下 Content type 'application/octet-stream' not supported 错误。我认为这个错误不是第一个错误的结果,而是与 eureka.client.webclient.enabled=true 相关。

2021-05-22 14:31:15.030  INFO 1852 --- [           main] c.n.d.s.t.d.RedirectingEurekaHttpClient  : Request execution error. endpoint=DefaultEndpoint{ serviceUrl='http://127.0.0.1:8761/eureka/},exception=Content type 'application/octet-stream' not supported for
 bodyType=com.netflix.discovery.shared.Applications stacktrace=org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'application/octet-stream' not supported for bodyType=com.netflix.discovery.shared.Applications
        at org.springframework.web.reactive.function.BodyExtractors.lambda$readWithMessageReaders$12(BodyExtractors.java:201)
        Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
        |_ checkpoint ? Body from UNKNowN  [DefaultClientResponse]
Stack trace:
                at org.springframework.web.reactive.function.BodyExtractors.lambda$readWithMessageReaders$12(BodyExtractors.java:201)
                at java.base/java.util.Optional.orElseGet(Optional.java:369)

应用程序

这是我的应用程序及其配置的关键部分。

application.yml

eureka:
  client:
    serviceUrl:
      defaultZone: ${vcap.services.sps-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,则一切正常。但是,我打算编写一个响应式微服务。所以,我对 Eureka 使用 RestTemplate 而不是 WebClient 感到困扰。

discoveryClientOptionalArgsConfiguration : Eureka HTTP Client uses RestTemplate.

我将如何将 @LoadBalanced WebClient 与 eureka.client.webclient.enabled=true 一起使用?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。