Redis 5.0版本Stream介绍,可以先看这篇文章:
一、pom.xml
<!--redis jedis依赖-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.2.0</version>
</dependency>
二、JedisUtil.java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.StreamEntryID;
import redis.clients.jedis.params.XReadGroupParams;
import redis.clients.jedis.params.XReadParams;
import redis.clients.jedis.resps.StreamEntry;
import java.util.*;
/**
* <Redis工具类>
*
* @author zxx
* @date 2022-03-22 17:06
* @since 1.0.0
*/
public enum JedisUtil {
INSTANCE;
private Logger logger = LoggerFactory.getLogger(JedisUtil.class);
private JedisCluster jedis = null;
/**
* Redis集群节点
*/
private final String REDIS_CLUSTER_QUEUE_NODES =
ConfigManager.getInstance().getString("pps.redis.cluster.nodes");
/**
* Redis队列默认数据流
*/
private final String DEFAULT_STREAM =
ConfigManager.getInstance().getString("pps.redis.cluster.queue.default.stream");
/**
* Redis默认数据流消费者组
*/
private final String DEFAULT_STREAM_GROUP =
ConfigManager.getInstance().getString("pps.redis.cluster.queue.default.stream.group");
/**
* Redis默认数据流消费者组消费者名称
*/
private final String DEFAULT_STREAM_GROUP_CONSUMER =
ConfigManager.getInstance().getString("pps.redis.cluster.queue.default.stream.group.consumer");
/**
* Redis队列默认数据流content-key
*/
private final String DEFAULT_STREAM_KEY =
ConfigManager.getInstance().getString("pps.redis.cluster.queue.default.stream.key");
/**
* 当前使用的stream
*/
private String curStream;
/**
* 当前使用的streamGroup
*/
private String curStreamGroup;
/**
* 当前使用的streamGroupConsumer
*/
private String curStreamGroupConsumer;
/**
* 当前使用的stream中数据的默认Key
*/
private String curStreamKey;
/**
* redis初始化
*/
public void jedisInit() {
try {
// 连接redis集群
Set<HostAndPort> jedisClusterNodes = new HashSet<>();
for (String nodeStr : REDIS_CLUSTER_QUEUE_NODES.split(",")) {
String ip = nodeStr.split(":")[0];
int port = Integer.parseInt(nodeStr.split(":")[1]);
jedisClusterNodes.add((new HostAndPort(ip, port)));
}
// 使用默认参数
jedis = new JedisCluster(jedisClusterNodes);
// 创建默认stream、默认消费者group
curStream = DEFAULT_STREAM;
curStreamGroup = DEFAULT_STREAM_GROUP;
curStreamGroupConsumer = DEFAULT_STREAM_GROUP_CONSUMER + ":" + CommonUtil.getCurServerIp();
curStreamKey = DEFAULT_STREAM_KEY;
xgroupCreate(curStream, curStreamGroup, Boolean.TRUE);
} catch (Exception e) {
if (!e.getMessage().contains("Group name already exists")) {
logger.error("【系统启动】Redis连接...失败!");
e.printStackTrace();
System.exit(1);
}
}
logger.info("【Redis-Stream】连接成功!"
+ "\n【参数】curStream:" + curStream
+ "\n【参数】curStreamGroup:" + curStreamGroup
+ "\n【参数】curStreamGroupConsumer:" + curStreamGroupConsumer
+ "\n【参数】curStreamKey:" + curStreamKey
);
}
/**
* 返回Jedis集群对象
*
* @return jedis
*/
public JedisCluster getJedisClient() {
if (jedis == null) {
jedisInit();
}
return jedis;
}
// -------------------- PPS默认工具 --------------------
public long xlenDefault() {
return this.xlen(curStream);
}
public StreamEntryID xaddDefault(String value) {
Map<String, String> content = new HashMap<>(16);
content.put(curStreamKey, value);
return this.xadd(curStream, StreamEntryID.NEW_ENTRY, content);
}
public StreamEntry xreadGroupDefaultOneBlock() {
XReadGroupParams xReadGroupParams = new XReadGroupParams();
xReadGroupParams.block(0);
xReadGroupParams.count(1);
xReadGroupParams.noAck();
Map<String, StreamEntryID> streams = new HashMap<>();
streams.put(curStream, StreamEntryID.UNRECEIVED_ENTRY);
logger.info("【Redis-Stream】Consumer:" + curStreamGroupConsumer + "阻塞读中...");
List<StreamEntry> retList = jedis.xreadGroup(curStreamGroup, curStreamGroupConsumer, xReadGroupParams, streams).get(0).getValue();
if (retList.size() != 0) {
return retList.get(0);
} else {
return null;
}
}
public long xackDefault(StreamEntryID... ids) {
return this.xack(curStream, curStreamGroup, ids);
}
public long xdelDefault(StreamEntryID... ids) {
return this.xdel(curStream, ids);
}
// -------------------- PPS默认工具 --------------------
// -------------------- Jedis工具 --------------------
public StreamEntryID xadd(String key, StreamEntryID streamEntryId, Map<String, String> content) {
return jedis.xadd(key, streamEntryId, content);
}
public long xlen(String key) {
return jedis.xlen(key);
}
public String xgroupCreate(String key, String group, Boolean makeStream) {
return jedis.xgroupCreate(key, group, null, makeStream);
}
public Map.Entry<String, List<StreamEntry>> xreadOne(XReadParams xReadParams, Map<String, StreamEntryID> streams) {
XReadGroupParams xReadGroupParams = new XReadGroupParams();
xReadGroupParams.count(1);
return this.xread(xReadParams, streams).get(0);
}
public List<Map.Entry<String, List<StreamEntry>>> xread(XReadParams xReadParams, Map<String, StreamEntryID> streams) {
return jedis.xread(xReadParams, streams);
}
public List<Map.Entry<String, List<StreamEntry>>> xreadGroup(String group, String consumer, XReadGroupParams xReadGroupParams, Map<String, StreamEntryID> streams) {
return jedis.xreadGroup(group, consumer, xReadGroupParams, streams);
}
public List<StreamEntry> xrange(String key, StreamEntryID start, StreamEntryID end, int count) {
return jedis.xrange(key, start, end, count);
}
public List<StreamEntry> xrevrange(String key, StreamEntryID start, StreamEntryID end, int count) {
return jedis.xrevrange(key, start, end, count);
}
public long xack(String key, String group, StreamEntryID... ids) {
return jedis.xack(key, group, ids);
}
public long xdel(String key, StreamEntryID... ids) {
return jedis.xdel(key, ids);
}
// -------------------- Jedis工具 --------------------
public String getCurStream() {
return curStream;
}
public String getCurStreamGroup() {
return curStreamGroup;
}
public String getCurStreamGroupConsumer() {
return curStreamGroupConsumer;
}
public String getCurStreamKey() {
return curStreamKey;
}
}
三、配置文件信息
# 磁盘IO操作存储接收 -> Redis存储接收数据 全局开关
pps.redis.switch=true
# Redis集群
pps.redis.cluster.nodes=172.22.0.1:6379,172.22.0.2:6379,172.22.0.3:6379,172.22.0.4:6379,172.22.0.5:6379,172.22.0.6:6379
# Redis队列默认数据流
pps.redis.cluster.queue.default.stream=PPS_DEFAULT_STREAM
# Redis队列默认数据流消费者组
pps.redis.cluster.queue.default.stream.group=PPS_DEFAULT_STREAM_GROUP
# Redis队列默认数据流消费者组消费者名称
pps.redis.cluster.queue.default.stream.group.consumer=PPS_DEFAULT_STREAM_GROUP_CONSUMER
# Redis队列默认数据流content-key
pps.redis.cluster.queue.default.stream.key=PPS_DEFAULT_STREAM_KEY
四、使用例子
// 发送数据到redis stream
StreamEntryID streamEntryId = JedisUtil.INSTANCE.xaddDefault(idx);
// 查看队列数据量
long curLen = JedisUtil.INSTANCE.xlenDefault();
// 阻塞读数据
StreamEntry streamEntry = JedisUtil.INSTANCE.xreadGroupDefaultOneBlock();
StreamEntryID streamEntryID = streamEntry.getID();
String streamData = streamEntry.getFields().get(JedisUtil.INSTANCE.getCurStreamKey());
// Redis-stream ACK回复
long ackRet = JedisUtil.INSTANCE.xackDefault(streamEntryID);
// Redis-stream DEL删除
long delRet = JedisUtil.INSTANCE.xdelDefault(streamEntryID);