Skip to main content

RabbitMQ Tutorial | Chapter 5 RabbitMQ Management

RabbitMQ Tutorial | Chapter 5 RabbitMQ Management

RabbitMQ Tutorial | Chapter 5 RabbitMQ Management

👨🏻‍💻 A programmer who loves photography👨🏻‍🎨 A designer
who loves coding🧕🏻 An editor who is good at design🧑🏻‍🏫 A ruthless coding enthusiast Hello everyone, I am a DevOps engineer. Welcome to share/collect/ Like/Looking!

This RabbitMQ tutorial provides learners with comprehensive content, starting from an introduction to RabbitMQ and covering the concept of message middleware, the installation and use of RabbitMQ, as well as the introduction of related concepts such as switches, queues, routing keys, etc. Going a step further, the tutorial explores the AMQP protocol, client development wizards, and how messages are sent and consumed. At the same time, learners can also learn about message transmission guarantees, advanced features such as dead letter queues, delay queues, priority queues, RPC implementation, etc. In addition, the tutorial also covers important topics such as RabbitMQ management, configuration, operation and maintenance, monitoring and cluster management, helping learners fully master the application of RabbitMQ. The entire tutorial is rich and detailed, suitable for beginners and experienced developers to learn from.

There are 11 chapters in total and more than 90,000 words. This article: Chapter 5 RabbitMQ Management.

Chapter 5 RabbitMQ Management

5.1 Multi-tenancy and permissions

RabbitMQ provides multi-tenancy features, allowing the creation of multiple independent virtual hosts (vhosts) on a single RabbitMQ instance. Each virtual host is equivalent to an independent message broker, with its own switches, queues, bindings, user permissions, etc., thereby achieving logical isolation. This allows different applications or user groups to have independent messaging environments on the same RabbitMQ instance without affecting each other.

Using the multi-tenant feature of RabbitMQ requires the following steps:

  1. Create a virtual host: You can create a new virtual host in the RabbitMQ management interface or using the RabbitMQ command line tool. Each virtual host has a unique name that is used to identify and distinguish different tenants.
  2. Set permissions for virtual hosts: In RabbitMQ, you can set different permissions for different users. Multiple users can be created and assigned to different virtual hosts to achieve access control for different tenants. Each user can be assigned read, write, management and other permissions on the virtual host.
  3. Access control policies: RabbitMQ also provides access control policies to restrict access to virtual hosts. Access to a virtual host can be controlled through policy settings, such as limiting the maximum number of connections, the maximum number of queues, the maximum memory usage, etc.

The following is sample code that uses RabbitMQ's multi-tenancy features:

	   1. public class RabbitMQMultiTenancyExample {

2.

3. private static final String VIRTUAL_HOST = "my_vhost";

4. private static final String USERNAME = "my_user";

5. private static final String PASSWORD = "my_password";

6.

7. public static void main(String[] args) throws Exception {

8. // Creating a Connection Factory

9. ConnectionFactory factory = new ConnectionFactory();

10. factory.setHost("localhost");

11. factory.setVirtualHost(VIRTUAL_HOST);

12. factory.setUsername(USERNAME);

13. factory.setPassword(PASSWORD);

14.

15. // Creating Connections and Channels

16. try (Connection connection = factory.newConnection();

17. Channel channel = connection.createChannel()) {

18. // Declare switches, queues, and other operations in a given virtual host

19. // ...

20.

21. // Publish the message to the specified virtual host

22. // ...

23. }

24. }

25. }



In the above example, we created a virtual host my_vhost, created a user named my_user for the virtual host, and set a corresponding password. Then in the code, we use this virtual host and user to connect to RabbitMQ, and perform corresponding message publishing and other operations.

In summary, through the multi-tenant feature of RabbitMQ, logical isolation of messages can be achieved on a single RabbitMQ instance, providing independent messaging environments for different applications or user groups, and can be achieved by setting permissions and access control policies for different users. Access control for virtual hosts.

5.2 User management

In RabbitMQ, you can create and manage users through the management interface or use the RabbitMQ command line tool, including setting user roles and permissions. Here are the steps to create and manage users through the RabbitMQ management interface and command line tools:

