项目场景:
系统升级,需要以前redis里的数据,记录一下配置双redis源,进行数据迁移
操作流程
·采用springboot基于java代码配置方式
1.引入包
<!-- 实现对 Spring Data Redis 的自动化配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<!-- 去掉对 Lettuce 的依赖,因为 Spring Boot 优先使用 Lettuce 作为 Redis 客户端 -->
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 引入 Jedis 的依赖,这样 Spring Boot 实现对 Jedis 的自动化配置 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
2.配置数据连接,创建bean
@Configuration
public class RedisConfigs {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.host2}")
private String secondHost;
@Value("${spring.redis.port2}")
private int secondPort;
@Value("${spring.redis.password2}")
private String secondPassword;
@Bean(name = "redisConnectionFactory")
@Primary //默认选择这个数据源进行执行
@Qualifier("redisConnectionFactory")
public RedisConnectionFactory redisConnectionFactory() {
RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
config.setHostName(host);
config.setPort(port);
config.setPassword(RedisPassword.of(password));
config.setDatabase(2);
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(config);
jedisConnectionFactory.getPoolConfig().setMaxTotal(300);
jedisConnectionFactory.getPoolConfig().setMaxIdle(10);
return jedisConnectionFactory;
}
@Bean(name = "secondRedisConnectionFactory")
@Qualifier("secondRedisConnectionFactory")
public RedisConnectionFactory secondRedisConnectionFactory() {
RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
config.setHostName(secondHost);
config.setPort(secondPort);
config.setPassword(RedisPassword.of(secondPassword));
config.setDatabase(0);
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(config);
jedisConnectionFactory.getPoolConfig().setMaxTotal(300);
jedisConnectionFactory.getPoolConfig().setMaxIdle(10);
return jedisConnectionFactory;
}
@Bean(name = "redisTemplateFirst")
public RedisTemplate<String, Object> redisTemplate(@Qualifier("redisConnectionFactory") RedisConnectionFactory factory) {
// 创建 RedisTemplate 对象
RedisTemplate<String, Object> template = new RedisTemplate<>();
// 使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
template.setConnectionFactory(factory);
// 设置开启事务支持
template.setEnableTransactionSupport(true);
// 设置key和value的序列化规则
template.setKeySerializer(new StringRedisSerializer()); // key采用String的序列化方式
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
// 设置hash key 和 hash value的序列化规则
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
return template;
}
@Bean(name = "redisTemplateSecond")
public RedisTemplate<String, Object> redisTemplateSecond(@Qualifier("secondRedisConnectionFactory") RedisConnectionFactory factory) {
// 创建 RedisTemplate 对象
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
// 设置开启事务支持
template.setEnableTransactionSupport(true);
// 设置key和value的序列化规则
template.setKeySerializer(new StringRedisSerializer()); // key采用String的序列化方式
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
// 设置hash key 和 hash value的序列化规则
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
return template;
}
}
3.在需要的地方注入bean
@Resource(name = "redisTemplateFirst")
private RedisTemplate<String, Object> redisOperator;
@Resource(name = "redisTemplateSecond")
private RedisTemplate<String, Object> redisTemplateSecond;