分散メッセージングシステム!? NSQ を試してみる。

こんにちは k-jun です。今回はリアルタイムな分散メッセージングシステム NSQ を試してみます。

https://github.com/nsqio/nsq

star 数から見るにかなり有名なプロダクトのようですね。各種クラウドサービスの キューイングサービスももとにしている技術は同じだったりするんでしょうか...?

Setup

$ brew install nsq

Run

ここ に従っていきます。

以下 3つのコマンドをそれぞれ 別ターミナルで起動します。

$ nsqlookupd
$ nsqd --lookupd-tcp-address=127.0.0.1:4160
$ nsqadmin --lookupd-http-address=127.0.0.1:4161

1つ目は名前解決、2つめは NSQ 本体となる daemon, 3つ目は GUI とかでしょうか。

f:id:K-jun1221:20211127163948p:plain

ということでここに 何かを放り込んでみます。

$ curl -d 'hello world 1' 'http://127.0.0.1:4151/pub?topic=test'
OK

何やら Topic が追加されましたね。SNS とかの Topic もここから来ているんでしょうか。

f:id:K-jun1221:20211127164305p:plain

手順にしたがって、nsq_to_file というコマンドを実行してみます。一緒にさらなるメッセージも追加。

$ curl -d 'hello world 2' 'http://127.0.0.1:4151/pub?topic=test'
OK
~/source-code/remix/my-remix-app master*
$ curl -d 'hello world 3' 'http://127.0.0.1:4151/pub?topic=test'
OK
$ nsq_to_file --topic=test --output-dir=/tmp --lookupd-http-address=127.0.0.1:4161
2021/11/27 16:44:10 INF    1 [test/nsq_to_file] querying nsqlookupd http://127.0.0.1:4161/lookup?topic=test
2021/11/27 16:44:10 INF    1 [test/nsq_to_file] (MacBook-Pro.local:4150) connecting to nsqd
[nsq_to_file] 2021/11/27 16:44:10.864197 INFO: [test/nsq_to_file] opening /tmp/test.MacBook-Pro.2021-11-27_16.log
[nsq_to_file] 2021/11/27 16:44:10.864381 INFO: [test/nsq_to_file] syncing 1 records to disk
[nsq_to_file] 2021/11/27 16:44:40.614075 INFO: [test/nsq_to_file] syncing 2 records to disk

管理画面を見てみて、キューがなくなっていることに気づきました。

f:id:K-jun1221:20211127164705p:plain

つまり、nsq_to_file というのはおそらく Topic を listen してファイルに吐き出しているものでは。と /tmp ディレクトリを見に行くと入ってた。

$ tail -f /tmp/test.MacBook-Pro.2021-11-27_16.log
hello world 1
hello world 2
hello world 3

なるほどこうやってメッセージをやり取りできるのか。

ドキュメントをよく見てみると、使用していた daemon の説明も載っています。

https://nsq.io/components/utilities.html を見てみると、他にもいろいろと便利そうなコマンドがあるんですね。

ということで分散メッセージングライブラリの NSQ を見てみました。今回はこのへんで。