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

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

客户端配置

在 1.x 中,通过在SDK客户端或客户端生成器上设置ClientConfiguration实例来修改客户端配置。在版本 2.x 中,已将客户端配置分为单独的配置类。使用单独的配置类,您可以为异步HTTP客户端和同步客户端配置不同的客户端,但仍使用相同的ClientOverrideConfiguration类。

例 版本 1.x 中的客户端配置
AmazonDynamoDBClientBuilder.standard() .withClientConfiguration(clientConfiguration) .build()
例 版本 2.x 中的同步客户端配置
ProxyConfiguration.Builder proxyConfig = ProxyConfiguration.builder(); ApacheHttpClient.Builder httpClientBuilder = ApacheHttpClient.builder() .proxyConfiguration(proxyConfig.build()); ClientOverrideConfiguration.Builder overrideConfig = ClientOverrideConfiguration.builder(); DynamoDbClient client = DynamoDbClient.builder() .httpClientBuilder(httpClientBuilder) .overrideConfiguration(overrideConfig.build()) .build();
例 版本 2.x 中的异步客户端配置
NettyNioAsyncHttpClient.Builder httpClientBuilder = NettyNioAsyncHttpClient.builder(); ClientOverrideConfiguration.Builder overrideConfig = ClientOverrideConfiguration.builder(); ClientAsyncConfiguration.Builder asyncConfig = ClientAsyncConfiguration.builder(); DynamoDbAsyncClient client = DynamoDbAsyncClient.builder() .httpClientBuilder(httpClientBuilder) .overrideConfiguration(overrideConfig.build()) .asyncConfiguration(asyncConfig.build()) .build();

HTTP客户

显著的变化

  • 在版本 2.x 中,您可以通过使用指定实现来更改在运行时使用的HTTP客户端。clientBuilder.httpClientBuilder

  • 使用clientBuilder.httpClient将客户端传递给服务HTTP客户端生成器时,如果服务HTTP客户端关闭,则默认情况下该客户端不会关闭。这允许您在服务HTTP客户端之间共享客户端。

  • 异步HTTP客户端现在使用非阻塞 IO。

  • 有些操作现在使用 HTTP /2 来提高性能。

设置变更

