@author Jacky wang
转载请注明出处 , https://www.jianshu.com/p/4cfedabca746
2018年09月12日,第一次补充更新.
看了网上一些Springboot
集成Redis
的很多资料,发现对Redis
的配置复杂了,自己总结了Springboot
集成Redis
的简单方式。
一. SpringBoot
集成Redis
单机版
1.1. 创建Maven
工程
搭建SpringBoot工程,包结构如下:
SpringBoot的标准包结构···
1.2. pom
文件添加依赖,properties
添加配置
pom.xml :
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dream</groupId>
<artifactId>spring-redis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- Springboot集成Redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- Springboot测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 热部署插件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
-----------------------------------------------------------------------------------------------------
application.properties:(很多其实是默认的,这里全部列出来)
#指定连接工厂使用的Database index,默认为: 0
spring.redis.database=0
#指定Redis server host,默认为: localhost
spring.redis.host=127.0.0.1
#指定Redis server的密码
#spring.redis.password=
#指定连接池最大的活跃连接数,-1表示无限,默认为8
spring.redis.pool.max-active=8
#指定连接池最大的空闲连接数,-1表示无限,默认为8
spring.redis.pool.max-idle=8
#指定当连接池耗尽时,新获取连接需要等待的最大时间,以毫秒单位,-1表示无限等待
spring.redis.pool.max-wait=-1
#指定连接池中空闲连接的最小数量,默认为0
spring.redis.pool.min-idle=2
#指定redis服务端端口,默认: 6379
spring.redis.port=6379
#指定redis server的名称
#spring.redis.sentinel.master
#指定sentinel节点,逗号分隔,格式为host:port.
#spring.redis.sentinel.nodes
#指定连接超时时间,毫秒单位,默认为0
spring.redis.timeout=0
1.3. RedisTemplate
的处理
/**
* Redis数据库操作对象
* @author wwj
*/
@Component
public class RedisClient {
@Autowired
private RedisTemplate redisTemplate;
/**
* 写入缓存
* @param key
* @param value
* @return
*/
public boolean set(final String key, Object value) {
boolean result = false;
try {
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
operations.set(key, value);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 写入缓存设置时效时间
* @param key
* @param value
* @return
*/
public boolean set(final String key, Object value, Long expireTime) {
boolean result = false;
try {
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
operations.set(key, value);
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 读取缓存
* @param key
* @return
*/
public Object get(final String key) {
Object result = null;
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
result = operations.get(key);
return result;
}
/**
* 判断缓存中是否有对应的value
* @param key
* @return
*/
public boolean exists(final String key) {
return redisTemplate.hasKey(key);
}
/**
* 删除对应的value
* @param key
*/
public void remove(final String key) {
if (exists(key)) {
redisTemplate.delete(key);
}
}
/**
* 批量删除对应的value
* @param keys
*/
public void remove(final String... keys) {
for (String key : keys) {
remove(key);
}
}
/**
* 批量删除key
* @param pattern
*/
public void removePattern(final String pattern) {
Set<Serializable> keys = redisTemplate.keys(pattern);
if (keys.size() > 0)
redisTemplate.delete(keys);
}
/**
* 哈希 添加
* @param key
* @param hashKey
* @param value
*/
public void hmSet(String key, Object hashKey, Object value){
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
hash.put(key,hashKey,value);
}
/**
* 哈希获取数据
* @param key
* @param hashKey
* @return
*/
public Object hmGet(String key, Object hashKey){
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
return hash.get(key,hashKey);
}
/**
* 列表添加
* @param k
* @param v
*/
public void lPush(String k,Object v){
ListOperations<String, Object> list = redisTemplate.opsForList();
list.rightPush(k,v);
}
/**
* 列表获取
* @param k
* @param l
* @param l1
* @return
*/
public List<Object> lRange(String k, long l, long l1){
ListOperations<String, Object> list = redisTemplate.opsForList();
return list.range(k,l,l1);
}
/**
* 集合添加
* @param key
* @param value
*/
public void add(String key,Object value){
SetOperations<String, Object> set = redisTemplate.opsForSet();
set.add(key,value);
}
/**
* 集合获取
* @param key
* @return
*/
public Set<Object> setMembers(String key){
SetOperations<String, Object> set = redisTemplate.opsForSet();
return set.members(key);
}
/**
* 有序集合添加
* @param key
* @param value
* @param scoure
*/
public void zAdd(String key,Object value,double scoure){
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
zset.add(key,value,scoure);
}
/**
* 有序集合获取
* @param key
* @param scoure
* @param scoure1
* @return
*/
public Set<Object> rangeByScore(String key,double scoure,double scoure1){
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
return zset.rangeByScore(key, scoure, scoure1);
}
}
到这里就可以使用,RedisClient
对Redis
进行操作了,上面给出的方法比较全,可以选择需要用到的进行使用。
1.4. 测试
1.4.1 Application
入口类
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
1.4.2 User实体类
为了测试,还提供了一个User实体类,因为产生了传输过程,因此这里必须要实现Serializable接口。
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private String username;
private int age;
public User() {
super();
}
public User(String username, int age) {
super();
this.username = username;
this.age = age;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [username=" + username + ", age=" + age + "]";
}
}
1.4.3 SpringbootRedisTest
测试类
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)//指定springboot的启动类
public class SpringBootRedisTest {
@Autowired
private RedisClient redisClient;
@Test
public void testSet() {
String key = "keyTest";
String val = "keyVal2";
redisClient.set(key, val);
User user = new User("jack", 24);
redisClient.set("user", user);
}
@Test
public void testGet() {
String key = "keyTest";
String key2 = "user";
String val = (String)redisClient.get(key);
User user = (User)redisClient.get(key2);
System.err.println(val);
System.err.println(user);
}
@Test
public void testRemove() {
String key = "keyTest";
String key2 = "user";
redisClient.remove(key);
redisClient.remove(key2);
}
@Test
public void testSetExpire() throws Exception {
String key = "testExpire";
String value = "hello";
long expireTime = 60L;//60秒后消失
redisClient.set(key, value, expireTime);
}
}
以上就是Springboot
集成Redis
的全部过程,实现起来是非常简单的。到这里总结一下:
1. 添加pom.xml : spring-boot-starter-data-redis的依赖
2. 配置application.properties
3. 对RedisTemplate进行部分功能的封装。
具体的测试结果就不贴出来了,这里贴一下testSetExpire()
的结果。
二. SpringBoot
集成Redis
集群版
2.1. SpringBoot
集成Redis
集群的两种方式:
2.1.1. SpringBoot内置配置支持:
步骤:
- 引入
Redis
的Maven
依赖 - 配置
application.properties
支持Redis
集群 - 引入
RedisTemplate
使用 - 测试
1. Maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2. 配置application.properties支持Redis集群
# RedisProperties
# 在群集中执行命令时要遵循的最大重定向数目。
spring.redis.cluster.max-redirects=3
# (普通集群,不使用则不用开启)以逗号分隔的“主机:端口”对列表进行引导。
spring.redis.cluster.nodes=192.168.5.122:9001,192.168.5.122:9002
spring.redis.cluster.timeout=1000\
3. 引入RedisTemplate使用
见单机版的RedisClient
4. 测试
@SpringBootTest(classes = Application.class)
@RunWith(value = SpringRunner.class)
public class TestRedis {
@Autowired
private RedisClient redisClient;
@Test
public void setTest() throws Exception {
List<User> users = new ArrayList<User>();
User user = new User("coco", "coco", "on");
User user2 = new User("jack", "jack", "on");
user.setCreateDate(new Date());
user2.setCreateDate(new Date());
users.add(user);
users.add(user2);
redisClient.set("users", FastJsonUtils.toJSONStringWithDateFormat(users));
}
@Test
public void getTest() throws Exception {
String json = (String) redisClient.get("users");
List<User> users = FastJsonUtils.toList(json, User.class);
for (User record : users) {
System.err.println(record.getUsername());
}
}
}
操作成功。
2.1.2 . 自定义JedisClusterConfig配置类支持Redis集群
步骤:
引入
Redis
的Maven
依赖自定义
Redis
的集群配置并以实体类接收编写
JedisClusterConfig
的配置类使用
JedisCluster
操作Redis
1. 引入Redis的Maven依赖(省略)
2. 自定义Redis的集群配置并以实体类接收
config/redis.properties:
# 本地环境集群
jack.redis.pool.nodes=192.168.5.122:9001,192.168.5.122:9002,192.168.5.122:9003
#redis超时时间,单位:秒
jack.redis.pool.timeout=7200
jack.redis.pool.maxAttempts=5
@Component
@ConfigurationProperties(prefix = "jack.redis.pool")
@PropertySource("classpath:/config/redis.properties")
public class RedisProperty {
private String nodes;// redis集群节点
private int timeout;// 连接超时时间
private int maxAttempts;// 重连次数
省略getter();setter();
}
3. 编写 JedisClusterConfig 的配置类
@SpringBootConfiguration
public class JedisClusterConfig {
@Autowired
private RedisProperty redisProperty;
@Bean
public JedisCluster getJedisCluster() {
String[] redisArray = redisProperty.getNodes().split(",");//获取服务器数组,不考虑空指针问题
Set<HostAndPort> nodes = new HashSet<>();
for (String ipport : redisArray) {
String[] ipports = ipport.split(":");
nodes.add(new HostAndPort(ipports[0].trim(), Integer.valueOf(ipports[1].trim())));
}
return new JedisCluster(nodes, redisProperty.getTimeout(), redisProperty.getMaxAttempts());
}
}
4. 使用配置类中的JedisCluster操作Redis
/**
* @ClassName: JedisClusterClient
* @Description:TODO(redis集群的基础操作工具)
* @author: wwj
* @date: 2018年8月27日 下午3:15:22
*/
@Component
public class JedisClusterClient {
@Autowired
private JedisCluster jedisCluster;
@Autowired
private RedisProperty redisProperty;
/**
* 写入缓存
*
* @param key
* @param value
* @return
*/
public boolean set(final String key, Object value) {
boolean result = false;
try {
if (value instanceof String) {
jedisCluster.set(key, value.toString());
} else {
jedisCluster.set(key, FastJsonUtils.toJSONStringWithDateFormat(value));
}
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 写入缓存设置时效时间
*
* @param key
* @param value
* @return
*/
public boolean set(final String key, Object value, Long expireTime) {
boolean result = false;
try {
if (value instanceof String) {
jedisCluster.setex(key, Integer.valueOf(expireTime + ""), value.toString());
} else {
jedisCluster.setex(key, Integer.valueOf(expireTime + ""), FastJsonUtils.toJSONStringWithDateFormat(value));
}
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 设置缓存,并且由配置文件指定过期时间
*
* @param key
* @param value
*/
public void setWithExpireTime(String key, String value) {
try {
int expireSeconds = redisProperty.getTimeout();
set(key, value, Long.valueOf(expireSeconds));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 读取缓存
*
* @param key
* @return
*/
public String get(final String key) {
return jedisCluster.get(key);
}
/**
* 判断缓存中是否有对应的value
*
* @param key
* @return
*/
public boolean exists(final String key) {
return jedisCluster.exists(key);
}
/**
* 删除对应的value
*
* @param key
*/
public void remove(final String key) {
if (exists(key)) {
jedisCluster.del(key);
}
}
/**
* 批量删除对应的value
*
* @param keys
*/
public void remove(final String... keys) {
for (String key : keys) {
remove(key);
}
}
}