Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Service discovery with spring webflux WebClient

Is it possible to use Ribbon and Eureka service discovery with spring webflux webclient?

I tried this code but getting an error during integration test.

reactor.core.Exceptions$ErrorCallbackNotImplemented: java.lang.IllegalArgumentException: URI is not absolute: /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
like image 867
Chandresh Mishra Avatar asked Jan 03 '19 18:01

Chandresh Mishra


1 Answers

Looking into this myself ... Piotr Minkowski answers the question well here ...

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

I will post the most relevant sections to this answer for convenience.

Create the load balanced web client builder

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

Which can then be used like

@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);
}
like image 197
James Gawron Avatar answered Sep 24 '22 05:09

James Gawron