spring boot添加的依赖忽略,在此记录一下踩过的坑,使用spring boot 2.X版本。
1、spring boot 关于redis配置如下,使用.yml
spring:
redis:
database: 0
port: 6379
timeout: 5000
host: 192.168.0.66
pool:
max-active: 8
max-idle: 10
min-idle: 0
max-wait: -1
对应maven只需要添加
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
此外不需要添加其他任何配置文件,在项目中可直接使用 RedisTemplate 和 StringRedisTemplate 进行接下来的操作,需要注意RedisTemplate 和 StringRedisTemplate维护的是两套缓存
RedisTemplate redisTemplate1 = ApplicationBeanFactory.getBean("stringRedisTemplate");
//或者
RedisTemplate redisTemplate1 = ApplicationBeanFactory.getBean("redisTemplate");
//或者
@Resource
RedisTemplate redisTemplate;
//或者
@Resource
StringRedisTemplate stringRedisTemplate;
以上配置是spring boot选择配置redis pool ,之前版本使用 jedis 作为pool,最新的使用 lettuce
以下是使用lettuce
spring:
redis:
database: 0
port: 6379
timeout: 5000
host: 192.168.0.66
lettuce:
pool:
max-idle: 10
min-idle: 0
max-wait: -1
max-active: 8
需要添加下面依赖
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
以下是使用 jedis
spring:
redis:
database: 0
port: 6379
timeout: 5000
host: 192.168.0.66
jedis:
pool:
max-idle: 10
min-idle: 0
max-wait: -1
max-active: 8
2、如果是项目中需要分别使用两个服务器的缓存redis,如何配置
1、开始的思路是,上面已经配置了一个,然后在简单配置一个
@SuppressWarnings("unchecked")
@Configuration
public class RedisConfig {
@Bean
private RedisConnectionFactory getRedisConnectionFactory() {
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(new JedisPoolConfig());
jedisConnectionFactory.setHostName("192.168.0.66");
jedisConnectionFactory.setPort(6379);
return jedisConnectionFactory;
}
@Bean
public RedisTemplate getRedisTemplate(){
return new StringRedisTemplate(getRedisConnectionFactory());
}
}
在项目中进行测试发现两个获取的是一个服务器的redis,开始怀疑可能是getRedisTemplate()方法名的原因,与系统配置的bean名称一样,将系统配置的bean替换了,后来替换其他的方法名依然无效,无法获取两个redis。
2、需要同时配置两个redis
spring:
redis:
database: 0
port: 6379
timeout: 5000
host-name: 192.168.0.66
jedis:
pool:
max-active: 8
max-idle: 10
min-idle: 0
max-wait: -1
redis-77:
database: 0
host-name: 192.168.0.77
port: 6379
timeout: 5000
jedis:
pool:
max-active: 8
max-idle: 10
min-idle: 0
max-wait: -1
配置文件如下:
@SuppressWarnings("unchecked")
@Configuration
public class RedisConfig {
private static Logger logger = LoggerFactory.getLogger(RedisConfig.class);
@Bean
@ConfigurationProperties(prefix = "redis-77.jedis.pool")
public JedisPoolConfig getRedisConfig() {
return new JedisPoolConfig();
}
@Bean
@ConfigurationProperties(prefix = "redis-77")
public JedisConnectionFactory getConnectionFactory() {
return new JedisConnectionFactory(getRedisConfig());
}
@Bean
@ConfigurationProperties(prefix = "spring.redis")
@Primary
public JedisConnectionFactory getConnectionFactory1() {
return new JedisConnectionFactory(getRedisConfig1());
}
@Bean
@ConfigurationProperties(prefix = "spring.redis.jedis.pool")
public JedisPoolConfig getRedisConfig1() {
return new JedisPoolConfig();
}
@Bean("redisTemplate")
public RedisTemplate getRedisTemplate() {
return new StringRedisTemplate(getConnectionFactory());
}
@Bean("redisTemplate1")
public RedisTemplate getRedisTemplate1() {
return new StringRedisTemplate(getConnectionFactory1());
}
}
需要注意的一点
1、@ConfigurationProperties(prefix = "redis-77") 注解的使用
2、 host-name: 192.168.0.66 该配置,之前配置单个源时是使用 host,如果使用上面方法需要修改为host-name。
3、需要给一个添加@Primary注解
4、如果使用@value进行配置属性,无所谓,只要能够正确获取值即可。
测试
public class Test {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(DemoApplication.class, args);
ApplicationBeanFactory.setApplicationContext(context);
RedisTemplate redisTemplate1 = ApplicationBeanFactory.getBean("redisTemplate1");
System.out.println("====>" + redisTemplate1.opsForValue().get("1"));
System.out.println();
RedisTemplate redisTemplate = ApplicationBeanFactory.getBean("redisTemplate");
System.out.println("====>" + redisTemplate.opsForValue().get("1"));
}
}
结果:
分别从不同服务器的缓存中获取数据。