When we install a RabbitMQ for the first time, we will probably manage the RabbitMQ via a Web page. By default, the default user we use for the first time is guest.

After a successful login, you can view all users in the Admin TAB:

You Can see that each user has a Can access virtual hosts property.

Songo is here to talk to you a little bit.

1. More tenants

RabbitMQ has a concept called multi-tenancy.

We install a RabbitMQ server, and each RabbitMQ server can create a number of virtual message servers, which are called virtual hosts, or vhosts for short.

In essence, each vhost is a small, independent RabbitMQ server with its own message queues, message switches, bindings and so on, and has its own privileges. Queues and switches in different Vhosts cannot be bound to each other. This ensures safe operation and avoids naming conflicts.

There is no need to treat vhosts in particular, just like physical RabbitMQ, as different vhosts provide logical separation to ensure that different application message queues can safely run independently.

In my opinion, what do we think about the relationship between vhost and RabbitMQ? RabbitMQ is an Excel file, whereas vhost is a sheet in an Excel file, and all of our operations are performed in one sheet.

Essentially, vhost is a concept in the AMQP protocol.

2. Create a vhost on the CLI

Let’s start by looking at how to create a vhost from the command line.

Docker RabbitMQ is installed in a docker container:

docker exec -it some-rabbit /bin/bash
Copy the code

Then run the following command to create a vhost named /myvh:

rabbitmqctl add_vhost myvh
Copy the code

The final result is as follows:

You can run the following command to view existing vhosts:

rabbitmqctl list_vhosts
Copy the code

Tracing can also be added with name and tracing. Tracing is the name of the vhost and tracing is the tracing function.

You can delete a vhost by running the following command:

rabbitmqctl delete_vhost myvh
Copy the code

When a Vhost is deleted, all message queues, switches, and bindings associated with the Vhost are deleted.

To set vhost to a user:

rabbitmqctl set_permissions -p myvh guest ".*" ".*" ".*"
Copy the code

The last three “.*” values are as follows:

  • Users have configurable permissions on all resources (create/delete message queues, create/delete switches, and so on).
  • The user has write permission (sending messages) on all resources.
  • The user has read permissions on all resources (message consumption, queue emptying, etc.).

Disallow a user from accessing a vhost:

rabbitmqctl clear_permissions -p myvh guest
Copy the code

3. Create vhosts on the management page

Of course, we can also manage vhosts on the web side:

In the Admin TAB, click Virtual Hosts on the right as follows:

Then click Add a new Virtual host below to Add a new vhost:

After entering a certain Vhost, you can modify its permissions and delete a Vhost, as shown below:

4. User management

Since the vhost is usually associated with the user, I will also mention the user operation here.

Add user javaboy and password 123 as follows:

rabbitmqctl add_user javaboy 123
Copy the code

You can run the following command to change the password of user Javaboy to 123456:

rabbitmqctl change_password javaboy 123456
Copy the code

You can run the following command to verify the user password:

rabbitmqctl authenticate_user javaboy 123456
Copy the code

The verification succeeds and fails as follows:

You can run the following command to view all current users:

The first column is the user name and the second column is the user role.

I already talked about user roles in the last article and won’t go over them here. Portal: How the RabbitMQ management page is used.

To set a role for a user, run the following command (to set the administrator role for Javaboy) :

rabbitmqctl set_user_tags javaboy administrator
Copy the code

Finally, the command to delete a user is as follows:

rabbitmqctl delete_user javaboy
Copy the code

5. Summary

A little bit of RabbitMQ lore for those interested