现在springboot配置redis
# 配置outredis
spring.redis.host: ******
spring.redis.password: ******
spring.redis.port: 6379
spring.redis.timeout: 6000
# 连接池最大连接数(使用负值表示没有限制) 默认为8
spring.redis.lettuce.pool.max-active: 100
# 连接池最大阻塞等待时间(使用负值表示没有限制) 默认为-1
spring.redis.lettuce.pool.max-wait: -1
# 连接池中的最大空闲连接 默认为8
spring.redis.lettuce.pool.max-idle: 8
# 连接池中的最小空闲连接 默认为 0
spring.redis.lettuce.pool.min-idle: 0
# 配置inredis
spring.redis2.host: ******
spring.redis2.password: ******
spring.redis2.port: 6379
写config
@Configuration
public class RedisAutoConfiguration {
@Bean
public RedisAspect openTracingRedisAspect() {
return new RedisAspect();
}
@Bean
@ConfigurationProperties(prefix = "spring.redis.lettuce.pool")
public GenericObjectPoolConfig redisPool() {
return new GenericObjectPoolConfig<>();
}
@Bean("dataFactory1")
@Primary
public LettuceConnectionFactory dataFactory1(@Value("${spring.redis.host}") String host,
@Value("${spring.redis.port}") int port,
@Value("${spring.redis.password}") String password,
GenericObjectPoolConfig config) {
RedisStandaloneConfiguration redisDataConfig = new RedisStandaloneConfiguration(host, port);
redisDataConfig.setPassword(RedisPassword.of(password));
LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(config).build();
LettuceConnectionFactory factory = new LettuceConnectionFactory(redisDataConfig, clientConfiguration);
return factory;
}
//第二个数据源的连接工厂
@Bean("dataFactory2")
public LettuceConnectionFactory dataFactory2(@Value("${spring.redis2.host}") String host,
@Value("${spring.redis2.port}") int port,
@Value("${spring.redis2.password}") String password,
GenericObjectPoolConfig config) {
RedisStandaloneConfiguration redisDataConfig = new RedisStandaloneConfiguration(host, port);
redisDataConfig.setPassword(RedisPassword.of(password));
LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(config).build();
LettuceConnectionFactory factory = new LettuceConnectionFactory(redisDataConfig, clientConfiguration);
return factory;
}
@Bean("redisTemplate")
@Primary
public RedisTemplate<String, String> getRedisTemplate(@Qualifier("dataFactory1") RedisConnectionFactory factory) {
RedisTemplate<String, String> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
// String 的序列化
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// key采用String的序列化方式
template.setKeySerializer(stringRedisSerializer);
// hash的key也采用String的序列化方式
template.setHashKeySerializer(stringRedisSerializer);
// value序列化方式采用jackson
template.setValueSerializer(stringRedisSerializer);
// hash的value序列化方式采用jackson
template.setHashValueSerializer(stringRedisSerializer);
template.afterPropertiesSet();
return template;
}
@Bean("redisTemplate2")
public RedisTemplate<String, String> getRedisTemplate2(@Qualifier("dataFactory2") RedisConnectionFactory factory) {
RedisTemplate<String, String> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
// Json序列化配置
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);
// String 的序列化
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// key采用String的序列化方式
template.setKeySerializer(stringRedisSerializer);
// hash的key也采用String的序列化方式
template.setHashKeySerializer(stringRedisSerializer);
// value序列化方式采用jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
// hash的value序列化方式采用jackson
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
后面使用
@Autowired @Qualifier("redisTemplate2")//@Qualifier("redisTemplate1") 指定使用哪个 private RedisTemplate redisTemplate;