可以在Spring Webflux WebClient中使用Ribbon和Eureka服务发现吗?
我尝试了这段代码,但是在集成测试中遇到了错误。
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
最佳答案
看着我自己... 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);
}