設定以網路為基礎的 HTTP 用戶端 - AWS SDK for Java 2.x

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

設定以網路為基礎的 HTTP 用戶端

中非同步作業的預設 HTTP 用戶端 AWS SDK for Java 2.x 是以網址為基礎。NettyNioAsyncHttpClient以 Netty 為基礎的用戶端是以 Netty 專案的非同步事件驅動網路架構為基礎。

作為替代的 HTTP 用戶端,您可以使用新的以 AWS CRT 為基礎的 HTTP 用戶端。本主題說明如何設定NettyNioAsyncHttpClient.

存取 NettyNioAsyncHttpClient

在大多數情況下,您可以在非同步程式中使用NettyNioAsyncHttpClient沒有任何明確設定的。您宣告非同步服務NettyNioAsyncHttpClient用戶端,SDK 會為您設定標準值。

如果您想要明確設定NettyNioAsyncHttpClient或將其與多個服務用戶端搭配使用,則需要將其設定為可用。

無需配置

當您在 Maven 中聲明對服務客戶端的依賴關係時,SDK 會添加對netty-nio-client工件的運行時依賴關係。這使得該NettyNioAsyncHttpClient類在運行時可用於您的代碼,但在編譯時不能使用。如果您未設定以 NetTic 為基礎的 HTTP 用戶端,則不需要為其指定相依性。

在 Maven pom.xml 文件的下<artifactId>dynamodb-enhanced</artifactId>面的 XML 片段中,以傳遞方式聲明的依賴關係帶來了基於 NETT 的 HTTP 客戶端。您不需要專門為其聲明依賴關係。

<dependencyManagement> <dependencies> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>bom</artifactId> <version>2.17.290</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>dynamodb-enhanced</artifactId> </dependency> </dependencies>

使用這些依賴關係,您無法進行任何 HTTP 配置更改,因為NettyNioAsyncHttpClient庫僅位於運行時類路徑上。

需要的配置

若要設定NettyNioAsyncHttpClient,您需要在編譯階段新增對netty-nio-client成品的相依性。

請參閱下面的 Maven pom.xml 文件的例子來配置NettyNioAsyncHttpClient

<dependencyManagement> <dependencies> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>bom</artifactId> <version>2.17.290</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>dynamodb-enhanced</artifactId> </dependency> <!-- By adding the netty-nio-client dependency, NettyNioAsyncHttpClient will be added to the compile classpath so you can configure it. --> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>netty-nio-client</artifactId> </dependency> </dependencies>

使用和配置 NettyNioAsyncHttpClient

您可以設定的執行個體以NettyNioAsyncHttpClient及建置服務用戶端,也可以將單一執行個體設定為在多個服務用戶端之間共用。

無論使用哪一種方法,您都可以使用 NettyNioAsyncHttpClient.Builder 來設定以網址為基礎的 HTTP 用戶端執行個體的屬性。

最佳做法:將NettyNioAsyncHttpClient執行個體專用於服務用戶端

如果您需要設定的執行個體NettyNioAsyncHttpClient,建議您建立專用NettyNioAsyncHttpClient執行個體。您可以使用服務客戶端構建器的httpClientBuilder方法來執行此操作。這樣,HTTP 客戶端的生命週期由 SDK 管理,如果NettyNioAsyncHttpClient實例不再需要時關閉,這有助於避免潛在的內存洩漏。

下列範例會建立DynamoDbAsyncClient執行個體所使用的DynamoDbEnhancedAsyncClient執行個體。DynamoDbAsyncClient執行個體包含具有connectionTimeoutmaxConcurrency值的NettyNioAsyncHttpClient執行個體。HTTP 執行個體是使用的httpClientBuilder方法建立的DynamoDbAsyncClient.Builder

匯入

import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider; import software.amazon.awssdk.awscore.defaultsmode.DefaultsMode; import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedAsyncClient; import software.amazon.awssdk.enhanced.dynamodb.extensions.AutoGeneratedTimestampRecordExtension; import software.amazon.awssdk.http.nio.netty.NettyNioAsyncHttpClient; import software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient; import java.time.Duration;

Code

