Reactor Netty provides the easy-to-use and easy-to-configure
HttpClient
.
It hides most of the Netty functionality that is required to create an HTTP
client
and adds Reactive Streams backpressure.
To connect the HTTP
client to a given HTTP
endpoint, you must create and configure a
HttpClient
instance.
The following example shows how to do so:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/http/client/connect/Application.java[role=include]
-
Creates a HttpClient instance ready for configuring.
-
Specifies that
GET
method will be used. -
Specifies the path.
-
Obtains the response HttpClientResponse
The following example uses WebSocket
:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/http/client/websocket/Application.java[role=include]
In order to connect to a specific host and port, you can apply the following configuration to the HTTP
client:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/http/client/address/Application.java[role=include]
-
Configures the
HTTP
host -
Configures the
HTTP
port
By default, the initialization of the HttpClient
resources happens on demand. This means that the first
request
absorbs the extra time needed to initialize and load:
-
the event loop group
-
the host name resolver
-
the native transport libraries (when native transport is used)
-
the native libraries for the security (in case of
OpenSsl
)
When you need to preload these resources, you can configure the HttpClient
as follows:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/http/client/warmup/Application.java[role=include]
-
Initialize and load the event loop group, the host name resolver, the native transport libraries and the native libraries for the security
-
Host name resolution happens with the first request. In this example, a connection pool is used, so with the first request the connection to the URL is established, the subsequent requests to the same URL reuse the connections from the pool.
To send data to a given HTTP
endpoint, you can provide a Publisher
by using the
send(Publisher)
method.
By default, Transfer-Encoding: chunked
is applied for those HTTP
methods for which
a request body is expected. Content-Length
provided through request headers disables Transfer-Encoding: chunked
,
if necessary. The following example sends hello
:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/http/client/send/Application.java[role=include]
-
Sends a
hello
string to the givenHTTP
endpoint
When sending data to a given HTTP
endpoint, you may need to send additional headers, cookies and other metadata.
You can use the following configuration to do so:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/http/client/send/headers/Application.java[role=include]
-
Disables
Transfer-Encoding: chunked
and providesContent-Length
header.
You can enable compression on the HTTP
client, which means the request header
Accept-Encoding
is added to the request headers. The following example shows how to do so:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/http/client/compression/Application.java[role=include]
You can configure the HTTP
client to enable auto-redirect support.
Reactor Netty provides two different strategies for auto-redirect support:
-
followRedirect(boolean)
: Specifies whether HTTP auto-redirect support is enabled for statuses301|302|303|307|308
. When it is303
status code,GET
method is used for the redirect. -
followRedirect(BiPredicate<HttpClientRequest, HttpClientResponse>)
: Enables auto-redirect support if the supplied predicate matches.
The following example uses followRedirect(true)
:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/http/client/redirect/Application.java[role=include]
To receive data from a given HTTP
endpoint, you can use one of the methods from
HttpClient.ResponseReceiver
.
The following example uses the responseContent
method:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/http/client/read/Application.java[role=include]
-
Receives data from a given
HTTP
endpoint -
Aggregates the data
-
Transforms the data as string
When receiving data from a given HTTP
endpoint, you can check response headers, status code, and other metadata.
You can obtain this additional metadata by using
HttpClientResponse
.
The following example shows how to do so.
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/http/client/read/status/Application.java[role=include]
-
Obtains the status code.
By default, Netty
configures some restrictions for the incoming responses, such as:
-
The maximum length of the initial line.
-
The maximum length of all headers.
-
The maximum length of the content or each chunk.
For more information, see HttpResponseDecoder
By default, the HTTP
client is configured with the following settings:
link:./../../reactor-netty-http/src/main/java/reactor/netty/http/HttpDecoderSpec.java[role=include]
link:./../../reactor-netty-http/src/main/java/reactor/netty/http/client/HttpResponseDecoderSpec.java[role=include]
When you need to change these default settings, you can configure the HTTP
client as follows:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/http/client/responsedecoder/Application.java[role=include]
-
The maximum length of all headers will be
16384
. When this value is exceeded, a TooLongFrameException is raised.
The following lifecycle callbacks are provided to let you extend the HttpClient
.
Callback | Description |
---|---|
|
Invoked when the request has been sent. |
|
Invoked after the remote address has been resolved successfully. |
|
Invoked after the response has been fully received. |
|
Invoked when initializing the channel. |
|
Invoked when the channel is about to connect. |
|
Invoked after the channel has been connected. |
|
Invoked after the channel has been disconnected. |
|
Invoked when the request has not been sent and when the response has not been fully received. |
|
Invoked when the response headers have been received, and the request is about to be redirected. |
|
Invoked when the request is about to be sent. |
|
Invoked when the request has not been sent. |
|
Invoked when the remote address is about to be resolved. |
|
Invoked in case the remote address hasn’t been resolved successfully. |
|
Invoked after the response headers have been received. |
|
Invoked when the response has not been fully received. |
The following example uses the doOnConnected
and doOnChannelInit
callbacks:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/http/client/lifecycle/Application.java[role=include]
-
Netty
pipeline is extended withReadTimeoutHandler
when the channel has been connected. -
Netty
pipeline is extended withLoggingHandler
when initializing the channel.
When you need configurations on a TCP level, you can use the following snippet
to extend the default TCP
client configuration (add an option, bind address etc.):
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/http/client/channeloptions/Application.java[role=include]
-
Configures the connection establishment timeout to 10 seconds.
-
Enables TCP
keepalive
. This means that TCP starts sendingkeepalive
probes when a connection is idle for some time. -
The connection needs to remain idle for 5 minutes before TCP starts sending
keepalive
probes. -
Configures the time between individual
keepalive
probes to 1 minute. -
Configures the maximum number of TCP
keepalive
probes to 8.
See [tcp-client] for more about TCP
level configurations.
When you need SSL or TLS, you can apply the configuration shown in the next example.
By default, if OpenSSL
is available, a
SslProvider.OPENSSL
provider is used as a provider. Otherwise, a
SslProvider.JDK provider is used
You can switch the provider by using
SslContextBuilder
or by setting -Dio.netty.handler.ssl.noOpenSsl=true
.
The following example uses SslContextBuilder
:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/http/client/security/Application.java[role=include]
By default, the HTTP
client sends the remote host name as SNI
server name.
When you need to change this default setting, you can configure the HTTP
client as follows:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/http/client/sni/Application.java[role=include]
By default, the HTTP
client retries the request once if it was aborted on the TCP
level.
By default, the HTTP
client supports HTTP/1.1
. If you need HTTP/2
, you can get it through configuration.
In addition to the protocol configuration, if you need H2
but not H2C (cleartext)
, you must also configure SSL.
Note
|
As Application-Layer Protocol Negotiation (ALPN) is not supported “out-of-the-box” by JDK8 (although some vendors backported ALPN to JDK8), you need an additional dependency to a native library that
supports it — for example, netty-tcnative-boringssl-static .
|
The following listing presents a simple H2
example:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/http/client/http2/H2Application.java[role=include]
-
Configures the client to support only
HTTP/2
-
Configures
SSL
The following listing presents a simple H2C
example:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/http/client/http2/H2CApplication.java[role=include]
link:./../../reactor-netty-http/src/main/java/reactor/netty/http/HttpProtocol.java[role=include]
The HTTP client supports built-in integration with Micrometer
.
It exposes all metrics with a prefix of reactor.netty.http.client
.
The following table provides information for the HTTP client metrics:
metric name | type | description |
---|---|---|
reactor.netty.http.client.data.received |
DistributionSummary |
Amount of the data received, in bytes |
reactor.netty.http.client.data.sent |
DistributionSummary |
Amount of the data sent, in bytes |
reactor.netty.http.client.errors |
Counter |
Number of errors that occurred |
reactor.netty.http.client.tls.handshake.time |
Timer |
Time spent for TLS handshake |
reactor.netty.http.client.connect.time |
Timer |
Time spent for connecting to the remote address |
reactor.netty.http.client.address.resolver |
Timer |
Time spent for resolving the address |
reactor.netty.http.client.data.received.time |
Timer |
Time spent in consuming incoming data |
reactor.netty.http.client.data.sent.time |
Timer |
Time spent in sending outgoing data |
reactor.netty.http.client.response.time |
Timer |
Total time for the request/response |
These additional metrics are also available:
The following table provides information for the HTTP client metrics when it is configured to serve HTTP/2
traffic:
metric name | type | description |
---|---|---|
reactor.netty.connection.provider.active.streams |
Gauge |
The number of the active HTTP/2 streams |
reactor.netty.connection.provider.pending.streams |
Gauge |
The number of requests that are waiting for opening HTTP/2 stream |
The following example enables that integration:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/http/client/metrics/Application.java[role=include]
-
Applies upper limit for the meters with
URI
tag -
Templated URIs will be used as a URI tag value when possible
-
Enables the built-in integration with Micrometer
Note
|
In order to avoid a memory and CPU overhead of the enabled metrics, it is important to convert the real URIs to templated URIs when possible. Without a conversion to a template-like form, each distinct URI leads to the creation of a distinct tag, which takes a lot of memory for the metrics. |
Note
|
Always apply an upper limit for the meters with URI tags. Configuring an upper limit on the number of meters can help in cases when the real URIs cannot be templated.
You can find more information at maximumAllowableTags .
|
When HTTP client metrics are needed for an integration with a system other than Micrometer
or you want
to provide your own integration with Micrometer
, you can provide your own metrics recorder, as follows:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/http/client/metrics/custom/Application.java[role=include]
-
Enables HTTP client metrics and provides
HttpClientMetricsRecorder
implementation.
The HTTP
client supports Unix Domain Sockets (UDS) when native transport is in use.
The following example shows how to use UDS support:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/http/client/uds/Application.java[role=include]
-
Specifies
DomainSocketAddress
that will be used
By default, the HttpClient
uses Netty’s domain name lookup mechanism that resolves a domain name asynchronously.
This is as an alternative of the JVM’s built-in blocking resolver.
When you need to change the default settings, you can configure the HttpClient
as follows:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/http/client/resolver/Application.java[role=include]
-
The timeout of each DNS query performed by this resolver will be 500ms.
The following listing shows the available configurations:
Configuration name | Description |
---|---|
|
The max time to live of the cached DNS resource records (resolution: seconds).
If the time to live of the DNS resource record returned by the DNS server is greater
than this max time to live, this resolver ignores the time to live from
the DNS server and uses this max time to live.
Default to |
|
The min time to live of the cached DNS resource records (resolution: seconds). If the time to live of the DNS resource record returned by the DNS server is less than this min time to live, this resolver ignores the time to live from the DNS server and uses this min time to live. Default: 0. |
|
The time to live of the cache for the failed DNS queries (resolution: seconds). Default: 0. |
|
When this setting is enabled, the resolver notifies as soon as all queries for the preferred address type are complete.
When this setting is disabled, the resolver notifies when all possible address types are complete.
This configuration is applicable for |
|
Disables the automatic inclusion of an optional record that tries to give a hint to the remote DNS server about how much data the resolver can read per response. By default, this setting is enabled. |
|
Specifies whether this resolver has to send a DNS query with the recursion desired (RD) flag set. By default, this setting is enabled. |
|
Sets a custom |
|
Sets the capacity of the datagram packet buffer (in bytes). Default: 4096. |
|
Sets the maximum allowed number of DNS queries to send when resolving a host name. Default: 16. |
|
Sets the number of dots that must appear in a name before an initial absolute query is made. Default: -1 (to determine the value from the OS on Unix or use a value of 1 otherwise). |
|
Sets the timeout of each DNS query performed by this resolver (resolution: milliseconds). Default: 5000. |
|
The list of the protocol families of the resolved address. |
|
The supplier of the local address to bind to. |
|
Enables an
|
|
Performs the communication with the DNS servers on the given
|
|
The list of search domains of the resolver. By default, the effective search domain list is populated by using the system DNS search domains. |
|
A specific logger and log level to be used by this resolver when generating detailed trace information in case of resolution failure. |
Sometimes, you may want to switch to the JVM built-in resolver. To do so, you can configure the HttpClient
as follows:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/http/client/resolver/custom/Application.java[role=include]
-
Sets the JVM built-in resolver.
This section describes various timeout configuration options that can be used in HttpClient
.
Configuring a proper timeout may improve or solve issues in the communication process.
The configuration options can be grouped as follows:
By default, HttpClient
uses a connection pool. When a request is completed successfully and if the connection is not scheduled for closing,
the connection is returned to the connection pool and can thus be reused for processing another request. The connection may
be reused immediately for another request or may stay idle in the connection pool for some time.
The following list describes the available timeout configuration options:
-
maxIdleTime
- The maximum time (resolution: ms) that this connection stays idle in the connection pool. By default,maxIdleTime
is not specified.
Note
|
When you configure maxIdleTime , you should consider the idle timeout configuration on the target server.
Choose a configuration that is equal to or less than the one on the target server. By doing so, you can reduce the I/O
issues caused by a connection closed by the target server.
|
-
maxLifeTime
- The maximum time (resolution: ms) that this connection stays alive. By default,maxLifeTime
is not specified. -
pendingAcquireTimeout
- The maximum time (resolution: ms) after which a pending acquire operation must complete, or aPoolAcquireTimeoutException
is thrown. Default: 45s.
By default, these timeouts are checked on connection release
or acquire
operations and, if some timeout is reached, the connection is closed and removed from the connection pool.
However, you can also configure the connection pool, by setting evictInBackground
, to perform periodic checks on connections.
To customize the default settings, you can configure HttpClient
as follows:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/http/client/pool/config/Application.java[role=include]
-
Configures the maximum time for a connection to stay idle to 20 seconds.
-
Configures the maximum time for a connection to stay alive to 60 seconds.
-
Configures the maximum time for the pending acquire operation to 60 seconds.
-
Every two minutes, the connection pool is regularly checked for connections that are applicable for removal.
This section provides information for the various timeout configuration options at the HttpClient
level.
Note
|
Reactor Netty uses Reactor Core as its Reactive Streams implementation, and you may want to use the timeout operator that Mono and Flux provide.
Keep in mind, however, that it is better to use the more specific timeout configuration options available in Reactor Netty, since they provide more control for a specific purpose and use case.
By contrast, the timeout operator can only apply to the operation as a whole, from establishing the connection to the remote peer to receiving the response.
|
HttpClient
provides an API for configuring a default response timeout for all requests. You can change this default response timeout
through an API for a specific request. By default,
responseTimeout
is not specified.
Note
|
It is always a good practice to configure a response timeout. |
To customize the default settings, you can configure HttpClient
as follows:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/http/client/read/timeout/Application.java[role=include]
-
Configures the default response timeout to 1 second.
-
Configures a response timeout for a specific request to 2 seconds.
The following listing shows all available connection timeout configuration options, but some of them may apply only to a specific transport.
-
CONNECT_TIMEOUT_MILLIS
- If the connection establishment attempt to the remote peer does not finish within the configured connect timeout (resolution: ms), the connection establishment attempt fails. Default: 30s. -
SO_KEEPALIVE
- When the connection stays idle for some time (the time is implementation dependent, but the default is typically two hours), TCP automatically sends akeepalive
probe to the remote peer. By default,SO_KEEPALIVE
is not enabled. When you run withEpoll
/NIO
(since Java 11 on Mac or Linux) transport, you may also configure:-
TCP_KEEPIDLE
- The maximum time (resolution: seconds) that this connection stays idle before TCP starts sendingkeepalive
probes, ifSO_KEEPALIVE
has been set. The maximum time is implementation dependent, but the default is typically two hours. -
TCP_KEEPINTVL
(Epoll)/TCP_KEEPINTERVAL
(NIO) - The time (resolution: seconds) between individualkeepalive
probes. -
TCP_KEEPCNT
(Epoll)/TCP_KEEPCOUNT
(NIO) - The maximum number ofkeepalive
probes TCP should send before dropping the connection.
-
Note
|
Sometimes, between the client and the server, you may have a network component that silently drops the idle connections without sending a response.
From the Reactor Netty point of view, in this use case, the remote peer just does not respond.
To be able to handle such a use case you may consider configuring
SO_KEEPALIVE .
|
To customize the default settings, you can configure HttpClient
as follows:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/http/client/channeloptions/Application.java[role=include]
-
Configures the connection establishment timeout to 10 seconds.
-
Enables TCP
keepalive
. This means that TCP starts sendingkeepalive
probes when a connection is idle for some time. -
The connection needs to remain idle for 5 minutes before TCP starts sending
keepalive
probes. -
Configures the time between individual
keepalive
probes to 1 minute. -
Configures the maximum number of TCP
keepalive
probes to 8.
HttpClient
supports the SSL/TLS functionality provided by Netty.
The following list describes the available timeout configuration options:
-
handshakeTimeout
- Use this option to configure the SSL handshake timeout (resolution: ms). Default: 10s.
Note
|
You should consider increasing the SSL handshake timeout when expecting slow network connections. |
-
closeNotifyFlushTimeout
- Use this option to configure the SSLclose_notify
flush timeout (resolution: ms). Default: 3s. -
closeNotifyReadTimeout
- Use this option to configure the SSLclose_notify
read timeout (resolution: ms). Default: 0s.
To customize the default settings, you can configure HttpClient
as follows:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/http/client/security/custom/Application.java[role=include]
-
Configures the SSL handshake timeout to 30 seconds.
-
Configures the SSL
close_notify
flush timeout to 10 seconds. -
Configures the SSL
close_notify
read timeout to 10 seconds.
HttpClient
supports the proxy functionality provided by Netty and provides a way to specify the
connection establishment timeout.
If the connection establishment attempt to the remote peer does not finish within the timeout,
the connection establishment attempt fails. Default: 10s.
To customize the default settings, you can configure HttpClient
as follows:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/http/client/proxy/Application.java[role=include]
-
Configures the connection establishment timeout to 20 seconds.
By default, the HttpClient
uses Netty’s domain name lookup mechanism to resolve a domain name asynchronously.
The following list describes the available timeout configuration options:
-
cacheMaxTimeToLive
- The maximum time to live of the cached DNS resource records (resolution: seconds). If the time to live of the DNS resource record returned by the DNS server is greater than this maximum time to live, this resolver ignores the time to live from the DNS server and uses this maximum time to live. Default:Integer.MAX_VALUE
. -
cacheMinTimeToLive
- The minimum time to live of the cached DNS resource records (resolution: seconds). If the time to live of the DNS resource record returned by the DNS server is less than this minimum time to live, this resolver ignores the time to live from the DNS server and uses this minimum time to live. Default: 0s. -
cacheNegativeTimeToLive
- The time to live of the cache for the failed DNS queries (resolution: seconds). Default: 0s. -
queryTimeout
- Sets the timeout of each DNS query performed by this resolver (resolution: milliseconds). Default: 5s.
To customize the default settings, you can configure HttpClient
as follows:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/http/client/resolver/Application.java[role=include]
-
The timeout of each DNS query performed by this resolver will be 500ms.