Using the RabbitMQ management interface:

  1. Open the RabbitMQ management interface, usually access http://localhost:15672 through a browser .
  2. Log in to the management interface using an administrator account. By default, the administrator username is guest and the password is guest, but it is recommended to change the default administrator password in a production environment.
  3. In the management interface, click the "Admin" tab and then click the "Add a user" button.
  4. Enter the username and password and click the "Add user" button to create a new user.
  5. Click the "Back to users" link, find the user you just created, and click the "Set permission" button.
  6. In the interface for setting user permissions, you can assign virtual hosts and roles to users; RabbitMQ has predefined several roles, such as "administrator", "monitoring", "policymaker", and "management". You can select a role according to your needs, or you can customize the role and assign permissions.
  7. Click the "Set permission" button to save the settings.

Use the RabbitMQ command line tool:

Use the rabbitmqctl command line tool at the command line to create and manage RabbitMQ users. the following are some commonly used commands:

  1. Create users:
rabbitmqctl add_user <username> <password

Example: rabbitmqctl add_user myuser mypassword

  1. Set the user role:
Example: rabbitmqctl add_user <username> <password> 1. set user role.

Example: rabbitmqctl set_user_tags myuser administrator
  1. Set user permissions:
rabbitmqctl set_permissions -p <vhost> <username> <conf> <write> <read>

Example: rabbitmqctl set_permissions -p my_vhost myuser ". _" ". _ " ". *"

Notes:

  • Administrator users usually have the highest privileges to create/delete virtual hosts, users, roles, etc. and have full access to all virtual hosts.
  • Non-admin users can usually only operate in their own virtual hosts.
  • You can view all current users and their roles with the rabbitmqctl list_users command.
  • In production environments, it is recommended that users be reasonably privileged and restricted to ensure security and isolation.

To summarize, you can create and manage users, including setting user roles and permissions, through the RabbitMQ management interface or command line tools. The administrator user has the highest privileges to create and manage other users and roles, and non-administrator users can operate in their own virtual hosts. When setting user privileges, it is recommended to make a reasonable division of privileges to ensure the security and isolation of the system.

5.3 Web Management

RabbitMQ provides a powerful and easy-to-use web management interface, also known as the RabbitMQ Management Plugin, which allows administrators and developers to easily manage queues, switches, connections, monitor the status of message queues, and view the health of the cluster. The following are the main features and benefits of the Web Management Interface:

  1. Visual management: The Web management interface shows the status and metrics of message queues in intuitive charts and tables, making management and monitoring more intuitive and easier to understand.
  2. Queue and Switch Management: Through the Web interface, you can easily create, delete, and view information about queues and switches, including names, types, bindings, and so on. Administrators can perform these operations directly from the interface, eliminating the need for command line tools.
  3. Connection and Channel Management: The web management interface provides detailed information about connections and channels. Administrators can view the status of the current connection and channel, the number of consumers, the virtual hosts they belong to, and so on.
  4. Virtual Host Management: Administrators can manage the creation and deletion of virtual hosts, and set permissions for virtual hosts to achieve multi-tenant isolation and access control.
  5. User Management: The Web management interface allows administrators to create, delete and manage users, as well as set roles and permissions for users.
  6. Monitoring and Performance Analysis: The Web management interface provides rich monitoring indicators and performance analysis information, including the number of messages in the queue, the number of consumers, throughput, etc., which can help administrators understand the status of the system in real time.
  7. Plug-in Management: RabbitMQ supports plug-in mechanism. Through the web management interface, you can easily view and enable various plug-ins to expand the functionality of RabbitMQ.
  8. Cluster Management: If you have deployed a RabbitMQ cluster, you can easily view the cluster status, node information and health status through the web management interface.

The web management interface not only simplifies the management and monitoring of RabbitMQ, but also provides an intuitive data visualization to help administrators better understand the status and performance of message queues. Meanwhile, the web management interface supports multiple languages, which is convenient for global use. It is recommended to enable the Web management interface in the production environment and set appropriate access control to ensure system security.

