Maven
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
技术:springBoot spring.data.redis
解决的问题:通过spring.data.redis 连接SSL协议的 redis(aws redis)
springBoot配置 ssl 设置为true
spring.redis.database = xxxx
spring.redis.port = xxxx
spring.redis.host = xxxxx
spring.redis.timeout = xxxx
spring.redis.password = xxxxx
spring.redis.ssl = true
解决方案1
package com.example.redistest.config;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.*;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
/**
* @author lucky
* @create 2022/7/26 20:12
*/
@Configuration
public class RedisConfig {
@Bean
public LettuceConnectionFactory redisConnectionFactory(RedisProperties redisProperties) {
//useSsl 是否使用 SSL
//disablePeerVerification 关闭SSL验证对等点
LettuceClientConfiguration lettuceClientConfiguration = LettuceClientConfiguration.builder()
.useSsl().disablePeerVerification().build();
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setHostName(redisProperties.getHost());
redisStandaloneConfiguration.setPort(redisProperties.getPort());
redisStandaloneConfiguration.setPassword(redisProperties.getPassword());
return new LettuceConnectionFactory(redisStandaloneConfiguration, lettuceClientConfiguration);
}
}
解决方案2:
package com.example.redistest.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.data.redis.LettuceClientConfigurationBuilderCustomizer;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.*;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
/**
* @author lucky
* @create 2022/7/26 20:12
*/
@Configuration(
proxyBeanMethods = false
)
public class RedisConfig {
@Bean
@ConditionalOnProperty(
value = {"spring.redis.ssl"},
havingValue = "true"
)
public LettuceClientConfigurationBuilderCustomizer lettuceClientConfigurationBuilderCustomizer(){
return (clientConfigurationBuilder) ->{
clientConfigurationBuilder.useSsl().disablePeerVerification();
};
}
}
Spring Data Redis 官方文档