RabbitMQ
2018-08-09
Ubuntu下快速安装:
apt install rabbitmq-server
rabbitmqctl基本操作
# 查看当前所有用户
$ rabbitmqctl list_users
# 查看默认guest用户的权限
$ rabbitmqctl list_user_permissions guest
# 由于RabbitMQ默认的账号用户名和密码都是guest。为了安全起见, 先删掉默认用户
$ rabbitmqctl delete_user guest
# 添加新用户
$ rabbitmqctl add_user username password
# 新增管理员用户
$ rabbitmqctl set_user_tags username administrator
# 赋予用户默认vhost的全部操作权限
$ rabbitmqctl set_permissions -p / username ".*" ".*" ".*"
# 查看用户的权限
$ rabbitmqctl list_user_permissions username
# 列出所有队列
$ rabbitmqctl list_queues
# 查看未确认队列
$ rabbitmqctl list_queues name messages_ready messages_unacknowledged
# 列出服务器上所有的交换器 直连交换机(direct), 主题交换机(topic), (头交换机)headers和 扇型交换机(fanout)
$ rabbitmqctl list_exchanges
# 列出所有现存的绑定
$ rabbitmqctl list_bindings
# 查看运行信息,版本等信息
$ rabbitmqctl status
配置
文件 /etc/rabbitmq/rabbitmq-env.conf
可以修改NODE_PORT来改变默认监听端口(默认5672)
也可修改/etc/rabbitmq/rabbitmq.config
[
{rabbit, [{tcp_listeners, [5670]}]}
].
如果两个文件中都进行了配置,rabbitmq会优先使用rabbtmq-env.conf中变量NODE_PORT的值作为真正的侦听端口。
配置 3.7.+
3.7版本以上支持新的配置语法,文件 /etc/rabbitmq/rabbitmq.conf
listeners.tcp.default = 21130
management.listener.port = 21131
mqtt.listeners.tcp.default = 21132
web_mqtt.tcp.port = 21133
# MQTT开启匿名远程访问
mqtt.allow_anonymous = true
loopback_users = none
开启web管理
rabbitmq-plugins enable rabbitmq_management
开启MQTT插件
rabbitmq-plugins enable rabbitmq_mqtt
开启 MQTT-over-WebSockets
rabbitmq-plugins enable rabbitmq_web_mqtt
Ubuntu18.04 升级rabbitmq到3.7.x
1.卸载原来的rabbitmq-server
apt remove rabbitmq-server
2.安装Erlang
wget -O- https://packages.erlang-solutions.com/ubuntu/erlang_solutions.asc | apt-key add -
echo "deb https://packages.erlang-solutions.com/ubuntu bionic contrib" | tee /etc/apt/sources.list.d/rabbitmq.list
apt update
apt -y install erlang
3.安装rabbitmq-server
wget -O- https://dl.bintray.com/rabbitmq/Keys/rabbitmq-release-signing-key.asc | apt-key add -
wget -O- https://www.rabbitmq.com/rabbitmq-release-signing-key.asc | apt-key add -
echo "deb https://dl.bintray.com/rabbitmq/debian $(lsb_release -sc) main" | tee /etc/apt/sources.list.d/rabbitmq.list
apt update
apt -y install rabbitmq-server
注意:rabbitmq 3.7.x 支持新的配置语法,不过配置文件名是rabbitmq.conf不是原来的rabbitmq.config
web管理配置文件
在/etc/rabbitmq/rabbitmq.config文件里
修改web管理端口(默认web管理端口15672)
[{rabbitmq_management, [{listener, [{port, 12345}]}]}].
开启guest远程访问(默认密码:guest)
[{rabbit, [{loopback_users, []}]}].
最终配置
[
{rabbitmq_management, [{listener, [{port, 11131}]}]},
{rabbit, [{tcp_listeners, [11130]},{loopback_users, []}]}
].
PHP + RabbitMQ
首先确保bcmath扩展已装
apt install php7.2-bcmath
然后安装 php-amqplib
composer require php-amqplib/php-amqplib
eg.
持久化+消息确认
发布 send.php
<?php
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
$connection = new AMQPStreamConnection('192.168.1.45', 11111, 'u1', 'u1');
$channel = $connection->channel();
$body = 'hi bric';
$queue_name = 'Hello_ERP';
$channel->queue_declare($queue_name, false, true, false, false);
$msg = new AMQPMessage($body,
array('delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT)
);
$channel->basic_publish($msg, '', $queue_name);
echo " [x] Sent '${body}'\n";
$channel->close();
$connection->close();
die;
接收 Receive.php
<?php
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
$queue_name = 'Hello_16988';
$connection = new AMQPStreamConnection('192.168.1.45', 11111, 'u1', 'u1');
$channel = $connection->channel();
$channel->queue_declare($queue_name, false, true, false, false);
echo " [*] Waiting for messages. To exit press CTRL+C\n";
$callback = function ($msg) {
echo " [x] Received ", $msg->body, "\n";
$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
};
$channel->basic_consume($queue_name, '', false, false, false, false, $callback);
while (count($channel->callbacks)) {
$channel->wait();
}
参考文档
https://xiaoxiami.gitbook.io/rabbitmq_into_chinese_php/