首先说下这几天搞SpringBoot整合Redis的感受,Springboot2.0版本和Springboot1.5版本真的是不同,由于现在最新是2.0,但是网上大部分都是1.5版本的,开始也没注意,所以一直在坑里转圈,下面是我的微总结
先说Springboot1.5.10版本的
1.所需jar包如下
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.application.properties静态资源的配置如下
# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database=1
# Redis服务器地址
spring.redis.host=192.168.3.10
# Redis服务器密码
spring.redis.password=admin
# Redis服务器连接端口
spring.redis.port=6379
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=20
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=500
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=5
# 连接超时时间(毫秒)
spring.redis.timeout=5
3.建立一个类,名为RedisDao
@Repository
public class RedisDao {
@Autowired
private StringRedisTemplate template;
public void setKey(String key,String value){
ValueOperations<String,String> ops = template.opsForValue();
ops.set(key, value);
}
public String getValue(String key){
ValueOperations<String,String> ops = this.template.opsForValue();
return ops.get(key);
}
}
4.建立一个测试类,名为testResdis
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoTest {
public static Logger logger =LoggerFactory.getLogger(DemoTest.class);
@Autowired
RedisDao redisDao;
@Test
public void testResdis(){
redisDao.setKey("name","cwh11111111111");
redisDao.setKey("age","2");
logger.info(redisDao.getValue("name"));
logger.info(redisDao.getValue("age"));
}
}
5.运行这个测试类,就会得到日志打印的结果cwh11111111111和2
再说Springboot2.0.10版本
这里和Springboot1.5.10不一样的也就是1和2,剩下类不用变,具体如下
1.所需jar包如下,这里用jedis
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- Spring Boot Redis依赖 -->
<!-- 注意:2.0必须是“spring-boot-starter-data-redis” 这个才行-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<!-- 1.5的版本默认采用的连接池技术是jedis 2.0以上版本默认连接池是lettuce, 在这里采用jedis,所以需要排除lettuce的jar-->
<exclusions>
<exclusion>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</exclusion>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 添加jedis客户端-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<!--spring2.0集成redis所需common-pool2-->
<!-- 必须加上,jedis依赖此 -->
<!-- spring boot 2.0 的操作手册有标注 大家可以去看看 地址是:https://docs.spring.io/spring-boot/docs/2.0.3.RELEASE/reference/htmlsingle/-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.5.0</version>
</dependency>
<!-- 将作为Redis对象序列化器 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
2.application.properties静态资源的配置如下
# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database=1
# Redis服务器地址
spring.redis.host=192.168.3.10
# Redis服务器密码
spring.redis.password=admin
# Redis服务器连接端口
spring.redis.port=6379
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=20
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1ms
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=500
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=5
# 连接超时时间(毫秒)
spring.redis.timeout=5ms
运行和上面结果一样
源码地址:https://github.com/charmsongo/springboot_redis
参考博客:https://blog.csdn.net/u011320740/article/details/79257423