- 场景:项目中需要用到连接两个redis数据源
-
实现:
-
首先在配置文件中配置数据源信息:
#redis spring.redis.database=88 spring.redis.host=XXXX spring.redis.port=6379 spring.redis.password=XXXX #redis2 spring.redis.database.ad=0 spring.redis.host.ad=XXXX spring.redis.port.ad=6379 spring.redis.password.ad=XXXX
spring.redis.jedis.pool.max-active=64 spring.redis.jedis.pool.max-wait=1000 spring.redis.jedis.pool.max-idle=8 spring.redis.jedis.pool.min-idle=1 spring.redis.timeout=5000
-
增加redis配置类:
@Configuration public class RedisConfigAd { /** * Logger */ private static final Logger lg = LoggerFactory.getLogger(RedisConfigAd.class); @Value("${spring.redis.host.ad}") public String host; @Value("${spring.redis.port.ad}") public int port; @Value("${spring.redis.password.ad}") public String password; @Value("${spring.redis.database.ad}") public int database; @Value("${spring.redis.jedis.pool.max-idle}") public int maxIdle; @Value("${spring.redis.jedis.pool.min-idle}") public int minIdle; @Value("${spring.redis.jedis.pool.max-active}") public int maxActive; @Value("${spring.redis.jedis.pool.max-wait}") public String maxWait; @Value("${spring.redis.timeout}") public String timeout; public JedisConnectionFactory jedisConnectionFactoryAd () { RedisStandaloneConfiguration rf = new RedisStandaloneConfiguration(); rf.setDatabase(database); rf.setHostName(host); rf.setPort(port); rf.setPassword(password); int to = Integer.parseInt(timeout); JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jpb = (JedisClientConfiguration.JedisPoolingClientConfigurationBuilder) JedisClientConfiguration.builder(); JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxIdle(maxIdle); jedisPoolConfig.setMinIdle(minIdle); jedisPoolConfig.setMaxTotal(maxActive); int l = Integer.parseInt(maxWait); jedisPoolConfig.setMaxWaitMillis(l); jpb.poolConfig(jedisPoolConfig); JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(rf, jpb.build()); return jedisConnectionFactory; }
-
```
@Bean(name = "redisTemplate1")
public RedisTemplate<String, Object> redisTemplate1( ) {
// 配置redisTemplate
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
redisTemplate.setConnectionFactory(jedisConnectionFactoryAd());
// StringRedisSerializer stringSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(keySerializer()); // key序列化
redisTemplate.setValueSerializer(valueSerializer()); // value序列化
redisTemplate.setHashKeySerializer(keySerializer()); // Hash key序列化
redisTemplate.setHashValueSerializer(valueSerializer()); // Hash value序列化
redisTemplate.setDefaultSerializer(valueSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
private RedisSerializer<String> keySerializer() {
return new StringRedisSerializer();
}
private RedisSerializer<Object> valueSerializer() {
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
return jackson2JsonRedisSerializer;
}
}
```
-
增加对RedisTemplate1的封装:
@Service public class RedisServiceAd { @Autowired @Resource(name = "redisTemplate1") private RedisTemplate<String,Object> redisTemplate1; public boolean expire(String key,long time){ try { if(time>0){ redisTemplate1.expire(key, time, TimeUnit.SECONDS); } return true; } catch (Exception e) { e.printStackTrace(); return false; } } ......
-
注意事项:
-
对于配置类中的RedisTemplate,需要加@Bean(name = “redisTemplate1”)指定名称;
同时,在注入RedisTemplate ,需要@Resource指定注入哪一个,如:
@Autowired
@Resource(name = “redisTemplate1”)
private RedisTemplate<String,Object> redisTemplate1; -
JedisConnectionFactory是单例的,所以只能有一个JedisConnectionFactory的bean实例,否则编译会报错。
-