在实际的项目中,可能一个项目需要操作多个不同redis的数据,那么我们就需要做相应的配置
以下是基于springboot
首先在我们项目的 application.proterties中添加如下配置。有几个就写几个。
注意这里的命名: spring.redis开头是springboot 自动配置默认的。
spring.redisbatch 是自己定制的
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=123
spring.redis.lettuce.pool.max-active=50
spring.redis.lettuce.pool.max-idle=50
spring.redis.lettuce.pool.min-idle=1
spring.redis.lettuce.pool.max-wait=50000
spring.redis.timeout=60000
spring.redisbatch.host=127.0.0.2
spring.redisbatch.port=6379
spring.redisbatch.password=123
spring.redisbatch.lettuce.pool.max-active=50
spring.redisbatch.lettuce.pool.max-idle=50
spring.redisbatch.lettuce.pool.min-idle=1
spring.redisbatch.lettuce.pool.max-wait=50000
spring.redisbatch.timeout=60000
然后新增一个SpringBatchConfig.java
代码如下:
@Configuration
public class RedisBatchConfig {
@Value("${spring.redisbatch.host}")
private String host;
@Value("${spring.redisbatch.port}")
private Integer port;
@Value("${spring.redisbatch.password}")
private String password;
@Value("${spring.redisbatch.lettuce.pool.max-active}")
private String maxActive;
@Value("${spring.redisbatch.lettuce.pool.max-idle}")
private String maxIdle;
@Value("${spring.redisbatch.lettuce.pool.min-idle}")
private String minIdle;
@Value("${spring.redisbatch.lettuce.pool.max-wait}")
private String maxWait;
@Value("${spring.redisbatch.timeout}")
private String timeOut;
@Bean("batchConnectionFactory")
public LettuceConnectionFactory batchConnectionFactory() {
RedisStandaloneConfiguration redisStandaloneConfiguration =
new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setHostName(host);
redisStandaloneConfiguration.setDatabase(0);
redisStandaloneConfiguration.setPassword(password);
redisStandaloneConfiguration.setPort(port);
GenericObjectPoolConfig genericObjectPoolConfig =
new GenericObjectPoolConfig();
genericObjectPoolConfig.setMaxIdle(Integer.parseInt(maxIdle));
genericObjectPoolConfig.setMinIdle(Integer.parseInt(minIdle));
genericObjectPoolConfig.setMaxTotal(Integer.parseInt(maxActive));
genericObjectPoolConfig.setMaxWaitMillis(Integer.parseInt(maxWait));
LettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilder
builder = LettucePoolingClientConfiguration.builder().
commandTimeout(Duration.ofMillis(Integer.parseInt(timeOut)));
builder.poolConfig(genericObjectPoolConfig);
LettuceClientConfiguration lettuceClientConfiguration = builder.build();
//根据配置和客户端配置创建连接
LettuceConnectionFactory lettuceConnectionFactory = new
LettuceConnectionFactory(redisStandaloneConfiguration,lettuceClientConfiguration);
lettuceConnectionFactory .afterPropertiesSet();
return lettuceConnectionFactory;
}
@Bean("batchRedisTemplate")
public RedisTemplate<String, Object> batchRedisTemplate(@Qualifier("batchConnectionFactory") LettuceConnectionFactory batchConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
redisTemplate.setConnectionFactory(batchConnectionFactory);
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
jackson2JsonRedisSerializer.setObjectMapper(om);
RedisSerializer<?> stringSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashKeySerializer(stringSerializer);
return redisTemplate;
}
}
也可以使用如下注解将application.properties中的数据放到一个配置类中,从这个配置类中取我们定制的数据。
@Component
@ConfigurationProperties("spring.redisbatch")
接下来我们就可以使用我们定制的这个bean了。
@Resource(name = "batchRedisTemplate")
private RedisTemplate<String, Object> batchRedisTemplate;
public void demo() {
batchRedisTemplate.opsForValue().set("yancun","yancun");
}
springboot框架采用的是自动配置。
它会读取所有项目 META-INF/spring.factories 的这个文件。
这个文件有个key 和value 。value就是一个或多个JAVA配置类。
比如RedisAutoConfiguration 如下图:
看下RedisAutoConfiguration的代码:
package org.springframework.boot.autoconfigure.data.redis;
// 省略 import 行
/**
* EnableAutoConfiguration Auto-configuration for Spring Data's Redis support.
*
*/
@Configuration
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
@Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })
public class RedisAutoConfiguration {
// 定义 bean RedisTemplate redisTemplate
@Bean
// 仅在该 bean 不存在的情况下才定义
@ConditionalOnMissingBean(name = "redisTemplate")
public RedisTemplate<Object, Object> redisTemplate(
RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
// 定义 bean StringRedisTemplate stringRedisTemplate
@Bean
// 仅在该 bean 不存在的情况下才定义
@ConditionalOnMissingBean
public StringRedisTemplate stringRedisTemplate(
RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
可以看到他读取的是RedisProperties中的配置数据。默认是spring.redis
这也是我们只配置了一个Java Config类的原因,因为springboot 已经帮我们自动配置了一个。