【问题标题】:Service discovery with spring webflux WebClient使用 spring webflux WebClient 进行服务发现
【发布时间】:2019-05-30 08:48:42
【问题描述】:

是否可以将 Ribbon 和 Eureka 服务发现与 spring webflux webclient 一起使用?

我尝试了此代码,但在集成测试期间出现错误。

reactor.core.Exceptions$ErrorCallbackNotImplemented:java.lang.IllegalArgumentException:URI 不是绝对的:/auth-service/auth-service/validate-manager-client-access

@Bean
  @LoadBalanced
  public WebClient loadBalancedWebClient() {
    return WebClient.create(baseURL);
  }

  @Override
  public Mono<Boolean> validateManagerClientAccess(Mono<LoginDTO> loginDTOMono) {
    return webClient
        .post()
        .uri(validateManagerURL)
        .body(loginDTOMono, LoginDTO.class)
        .retrieve()
        .bodyToMono(Boolean.class);
  }

# Remote Services Configuration
remote:
  auth-service:
    service-id: auth-service
    path:
      validate-manager-client-access: /auth-service/validate-manager-client-access

【问题讨论】:

    标签: spring spring-boot microservices spring-cloud-netflix reactive


    【解决方案1】:

    我自己正在研究这个...... Piotr Minkowski 在这里很好地回答了这个问题......

    https://dzone.com/articles/reactive-microservices-with-spring-webflux-and-spr

    为方便起见,我将在此答案中发布最相关的部分。

    创建负载平衡的 Web 客户端构建器

     @Bean
     @LoadBalanced
     public WebClient.Builder loadBalancedWebClientBuilder() {
         return WebClient.builder();
     }
    

    然后可以像这样使用

    @Autowired
    private WebClient.Builder webClientBuilder;
    @GetMapping("/{id}/with-accounts")
    public Mono findByIdWithAccounts(@PathVariable("id") String id) {
        LOGGER.info("findByIdWithAccounts: id={}", id);
        Flux accounts= webClientBuilder.build().get().uri("http://accountservice/customer/{customer}", id).retrieve().bodyToFlux(Account.class);
    return accounts
          .collectList()
          .map(a -> new Customer(a))
          .mergeWith(repository.findById(id))
          .collectList()
          .map(CustomerMapper::map);
    }
    

    【讨论】:

      猜你喜欢
      • 2018-06-11
      • 2018-05-09
      • 2019-08-02
      • 2021-01-15
      • 2019-12-28
      • 1970-01-01
      • 2021-03-23
      • 2018-08-18
      • 1970-01-01
      相关资源
      最近更新 更多