spring连接传输中加密redis

 spring连接加密传输redis

 遇到问题:spring不能连接Amazon ElastiCache 传输中加密功能的 Redis 节点 

 解决答案:设置ssl为true

  • 代码配置RedisTemplate
package com.xxx.config;

import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisTemplateConfig {
    @Value("${spring.redis.host}")
    private String host;
    @Value("${spring.redis.database}")
    private int database;
    @Value("${spring.redis.port}")
    private int port;
    @Value("${spring.redis.password}")
    private String password;

    @Bean
    public StringRedisTemplate rirStoreRedisTemplate() {
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setDatabase(database);
        redisStandaloneConfiguration.setPort(port);
        redisStandaloneConfiguration.setHostName(host);
        redisStandaloneConfiguration.setPassword(password);
        LettuceClientConfiguration lettuceClientConfiguration = LettuceClientConfiguration.builder().useSsl().build();
        LettuceConnectionFactory factory = new LettuceConnectionFactory(redisStandaloneConfiguration, lettuceClientConfiguration);
        factory.afterPropertiesSet(); // 必须初始化实例
        StringRedisTemplate redisTemplate = new StringRedisTemplate(factory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericToStringSerializer<>(Object.class));
        redisTemplate.setValueSerializer(new GenericToStringSerializer<>(Object.class));
        redisTemplate.setDefaultSerializer(new FastJsonRedisSerializer(Object.class));
        return redisTemplate;
    }

}
  •  springboot自动配置RedisTemplate  
spring.redis.host=xxx
spring.redis.port=6379
spring.redis.database=1
spring.redis.password=xxx
spring.redis.ssl=true

redis-cli连接

  https://amazonaws-china.com/cn/premiumsupport/knowledge-center/elasticache-connect-redis-node/

你可能感兴趣的:(-数据库-,-spring,redis)