@[toc] RabbitMQ can also play this way!

RabbitMQ can be managed via the RabbitMQ web page.

  • How do I use the RabbitMQ management page

However, if we have installed the RabbitMQ_management plugin, that is, the Web management client for RabbitMQ, then we can use the REST API to manage RabbitMQ.

Some of you may ask, what’s the use of this?

If our project uses a graphical tool such as Granglia or Graphite and we want to capture the current message consumption/accumulation on RabbitMQ, we can use the REST API to query this information and transfer the query results to the new graphical tool. Since the REST API is HTTP requests, it supports a variety of clients, as long as it can send HTTP requests. Isn’t it particularly convenient?

1. REST API

REST API: REST API: REST API: REST API: REST API

Representational State Transfer (REST) is a Web software architecture style. It is a style rather than a standard. Web services that match or are compatible with this architecture style are called REST services.

REST services are concise and hierarchical, often based on existing widely popular protocols and standards such as HTTP, URI, XML, and HTML. In REST, resources are specified by URIs. Adding, deleting, modifying, and querying resources can be implemented using HTTP methods such as GET, POST, PUT, and DELETE.

With REST, caching can be used more efficiently to improve response times, while communication session state in REST is maintained by the client, which allows different servers to handle different requests in a series of requests, thus improving server scalability.

A well-designed Web software architecture must meet the REST style in a back-to-front separation project.

2. Open the Web management page

In general, there are two ways to start a Web management page:

  1. When installing RabbitMQ, selectrabbitmq:3-managementImage, the installation command is as follows:
docker run -d --rm --hostname my-rabbit --name some-rabbit -p 15672:15672 -p 5672:5672 rabbitmq:3-management
Copy the code

This will enable RabbitMQ to use the Web management page directly.

  1. Choose a normal image when installingrabbitmq:3The installation command is as follows:
docker run -d --hostname my-rabbit --name some-rabbit2 -p 5673:5672 -p 25672:15672 rabbitmq:3
Copy the code

Once this is installed, we need to enter the container and manually enable the Web management plug-in, using the following command:

docker exec -it some-rabbit2 /bin/bash
rabbitmq-plugins enable rabbitmq_management
Copy the code

The first command is to enter the container, and the second command is to enable the Web management plug-in. The result is as follows:

Open the Web administration page in either of these two ways, and then you can use the REST API.

Practice 3.

Let’s take a look at some common REST API operations.

You can use CURL to send a request, or you can use POSTMAN to send a request. Songo here is going to show you both ways.

3.1 Viewing Queue Statistics

For example, if we want to view the statistics of the hello-queue queue under the virtual host myvh, we can view them as follows:

curl -i -u javaboy:123 http://localhost:15672/api/queues/myvh/hello-queue
Copy the code

-i Displays the response header information.

The final result is as follows:

If we return only JSON data without the response header, we can use Python to format the data as follows:

As you can see, the returned data is formatted.

We can also use POSTMAN to send the request as follows:

Set the authentication mode to Basic Auth and set the correct user name and password.

POSTMAN requests are still much more convenient.

3.2 Creating a Queue

Under the/myvh virtual host to create a named javaboy – queue queue, use CURL request as follows:

curl -i -u javaboy:123 -XPUT -H "Content-Type:application/json" -d '{"auto_delete":false,"durable":true}' http://localhost:15672/api/queues/myvh/javaboy-queue
Copy the code

Note that the request is a PUT request, and the request parameter is JSON. There are two things in JSON, one is auto_delete, which says whether the queue will be automatically deleted if there is no consumer subscription to the queue (this property can be set to true for temporary queues). Durable queue durable queue durable queue durable queues persist after RabbitMQ restarts. These two parameters are easy to understand if you create queues in Java code, as they are often used when creating queues in Java code.

Of course, we can also use POSTMAN to send requests:

If 201 Created is returned, the queue is successfully Created.

Note however that the username/password is set in the Authorization TAB:

3.3 Viewing Current Connection Information

We can view the current connection information with the following request:

The request is as follows:

curl -i -u javaboy:123 http://localhost:15672/api/connections
Copy the code

POSTMAN can be viewed as follows:

3.4 Viewing The Current User Information

curl -i -u javaboy:123 http://localhost:15672/api/users
Copy the code

POSTMAN displays the following information:

3.5 Creating a User

Create a user whose name is zhangsan, password is 123, role is administrator.

CURL:

curl -i -u javaboy:123 -H "{Content-Type:application/json}" -d '{"password":"123","tags":"administrator"}' -XPUT http://localhost:15672/api/users/zhangsan
Copy the code

POSTMAN:

3.6 Setting vhost for a New User

Set user name zhangsan to vhost name myvh:

curl -i -u javaboy:123 -H "{Content-Type:application/json}" -d '{"configure":".*","write":".*","read":".*"}' -XPUT http://localhost:15672/api/permissions/myvh/zhangsan
Copy the code

The parameters are specific permission information:

POSTMAN requests are as follows:

To give you a few examples of how the RabbitMQ API can be used, go to the RabbitMQ management page and click on the HTTP API button at the bottom. There is a complete document:

4. Summary

Interested partners can try oh ~