嗨即时通讯使用Web客户端从春天webflux。我有一些这样的代码:
@Configuration
class WebClientConfig(
@Value("\${url}")
private val url: String
) {
@Bean
@Primary
fun webClient(): WebClient {
return createWebClient(700)
}
@Bean("more_timeout")
fun webClientMoreTimeout(): WebClient {
return createWebClient(3000)
}
private fun createWebClient(timeout: Int): WebClient{
val httpClient = HttpClient.create()
.tcpConfiguration { client -> client.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, timeout) }
return WebClient.builder()
.baseUrl(url)
.clientConnector(ReactorClientHttpConnector(httpClient))
.build()
}
}
这种配置是因为我需要不同的超时通话。 Supose我有一个服务的,所以我想等待响应最多3秒,supose y的另一种服务B,C等,这些不是我的回应非常重要,这是非常importart对我的反应,我只会等待700毫秒来产生响应。谁我可以存档吗?以前的配置不工作,因为Web客户端是inmutable。
我认为你不能在Web客户端级别做到这一点,但你可以在反应堆的水平,像这样做:
return webClient.post()
.uri { uriBuilder ->
uriBuilder.path(PATH)
.build()
}
.body(BodyInserters.fromObject(Request()))
.accept(MediaType.APPLICATION_JSON_UTF8)
.exchange()
.timeout(Duration.ofMillis(1000L))