示例:禁用集群模式并启用 TLS 的 Lettuce 配置 - ElastiCache 适用于 Redis 的 Amazon
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

示例:禁用集群模式并启用 TLS 的 Lettuce 配置

注意

以下示例中的超时适用于运行键和值长度最多 20 字节的 SET/GET 命令的测试。当命令较复杂或键和值较大时,处理时间可能会更长。您应该根据应用程序的用例设置超时。

// Set DNS cache TTL public void setJVMProperties() { java.security.Security.setProperty("networkaddress.cache.ttl", "10"); } private static final Duration META_COMMAND_TIMEOUT = Duration.ofMillis(1000); private static final Duration DEFAULT_COMMAND_TIMEOUT = Duration.ofMillis(250); // Socket connect timeout should be lower than command timeout for Lettuce private static final Duration CONNECT_TIMEOUT = Duration.ofMillis(100); // Create RedisURI from the primary/reader endpoint clusterEndpoint = <primary/reader-endpoint> // TODO: add your node endpoint RedisURI redisUriStandalone = RedisURI.Builder.redis(clusterEndpoint).withPort(6379).withSsl(true).withDatabase(0).build(); ClientResources clientResources = DefaultClientResources.builder() .dnsResolver(new DirContextDnsResolver()) .reconnectDelay( Delay.fullJitter( Duration.ofMillis(100), // minimum 100 millisecond delay Duration.ofSeconds(10), // maximum 10 second delay 100, TimeUnit.MILLISECONDS)) // 100 millisecond base .build(); // Use a dynamic timeout for commands, to avoid timeouts during // slow operations. class DynamicTimeout extends TimeoutSource { private static final Set<ProtocolKeyword> META_COMMAND_TYPES = ImmutableSet.<ProtocolKeyword>builder() .add(CommandType.FLUSHDB) .add(CommandType.FLUSHALL) .add(CommandType.INFO) .add(CommandType.KEYS) .build(); private final Duration metaCommandTimeout; private final Duration defaultCommandTimeout; DynamicTimeout(Duration defaultTimeout, Duration metaTimeout) { defaultCommandTimeout = defaultTimeout; metaCommandTimeout = metaTimeout; } @Override public long getTimeout(RedisCommand<?, ?, ?> command) { if (META_COMMAND_TYPES.contains(command.getType())) { return metaCommandTimeout.toMillis(); } return defaultCommandTimeout.toMillis(); } } TimeoutOptions timeoutOptions = TimeoutOptions.builder() .timeoutSource(new DynamicTimeout(DEFAULT_COMMAND_TIMEOUT, META_COMMAND_TIMEOUT)) .build(); final SocketOptions socketOptions = SocketOptions.builder().connectTimeout(CONNECT_TIMEOUT).keepAlive(true).build(); ClientOptions clientOptions = ClientOptions.builder().timeoutOptions(timeoutOptions).socketOptions(socketOptions).build(); RedisClient redisClient = RedisClient.create(clientResources, redisUriStandalone); redisClient.setOptions(clientOptions);