MacOS下安装RabbitMQ

2,155 阅读1分钟

1、安装 brew install rabbitmq

☁  brew install rabbitmq
=> Installing rabbitmq
==> Downloading https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.7.16/rabbitmq-server-generic-unix-3.7.16.tar.xz
==> Downloading from https://github-production-release-asset-2e65be.s3.amazonaws.com/924551/fb0f9b80-a115-11e9-9614-f1167417e0a4?X-Amz-Algori
######################################################################## 100.0%
==> /usr/bin/unzip -qq -j /usr/local/Cellar/rabbitmq/3.7.16/plugins/rabbitmq_management-3.7.16.ez rabbitmq_management-3.7.16/priv/www/cli/rab
==> Caveats
Management Plugin enabled by default at http://localhost:15672

Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

To have launchd start rabbitmq now and restart at login:
  brew services start rabbitmq
Or, if you don't want/need a background service you can just run:
  rabbitmq-server
==> Summary
🍺  /usr/local/Cellar/rabbitmq/3.7.16: 237 files, 14.4MB, built in 25 seconds
==> Caveats
==> erlang
Man pages can be found in:
  /usr/local/opt/erlang/lib/erlang/man

Access them with `erl -man`, or add this directory to MANPATH.
==> rabbitmq
Management Plugin enabled by default at http://localhost:15672

Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

2、加入环境变量

export PATH=$PATH:/usr/local/opt/rabbitmq/sbin


先通过上面两步,安装rabbitmq服务,参考官网

然后要启动,因为上一步加入环境变量,所以任何目录都能启动 

☁  Jarvis  rabbitmq-server

  ##  ##
  ##  ##      RabbitMQ 3.7.16. Copyright (C) 2007-2019 Pivotal Software, Inc.
  ##########  Licensed under the MPL.  See https://www.rabbitmq.com/
  ######  ##
  ##########  Logs: /usr/local/var/log/rabbitmq/rabbit@localhost.log
                    /usr/local/var/log/rabbitmq/rabbit@localhost_upgrade.log

              Starting broker...
 completed with 6 plugins.

此时在浏览器输入http://localhost:15672即可进入rabbitmq控制终端登录页面,默认用户名和密码为 guest/guest.


此时能监测到服务状态  

1、php Sending.php

<?phprequire_once __DIR__.'/vendor/autoload.php';use PhpAmqpLib\Connection\AMQPStreamConnection;use PhpAmqpLib\Message\AMQPMessage;$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');$channel = $connection->channel();$channel->queue_declare('hello', false, false, false, false);$msg = new AMQPMessage('Hello World!');$channel->basic_publish($msg, '', 'hello');echo " [x] Sent 'Hello World!'\n";$channel->close();$connection->close();


2、php Receiving.php

<?phprequire_once __DIR__ . '/vendor/autoload.php';use PhpAmqpLib\Connection\AMQPStreamConnection;$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');$channel = $connection->channel();$channel->queue_declare('hello', false, false, false, false);echo " [*] Waiting for messages. To exit press CTRL+C\n";$callback = function ($msg) {  echo ' [x] Received ', $msg->body, "\n";};$channel->basic_consume('hello', '', false, true, false, false, $callback);while ($channel->is_consuming()) {    $channel->wait();}


于是乎

☁  amqplib  php Sending.php
 [x] Sent 'Hello World!'

☁  amqplib  php Receiving.php
 [*] Waiting for messages. To exit press CTRL+C
 [x] Received Hello World!
 [x] Received Hello World!
 [x] Received Hello World!
 [x] Received Hello World!