// DynamoDbAsyncClient is the lower-level client used by the enhanced client. DynamoDbAsyncClient dynamoDbAsyncClient = DynamoDbAsyncClient .builder() .httpClientBuilder(NettyNioAsyncHttpClient.builder() .connectionTimeout(Duration.ofMillis(5_000)) .maxConcurrency(100) .tlsNegotiationTimeout(Duration.ofMillis(3_500))) .defaultsMode(DefaultsMode.IN_REGION) .credentialsProvider(EnvironmentVariableCredentialsProvider.create()) .build(); // Singleton: Use dynamoDbAsyncClient and enhancedClient for all requests. DynamoDbEnhancedAsyncClient enhancedClient = DynamoDbEnhancedAsyncClient .builder() .dynamoDbClient(dynamoDbAsyncClient) .extensions(AutoGeneratedTimestampRecordExtension.create()) .build(); // Perform work with the dynamoDbAsyncClient and enhancedClient. // Requests completed: Close dynamoDbAsyncClient. dynamoDbAsyncClient.close();

替代方法:共享一個NettyNioAsyncHttpClient實例

為了協助降低應用程式的資源和記憶體使用量,您可以設定 a NettyNioAsyncHttpClient 並在多個服務用戶端之間共用。HTTP 連線集區將會共用,進而降低資源使用量。

注意

共用NettyNioAsyncHttpClient執行個體時,您必須在準備好處理執行個體時將其關閉。當服務客戶端關閉時,SDK 不會關閉實例。

下列範例會設定兩個服務用戶端所使用的 Nettty 型 HTTP 用戶端。配置的NettyNioAsyncHttpClient實例傳遞給每個構建器的httpClient方法。當不再需要服務用戶端和 HTTP 用戶端時,程式碼會明確地關閉它們。該代碼最後關閉 HTTP 客戶端。

匯入

import software.amazon.awssdk.http.SdkHttpClient; import software.amazon.awssdk.http.apache.ApacheHttpClient; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.s3.S3Client;

Code

// Create a NettyNioAsyncHttpClient shared instance. SdkAsyncHttpClient nettyHttpClient = NettyNioAsyncHttpClient.builder().maxConcurrency(100).build(); // Singletons: Use the s3AsyncClient, dbAsyncClient, and enhancedAsyncClient for all requests. S3AsyncClient s3AsyncClient = S3AsyncClient.builder() .httpClient(nettyHttpClient) .build(); DynamoDbAsyncClient dbAsyncClient = DynamoDbAsyncClient.builder() .httpClient(nettyHttpClient) .defaultsMode(DefaultsMode.IN_REGION) .credentialsProvider(EnvironmentVariableCredentialsProvider.create()) .build(); DynamoDbEnhancedAsyncClient enhancedAsyncClient = DynamoDbEnhancedAsyncClient.builder() .dynamoDbClient(dbAsyncClient) .extensions(AutoGeneratedTimestampRecordExtension.create()) .build(); // Perform work with s3AsyncClient, dbAsyncClient, and enhancedAsyncClient. // Requests completed: Close all service clients. s3AsyncClient.close(); dbAsyncClient.close() nettyHttpClient.close(); // Explicitly close nettyHttpClient.

代理配置示例

以下代碼片段使用 Netty HTTP 客戶端的代理配置生成器

SdkAsyncHttpClient nettyHttpClient = NettyNioAsyncHttpClient.builder() .proxyConfiguration(ProxyConfiguration.builder() .scheme("https") .host("myproxy") .port(1234) .username("username") .password("password") .nonProxyHosts(Set.of("localhost", "host.example.com")) .build()) .build();

代理伺服器組態的對等 Java 系統屬性顯示在下列命令列程式碼片段中。

$ java -Dhttps.proxyHost=myproxy -Dhttps.proxyPort=1234 -Dhttps.proxyUser=username \ -Dhttps.proxyPassword=password -Dhttp.nonProxyHosts=localhost|host.example.com -cp ... App
重要

若要使用任何 HTTPS 代理系統屬性,必須在程式碼中將scheme屬性設定為https。如果未在程式碼中設定配置屬性,配置會預設為 HTTP,而 SDK 只會尋找http.*系統屬性。

使用環境變數的對等設定為:

// Set the following environment variables. // $ export HTTPS_PROXY="https://username:password@myproxy:1234" // $ export NO_PROXY="localhost|host.example.com" // Set the 'useSystemPropertyValues' to false on the proxy configuration. SdkAsyncHttpClient nettyHttpClient = NettyNioAsyncHttpClient.builder() .proxyConfiguration(ProxyConfiguration.builder() .useSystemPropertyValues(Boolean.FALSE) .build()) .build(); // Run the application. // $ java -cp ... App