搭建SpringBoot(三) 集成Redis,消息队列
- 引入依赖
- 配置文件加入配置
- 注入
- 注入RedisTemplate、和消息监听
- redisService工具类和消息接收类
- redisService工具类
- 消息接收类
- 测试
- 存取数据测试
引入依赖
这里springboot用的版本是2.2.0.RELEASE 有的版本需要加入commons-pool2的包 这里不需要
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
配置文件加入配置
在配置文件中加入redis的连接配置
# redis相关配置
redis:
database: 1
host: 127.0.0.1
password: jijilan..
port: 6380
timeout: 6000ms # 连接超时时长(毫秒)
注入
注入RedisTemplate、和消息监听
package cn.jijl.mist.common.config;
import cn.jijl.mist.common.result.SysConstant;
import cn.jijl.mist.modules.redis.ReceiveMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.util.concurrent.CountDownLatch;
/**
* @Author: jijl
* @Description: redis配置类
* @Date: 2019/6/27 15:05
**/
@Slf4j
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
// 配置redisTemplate
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.afterPropertiesSet();
redisTemplate.opsForValue().get(SysConstant.PROJECT_NAME);
log.info("-------------------------------------------------");
log.info("---------------local redis success---------------");
log.info("-------------------------------------------------");
return redisTemplate;
}
/**
* 配置监听器1
*
* @return
*/
@Bean
public MessageListenerAdapter listenerAdapter(ReceiveMessage receiveMessage) {
MessageListenerAdapter messageListenerAdapter = new MessageListenerAdapter(receiveMessage, "receiveMessage");
messageListenerAdapter.setSerializer(new GenericJackson2JsonRedisSerializer());
return messageListenerAdapter;
}
/**
* 配置监听器2
*
* @return
*/
@Bean
public MessageListenerAdapter listenerAdapter2(ReceiveMessage receiveMessage) {
MessageListenerAdapter messageListenerAdapter = new MessageListenerAdapter(receiveMessage, "receiveMessage2");
messageListenerAdapter.setSerializer(new GenericJackson2JsonRedisSerializer());
return messageListenerAdapter;
}
/**
* 初始化监听器
*
* @param connectionFactory
* @param listenerAdapter
* @param listenerAdapter2
* @return
*/
@Bean
public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter,
MessageListenerAdapter listenerAdapter2) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.addMessageListener(listenerAdapter, new PatternTopic(SysConstant.Redis.MESSAGE1));
container.addMessageListener(listenerAdapter2, new PatternTopic(SysConstant.Redis.MESSAGE2));
return container;
}
/**
* 消息监听者1
*
* @return
*/
@Bean
public ReceiveMessage receiveMessage(CountDownLatch countDownLatch) {
return new ReceiveMessage(countDownLatch);
}
/**
* 消息监听者2
*
* @return
*/
@Bean
public ReceiveMessage receiveMessage2(CountDownLatch countDownLatch) {
return new ReceiveMessage(countDownLatch);
}
@Bean
public CountDownLatch countDownLatch() {
return new CountDownLatch(1);
}
}
redisService工具类和消息接收类
redisService工具类
package cn.jijl.mist.modules.redis;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisConnectionUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* @Author: jijl
* @Description: redis工具类
* @Date: 2019/7/2 16:53
**/
@Slf4j
@Component
public class RedisService {
@Autowired
private RedisTemplate redisTemplate;
public void redisColse() {
RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());
}
//=============================common============================
/**
* 指定缓存失效时间
*
* @param key 键
* @param time 时间(秒)
* @return
*/
public boolean expire(String key, long time) {
try {
if (time > 0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 根据key 获取过期时间
*
* @param key 键 不能为null
* @return 时间(秒) 返回0代表为永久有效
*/
public long getExpire(String key) {
long timeOut = redisTemplate.getExpire(key, TimeUnit.SECONDS);
redisColse();
return timeOut;
}
/**
* 判断key是否存在
*
* @param key 键
* @return true 存在 false不存在
*/
public boolean hasKey(String key) {
boolean flag = false;
try {
flag = redisTemplate.hasKey(key);
} catch (Exception e) {
e.printStackTrace();
flag = false;
} finally {
redisColse();
return flag;
}
}
/**
* 删除缓存
*
* @param key 可以传一个值 或多个
*/
@SuppressWarnings("unchecked")
public void del(String... key) {
if (key != null && key.length > 0) {
if (key.length == 1) {
redisTemplate.delete(key[0]);
} else {
redisTemplate.delete(CollectionUtils.arrayToList(key));
}
}
redisColse();
}
//============================String=============================
/**
* 普通缓存获取
*
* @param key 键
* @return 值
*/
public Object get(String key) {
Object o = redisTemplate.opsForValue().get(key);
redisColse();
return o;
}
/**
* 普通缓存放入
*
* @param key 键
* @param value 值
* @return true成功 false失败
*/
public boolean set(String key, Object value) {
boolean flag = false;
try {
redisTemplate.opsForValue().set(key, value);
flag = true;
} catch (Exception e) {
e.printStackTrace();
flag = false;
} finally {
redisColse();
return flag;
}
}
/**
* 普通缓存放入并设置时间
*
* @param key 键
* @param value 值
* @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期
* @return true成功 false 失败
*/
public boolean set(String key, Object value, long time) {
boolean flag = false;
try {
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
set(key, value);
}
flag = true;
} catch (Exception e) {
e.printStackTrace();
flag = false;
} finally {
redisColse();
return flag;
}
}
/**
* 递增
*
* @param key 键
* @param delta 要增加几(大于0)
* @return
*/
public long incr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递增因子必须大于0");
}
long timeOut = redisTemplate.opsForValue().increment(key, delta);
redisColse();
return timeOut;
}
/**
* 递减
*
* @param key 键
* @param delta 要减少几(小于0)
* @return
*/
public long decr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递减因子必须大于0");
}
long timeOut = redisTemplate.opsForValue().increment(key, -delta);
return timeOut;
}
//================================Map=================================
/**
* HashGet
*
* @param key 键 不能为null
* @param item 项 不能为null
* @return 值
*/
public Object hget(String key, String item) {
Object o = redisTemplate.opsForHash().get(key, item);
redisColse();
return o;
}
/**
* 获取hashKey对应的所有键值
*
* @param key 键
* @return 对应的多个键值
*/
public Map<Object, Object> hmget(String key) {
Map<Object, Object> map = redisTemplate.opsForHash().entries(key);
redisColse();
return map;
}
/**
* HashSet
*
* @param key 键
* @param map 对应多个键值
* @return true 成功 false 失败
*/
public boolean hmset(String key, Map<String, Object> map) {
boolean flag = false;
try {
redisTemplate.opsForHash().putAll(key, map);
flag = true;
} catch (Exception e) {
e.printStackTrace();
flag = false;
} finally {
redisColse();
return flag;
}
}
/**
* HashSet 并设置时间
*
* @param key 键
* @param map 对应多个键值
* @param time 时间(秒)
* @return true成功 false失败
*/
public boolean hmset(String key, Map<String, Object> map, long time) {
boolean flag = false;
try {
redisTemplate.opsForHash().putAll(key, map);
if (time > 0) {
expire(key, time);
}
flag = true;
} catch (Exception e) {
e.printStackTrace();
flag = false;
} finally {
redisColse();
return flag;
}
}
/**
* 向一张hash表中放入数据,如果不存在将创建
*
* @param key 键
* @param item 项
* @param value 值
* @return true 成功 false失败
*/
public boolean hset(String key, String item, Object value) {
boolean flag = false;
try {
redisTemplate.opsForHash().put(key, item, value);
flag = true;
} catch (Exception e) {
e.printStackTrace();
flag = false;
} finally {
redisColse();
return flag;
}
}
/**
* 向一张hash表中放入数据,如果不存在将创建
*
* @param key 键
* @param item 项
* @param value 值
* @param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
* @return true 成功 false失败
*/
public boolean hset(String key, String item, Object value, long time) {
boolean flag = false;
try {
redisTemplate.opsForHash().put(key, item, value);
if (time > 0) {
expire(key, time);
}
flag = true;
} catch (Exception e) {
e.printStackTrace();
flag = false;
} finally {
redisColse();
return flag;
}
}
/**
* 删除hash表中的值
*
* @param key 键 不能为null
* @param item 项 可以使多个 不能为null
*/
public void hdel(String key, Object... item) {
redisTemplate.opsForHash().delete(key, item);
redisColse();
}
/**
* 判断hash表中是否有该项的值
*
* @param key 键 不能为null
* @param item 项 不能为null
* @return true 存在 false不存在
*/
public boolean hHasKey(String key, String item) {
boolean flag = redisTemplate.opsForHash().hasKey(key, item);
redisColse();
return flag;
}
/**
* hash递增 如果不存在,就会创建一个 并把新增后的值返回
*
* @param key 键
* @param item 项
* @param by 要增加几(大于0)
* @return
*/
public double hincr(String key, String item, double by) {
double num = redisTemplate.opsForHash().increment(key, item, by);
redisColse();
return num;
}
/**
* hash递减
*
* @param key 键
* @param item 项
* @param by 要减少记(小于0)
* @return
*/
public double hdecr(String key, String item, double by) {
double num = redisTemplate.opsForHash().increment(key, item, -by);
redisColse();
return num;
}
//============================set=============================
/**
* 根据key获取Set中的所有值
*
* @param key 键
* @return
*/
public Set<Object> sGet(String key) {
try {
Set<Object> set = redisTemplate.opsForSet().members(key);
return set;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
redisColse();
}
}
/**
* 根据value从一个set中查询,是否存在
*
* @param key 键
* @param value 值
* @return true 存在 false不存在
*/
public boolean sHasKey(String key, Object value) {
boolean flag = false;
try {
flag = redisTemplate.opsForSet().isMember(key, value);
return flag;
} catch (Exception e) {
e.printStackTrace();
flag = false;
} finally {
redisColse();
return flag;
}
}
/**
* 将数据放入set缓存
*
* @param key 键
* @param values 值 可以是多个
* @return 成功个数
*/
public long sSet(String key, Object... values) {
try {
long num = redisTemplate.opsForSet().add(key, values);
return num;
} catch (Exception e) {
e.printStackTrace();
return 0;
} finally {
redisColse();
}
}
/**
* 将set数据放入缓存
*
* @param key 键
* @param time 时间(秒)
* @param values 值 可以是多个
* @return 成功个数
*/
public long sSetAndTime(String key, long time, Object... values) {
try {
Long count = redisTemplate.opsForSet().add(key, values);
if (time > 0) expire(key, time);
return count;
} catch (Exception e) {
e.printStackTrace();
return 0;
} finally {
redisColse();
}
}
/**
* 获取set缓存的长度
*
* @param key 键
* @return
*/
public long sGetSetSize(String key) {
try {
long num = redisTemplate.opsForSet().size(key);
return num;
} catch (Exception e) {
e.printStackTrace();
return 0;
} finally {
redisColse();
}
}
/**
* 移除值为value的
*
* @param key 键
* @param values 值 可以是多个
* @return 移除的个数
*/
public long setRemove(String key, Object... values) {
try {
Long count = redisTemplate.opsForSet().remove(key, values);
return count;
} catch (Exception e) {
e.printStackTrace();
return 0;
} finally {
redisColse();
}
}
//===============================list=================================
/**
* 获取list缓存的内容
*
* @param key 键
* @param start 开始
* @param end 结束 0 到 -1代表所有值
* @return
*/
public List<Object> lGet(String key, long start, long end) {
try {
List<Object> list = redisTemplate.opsForList().range(key, start, end);
return list;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
redisColse();
}
}
/**
* 获取list缓存的长度
*
* @param key 键
* @return
*/
public long lGetListSize(String key) {
try {
long num = redisTemplate.opsForList().size(key);
return num;
} catch (Exception e) {
e.printStackTrace();
return 0;
} finally {
redisColse();
}
}
/**
* 通过索引 获取list中的值
*
* @param key 键
* @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
* @return
*/
public Object lGetIndex(String key, long index) {
try {
Object o = redisTemplate.opsForList().index(key, index);
return o;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
redisColse();
}
}
/**
* 将list放入缓存
*
* @param key 键
* @param value 值
* @return
*/
public boolean lSet(String key, Object value) {
try {
redisTemplate.opsForList().rightPush(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
redisColse();
}
}
/**
* 将list放入缓存
*
* @param key 键
* @param value 值
* @param time 时间(秒)
* @return
*/
public boolean lSet(String key, Object value, long time) {
try {
redisTemplate.opsForList().rightPush(key, value);
if (time > 0) expire(key, time);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
redisColse();
}
}
/**
* 将list放入缓存
*
* @param key 键
* @param value 值
* @return
*/
public boolean lSet(String key, List<Object> value) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
redisColse();
}
}
/**
* 将list放入缓存
*
* @param key 键
* @param value 值
* @param time 时间(秒)
* @return
*/
public boolean lSet(String key, List<Object> value, long time) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
if (time > 0) expire(key, time);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
redisColse();
}
}
/**
* 根据索引修改list中的某条数据
*
* @param key 键
* @param index 索引
* @param value 值
* @return
*/
public boolean lUpdateIndex(String key, long index, Object value) {
try {
redisTemplate.opsForList().set(key, index, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
redisColse();
}
}
/**
* 移除N个值为value
*
* @param key 键
* @param count 移除多少个
* @param value 值
* @return 移除的个数
*/
public long lRemove(String key, long count, Object value) {
try {
Long remove = redisTemplate.opsForList().remove(key, count, value);
return remove;
} catch (Exception e) {
e.printStackTrace();
return 0;
} finally {
redisColse();
}
}
//===============================消息部分=================================
/**
* 发送消息
*
* @param channel 通道
* @param message 消息
*/
public void sendMessage(String channel, Serializable message) {
redisTemplate.convertAndSend(channel, message);
}
}
消息接收类
package cn.jijl.mist.modules.redis;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.Serializable;
import java.util.concurrent.CountDownLatch;
/**
* @Author jijl
* @Description: 接收消息
* @Date 16:42 2019/10/28
* @Param
* @return
**/
@Slf4j
public class ReceiveMessage{
private CountDownLatch latch;
@Autowired
public ReceiveMessage(CountDownLatch countDownLatch) {
this.latch = countDownLatch;
}
public void receiveMessage(Serializable message) {
log.info("message2接收到消息了:{}", message);
try {
Thread.sleep(2000);
}catch (Exception e){
}
log.info("这是两秒后打印-----");
}
public void receiveMessage2(Serializable message) {
log.info("message2接收到消息了:{}", message);
}
}
测试
存取数据测试
package cn.jijl.mist.modules.controller;
import cn.jijl.mist.common.result.ResultView;
import cn.jijl.mist.common.result.SysConstant;
import cn.jijl.mist.modules.redis.RedisService;
import org.apache.catalina.Manager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
/**
* @Author jijl
* @Description: 测试controller
* @Date 15:34 2019/10/24
**/
@RequestMapping("/redis")
@RestController
public class RedisController {
@Autowired
private RedisService redisService;
@GetMapping("/testSet")
public ResultView testSet() {
redisService.set("test", "这是一段测试数据");
return ResultView.ok();
}
@GetMapping("/testGet")
public ResultView testGet() {
return ResultView.ok(redisService.get("test"));
}
@GetMapping("/testMassage")
public ResultView testMassage() {
redisService.sendMessage(SysConstant.Redis.MESSAGE1, "一段消息");
return ResultView.ok();
}
}
- 插入测试
- 使用redis客户端工具查看已存数据库,可以多测试几种类型
- 读取测试
- 消息测试
返回成功后 控制台才打印出后一句,成功
集成时遇到了很多问题,参考了很多资料