配置基于 Apache 的 HTTP 客户端 - Amazon SDK for Java 2.x
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

配置基于 Apache 的 HTTP 客户端

默认情况下,中的同步服务客户端 Amazon SDK for Java 2.x 使用基于 Apache 的 HTTP 客户端。ApacheHttpClient该软件开发工具包基ApacheHttpClient于 Apache HttpClient

SDK 还提供 UrlConnectionHttpClient,加载速度更快,但功能较少。有关配置 UrlConnectionHttpClient 的信息,请参阅配置基于 URLConnection 的 HTTP 客户端

要查看可供您使用的全套配置选项,请参阅 ApacheHttpClient.Builder 和 ProxyConfiguration. BuilderApacheHttpClient

访问 ApacheHttpClient

在大多数情况下,您无需进行任何显式配置即可使用 ApacheHttpClient。您只需声明您的服务客户端,SDK 将使用标准值为您配置 ApacheHttpClient

如果要显式配置 ApacheHttpClient 或将其用于多个服务客户端,则需要将其设置为可供配置。

无需配置

当您在 Maven 中声明对服务客户端的依赖项时,SDK 会添加对 apache-client 构件的运行时系统依赖项。这使得 ApacheHttpClient 类在运行时可供您的代码使用,但在编译时不可用。如果您没有配置基于 Apache 的 HTTP 客户端,则无需为其指定依赖项。

在以下 Maven pom.xml 文件的 XML 片段中,使用 <artifactId>s3</artifactId> 声明的依赖项会自动引入基于 Apache 的 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 并配置了具有 maxConnectionsconnectionTimeout 值的 ApacheHttpClient 嵌入式实例。HTTP 实例是使用 S3Client.BuilderhttpClientBuilder 方法创建的。

导入

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

代码

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;

代码

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
注意

Apache HTTP 客户端目前不支持 HTTPS 代理系统属性或 HTTPS_PROXY 环境变量。