RabbitMQ においてメッセージは主に以下のような流れを経る
AMQP 0-9-1 Model in Brief The AMQP 0-9-1 Model has the following view of the world: messages are published to exchanges, which are often compared to post offices or mailboxes. Exchanges then distribute message copies to queues using rules called bindings. Then the broker either deliver messages to consumers subscribed to queues, or consumers fetch/pull messages from queues on demand. When publishing a message, publishers may specify various message attributes (message meta-data). Some of this meta-data may be used by the broker, however, the rest of it is completely opaque to the broker and is only used by applications that receive the message. - AMQP 0-9-1 Model Explained
日本語で整理すると RabbitMQ を通したメッセージのやりとりには主に以下のような役者が登場することとなる
bindings
というルールに基づいて Queue にコピーする役割を持つ
see also: RabbitMQ Tutorials
単純に brew で導入可能: The Homebrew RabbitMQ Formula
brew install rabbitmq
キューには以下のような設定がある
環境変数の設定
$ HOST= $ USERNAME= $ PASSWORD=
Publish
$ rabbitmqadmin --host $HOST --port 443 --username=$USERNAME --password=$PASSWORD --ssl publish routing_key=Test payload="hello" Message published
List Queues
$ rabbitmqadmin --host $HOST --port 443 --username=$USERNAME --password=$PASSWORD --ssl list queues name messages +------+----------+ | name | messages | +------+----------+ | Test | 1 | +------+----------+
Cousume
$ rabbitmqadmin --host $HOST --port 443 --username=$USERNAME --password=$PASSWORD --ssl get queue=Test ackmode=ack_requeue_false +-------------+----------+---------------+---------+---------------+------------------+------------+-------------+ | routing_key | exchange | message_count | payload | payload_bytes | payload_encoding | properties | redelivered | +-------------+----------+---------------+---------+---------------+------------------+------------+-------------+ | Test | | 0 | hello | 5 | string | | False | +-------------+----------+---------------+---------+---------------+------------------+------------+-------------+ $ rabbitmqadmin --host $HOST --port 443 --username=$USERNAME --password=$PASSWORD --ssl list queues name messages +------+----------+ | name | messages | +------+----------+ | Test | 0 | +------+----------+
rabtap は以下のとおり Docker イメージを用いてして手軽に利用可能
$ docker run --rm -ti ghcr.io/jandelgado/rabtap:latest
環境変数の設定
USERNAME= PASSWORD= HOST= PORT= AMQP_URI=amqps://$USERNAME:$PASSWORD@$HOST:$PORT
Publish
$ echo test > ./message $ docker run -v "$PWD":/usr/local/src --rm -ti ghcr.io/jandelgado/rabtap:latest pub --uri $AMQP_URI --routingkey=Test /usr/local/src/message
Subscribe
$ docker run -v "$PWD":/usr/local/src --rm -ti ghcr.io/jandelgado/rabtap:latest sub --uri $AMQP_URI Test ------ message received on 2022-05-01T14:46:41Z ------ exchange.......: routingkey.....: Test test