首页 > etcd消息队列queue,如何解决global的index增长?

etcd消息队列queue,如何解决global的index增长?

(初用etcd)我现在想用etcd 来做能阻塞的消息队列,文档里提到了queue,问题在于

"key": "/queue/00000000000000000029"

后的数字是根据index生成的,但index是个global的,这时我index+1后wait=true,

curl 'http://127.0.0.1:2379/v2/keys/queue/00000000000000000030?wait=true'

如果中图有其它key的操作,就会出现问题,这个应该怎么解决?


不知道你看的文档是不是这个https://github.com/coreos/etcd/blob/6acb3d67fbe131b3b2d5d010e00ec80182be4628/Documentation/v2/api.md#atomically-creating-in-order-keys

从你描述分析,你的需求可能是两种情况:
一是分布式锁,拿到锁以后才能继续操作,实现全局顺序操作。
二是消息队列,多个生产者向队列写,一个消费者读,实现全局顺序操作。

如果是第二种需求,可以参考etcd的原文。
原文描述:

so the next key can be more than previous + 1.

To enumerate the in-order keys as a sorted list, use the "sorted" parameter.

curl -s 'http://127.0.0.1:2379/v2/keys/queue?recursive=true&sorted=true'

如果使用这种方式,建议使用V3版本,性能可能好一些。

【热门文章】
【热门文章】