问题描述
是否可以将 Ribbon 和 Eureka 服务发现与 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 不是绝对的:/auth-service/auth-service/validate-manager-client-access
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
推荐答案
自己调查这个问题... Piotr Minkowski 在这里很好地回答了这个问题...
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();
}
然后可以像
@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);
}
这篇关于使用 spring webflux WebClient 发现服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!