PHP中使用RabbitMQ

目录
  1. Windows下安装
    1. 下载安装 erlang 和 rabbitmq
    2. 使用命令 erl 和 rabbitmq-server 查看是否安装成功
    3. 启动rabbitmq
    4. rabbitmq 管理页面
      1. 添加插件 rabbitmq_management
  2. Centos7安装Erlang和RabbitMq
    1. 安装依赖
    2. Erlang安装
      1. 下载
      2. 安装
      3. 添加环境变量
    3. RabbitMq安装
      1. erlang和rabbitmq版本对照地址
      2. 下载
      3. 安装
      4. 添加环境变量
      5. 创建配置目录
      6. 启动
      7. 状态
      8. 停止
    4. 用户配置
      1. 添加一个用户
      2. 配置权限
      3. 查看用户权限
      4. 设置tag
      5. 用户列表
      6. 删除用户
    5. rabbitmq 管理页面
      1. 添加插件 rabbitmq_management
  3. php-amqplib使用
    1. composer.json配置
    2. config.php
    3. recive.php
    4. send.php
    5. 查看队列

Windows下安装

下载安装 erlangrabbitmq

erlang下载地址:http://erlang.org/download/otp_win64_23.1.exe

rabbitmq下载地址:https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.8.9/rabbitmq-server-3.8.9.exe

使用命令 erlrabbitmq-server 查看是否安装成功

安装路径自行选择,添加环境变量

erlang路径:D:\soft\erl10.0.1\bin

rabbitmq路径:D:\soft\RabbitMQ Server\rabbitmq_server-3.7.7\sbin

启动rabbitmq

进入到D:\soft\RabbitMQ Server\rabbitmq_server-3.7.7\sbin目录下,执行rabbitmq-server.bat文件

rabbitmq 管理页面

添加插件 rabbitmq_management

方便在网页上管理和查看消息队列相关信息,执行命令:rabbitmq-plugins.bat enable rabbitmq_management

在浏览器打开:http://localhost:15672/,默认账号密码均为 guest

Centos7安装Erlang和RabbitMq

安装依赖

1
yum -y install gcc glibc-devel make ncurses-devel openssl-devel xmlto perl wget gtk2-devel binutils-devel

Erlang安装

下载

1
wget http://erlang.org/download/otp_src_23.1.tar.gz

安装

1
2
3
4
5
tar zxvf otp_src_23.1.tar.gz
cd otp_src_23.1
./configure --prefix=/usr/local/erlang
make
make install

如果遇到 ./configure: line 4659: wx-config: command not found错误,可以忽略

添加环境变量

1
2
3
4
5
6
7
8
9
vim ~/.bash_profile
#在PATH后面继续添加 :/usr/local/erlang/bin
source ~/.bash_profile

[root@test1 bin]# erl
Erlang/OTP 23 [erts-11.1] [source] [64-bit] [smp:2:2] [ds:2:2:10] [async-threads:1] [hipe]

Eshell V11.1 (abort with ^G)
1> halt().

erl进入之后,输入halt().退出来

RabbitMq安装

erlang和rabbitmq版本对照地址

1
https://www.rabbitmq.com/which-erlang.html

下载

1
wget https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.8.9/rabbitmq-server-generic-unix-3.8.9.tar.xz

安装

1
2
3
4
5
xz -d rabbitmq-server-generic-unix-3.8.9.tar.xz
tar -xf rabbitmq-server-generic-unix-3.8.9.tar
mv rabbitmq_server-3.8.9 /usr/local/
cd /usr/local/
mv rabbitmq_server-3.8.9 rabbitmq_server

添加环境变量

1
2
3
vim ~/.bash_profile
#在PATH后面继续添加 :/usr/local/rabbitmq_server/sbin
source ~/.bash_profile

创建配置目录

1
mkdir /etc/rabbitmq

启动

1
rabbitmq-server -detached

状态

1
rabbitmqctl status

停止

1
rabbitmqctl stop

用户配置

添加一个用户

1
rabbitmqctl add_user xpmozong 123456

配置权限

1
rabbitmqctl set_permissions -p "/" xpmozong ".*" ".*" ".*"

查看用户权限

1
rabbitmqctl list_user_permissions xpmozong

设置tag

1
rabbitmqctl set_user_tags xpmozong administrator

用户列表

1
rabbitmqctl list_users

删除用户

1
rabbitmqctl delete_user guest

rabbitmq 管理页面

添加插件 rabbitmq_management

方便在网页上管理和查看消息队列相关信息,执行命令:rabbitmq-plugins enable rabbitmq_management

在浏览器打开:http://192.168.0.101:15672/, guest只能本地使用,可以使用xpmozong登录

php-amqplib使用

composer.json配置

1
2
3
4
5
{
"require": {
"php-amqplib/php-amqplib": "^2.12.0"
}
}

config.php

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
return [
'vendor' => [
'path' => dirname(__DIR__) . '/vendor'
],
'rabbitmq' => [
'host' => '127.0.0.1',
'port' => '5672',
'login' => 'guest',
'password' => 'guest',
'vhost' => '/'
]
];

recive.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php

$config = require "../config/config.php";

require_once $config['vendor']['path'] . '/autoload.php';

use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Wire\AMQPReader;

$connection = new AMQPStreamConnection($config['rabbitmq']['host'], $config['rabbitmq']['port'],
$config['rabbitmq']['login'], $config['rabbitmq']['password'], $config['rabbitmq']['vhost']);
$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(count($channel->callbacks)) {
$channel->wait();
}

$channel->close();
$connection->close();

send.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php

$config = require "../config/config.php";

require_once $config['vendor']['path'] . '/autoload.php';

use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

$connection = new AMQPStreamConnection($config['rabbitmq']['host'], $config['rabbitmq']['port'],
$config['rabbitmq']['login'], $config['rabbitmq']['password'], $config['rabbitmq']['vhost']);
$channel = $connection->channel();

//发送方其实不需要设置队列, 不过对于持久化有关,建议执行该行
$channel->queue_declare('hello', false, false, false, false);

$msg = new AMQPMessage('Hello World!');
$channel->basic_publish($msg, '', 'hello');

var_dump($msg->getMessageCount());

echo " [x] Sent 'Hello World!'\n";

$channel->close();
$connection->close();

查看队列

http://localhost:15672/#/queues 可以在这里看队列情况。