5.4 Application and Cluster Management

Managing RabbitMQ applications and clusters includes starting and shutting down applications, as well as configuring and managing cluster nodes. The following describes each of these aspects:

Application startup and shutdown:

  1. Starting the application: Before starting the RabbitMQ application, you need to start the Erlang node. You can then start the RabbitMQ application using a command line tool or a script.
    • Starting with command line tools: You can start a RabbitMQ application by entering the command rabbitmq-server at the command line.
    • Starting with a script: Different operating systems may use different startup scripts. Typically, Linux systems use the service rabbitmq-server start command, while Windows systems can be started through the service panel.
  1. Shutting down the application: To stop the RabbitMQ application, you can use the following methods:
    • Use command line tools: Enter the command rabbitmqctl stop on the command line to stop the RabbitMQ application.
      • Using scripts: On Linux systems, you can stop the RabbitMQ application by using the service rabbitmq-server stop command. On Windows systems, it can also be stopped from the Services panel.

Configuration and management of cluster nodes:

  1. Cluster Configuration: To configure a RabbitMQ cluster, you need to set up the same /etc/rabbitmq/rabbitmq.config configuration file on each node. The configuration file contains the node name, cluster node information, network ports, authentication, and other configuration. Ensure that the configuration file is consistent across all nodes, and then start all nodes.
  2. Joining and Leaving Cluster Nodes: To join the cluster, you need to specify the names of other nodes in the node configuration file. After the node starts, it will automatically connect to other nodes and join the cluster. To leave the cluster, you can disable the cluster plugin via command line tool or script, and then restart the node.
  3. Cluster Health Monitoring: You can use the RabbitMQ web management interface or command line tools to monitor the health of the cluster. You can use the RabbitMQ web management interface or command-line tools to monitor the health of the cluster. You can check the node status, queue status, message status, and other metrics to ensure that the cluster is functioning properly.
  4. Upgrading and Maintaining Cluster Nodes: If you need to upgrade or maintain a node in a cluster, you can first remove the node from the cluster, then perform the corresponding operation, and then add the node to the cluster again after completion.

Precautions:

  • When setting up a cluster, make sure that the time of all nodes is synchronized to avoid problems caused by time inconsistencies.
  • Ensure that all nodes are able to access each other and that communication ports are not blocked by firewalls.
  • When modifying configuration files or performing operations, be sure to back up the relevant data to prevent data loss.

To summarize, managing RabbitMQ applications and clusters includes starting and shutting down applications, configuring cluster nodes, joining and leaving clusters, monitoring cluster health, and upgrading and maintaining nodes. Ensure that the nodes are configured consistently and function properly to guarantee the stability and reliability of the RabbitMQ cluster.

5.4.1 Application Management

Starting and shutting down RabbitMQ applications can be performed using command line tools or scripts. At the same time, RabbitMQ provides a web-based management interface to perform some common application management operations. The following is a detailed description:

Starting and shutting down a RabbitMQ application:

  1. Start the RabbitMQ application:

** On a Linux system, open a terminal and enter the following command to start the RabbitMQ application: ** On a Windows system, open a command prompt and enter the following command to start the RabbitMQ application.

  1. If everything is OK, the RabbitMQ application will start and listen on the default port 5672. Before starting the RabbitMQ application, make sure that you have installed and properly configured your Erlang/OTP environment.
    
2. rabbitmq-server



  1. Shut down the RabbitMQ application:
    • On Linux systems, you can terminate the RabbitMQ application by using Ctrl + C. * * On Windows systems, you can terminate the RabbitMQ application by using Ctrl + C.
      • On Windows systems, you can terminate the RabbitMQ application by typing Ctrl + C in a command prompt or by closing a command line window.

Some common operations in application management:

