我有一个需求是根据请求参数连接不同地址的redis
所以我想写一个函数动态获取redisTemplate,传入指定的redis配置参数,以便连接不同的redis,这个函数类似这样:
public static RedisTemplate<Object, Object> getRedisTemplate(RedisConnectInfo info) {
// redisStandaloneConfiguration
RedisStandaloneConfiguration redisStandaloneConfiguration = redisStandaloneConfigurationBuild(info);
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
// 序列化方式
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setConnectionFactory(new LettuceConnectionFactory(
redisStandaloneConfiguration,
LettuceClientConfiguration.builder().build()
));
// redisTemplate.setConnectionFactory(new JedisConnectionFactory(redisStandaloneConfiguration));
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
// RedisConfigInfo是一个包含redis地址密码等信息的简单pojo
public static RedisStandaloneConfiguration redisStandaloneConfigurationBuild(RedisConfigInfo info) {
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setHostName(info.getHost());
redisStandaloneConfiguration.setDatabase(info.getDbIndex());
redisStandaloneConfiguration.setPassword(RedisPassword.of(info.getPassword()));
redisStandaloneConfiguration.setPort(info.getPort());
return redisStandaloneConfiguration;
}
我在测试中这样使用:
@Test
public void testRedisTemplateSet() {
RedisTemplate<Object, Object> redisTemplate = CacheUtils.getRedisTemplate(new RedisConfigInfo("127.0.0.1", "password", 6379));
redisTemplate.opsForValue().set("foo", "bar");
}
但无论我使用LettuceConnectionFactory
还是JedisConnectionFactory
作为RedisConnectionFactory
的实现,最后都得到类似(如果是JedisConnectionFactory 则报JedisConnectionFactory was not initialized)的这样一个错误:
java.lang.IllegalStateException: LettuceConnectionFactory was not initialized through afterPropertiesSet()
我想知道我这样写是否正确,还有如何解决这个报错,谢谢
自己实现与redis连接就行了 不需要使用redisTemplate
这个不光要考虑创建 还涉及缓存创建的连接 不能无限制的创建下去