因为RabbitMQ的服务端是基于 Erlang编写的,所以,首先需要安装Erlang。
下载地址如下:
https://www.erlang.org/downloads
此处下载比较慢,可以参考如下百度网盘:
链接:https://pan.baidu.com/s/1oXq5QX3IlzRq5L-mk1gC4Q
提取码:luna
选择相应的安装目录,一路next即可。
在系统变量中新建变量
变量名(N):ERLANG_HOME
变量值(V):H:\SW_install\erl-24.1
注意:变量值需要根据自己的实际情况进行修改
将 ;%ERLANG_HOME%\bin 添加到 Path 变量值的最后。
echo %ERLANG_HOME%
下载地址如下:
https://www.rabbitmq.com/install-windows.html#installer
选择相应的安装目录,一路next即可。
在系统变量中新建变量
变量名(N):RABBITMQ_SERVER
变量值(V):H:\SW_install\RabbitMQ Server\rabbitmq_server-3.9.7
注意:变量值需要根据自己的实际情况进行修改
将 ;%RABBITMQ_SERVER%\sbin 添加到 Path 变量值的最后。
echo %RABBITMQ_SERVER%
cmd命令打开命令行窗口,进入到rabbitmq的安装bin目录(我的是:H:\SW_install\RabbitMQ Server\rabbitmq_server-3.9.7\sbin)
执行如下命令,进行插件安装:
rabbitmq-plugins.bat enable rabbitmq_management
如果此处报如下错误:
ERLANG_HOME not set correctly
在确认配置的环境变量没问题的情况下,从C:目录逐层到RabbitMQ的安装bin目录,再次执行插件安装,即可解决。
net start RabbitMQ
或者
在rabbitmq的sbin目录下,双击执行 rabbitmq-server.bat 文件。
net stop RabbitMQ
rabbitmqctl status
http://localhost:15672
账号/密码:guest/guest
<dependency>
<groupId>com.rabbitmqgroupId>
<artifactId>amqp-clientartifactId>
<version>5.13.1version>
dependency>
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();
}
}
运行结果如下:
Send 'Hello, RabbitMQ!'
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);
}
}
运行结果如下:
Waiting for messages...
Received 'Hello, RabbitMQ!'
1)查看用户列表:rabbitmqctl list_users
2)新增一个用户:rabbitmqctl add_user root password
3)删除一个用户:rabbitmqctl delete_user test
4)修改用户密码:rabbitmqctl change_password root 123456
5)授予管理员角色:rabbitmqctl set_user_tags root administrator
6)授予用户权限:rabbitmqctl set_permissions -p / root “.” “.” “.*”
7)查看队列列表:rabbitmqctl list_queues
8)删除一个队列:rabbitmqctl delete_queue test
9)重置(如果忘记密码,可以进行应用重置,但是数据会丢失,慎用!!!):
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl start_app