Skip to content

Commit 7f62c5a

Browse files
committed
Provide a How-To for customizing Reactor Netty's TcpClient
Closes gh-17856
1 parent da1d7cf commit 7f62c5a

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,19 @@ The following example configures `HttpComponentsClientRequestFactory` with an `H
12311231
include::{code-examples}/web/client/RestTemplateProxyCustomizationExample.java[tag=customizer]
12321232
----
12331233

1234+
[[howto-webclient-reactor-netty-customization]]
1235+
=== Configure the TcpClient used by a Reactor Netty-based WebClient
1236+
When Reactor Netty is on the classpath a Reactor Netty-based `WebClient` is auto-configured.
1237+
To customize the client's handling of network connections, provide a `ClientHttpConnector` bean.
1238+
The following example configures a 60 second read timeout and adds a `ReadTimeoutHandler`:
1239+
1240+
[source,java,indent=0]
1241+
----
1242+
include::{code-examples}/web/reactive/function/client/ReactorNettyClientCustomizationExample.java[tag=custom-http-connector]
1243+
----
1244+
1245+
TIP: Note the use of `ReactorResourceFactory` for the connection provider and event loop resources.
1246+
This ensures efficient sharing of resources for the server receiving requests and the client making requests.
12341247

12351248

12361249
[[howto-logging]]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.web.reactive.function.client;
18+
19+
import io.netty.channel.ChannelOption;
20+
import io.netty.handler.timeout.ReadTimeoutHandler;
21+
import reactor.netty.http.client.HttpClient;
22+
import reactor.netty.tcp.TcpClient;
23+
24+
import org.springframework.context.annotation.Bean;
25+
import org.springframework.context.annotation.Configuration;
26+
import org.springframework.http.client.reactive.ClientHttpConnector;
27+
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
28+
import org.springframework.http.client.reactive.ReactorResourceFactory;
29+
import org.springframework.web.reactive.function.client.WebClient;
30+
31+
/**
32+
* Example configuration for customizing the Reactor Netty-based {@link WebClient}.
33+
*
34+
* @author Andy Wilkinson
35+
*/
36+
@Configuration
37+
public class ReactorNettyClientCustomizationExample {
38+
39+
// tag::custom-http-connector[]
40+
@Bean
41+
ClientHttpConnector clientHttpConnector(ReactorResourceFactory resourceFactory) {
42+
TcpClient tcpClient = TcpClient.create(resourceFactory.getConnectionProvider())
43+
.runOn(resourceFactory.getLoopResources()).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 60000)
44+
.doOnConnected((connection) -> connection.addHandlerLast(new ReadTimeoutHandler(60)));
45+
return new ReactorClientHttpConnector(HttpClient.from(tcpClient));
46+
}
47+
// end::custom-http-connector[]
48+
49+
}

0 commit comments

Comments
 (0)