RabbitMQ provides a web management interface through which you can perform some common operations in application management, including:

  1. Queue Management: In the web interface, you can create, delete and view queues. In the web interface, you can create, delete, and view queues. You can also view the number of messages in the queue, the number of consumers, and other metrics.
  2. Switch Management: You can create, delete and view switches, and set the type and properties of switches.
  3. Virtual Host Management: You can create, delete and view virtual hosts, and set the permissions and access control policies for virtual hosts.
  4. User Management: You can create, delete and manage users, and set user roles and permissions.
  5. Connection and Channel Management: You can view the current connection and channel status, number of consumers and other information.
  6. Monitoring and Performance Analysis: The Web management interface provides rich monitoring indicators and performance analysis information to help administrators understand the status and performance of the system in real time.
  7. Plug-in Management: You can view and enable various plug-ins to extend the functionality of RabbitMQ.

All the above operations can be performed intuitively through the web management interface without using command line tools. Through the web management interface, administrators can more easily carry out application management and monitoring work to improve work efficiency.

To summarize, you can use command line tools or scripts to start and shut down RabbitMQ applications. In terms of application management, RabbitMQ's web management interface provides a series of functions, including creating, deleting, viewing, and setting operations of queues, switches, virtual hosts, users, and so on, as well as providing monitoring and performance analysis information to help administrators better manage and monitor RabbitMQ applications.

5.4.2 Cluster Management

Configuring and managing RabbitMQ clusters can be achieved through multi-machine, multi-node configuration and single-machine, multi-node configuration. The following describes the steps for each of these two configurations:

Multi-machine multi-node configuration:

In a multi-machine, multi-node configuration, different nodes of RabbitMQ run on different machines to form a distributed cluster. This can achieve higher reliability and fault tolerance.

  1. Ensure network interoperability between nodes: First ensure that the network between all nodes is interoperable, that is, the nodes can communicate with each other. Make sure that the network ports are not blocked by firewalls.
  2. Install and configure Erlang/OTP: Install and configure the same version of Erlang/OTP on each machine. RabbitMQ is based on Erlang, so all nodes must use the same version of Erlang/OTP.
  3. Configure the node name: Set the same node name in the configuration file for each node. The configuration file is usually located in /etc/rabbitmq/rabbitmq.config or /etc/rabbitmq/rabbitmq-env.conf.
  4. Start RabbitMQ nodes: Start the RabbitMQ application on each node separately. Start the RabbitMQ application by entering the command rabbitmq-server on the command line.
  5. Join the cluster: Set up the same rabbitmq.conf configuration file on each node, containing the names of the other nodes. After each node starts, they will automatically connect to the other nodes and join the cluster.

Single-machine multi-node configuration:

In a single-machine multi-node configuration, multiple RabbitMQ nodes run on the same machine. These nodes can share the same Erlang/OTP virtual machine, but the port numbers must be different for each node.

  1. Install and Configure Erlang/OTP: Install and configure Erlang/OTP on the machine to ensure that the Erlang/OTP environment is capable of supporting multi-node operation. 2.
  2. Configure Node Names and Port Numbers: Set different node names and port numbers in each node's configuration file. The configuration file is usually located in /etc/rabbitmq/rabbitmq.config or /etc/rabbitmq/rabbitmq-env.conf.
  3. Start RabbitMQ nodes: Start the RabbitMQ application on each node separately. Start the RabbitMQ application by typing rabbitmq-server -detached at the command line.
  4. Join the cluster: Set up the same rabbitmq.conf configuration file on each node, containing the names and port numbers of the other nodes. After each node starts, they will automatically connect to the other nodes and join the cluster.

Whether you have a multi-machine, multi-node configuration or a single-machine, multi-node configuration, ensure network interoperability and configuration consistency between nodes in the cluster. Once configured, you can monitor and manage the cluster status through RabbitMQ's web management interface or command line tools. Clustering can provide higher availability and performance, guaranteeing high reliability and distributed processing of messages. When configuring clustering, be sure to back up relevant data to prevent data loss.

5.5 Server-Side Status

