SpringCloud 多个WebClient对象
2023-06-08 11:19 更新
如果要使WebClient
负载不均衡,请创建一个WebClient
bean并注入它。要访问负载均衡的WebClient
,请在创建@Bean时使用@LoadBalanced
限定符,如以下示例所示:
@Configuration
public class MyConfiguration {
@LoadBalanced
@Bean
WebClient.Builder loadBalanced() {
return WebClient.builder();
}
@Primary
@Bean
WebClient.Builder webClient() {
return WebClient.builder();
}
}
public class MyClass {
@Autowired
private WebClient.Builder webClientBuilder;
@Autowired
@LoadBalanced
private WebClient.Builder loadBalanced;
public Mono<String> doOtherStuff() {
return loadBalanced.build().get().uri("http://stores/stores")
.retrieve().bodyToMono(String.class);
}
public Mono<String> doStuff() {
return webClientBuilder.build().get().uri("http://example.com")
.retrieve().bodyToMono(String.class);
}
}
以上内容是否对您有帮助:
更多建议: