@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private Integer port;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.pool.max-active}")
private int redisPoolMaxActive;
@Value("${spring.redis.pool.max-wait}")
private int redisPoolMaxWait;
@Value("${spring.redis.pool.max-idle}")
private int redisPoolMaxIdle;
@Value("${spring.redis.pool.min-idle}")
private int redisPoolMinIdle;
@Bean(name = "db0RedisTemplate")
public RedisTemplate db0RedisTemplate() {
return getRedisConnectionFactory(0);
}
public RedisTemplate<String, String> getRedisConnectionFactory(int database) {
JedisConnectionFactory jedisFactory = new JedisConnectionFactory();
jedisFactory.setHostName(host);
jedisFactory.setPort(port);
if(StrUtil.isNotBlank(password)){
jedisFactory.setPassword(password);
}
jedisFactory.setDatabase(database);
JedisPoolConfig poolConfig = new JedisPoolConfig(); // 进行连接池配置
poolConfig.setMaxTotal(redisPoolMaxActive);
poolConfig.setMaxIdle(redisPoolMaxIdle);
poolConfig.setMinIdle(redisPoolMinIdle);
poolConfig.setMaxWaitMillis(redisPoolMaxWait);
jedisFactory.setPoolConfig(poolConfig);
jedisFactory.afterPropertiesSet(); // 初始化连接池配置
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(jedisFactory);
return redisTemplate;
}
redisTemplate连接多个redis库
于 2022-05-10 16:16:44 首次发布