设置 1.x 2.x 同步,Apache 2.x Async,Netty
ClientConfiguration clientConfig = new ClientConfiguration()
ApacheHttpClient.Builder httpClientBuilder = ApacheHttpClient.builder()
NettyNioAsyncHttpClient.Builder httpClientBuilder = NettyNioAsyncHttpClient.builder()
最大连接数
clientConfig.setMaxConnections(...) clientConfig.withMaxConnections(...)
httpClientBuilder.maxConnections(...)
httpClientBuilder.maxConcurrency(...)
连接超时
clientConfig.setConnectionTimeout(...) clientConfig.withConnectionTimeout(...)
httpClientBuilder.connectionTimeout(...) httpClientBuilder.connectionAcquisitionTimeout(...)
httpClientBuilder.connectionTimeout(...)
套接字超时
clientConfig.setSocketTimeout(...) clientConfig.withSocketTimeout(...)
httpClientBuilder.socketTimeout(...)
httpClientBuilder.writeTimeout(...) httpClientBuilder.readTimeout(...)
连接 TTL
clientConfig.setConnectionTTL(...) clientConfig.withConnectionTTL(...)
httpClientBuilder.connectionTimeToLive(...)
httpClientBuilder.connectionTimeToLive(...)
连接最大空闲时间
clientConfig.setConnectionMaxIdleMillis(...) clientConfig.withConnectionMaxIdleMillis(...)
httpClientBuilder.connectionMaxIdleTime(...)
httpClientBuilder.connectionMaxIdleTime(...)
处于非活动状态后进行验证
clientConfig.setValidateAfterInactivityMillis(...) clientConfig.withValidateAfterInactivityMillis(...)
不支持(请求功能 不支持(请求功能
本地地址
clientConfig.setLocalAddress(...) clientConfig.withLocalAddress(...)
httpClientBuilder.localAddress(...)
不支持
已启用 “预期继续”
clientConfig.setUseExpectContinue(...) clientConfig.withUseExpectContinue(...)
httpClientBuilder.expectContinueEnabled(...)
不支持(请求功能
连接收割者
clientConfig.setUseReaper(...) clientConfig.withReaper(...)
httpClientBuilder.useIdleConnectionReaper(...)
httpClientBuilder.useIdleConnectionReaper(...)
AmazonDynamoDBClientBuilder.standard() .withClientConfiguration(clientConfiguration) .build()
DynamoDbClient.builder() .httpClientBuilder(httpClientBuilder) .build()
DynamoDbAsyncClient.builder() .httpClientBuilder(httpClientBuilder) .build()

HTTP客户端代理

设置 1.x 2.x 同步,Apache 2.x Async,Netty
ClientConfiguration clientConfig = new ClientConfiguration()
ProxyConfiguration.Builder proxyConfig = ProxyConfiguration.builder()
ProxyConfiguration.Builder proxyConfig = ProxyConfiguration.builder()
代理主机
clientConfig.setProxyHost(...) clientConfig.withProxyHost(...)
proxyConfig.endpoint(...)
proxyConfig.host(...)
代理端口
clientConfig.setProxyPort(...) clientConfig.withProxyPort(...)
proxyConfig.endpoint(...)

代理端口嵌入在 endpoint

proxyConfig.port(...)
代理用户名
clientConfig.setProxyUsername(...) clientConfig.withProxyUsername(...)
proxyConfig.username(...)
proxyConfig.username(...)
代理密码
clientConfig.setProxyPassword(...) clientConfig.withProxyPassword(...)
proxyConfig.password(...)
proxyConfig.password(...)
代理域
clientConfig.setProxyDomain(...) clientConfig.withProxyDomain(...)
proxyConfig.ntlmDomain(...)
不支持(请求功能
代理工作站
clientConfig.setProxyWorkspace(...) clientConfig.withProxyWorkstation(...)
proxyConfig.ntlmWorkstation(...)
不支持(请求功能
代理身份验证方法
clientConfig.setProxyAuthenticationMethods(...) clientConfig.withProxyAuthenticationMethods(...)

不支持

不支持(请求功能
抢占式基本代理身份验证
clientConfig.setPreemptiveBasicProxyAuth(...) clientConfig.withPreemptiveBasicProxyAuth(...)
proxyConfig.preemptiveBasicAuthenticationEnabled(...)
不支持(请求功能
非代理主机
clientConfig.setNonProxyHosts(...) clientConfig.withNonProxyHosts(...)
proxyConfig.nonProxyHosts(...)
proxyConfig.nonProxyHosts(...)
禁用套接字代理
clientConfig.setDisableSocketProxy(...) clientConfig.withDisableSocketProxy(...)
不支持(请求功能 不支持(请求功能
AmazonDynamoDBClientBuilder.standard() .withClientConfiguration(clientConfiguration) .build()
httpClientBuilder.proxyConfiguration( proxyConfig.build())
httpClientBuilder.proxyConfiguration( proxyConfig.build())

客户机覆盖

设置 1.x 2.x
ClientConfiguration clientConfig = new ClientConfiguration()
ClientOverrideConfiguration.Builder overrideConfig = ClientOverrideConfiguration.builder()
用户代理前缀
clientConfig.setUserAgentPrefix(...) clientConfig.withUserAgentPrefix(...)
overrideConfig.advancedOption( SdkAdvancedClientOption.USER_AGENT_PREFIX, ...)
用户代理后缀
clientConfig.setUserAgentSuffix(...) clientConfig.withUserAgentSuffix(...)
overrideConfig.advancedOption( SdkAdvancedClientOption.USER_AGENT_SUFFIX, ...)
Signer
clientConfig.setSignerOverride(...) clientConfig.withSignerOverride(...)
overrideConfig.advancedOption( SdkAdvancedClientOption.SIGNER, ...)
其他标题
clientConfig.addHeader(...) clientConfig.withHeader(...)
overrideConfig.putHeader(...)
请求超时
clientConfig.setRequestTimeout(...) clientConfig.withRequestTimeout(...)
overrideConfig.apiCallAttemptTimeout(...)
客户端执行超时
clientConfig.setClientExecutionTimeout(...) clientConfig.withClientExecutionTimeout(...)
overrideConfig.apiCallTimeout(...)
使用 Gzip
clientConfig.setUseGzip(...) clientConfig.withGzip(...)

不支持(请求功能

套接字缓冲区大小提示
clientConfig.setSocketBufferSizeHints(...) clientConfig.withSocketBufferSizeHints(...)
不支持(请求功能
缓存响应元数据
clientConfig.setCacheResponseMetadata(...) clientConfig.withCacheResponseMetadata(...)
不支持(请求功能
响应元数据缓存大小
clientConfig.setResponseMetadataCacheSize(...) clientConfig.withResponseMetadataCacheSize(...)
不支持(请求功能
DNS解析器
clientConfig.setDnsResolver(...) clientConfig.withDnsResolver(...)
不支持(请求功能
TCP保持活力
clientConfig.setUseTcpKeepAlive(...) clientConfig.withTcpKeepAlive(...)

此选项现在位于HTTP客户机配置中

- ApacheHttpClient.builder().tcpKeepAlive(true) - NettyNioAsyncHttpClient.builder().tcpKeepAlive(true)
安全随机
clientConfig.setSecureRandom(...) clientConfig.withSecureRandom(...)
不支持(请求功能
AmazonDynamoDBClientBuilder.standard() .withClientConfiguration(clientConfiguration) .build()
DynamoDbClient.builder() .httpClientBuilder(httpClientBuilder) .build()

客户端重试次数

设置 1.x 2.x
ClientConfiguration clientConfig = new ClientConfiguration()
RetryPolicy.Builder retryPolicy = RetryPolicy.builder()
最大错误重试次数
clientConfig.setMaxErrorRetry(...) clientConfig.withMaxErrorRetry(...)
retryPolicy.numRetries(...)
使用受限制的重试次数
clientConfig.setUseThrottleRetries(...) clientConfig.withUseThrottleRetries(...)
不支持
限制前的最大连续重试次数
clientConfig.setMaxConsecutiveRetriesBeforeThrottling(...) clientConfig.withMaxConsecutiveRetriesBeforeThrottling(...)
不支持
AmazonDynamoDBClientBuilder.standard() .withClientConfiguration(clientConfiguration) .build()
DynamoDbClient.builder() .httpClientBuilder(httpClientBuilder) .build()

异步客户端

设置 1.x 2.x
ClientAsyncConfiguration.Builder asyncConfig = ClientAsyncConfiguration.builder()
执行程序
AmazonDynamoDBAsyncClientBuilder.standard() .withExecutorFactory(...) .build()
asyncConfig.advancedOption( SdkAdvancedAsyncClientOption.FUTURE_COMPLETION_EXECUTOR, ...)
DynamoDbAsyncClient.builder() .asyncConfiguration(asyncConfig) .build()

其他客户机变更

1.x 中的以下ClientConfiguration选项在 2.x 中已更改,没有直接SDK等效项。

设置 1.x 等同于 2.x
协议
clientConfig.setProtocol(Protocol.HTTP) clientConfig.withProtocol(Protocol.HTTP)

协议设置为HTTPS默认设置。要修改设置,请在客户端生成器上指定协议设置HTTP端点:

clientBuilder.endpointOverride( URI.create("http://..."))