常见的消息队列有:RabbitMQ,RocketMQ,Kafka等。Zookeeper作为一个分布式的小文件管理系统,同样能实现简单的队列功能。Zookeeper不适合存储大数据量存储,官方并不推荐作为队列使用,但由于实现简单,集群搭建较为便利,因此在一些吞吐量不高的小型系统中还是比较好用的。
本案例设立一个订单生产者,两个订单消费者,订单生产者将下单信息存入Zookeeper队列,两个消费者监听队列,共同消费订单。以此模拟下单业务,使用队列来提升订单系统的可用性及处理订单的吞吐量。
在Spring Boot工程中,使用Apache Curator作为操作Zookeeper的API。Apache Curator大大简化了ZooKeeper客户端的使用,并为常见的分布式协同服务提供了高质量的实现。
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.apache.zookeeper
zookeeper
3.4.7
org.apache.curator
curator-framework
4.0.1
org.apache.curator
curator-recipes
4.0.1
com.alibaba
fastjson
1.2.70
org.springframework.boot
spring-boot-maven-plugin
# 初始化sleep的时间,用于计算之后的每次重试的sleep时间
zk.baseSleepTimeMS=1000
# 最大重试次数
zk.maxRetries=1
# zk服务端地址
zk.connectString=127.0.0.1:2181
# 会话超时时间,单位毫秒
zk.sessionTimeoutMs=1000
# 连接超时时间,单位毫秒
zk.connectionTimeoutMs=1000
zk.queueName=/order
public class ZKQueueUtils {
private static int baseSleepTimeMS;
private static int maxRetries;
private static String connectString;
private static CuratorFramework client;
private static int sessionTimeoutMs;
private static int connectionTimeoutMs;
private static DistributedQueue queue;
private static String queueName;
// 初始化连接
static{
ClassPathResource classPathResource = new ClassPathResource("zookeeper.properties");
Properties properties = new Properties();
try {
properties.load(classPathResource.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
baseSleepTimeMS = Integer.valueOf(properties.getProperty("zk.baseSleepTimeMS"));
maxRetries = Integer.valueOf(properties.getProperty("zk.maxRetries"));
connectString = properties.getProperty("zk.connectString");
sessionTimeoutMs = Integer.valueOf(properties.getProperty("zk.sessionTimeoutMs"));
connectionTimeoutMs = Integer.valueOf(properties.getProperty("zk.connectionTimeoutMs"));
queueName=properties.getProperty("zk.queueName");
initClient();
if(queueName != null && !"".equals(queueName))
createQueue();
}
public static CuratorFramework getClient(){
return ZKQueueUtils.client;
}
public static void initClient(){
RetryPolicy retryPolicy = new ExponentialBackoffRetry(baseSleepTimeMS,maxRetries);
ZKQueueUtils.client = CuratorFrameworkFactory.newClient(connectString, sessionTimeoutMs, connectionTimeoutMs, retryPolicy);
ZKQueueUtils.client.start();
}
public static void closeClient(){
ZKQueueUtils.client.close();
}
/**
* 创建队列
* @param
*/
public static void createQueue(){
QueueBuilder builder = QueueBuilder.builder(client, null, createQueueSerializer(), queueName);
queue = builder.buildQueue();
try {
queue.start();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 创建队列
* @param
*/
public static DistributedQueue getQueue(){
return queue;
}
public static void setQueueData(String data){
try {
queue.put(data);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void closeQueue(){
try {
queue.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static QueueSerializer createQueueSerializer() {
return new QueueSerializer(){
@Override
public byte[] serialize(String item) {
return item.getBytes();
}
@Override
public String deserialize(byte[] bytes) {
return new String(bytes);
}
};
}
}
@RestController
@RequestMapping("/order")
public class OrderController {
private static final int count = 1000;
@RequestMapping("/add")
public String addOrder(){
// 模拟下单
for (int i = 0; i < count; i++) {
Map map = new HashMap<>();
map.put("orderId", UUID.randomUUID().toString());
map.put("volumn", new Random().nextInt(100) + 1);
map.put("instrumentID","ag"+i);
map.put("price",count * i);
map.put("dataTime",new Date());
map.put("seq",i);
// 订单对象存入队列
ZKQueueUtils.setQueueData(JSON.toJSONString(map));
}
return "success";
}
}
两个消费者代码相同,同时启动两个main方法即可。consumeMessage为消费者消费消息时,所执行的回调方法。builder.lockPath()方法,在两个消费者同时消费时会进行加锁操作,确保每个消息只能被一个消费者消费。并且,当其中一个消费者消费过程中发生错误时,该消息将重回队列。使用lockPath()方法会降低性能,但能减少消息丢失的概率。
public class Consumer1 {
public static void main(String[] args) {
DistributedQueue queue = null;
try {
CuratorFramework client = ZKQueueUtils.getClient();
QueueBuilder builder = QueueBuilder.builder(client, new QueueConsumer() {
@Override
public void consumeMessage(String s) throws Exception {
// Map map = JSON.parseObject(s, Map.class);
System.out.println(s);
}
@Override
public void stateChanged(CuratorFramework curatorFramework, ConnectionState connectionState) {
}
}, createQueueSerializer(), "/order");
queue = builder.lockPath("/orderlock").buildQueue();
queue.start();
// 阻塞进程
System.in.read();
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(queue != null) {
queue.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
消费者1:
消费2:
经过测试,1000笔订单两个消费者可同时进行消费,且不会重复消费,6-7秒即可完成,性能还是相对不错的。
之前有用ZK的顺序节点和监听机制自己实现分布式队列,但效果并不好,欢迎有经验的大牛们指点和一同讨论~
本案例源码:zookeeper-queue: zookeeper实现分布式队列