以下步骤将在您的本地机器上运行一个小型 NSQ 集群,并逐步介绍发布、消费消息以及将消息归档到磁盘。
按照 安装 文档中的说明进行操作。
在一个 shell 中启动 nsqlookupd:
$ nsqlookupd
在另一个 shell 中启动 nsqd:
$ nsqd --lookupd-tcp-address=127.0.0.1:4160
注意:如果您的系统主机名无法解析为 127.0.0.1,则添加 --broadcast-address=127.0.0.1
在另一个 shell 中启动 nsqadmin:
$ nsqadmin --lookupd-http-address=127.0.0.1:4161
发布初始消息(这也会在集群中创建主题):
$ curl -d 'hello world 1' 'http://127.0.0.1:4151/pub?topic=test'
最后,在另一个 shell 中启动 nsq_to_file:
$ nsq_to_file --topic=test --output-dir=/tmp --lookupd-http-address=127.0.0.1:4161
向 nsqd 发布更多消息:
$ curl -d 'hello world 2' 'http://127.0.0.1:4151/pub?topic=test'
$ curl -d 'hello world 3' 'http://127.0.0.1:4151/pub?topic=test'
要验证一切按预期工作,请在 Web 浏览器中打开 http://127.0.0.1:4171/ 以查看
nsqadmin UI 并查看统计信息。此外,请检查写入 /tmp 的日志文件(test.*.log)的内容。
这里的重要经验是 nsq_to_file(客户端)没有被明确告知 test 主题的生产位置,它从 nsqlookupd 获取此信息,并且尽管连接的时机不同,也不会丢失任何消息。