Configuring RabbitMQ Resources
Accessing RabbitMQ
Web Based Management UI
The easiest way to configure RabbitMQ is via the web based ManagementUI. Although useful for ad hoc monitoring and administration, this tool is not suitable for repeatable configuration in a DevOps environment. The Management UI requires the RabbitMQ 'Management Plugin'. Not every RabbitMQ deployment will have the 'Management Plugin' installed.
Rabbit REST API
This endpoint is typically at http://<rabbit-server>:<rabbit-port>:/api
. The REST API is flexible - it can be used by any client that talks HTTP but is not human friendly. The output is very verbose. This example gets information about the queue called 'q.type1' from the virtual host 'vhMon01'.
curl -u $RBT_USER:$RBT_PASSWD http://localhost:15672/api/queues/vhMon01/q.type1
Documentation about the REST API is available at 'http://<rabbit-server>:<rabbit-port>/api/index.html
'
rabbitmqadmin Command Line Tool
The rabbitmqadmin
command line tool can be downloaded from each RabbitMQ server that has the 'Management Plugin' installed. It can be downloaded from 'http://<rabbit-server>:<rabbit-port>/cli/rabbitmqadmin
'. It talks to the RabbitMQ server using the Rabbit Rest API. The rabbitmqadmin
tool requires python as a pre-requisite. The rabbitmqadmin
tool can be pointed at a RabbitMQ server on another machine using optional --host
and --port
parameters.
It is reasonably straightforward to perform most RabbitMQ configuration tasks using the rabbitmqadmin
tool.
rabbitmqctl Command Line Tool
The following text is copied from this excellent stackoverflow answer
rabbitmqctl
is native command line tool written in Erlang, in general, it is used inside the same server where RabbitMQ is running.rabbitmqadmin
is a Python interface for the HTTP management UI. You cannot use it if management UI plugin is not installed.
There is an overlap between the two tools, but there are a few features that you can use only with one of them.
With rabbitmqctl
you can:
- manage the rabbitmq clustuer, by adding/removing nodes.
- reset the node
- handle logs files rotation
- stop the node
rabbitmqadmin
is more for general admin, you have the same features you have in the management UI, you can also retrieve the node statistics.
rabbitmq-plugins Command Line Tool
One of the limitations of the rabbitmqadmin
tool is that you can't use to to check which plugins have been installed. To look at which plugins have been explicitly enabled within RabbitMQ - you have to use the rabbitmq-plugins
command which is available on the RabbitMQ server (and not available remotely over http)
rabbitmq-plugins list -e Listing plugins with pattern ".*" ... Configured: E = explicitly enabled; e = implicitly enabled | Status: * = running on rabbit@d2c02ccab9ce |/ [E*] rabbitmq_management 3.7.18 [e*] rabbitmq_management_agent 3.7.18 [e*] rabbitmq_web_dispatch 3.7.18
Overview of RabbitMQ Resources
Most message related resources (queues/exchanges) in RabbitMQ are scoped to a Virtual Host(vhost). A Virtual Host is just a namespace holder. It has a name. The default Virtual Host is named '/'. Virtual Hosts don't have have have a name starting with '/'.
The following resources belong to a Virtual Host (namespace)
- Exchanges - where a producer sends messages to
- Queues - where a consumer gets messages from
- Bindings - these help route messages between exchanges and queues/other exchanges
- Policies - these are useful for "extra" behaviours that can apply to multiple resources in one place - for example when messages are not processed or not routed.
The following resources DO NOT belong to a Virtual Host (vhost)
- Vhosts ( they are not hierarchical )
- Users - each user can be associated with
- a password (optional)
- a list of Management UI tags - these tags limit what a user can do when logged into Management UI console.
- three regexp patterns one each for read / write / configure
- topic permissions (required for MQTT/ STOMP? - not required for TIS)
Using rabbitmqadmin Command Line Tool to configure RabbitMQ
The rabbitmqadmin
tool can be used to create individual queues/exchanges etc. This works okay but you'd quickly end up with large shell scripts containing lists of separate rabbitmqadmin
commands.
A better approach is to use the Management UI to create a RabbitMQ configuration of exchanges/queues etc. First use the Management UI to create a fresh vhost (virtual host).
You can declare(create) and delete vhosts in the management UI. Switch to that virtual host and create the desired RabbitMQ configuration by hand. Within the ManagementUI you can send test messages to the exchanges in your configuration and check that the messages are routed as expected. When you are happy with your configuration, you can export the RabbitMQ Configuration for just for that vhost.
RabbitMQ Configuration Files
A RabbitMQ config file either be associated with a single vhost or all vhosts. If you export a RabbitMQ configuration file for a single vhost - it will not contain any user information. Users do not belong to vhosts. If you export a RabbitMQ configuration file for ALL vhosts - the exported file will contain all the users, all the vhosts and all the permissions linking users to vhosts.
- Rabbit Config file for all vhosts - includes users
- Rabbit Config file for single vhost - no users or user permissions
Don't forget : when you export a RabbitMQ configuration file - you are only exporting the configuration of the exchange/queues etc - YOU ARE NOT EXPORTING THE MESSAGES !
When you import a RabbitMQ configuration file into a vhost - it adds to whatever's already there. To reset a vhost to the contents of a config file (and nothing else), best perform the following steps:
- delete the vhost - ( be careful - this will delete the vhosts resources and the messages held on the vhosts queues too )
- declare (create) the vhost - it will be empty - (expect for the standard amq queues/exchanges)
- import the config into the vhost
rabbitmqadmin commands
This section shows examples of several rabbitmqadmin
commands. To use rabbitmqadmin
you need a username/password. A fresh install of RabbitMQ typically comes with an 'admin' user.
The following rabbitmqadmin
commands assume the following, pre-defined, environmental variables:
Name | Description |
---|---|
RBT_USER | rabbit username |
RBT_PASSWD | rabbit password |
VHOST | the vhost being used |
Executing commands against a remote RabbitMQ server
If you want to run rabbitmqadmin
and point to a RabbitMQ server which is not localhost:15672,
the rabbitmqadmin
supports these 2 optional parameters:
--host=HOST, -H HOST connect to host HOST [default: localhost] --port=PORT, -P PORT connect to port PORT [default: 15672]
Create a new vhost
$ rabbitmqadmin --user=$RBT_USER --password=$RBT_PASSWD declare vhost name=vhMon01 vhost declared
List all vhosts
$ rabbitmqadmin --user=$RBT_USER --password=$RBT_PASSWD list vhosts +---------+----------+ | name | messages | +---------+----------+ | / | 0 | | vhMon01 | 6 | +---------+----------+
Delete a vhost
When a vhost is deleted, it deletes all the exchanges/queues and messages it contains)
$ rabbitmqadmin --user=$RBT_USER --password=$RBT_PASSWD delete vhost name=vhMon01 vhost deleted
Import Rabbit Configuration File into a vhost
You might want to ensure the vhost doesn't have any resources in it as this command adds to whatever's in the vhost already.
$ rabbitmqadmin --user=$RBT_USER --password=$RBT_PASSWD --vhost=$VHOST import rabbit_config_for_vhost.json Uploaded definitions from "localhost" to rabbit_config_for_vhost.json. The import process may take some time. Consult server logs to track progress.
Export Rabbit Configuration for a vhost to a json config file
Note: by using the --vhost
option - you are exporting configuration for a single vhost.
$ rabbitmqadmin --user=$RBT_USER --password=$RBT_PASSWD --vhost=$VHOST export rabbit_config_for_vhost_exp.json Exported definitions for localhost to "rabbit_config_for_vhost_exp.json"
Purge all messages on queues for a vhost
This is a single (but long) unix command line
theQueues=$(rabbitmqadmin --user=$RBT_USER --password=$RBT_PASSWD --vhost=$VHOST list queues -f bash); for queue in $theQueues;\ do rabbitmqadmin --user=$RBT_USER --password=$RBT_PASSWD --vhost=$VHOST purge queue name=$queue; done
List the Exchanges associated with a vhost
$ rabbitmqadmin --user=$RBT_USER --password=$RBT_PASSWD --vhost=$VHOST list exchanges +--------------------+---------+ | name | type | +--------------------+---------+ | | direct | | amq.direct | direct | | amq.fanout | fanout | | amq.headers | headers | | amq.match | headers | | amq.rabbitmq.trace | topic | | amq.topic | topic | | ex.entry.point | fanout | | ex.router.main | topic | | sys.ex.audit | fanout | | sys.ex.dead.letter | fanout | | sys.ex.unrouted | fanout | +--------------------+---------+
List the Queues associated with a vhost
This can be used to quickly check if there are any queued messages before RabbitMQ admin.
$ rabbitmqadmin --user=$RBT_USER --password=$RBT_PASSWD --vhost=$VHOST list queues +----------------------+----------+ | name | messages | +----------------------+----------+ | q.type1 | 0 | | q.type2 | 0 | | sys.q.audit.01 | 0 | | sys.q.dead.letter.01 | 0 | | sys.q.unrouted.01 | 0 | +----------------------+----------+
List the Bindings associated with a vhost
rabbitmqadmin --user=$RBT_USER --password=$RBT_PASSWD --vhost=$VHOST list bindings +--------------------+----------------------+----------------------+ | source | destination | routing_key | +--------------------+----------------------+----------------------+ | | q.type1 | q.type1 | | | q.type2 | q.type2 | | | sys.q.audit.01 | sys.q.audit.01 | | | sys.q.dead.letter.01 | sys.q.dead.letter.01 | | | sys.q.unrouted.01 | sys.q.unrouted.01 | | ex.entry.point | ex.router.main | | | ex.entry.point | sys.ex.audit | | | ex.router.main | q.type1 | type1 | | ex.router.main | q.type2 | type2 | | sys.ex.audit | sys.q.audit.01 | | | sys.ex.dead.letter | sys.q.dead.letter.01 | | | sys.ex.unrouted | sys.q.unrouted.01 | | +--------------------+----------------------+----------------------+
List the Policies associated with a vhost
rabbitmqadmin --user=$RBT_USER --password=$RBT_PASSWD --vhost=$VHOST list policies +---------+-----------------------+-----------+----------------------------------------------------------------------+--------------+----------+ | vhost | name | apply-to | definition | pattern | priority | +---------+-----------------------+-----------+----------------------------------------------------------------------+--------------+----------+ | vhMon01 | unroutedMessagePolicy | exchanges | {"alternate-exchange": "sys.ex.unrouted"} | ^ex.router.* | 0 | | vhMon01 | messageTimeoutPolicy | queues | {"message-ttl": 30000, "dead-letter-exchange": "sys.ex.dead.letter"} | ^q.* | 1 | +---------+-----------------------+-----------+----------------------------------------------------------------------+--------------+----------+
List the RabbitMQ users
The list of users is not vhost specific.
Error rendering macro 'code': Invalid value specified for parameter 'firstline'rabbitmqadmin --user=$RBT_USER --password=$RBT_PASSWD list users +--------+--------------------------------+--------------------------------------------------+---------------+ | name | hashing_algorithm | password_hash | tags | +--------+--------------------------------+--------------------------------------------------+---------------+ | rabbit | rabbit_password_hashing_sha256 | F1Aa/9adwlzfjHBLNnGF2yHPDMMSJD8h6fuhl+XJ21YbxUCR | administrator | +--------+--------------------------------+--------------------------------------------------+---------------+
List the Permissions linking Users with Vhosts
$ rabbitmqadmin --user=$RBT_USER --password=$RBT_PASSWD list permissions +---------+-----------+------+--------+-------+ | vhost | configure | read | user | write | +---------+-----------+------+--------+-------+ | / | .* | .* | rabbit | .* | | vhMon01 | .* | .* | rabbit | .* | +---------+-----------+------+--------+-------+
Sending a simple message to an Exchange in a vhost
rabbitmqadmin --user=$RBT_USER --password=$RBT_PASSWD --vhost=$VHOST publish routing_key="type1" exchange="ex.topic.01" payload="type1 test message"
Sending a JSON File to an Exchange in a vhost
In the example here, the file 'message01.json
' contains json. It seems odd but the rabbitmqadmin
command doesn't support sending files.
JSON=$(cat message01.json);rabbitmqadmin --user=$RBT_USER --password=$RBT_PASSWD --vhost=$VHOST publish exchange="ex.entry.point" properties='{"content_type":"application/json"}' routing_key="type1" payload="$JSON"
Look at a single message on a Queue
This is the similar to the command for consuming a single messages but here the default value for 'ackmode' is 'ack_requeue_true'
rabbitmqadmin --user=$RBT_USER --password=$RBT_PASSWD --vhost=$VHOST get queue=queue1 +-------------+----------+---------------+-------------+---------------+------------------+-------------+ | routing_key | exchange | message_count | payload | payload_bytes | payload_encoding | redelivered | +-------------+----------+---------------+-------------+---------------+------------------+-------------+ | queue1 | | 3 | message one | 11 | string | True | +-------------+----------+---------------+-------------+---------------+------------------+-------------+
Look at multiple messages on a Queue
This is the similar to the command for consuming messages but here the default value for 'ackmode' is 'ack_requeue_true'
rabbitmqadmin --user=$RBT_USER --password=$RBT_PASSWD --vhost=$VHOST get queue=queue1 count=3 +-------------+----------+---------------+---------------+---------------+------------------+-------------+ | routing_key | exchange | message_count | payload | payload_bytes | payload_encoding | redelivered | +-------------+----------+---------------+---------------+---------------+------------------+-------------+ | queue1 | | 3 | message one | 11 | string | True | | queue1 | | 2 | message two | 11 | string | True | | queue1 | | 1 | message three | 13 | string | True | +-------------+----------+---------------+---------------+---------------+------------------+-------------+
Consume a single message from a Queue
This is similar to looking at messages except ackmode=ack_requeue_false
rabbitmqadmin --user=$RBT_USER --password=$RBT_PASSWD --vhost=$VHOST get ackmode=ack_requeue_false queue=queue1 +-------------+----------+---------------+-------------+---------------+------------------+-------------+ | routing_key | exchange | message_count | payload | payload_bytes | payload_encoding | redelivered | +-------------+----------+---------------+-------------+---------------+------------------+-------------+ | queue1 | | 2 | message one | 11 | string | False | +-------------+----------+---------------+-------------+---------------+------------------+-------------+
Consume multiple messages from a Queue
This is similar to looking at messages except ackmode=ack_requeue_false.
Notice that we requested 3 messages from the Queue but only 2 were available.
rabbitmqadmin --user=$RBT_USER --password=$RBT_PASSWD --vhost=$VHOST get queue=queue1 count=3 ackmode=ack_requeue_false +-------------+----------+---------------+---------------+---------------+------------------+-------------+ | routing_key | exchange | message_count | payload | payload_bytes | payload_encoding | redelivered | +-------------+----------+---------------+---------------+---------------+------------------+-------------+ | queue1 | | 1 | message two | 11 | string | False | | queue1 | | 0 | message three | 13 | string | False | +-------------+----------+---------------+---------------+---------------+------------------+-------------+
Slack: https://hee-nhs-tis.slack.com/
Jira issues: https://hee-tis.atlassian.net/issues/?filter=14213