分布式缓存服务 DCS-Lettuce客户端连接Redis(Java):SSL连接配置(可选配置)

时间:2024-10-24 19:37:49

SSL连接配置(可选配置)

当实例开启了SSL,通过SSL连接实例时,请使用以下内容替换基于Bean方式配置中的LettuceClientConfiguration构造方法clientConfiguration()。Redis实例支持SSL的情况请参考配置Redis SSL 数据加密 传输。

  • 单机、主备、读写分离、Proxy集群实例配置
    @Bean
    public LettuceClientConfiguration clientConfiguration() {
    
        SocketOptions socketOptions = SocketOptions.builder().connectTimeout(Duration.ofMillis(redisConnectTimeout)).build();
    
        SslOptions sslOptions = SslOptions.builder()
            .trustManager(new File(certificationPath))
            .build();
    
        ClientOptions clientOptions = ClientOptions.builder()
            .sslOptions(sslOptions)
            .autoReconnect(true)
            .pingBeforeActivateConnection(true)
            .cancelCommandsOnReconnectFailure(false)
            .disconnectedBehavior(ClientOptions.DisconnectedBehavior.ACCEPT_COMMANDS)
            .socketOptions(socketOptions)
            .build();
        LettuceClientConfiguration clientConfiguration = LettuceClientConfiguration.builder()
            .commandTimeout(Duration.ofMillis(redisReadTimeout))
            .readFrom(ReadFrom.MASTER)
            .clientOptions(clientOptions)
            .useSsl()
            .build();
    
        return clientConfiguration;
    }
  • Cluster集群实例配置
    @Bean
    public LettuceClientConfiguration clientConfiguration() {
    
        SocketOptions socketOptions = SocketOptions.builder().connectTimeout(Duration.ofMillis(redisConnectTimeout)).build();
    
        SslOptions sslOptions = SslOptions.builder()
            .trustManager(new File(certificationPath))
            .build();
    
        ClusterTopologyRefreshOptions topologyRefreshOptions = ClusterTopologyRefreshOptions.builder()
            .enableAllAdaptiveRefreshTriggers()
            .enablePeriodicRefresh(Duration.ofMillis(redisClusterTopologyRefreshPeriodMillis))
            .build();
    
        ClusterClientOptions clientOptions = ClusterClientOptions.builder()
            .sslOptions(sslOptions)
            .autoReconnect(true)
            .pingBeforeActivateConnection(true)
            .cancelCommandsOnReconnectFailure(false)
            .disconnectedBehavior(ClientOptions.DisconnectedBehavior.ACCEPT_COMMANDS)
            .socketOptions(socketOptions)
            .topologyRefreshOptions(topologyRefreshOptions)
            .build();
    
    
        LettuceClientConfiguration clientConfiguration = LettuceClientConfiguration.builder()
            .commandTimeout(Duration.ofMillis(redisReadTimeout))
            .readFrom(ReadFrom.MASTER)
            .clientOptions(clientOptions)
            .useSsl()
            .build();
    
        return clientConfiguration;
    }
support.huaweicloud.com/usermanual-dcs/dcs-ug-211105002.html