設定以阿帕奇為基礎的 HTTP 用戶端 - AWS SDK for Java 2.x

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

設定以阿帕奇為基礎的 HTTP 用戶端

依預設,ApacheHttpClient中的同步服務 AWS SDK for Java 2.x 用戶端會使用以 Apache 為基礎的 HTTP 用戶端。該 SDK ApacheHttpClient 是基於阿帕奇HttpClient

SDK 也提供了UrlConnectionHttpClient載入速度更快,但功能較少。如需有關配置的資訊UrlConnectionHttpClient,請參閱設定以網址連線為基礎的 HTTP 用戶端

若要查看可用的完整組態選項集ApacheHttpClient,請參閱 ApacheHttpClient.Builder 和 ProxyConfiguration. Builder

訪問 ApacheHttpClient

在大多數情況下,您可以使用ApacheHttpClient沒有任何明確組態的。您宣告您的服務ApacheHttpClient用戶端,SDK 會為您設定標準值。

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

無需配置

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

在一個 Maven pom.xml 文件的下面的 XML 片段,與聲明的依賴關係<artifactId>s3</artifactId>自動帶來基於阿帕奇的 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> <!-- The s3 dependency automatically adds a runtime dependency on the ApacheHttpClient--> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>s3</artifactId> </dependency> </dependencies>

使用這些相依性時,您無法進行任何明確的 HTTP 組態變更,因為程式ApacheHttpClient庫僅位於執行階段類別路徑上。

需要的配置

若要設定ApacheHttpClient,您需要在編譯階段新增程式apache-client庫的相依性。

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

<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>s3</artifactId> </dependency> <!-- By adding the apache-client dependency, ApacheHttpClient will be added to the compile classpath so you can configure it. --> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>apache-client</artifactId> </dependency> </dependencies>

使用和配置 ApacheHttpClient

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

無論使用哪一種方法,您都可以使用ApacheHttpClient.Builder來設定以 Apache 為基礎的 HTTP 用戶端的內容。

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

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

下列範例會建立S3Client和設定 for 和 values 的內嵌執行個ApacheHttpClientmaxConnectionsconnectionTimeoutHTTP 執行個體是使用的方httpClientBuilder法建立的S3Client.Builder

匯入

import software.amazon.awssdk.http.apache.ApacheHttpClient; import software.amazon.awssdk.services.s3.S3Client; import java.time.Duration;

Code

S3Client s3Client = S3Client // Singleton: Use the s3Client for all requests. .builder() .httpClientBuilder(ApacheHttpClient.builder() .maxConnections(100) .connectionTimeout(Duration.ofSeconds(5)) ).build(); // Perform work with the s3Client. s3Client.close(); // Requests completed: Close all service clients.

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

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

注意

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

下列範例會設定兩個服務用戶端所使用的 Apache 型 HTTP 用戶端。配置的ApacheHttpClient實例傳遞給每個構建器的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

SdkHttpClient apacheHttpClient = ApacheHttpClient.builder() .maxConnections(100).build(); // Singletons: Use the s3Client and dynamoDbClient for all requests. S3Client s3Client = S3Client.builder() .httpClient(apacheHttpClient).build(); DynamoDbClient dynamoDbClient = DynamoDbClient.builder() .httpClient(apacheHttpClient).build(); // Perform work with the s3Client and dynamoDbClient. // Requests completed: Close all service clients. s3Client.close(); dynamoDbClient.close(); apacheHttpClient.close(); // Explicitly close apacheHttpClient.

代理配置示例

下面的代碼片段使用了 Apache HTTP 客戶端的代理配置生成器

SdkHttpClient apacheHttpClient = ApacheHttpClient.builder() .proxyConfiguration(ProxyConfiguration.builder() .endpoint(URI.create("http://example.com:1234")) .username("username") .password("password") .addNonProxyHost("localhost") .addNonProxyHost("host.example.com") .build()) .build();

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

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

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

// Set the following environment variables. // $ export HTTP_PROXY="http://username:password@example.com:1234" // $ export NO_PROXY="localhost|host.example.com" // Set the 'useSystemPropertyValues' to false on the proxy configuration. SdkHttpClient apacheHttpClient = ApacheHttpClient.builder() .proxyConfiguration(ProxyConfiguration.builder() .useSystemPropertyValues(Boolean.FALSE) .build()) .build(); // Run the application. // $ java -cp ... App
注意

HTTP 用戶端目前不支援 HTTPS 代理伺服器系統屬性或環境變數。