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
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With