Package com.azure.core.http.netty
Azure Core Http Netty client library is a plugin for the azure-core HTTP client API. It allows you to use Netty as the underlying HTTP client for communicating with Azure services. It is the default HTTP client used in all Azure SDK for Java libraries, but you can also replace it with other implementations such as OkHttp or the JDK 11 HttpClient. You can also configure various aspects of the Netty client, such as proxy, protocol, or chunk size. For more details refer to our conceptual documentation.
Sample: Construct NettyAsyncHttpClient with Default Configuration
The following code sample demonstrates the creation of a Netty HttpClient that uses port 80 and has no proxy.
HttpClient client = new NettyAsyncHttpClientBuilder().build();
Using NettyAsyncHttpClient with Http Proxy
Configuring the Netty client with a proxy in the context of Azure Java SDK is relevant when your application needs to communicate with Azure services through a proxy server. For more details refer to our conceptual documentation.
The following code sample demonstrates the creation of a Netty HttpClient that is using a proxy.
HttpClient client = new NettyAsyncHttpClientBuilder()
.proxy(new ProxyOptions(ProxyOptions.Type.HTTP, new InetSocketAddress("<proxy-host>", 8888)))
.build();
Using NettyAsyncHttpClient with HTTP/2 Support
The following code sample demonstrates the creation of a Netty HttpClient that supports both the HTTP/1.1 and HTTP/2 protocols, with HTTP/2 being the preferred protocol.
// Constructs an HttpClient that supports both HTTP/1.1 and HTTP/2 with HTTP/2 being the preferred protocol.
HttpClient client = new NettyAsyncHttpClientBuilder(reactor.netty.http.client.HttpClient.create()
.protocol(HttpProtocol.HTTP11, HttpProtocol.H2))
.build();
It is also possible to create a Netty HttpClient that only supports HTTP/2.
// Constructs an HttpClient that only supports HTTP/2.
HttpClient client = new NettyAsyncHttpClientBuilder(reactor.netty.http.client.HttpClient.create()
.protocol(HttpProtocol.H2))
.build();
Using NettyAsyncHttpClient with Custom Max Chunk Size
The adjustment of the max chunk size involves the modification of the maximum size of ByteBufs returned by Netty,
subsequently converted to ByteBuffer. Altering the chunk size can yield positive performance effects,
particularly
notable in APIs like the download to file methods within Azure Storage libraries such as azure-storage-blob,
azure-storage-file-datalake, and azure-storage-file-shares. Notably, improvements in performance have been
consistently observed in the range of 32KB to 64KB.
The following code sample demonstrates the creation of a Netty HttpClient that uses a custom max chunk size.
// Constructs an HttpClient with a modified max chunk size.
// Max chunk size modifies the maximum size of ByteBufs returned by Netty (later converted to ByteBuffer).
// Changing the chunk size can positively impact performance of APIs such as Storage's download to file methods
// provided in azure-storage-blob, azure-storage-file-datalake, and azure-storage-file-shares (32KB - 64KB have
// shown the most consistent improvement).
HttpClient httpClient = new NettyAsyncHttpClientBuilder(reactor.netty.http.client.HttpClient.create()
.httpResponseDecoder(httpResponseDecoderSpec -> httpResponseDecoderSpec.maxChunkSize(64 * 1024)))
.build();
- See Also:
-
ClassesClassDescriptionBuilder class responsible for creating instances of
HttpClientbacked by Reactor Netty.The NettyAsyncHttpClientProvider class is an implementation of the HttpClientProvider interface that provides an instance of HttpClient based on Netty.