一、Homebrew安装
二、安装Rabbitmq
1、更新homebrew
brew update
2、安装指令,brew会自动安装rabbitmq所需的依赖,包括erlang
brew install rabbitmq
3、网络原因,会出现类似如下的错误,稍后重新执行安装指令即可
4、等待安装,直到出现如下图,则已经安装完成
看到端口默认为15672
三、配置Mac环境变量
1、在用户目录下
cd ~
2、编辑.bash_profile
文件
open -e .bash_profile
3、将下面代码拷贝到上述文件中,保存【注意:rabbitmq版本号一致】
export RABBITMQ_HOME=/usr/local/Cellar/rabbitmq/3.7.16
export PATH=$PATH:$RABBITMQ_HOME/sbin
4、继续终端输入以下指令,即可生效
source .bash_profile
四、Rabbitmq服务启动
1、启动服务
sudo rabbitmq-server
2、安装RabiitMQ的可视化监控插件,新建终端(Command+N)
cd /usr/local/Cellar/rabbitmq/3.7.16/sbin
// 启用rabbitmq management插件
sudo sbin/rabbitmq-plugins enable rabbitmq_management
3、访问可视化后台管理页面(默认的用户名密码都是guest)
http://localhost:15672
五、测试运行
1、利用官方测试工具进行测试(亦可自己写个生产者消费者进行测试)
Getting started with RabbitMQ — RabbitMQ
官方提供的代码:
-
发送端:
Send.java
/** * RabbitmMQ官方发送端 * @date 2019.06.26 * @author yawu */ public class Send { private final static String QUEUE_NAME = "hello"; public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); try (Connection connection = factory.newConnection(); Channel channel = connection.createChannel()) { channel.queueDeclare(QUEUE_NAME, false, false, false, null); String message = "Hello World!"; channel.basicPublish("", QUEUE_NAME, null, message.getBytes()); System.out.println(" [x] Sent '" + message + "'"); } } }
-
接收端:
Recv.java
/** * RabbitmMQ官方接收端 * * @author yawu * @date 2019.06.26 */ public class Recv { private final static String QUEUE_NAME = "hello"; public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); DeliverCallback deliverCallback = (consumerTag, delivery) -> { String message = new String(delivery.getBody(), "UTF-8"); System.out.println(" [x] Received '" + message + "'"); }; channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> { }); } }
2、运行发送端接收端代码,观察后台管理页面情况