RabbitMQ provides several ways to view server status information, including queue, switch, connection, and other statistics. The following are some common ways to view server status information:

  1. Web Management Interface: RabbitMQ's web management interface is the most intuitive and commonly used way to view server status information. By accessing http://localhost:15672 (the default address) or the address of the management interface you configured via a browser, after logging in, you can see several tabs on the interface, each showing different statistics, such as:
    • Overview: Shows overview information about the entire RabbitMQ server, including active connections, number of queues, message publish rate, and so on.
    • Queues: shows detailed information about all the queues, including the name of the queue, the number of messages, the number of consumers, and so on.
    • Exchanges: shows detailed information about all exchanges, including the name, type, and binding information of the exchanges.
    • Connections: shows the details of all current connections, including the name, client address, virtual host, etc. of the connection.
    • Channels: shows the details of all current channels, including the number of channels, number of consumers, and so on.
  1. Command Line Tools: RabbitMQ provides the command line tool rabbitmqctl to view server status information. You can enter the following commands in a terminal or command prompt to get the corresponding statistics:
    • View queue information: rabbitmqctl list_queues
    • View switch information: rabbitmqctl list_exchanges
    • View connection information: rabbitmqctl list_connections
    • View channel information: rabbitmqctl list_channels
    • View node status: rabbitmqctl status
  1. HTTP API Interface: RabbitMQ provides an HTTP API interface that allows you to send HTTP requests to obtain status information about a server. The API interface is usually located at http://localhost:15672/api/ (the default address) and can be accessed using the curl command or any HTTP client library. You can use the curl command or any HTTP client library to access the API interface and obtain various statistics.
  2. Plug-ins: RabbitMQ supports a variety of plug-ins that can provide additional functionality and statistics. You can check the official RabbitMQ documentation for more information about plug-ins.

In general, RabbitMQ provides a wealth of ways to view server status information, including web management interfaces, command line tools, HTTP API interfaces, etc. By monitoring the status information of the server, you can better understand the running status of RabbitMQ and help tune and troubleshoot problems.

5.6 HTTP API interface management

RabbitMQ provides HTTP API interfaces through which RabbitMQ can be easily managed and monitored. The address of the API interface is usually http://localhost:15672/api/ (the default address). You can use the curl command or any HTTP client library to access the API interface. The following are some commonly used HTTP API interfaces and their usage:

  1. Get the queue list:

GET /api/queues

This interface is used to get the list of all queues. The returned JSON data contains information such as the name of the queue, the number of messages, and the number of consumers.

  1. Get the switch list:

GET /api/exchanges

This interface is used to get a list of all switches. The returned JSON data contains information such as the name of the switch, its type, and whether it is persistent.

  1. Get the connection list:

GET /api/connections

This interface is used to get a list of all current connections. The returned JSON data includes the connection name, client address, virtual host and other information.

  1. Get channel list:

GET /api/channels

This interface is used to get the list of all current channels. The returned JSON data contains information such as the channel number, whether it is active, and the number of consumers.

  1. Get node information:

GET /api/nodes

This interface is used to obtain information about all nodes. The returned JSON data contains the node name, memory usage, disk usage and other information.

  1. Get the number of messages in the queue:

GET /api/queues/vhost/queue-name

This interface is used to get the number of messages in a specific queue. Where vhost is the virtual host name and queue-name is the queue name.

  1. Get the message details of the queue:

GET /api/queues/vhost/queue-name/get

This interface is used to obtain message details from the queue. Where vhost is the virtual host name and queue-name is the queue name.

Through the HTTP API interface, you can programmatically access RabbitMQ status information and monitor and manage it as needed. Please note that for security reasons, the API interface requires authentication. You need to use the RabbitMQ username and password for authentication, which can be achieved by adding the HTTP header Authorization: Basic base64encode(username:password).

At the same time, you need to handle sensitive information with caution when using the API interface to avoid leaking sensitive data. It is recommended to only use the API interface in a trusted environment. When programming, you can use scripts, HTTP client libraries, or REST client libraries to access the API interface to more conveniently monitor and manage RabbitMQ.

5.7 Summary

This chapter introduces the management features of RabbitMQ, including multi-tenancy and permissions, user management, web management, application and cluster management, server status and HTTP API interface management. In the next chapter, we will learn the configuration of RabbitMQ, including environment variables, configuration files, parameters and strategies.