本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
設定以網址連線為基礎的 HTTP 用戶端
與默認值相比, AWS SDK for Java 2.x 提供了一個更輕的 UrlConnectionHttpClient
HTTP 客戶端。ApacheHttpClient
這UrlConnectionHttpClient
是基於 Java 的URLConnection
.
UrlConnectionHttpClient
載入速度比以 Apaches 為基礎的 HTTP 用戶端更快,但功能較少。由於加載速度更快,因此對於 Java AWS Lambda
函數來說是一個很好的解決方案。
UrlConnectionHttpClient
有數個可供您存取的可設定選項
注意
UrlConnectionHttpClient
不支援 HTTP 修補程式方法。
少數 AWS API 操作需要 PATCH 請求。這些作業名稱通常以開頭Update*
。以下是幾個範例。
-
AWS Security Hub API 中的幾個
Update*
操作以及BatchUpdateFindings操作 -
所有 Amazon API Gateway API
Update*
操作 -
Amazon WorkDocs API 中的幾項
Update*
操作
如果您可能使用UrlConnectionHttpClient
,請先參考您正在使用 AWS 服務 的 API 參考。檢查您需要的作業是否使用 PATCH 作業。
訪問 UrlConnectionHttpClient
若要設定和使用UrlConnectionHttpClient
,請在檔案中宣告對 url-connection-client
Maven 工pom.xml
件的相依性。
與不同的UrlConnectionHttpClient
是ApacheHttpClient
,不會自動添加到您的項目中,因此使用必須特別聲明它。
下列pom.xml
檔案範例顯示使用和設定 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> <!-- other dependencies such as s3 or dynamodb --> <dependencies> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>url-connection-client</artifactId> </dependency> </dependencies>
使用和配置 UrlConnectionHttpClient
您可以設定的執行個體以UrlConnectionHttpClient
及建置服務用戶端,也可以將單一執行個體設定為在多個服務用戶端之間共用。
使用任何一種方法,您都可以使用 UrlConnectionHttpClient.Builder
最佳做法:將UrlConnectionHttpClient
執行個體專用於服務用戶端
如果您需要設定的執行個體UrlConnectionHttpClient
,建議您建置專用UrlConnectionHttpClient
執行個體。您可以使用服務客戶端構建器的httpClientBuilder
方法來執行此操作。這樣,HTTP 客戶端的生命週期由 SDK 管理,如果UrlConnectionHttpClient
實例不再需要時關閉,這有助於避免潛在的內存洩漏。
下列範例會建立S3Client
和設定 for 和 values 的內嵌執行個UrlConnectionHttpClient
體socketTimeout
。proxyConfiguration
該proxyConfiguration
方法採用類型的 Java lambda 表達式 Consumer<ProxyConfiguration.Builder
。
匯入
import software.amazon.awssdk.http.SdkHttpClient; import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient; import java.net.URI; import java.time.Duration;
Code
// Singleton: Use the s3Client for all requests. S3Client s3Client = S3Client.builder() .httpClientBuilder(UrlConnectionHttpClient.builder() .socketTimeout(Duration.ofMinutes(5)) .proxyConfiguration(proxy -> proxy.endpoint(URI.create("http://proxy.mydomain.net:8888")))) .credentialsProvider(EnvironmentVariableCredentialsProvider.create()) .build(); // Perform work with the s3Client. s3Client.close(); // Requests completed: Close the s3client.
替代方法:共享一個UrlConnectionHttpClient
實例
為了協助降低應用程式的資源和記憶體使用量,您可以設定UrlConnectionHttpClient
並在多個服務用戶端之間共用它。HTTP 連線集區將會共用,進而降低資源使用量。
注意
共用UrlConnectionHttpClient
執行個體時,您必須在準備好處理執行個體時將其關閉。當服務客戶端關閉時,SDK 不會關閉實例。
下列範例會設定兩個服務用戶端所使用的 URL 連線型 HTTP 用戶端。配置的UrlConnectionHttpClient
實例傳遞給每個構建器的httpClient
方法。當不再需要服務用戶端和 HTTP 用戶端時,程式碼會明確地關閉它們。該代碼最後關閉 HTTP 客戶端。
匯入
import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider; import software.amazon.awssdk.awscore.defaultsmode.DefaultsMode; import software.amazon.awssdk.http.SdkHttpClient; import software.amazon.awssdk.http.urlconnection.ProxyConfiguration; import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.s3.S3Client; import java.net.URI; import java.time.Duration;
Code
SdkHttpClient urlHttpClient = UrlConnectionHttpClient.create(); // Singletons: Use the s3Client and dynamoDbClient for all requests. S3Client s3Client = S3Client.builder() .httpClient(urlHttpClient) .defaultsMode(DefaultsMode.IN_REGION) .credentialsProvider(EnvironmentVariableCredentialsProvider.create()) .build(); DynamoDbClient dynamoDbClient = DynamoDbClient.builder() .httpClient(urlHttpClient) .defaultsMode(DefaultsMode.IN_REGION) .credentialsProvider(EnvironmentVariableCredentialsProvider.create()) .build(); // Perform work with the s3Client and dynamoDbClient. // Requests completed: Close all service clients. s3Client.close(); dynamoDbClient.close(); urlHttpClient.close();
在應用程式UrlConnectionHttpClient
中使用時,您必須使用服務用戶端建置器的httpClientBuilder
方法,為每個服務用戶端提供ApacheHttpClient
執行個體或執行個體。URLConnectionHttpClient
如果您的程式使用多個服務用戶端,且下列兩項都成立,就會發生例外狀況:
-
一個服務客戶端被配置為使用一個
UrlConnectionHttpClient
實例 -
另一個服務客戶端使用默認值,
ApacheHttpClient
而不使用httpClient()
或httpClientBuilder()
方法明確構建它
異常將聲明在類路徑上找到了多個 HTTP 實現。
下列範例程式碼片段會導致例外狀況。
// The dynamoDbClient uses the UrlConnectionHttpClient DynamoDbClient dynamoDbClient = DynamoDbClient.builder() .httpClient(UrlConnectionHttpClient.create()) .build(); // The s3Client below uses the ApacheHttpClient at runtime, without specifying it. // An SdkClientException is thrown with the message that multiple HTTP implementations were found on the classpath. S3Client s3Client = S3Client.create(); // Perform work with the s3Client and dynamoDbClient. dynamoDbClient.close(); s3Client.close();
透過明確設定S3Client
與ApacheHttpClient
.
DynamoDbClient dynamoDbClient = DynamoDbClient.builder() .httpClient(UrlConnectionHttpClient.create()) .build(); S3Client s3Client = S3Client.builder() .httpClient(ApacheHttpClient.create()) // Explicitly build the ApacheHttpClient. .build(); // Perform work with the s3Client and dynamoDbClient. dynamoDbClient.close(); s3Client.close();
注意
若要明確建立ApacheHttpClient
,您必須在 Maven 專案檔案中新增對apache-client工件的相依性。
代理配置示例
下面的代碼片段使用代理配置生成器的 URL 連接 HTTP 客戶端
SdkHttpClient urlHttpClient = UrlConnectionHttpClient.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 = UrlConnectionHttpClient.builder() .proxyConfiguration(ProxyConfiguration.builder() .useSystemPropertyValues(Boolean.FALSE) .build()) .build(); // Run the application. // $ java -cp ... App
注意
以網址連線為基礎的 HTTP 用戶端目前不支援 HTTPS 代理伺服器系統屬性或環境變數。