1. Download and install Erlang
Because the RabbitMQ server is written based on Erlang, Erlang needs to be installed first.
1) Download
The download address is as follows:
https://www.erlang.org/downloads
The download here is relatively slow, you can refer to the following Baidu network disk:
Link: https://pan.baidu.com/s/1oXq5QX3IlzRq5L-mk1gC4Q
Extraction code: luna
2) install
Select the corresponding installation directory, all the way to next.
3) Configure environment variables
Create a new variable in the system variable variable
name (N): ERLANG_HOME
variable value (V): H:\SW_install\erl-24.1
Note: The variable value needs to be modified according to your actual situation
Add ;%ERLANG_HOME%\bin to the end of the Path variable value.
4) Verify the correctness of the environment variable configuration
echo %ERLANG_HOME%
2. Download and install RabbitMQ
1) Download
The download address is as follows:
https://www.rabbitmq.com/install-windows.html#installer
2) install
Select the corresponding installation directory, all the way to next.
3) Configure environment variables
Create a new variable in the system variable variable
name (N): RABBITMQ_SERVER
variable value (V): H:\SW_install\RabbitMQ Server\rabbitmq_server-3.9.7
Note: The variable value needs to be modified according to your actual situation
Add ;%RABBITMQ_SERVER%\sbin to the end of the Path variable value.
4) Verify the correctness of the environment variable configuration
echo %RABBITMQ_SERVER%
5) Install rabbitmq-plugins
The cmd command opens the command line window and enters the rabbitmq installation bin directory (mine is: H:\SW_install\RabbitMQ Server\rabbitmq_server-3.9.7\sbin)
Execute the following command to install the plugin:
rabbitmq-plugins.bat enable rabbitmq_management
If the following error is reported here:
ERLANG_HOME not set correctly
After confirming that the configured environment variables are correct, go from the C: directory to the RabbitMQ installation bin directory layer by layer, and execute the plug-in installation again to solve the problem.
6) Start the RabbitMQ service
net start RabbitMQ
Or
in the sbin directory of rabbitmq, double-click to execute the rabbitmq-server.bat file.
7) Stop the RabbitMQ service
net stop RabbitMQ
8) View service status
rabbitmqctl status
9) Page access to RabbitMQ
http://localhost:15672
account/password: guest / guest
After login, the page is as follows:
3. Simple use of RabbitMQ
1) RabbitMQ's pom reference
<!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client -->
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.13.1</version>
</dependency>
2) Message sender
package com.miracle.luna.rabbitmq;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.nio.charset.StandardCharsets;
/**
* @author Miracle Luna
* @date 2021/10/19
*/
public class SendMQ {
private final static String QUEUE_NAME = "hello";
public static void main(String[] args) throws Exception{
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setPort(5672);
factory.setUsername("guest");
factory.setPassword("guest");
final Connection connection = factory.newConnection();
final Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = "Hello, RabbitMQ!";
channel.basicPublish("", QUEUE_NAME, null, message.getBytes(StandardCharsets.UTF_8));
System.out.println("Send '" + message + "'");
channel.close();
connection.close();
}
}
The result of the operation is as follows:
Send 'Hello, RabbitMQ!'
3) Message receiver
package com.miracle.luna.rabbitmq;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
/**
* @author Miracle Luna
* @date 2021/10/19
*/
public class RecvMQ {
private final static String QUEUE_NAME = "hello";
public static void main(String[] args) throws Exception{
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setPort(5672);
factory.setUsername("guest");
factory.setPassword("guest");
final Connection connection = factory.newConnection();
final Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
System.out.println("Waiting for messages...");
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String message = new String(body, StandardCharsets.UTF_8);
System.out.println("Received '" + message + "'");
}
};
channel.basicConsume(QUEUE_NAME, true, consumer);
}
}
The result of the operation is as follows:
Waiting for messages...
Received 'Hello, RabbitMQ!'
You can see the number of messages in all queues in the Queues tab
4. Common commands
1) View user list: rabbitmqctl list_users
2) Add a new user: rabbitmqctl add_user root password
3) Delete a user: rabbitmqctl delete_user test
4) Modify user password: rabbitmqctl change_password root 123456
5) Grant administrator role: rabbitmqctl set_user_tags root administr ator
6 ) Grant user permissions: rabbitmqctl set_permissions -p / root "." "." ".*"
7) View queue list: rabbitmqctl list_queues
8) Delete a queue: rabbitmqctl delete_queue test
9) Reset (if you forget the password, you can apply it Reset, but data will be lost, use with caution!!!):
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl start_app