💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
### 1、下载 & 安装 Erlang 因为RabbitMQ的服务端是基于 Erlang编写的,所以,首先需要安装Erlang。 **1)下载** 下载地址如下: [https://www.erlang.org/downloads](https://www.erlang.org/downloads) (此处下载比较慢,可以参考如下百度网盘) 链接:[https://pan.baidu.com/s/1oXq5QX3IlzRq5L-mk1gC4Q](https://pan.baidu.com/s/1oXq5QX3IlzRq5L-mk1gC4Q) 提取码:**luna** ![](https://img2020.cnblogs.com/blog/1148440/202110/1148440-20211019202908382-1774778501.png) **2)安装** 选择相应的安装目录,一路next即可 **3)配置环境变量** 在系统变量中新建变量 变量名(N):ERLANG\_HOME 变量值(V):H:\\SW\_install\\erl-24.1 (注意:变量值需要根据自己的实际情况进行修改) ![](https://img2020.cnblogs.com/blog/1148440/202110/1148440-20211019203228949-1129079437.png) 将;%ERLANG\_HOME%\\bin添加到Path变量值的最后 **4)验证环境变量配置的正确性** ~~~ echo %ERLANG_HOME% ~~~ ![](https://img2020.cnblogs.com/blog/1148440/202110/1148440-20211019204315001-1361818414.png) ### 2、下载 & 安装 RabbitMQ **1)下载** 下载地址如下: [https://www.rabbitmq.com/install-windows.html#installer](https://www.rabbitmq.com/install-windows.html#installer) ![](https://img2020.cnblogs.com/blog/1148440/202110/1148440-20211019203815114-897881368.png) **2)安装** 选择相应的安装目录,一路next即可 **3)配置环境变量** 在系统变量中新建变量 变量名(N):RABBITMQ\_SERVER 变量值(V):H:\\SW\_install\\RabbitMQ Server\\rabbitmq\_server-3.9.7 (注意:变量值需要根据自己的实际情况进行修改) ![](https://img2020.cnblogs.com/blog/1148440/202110/1148440-20211019203924655-404997970.png) 将;%RABBITMQ\_SERVER%\\sbin添加到Path变量值的最后 ![](https://img2020.cnblogs.com/blog/1148440/202110/1148440-20211019204720538-555503162.png) **4)验证环境变量配置的正确性** ~~~ echo %RABBITMQ_SERVER% ~~~ ![](https://img2020.cnblogs.com/blog/1148440/202110/1148440-20211019204438371-1180067271.png) **5)安装 rabbitmq-plugins** 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目录,再次执行插件安装,即可解决。 **6)启动 RabbitMQ 服务** ~~~ net start RabbitMQ ~~~ 或者在rabbitmq的sbin目录下,双击执行rabbitmq-server.bat文件。 **7)停止 RabbitMQ 服务** ~~~ net stop RabbitMQ ~~~ **8)查看服务状态** ~~~ rabbitmqctl status ~~~ **9)页面访问 RabbitMQ** **http://localhost:15672** 账号/密码:guest/guest ![](https://img2020.cnblogs.com/blog/1148440/202110/1148440-20211019210151110-1048817845.png) 登录后,页面如下: ![](https://img2020.cnblogs.com/blog/1148440/202110/1148440-20211019210238246-1087954201.png) 可以通过页面,添加新的用户(Admin--> Users--> Add a user --> Add user) ![](https://img2020.cnblogs.com/blog/1148440/202110/1148440-20211019211555362-188068244.png) ### 3、RabbitMQ 的简单使用 **1)RabbitMQ 的 pom 引用** [![复制代码](https://common.cnblogs.com/images/copycode.gif)](javascript:void(0); "复制代码") ~~~ <!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client --> <dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version>5.13.1</version> </dependency> ~~~ [![复制代码](https://common.cnblogs.com/images/copycode.gif)](javascript:void(0); "复制代码") **2)消息发送方** [![复制代码](https://common.cnblogs.com/images/copycode.gif)](javascript:void(0); "复制代码") ~~~ 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(); } } ~~~ [![复制代码](https://common.cnblogs.com/images/copycode.gif)](javascript:void(0); "复制代码") 运行结果如下: ~~~ Send 'Hello, RabbitMQ!' ~~~ **3)消息接收方** [![复制代码](https://common.cnblogs.com/images/copycode.gif)](javascript:void(0); "复制代码") ~~~ 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); } } ~~~ [![复制代码](https://common.cnblogs.com/images/copycode.gif)](javascript:void(0); "复制代码") 运行结果如下: ~~~ Waiting for messages... Received 'Hello, RabbitMQ!' ~~~ 可以在Queues页签中,看到所有队列中的消息数量 ![](https://img2020.cnblogs.com/blog/1148440/202110/1148440-20211019212707001-1014332441.png) ### 4、常用命令 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 https://www.cnblogs.com/miracle-luna/p/15426463.html