项目需要使用Redis来做缓存,研究了一下如何将其与Spring Boot整合。网上的demo要么就是太过于庞大,要么就是版本过于陈旧,配置时候会有各种坑。因此自己在踩过了各种坑之后,写一个小demo来记录下:
1.项目结构:
2.pom的依赖配置:
本人使用的Spring Boot是2.0.4.RELEASE版本的:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
添加redis的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
添加common-pool2的依赖:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.0</version>
</dependency>
我在这里尝试过不加version,让springboot自己选择最合适的版本,结果会报错。参考别人的例子,选择2.0版本,没有问题。
3.修改application.yml:
这里的host填写自己redis的IP,timeout别设置为0,否则运行会报错。
redis 单机配置
spring:
redis:
host:
port: 6379
lettuce:
pool:
max-wait: 100000
max-idle: 10
max-active: 100
timeout: 5000
redis 集群配置
redis:
database: 0
# 集群设置 begin
cluster:
nodes:
- 10.217.17.70:7000
- 10.217.17.74:7000
- 10.217.17.75:7000
max-redirects: 3 # 获取失败 最大重定向次数
#集群设置 end
#单节点 begin
# host: 10.217.17.74
# port: 7000
#单节点 end
lettuce:
pool:
max-wait: 100000
max-idle: 10
max-active: 100
timeout: 5000
3.编写dao层 :
package com.hg.redis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;
import java.util.concurrent.TimeUnit;
/**
* @Description:
* @Author: jiangfan
* @CreateTime 2018/9/27 上午10:14
*/
@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.修改测试启动类:
package com.hg.redis;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisApplicationTests {
@Test
public void contextLoads() {
}
}
5.编写一个测试类:
package com.hg.redis;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @Description:
* @Author: jiangfan
* @CreateTime 2018/9/27 上午10:26
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootRedisApplicationTests {
public static Logger logger = LoggerFactory.getLogger(SpringbootRedisApplicationTests.class);
@Test
public void contextLoads(){}
@Autowired
RedisDao redisDao;
@Test
public void testRedis() {
redisDao.setKey("name","jerry");
redisDao.setKey("age","11");
logger.info(redisDao.getValue("name"));
logger.info(redisDao.getValue("age"));
}
}
6.运行测试类: