一、使用Docker安装部署RabbitMQ
1、docker search rabbitmq:management
2、docker pull rabbitmq:management
注:如果docker pull rabbitmq 后面不带management,启动rabbitmq后是无法打开管理界面的,所以我们要下载带management插件的rabbitmq.
3、创建rabbitmq容器的shell脚本(创建容器、启动服务、添加admin用户)
#/bin/sh
docker run --name rabbitmq --publish 5671:5671 --publish 5672:5672 --publish 4369:4369 --publish 25672:25672 --publish 15671:15671 --publish 15672:15672 -dit rabbitmq:management /bin/bash
&& docker exec -d rabbitmq /bin/bash -c "rabbitmq-server --detached" && echo "starting..."
&& docker exec -d rabbitmq /bin/bash -c "sleep 10s; rabbitmqctl add_user admin 123456 && rabbitmqctl set_user_tags admin administrator && rabbitmqctl set_permissions -p '/' admin '.*' '.*' '.*'"
&& docker ps
4、停止容器的shell脚本
#/bin/sh
docker stop rabbitmq && docker rm rabbitmq && echo "success"
5、容器启动之后就可以访问web 管理端了 http://宿主机IP:15672,默认用户为guest(密码也是 guest),但只能访问localhost。
二、PHP安装扩展AMQP
1. 安装rabbitmq-c,rabbitmq-c是一个用于C语言的,与AMQP server进行交互的client库。
下载地址:https://github.com/alanxz/rabbitmq-c
unzip rabbitmq-c-master.zip
cd rabbitmq-c-master
mkdir /opt/rabbitmq-c
mkdir build && cd build
cmake -DCMAKE_INSTALL_PREFIX=/opt/rabbitmq-c ..
cmake --build . --target install
2. 从https://pecl.php.net/package/amqp下载 amqp-1.9.3.tgz
wget https://pecl.php.net/get/amqp-1.9.3.tgz
tar -zxvf amqp-1.9.3.tgz
cd amqp-1.9.3
/opt/php72/bin/phpize
./configure --with-php-config=/opt/php72/bin/php-config --with-amqp --with-librabbitmq-dir=/opt/rabbitmq-c
make && make install
3. 然后在php.ini添加 extension=amqp.so,然后执行/opt/php72/bin/php -m,看列出的已安装扩展是否存在amqp。
三、测试
1. 新建rabbit_consumer.php作为消费者
//配置信息
$conn_args = array(
'host' => '127.0.0.1',
'port' => '5672',
'login' => 'guest',
'password' => 'guest',
'vhost'=>'/'
);
$e_name = 'e_linvo'; //交换机名
$q_name = 'q_linvo'; //队列名
$k_route = 'key_1'; //路由key
//创建连接和channel
$conn = new AMQPConnection($conn_args);
if (!$conn->connect()) {
die("Cannot connect to the broker!n");
}
$channel = new AMQPChannel($conn);
//创建交换机
$ex = new AMQPExchange($channel);
$ex->setName($e_name);
$ex->setType(AMQP_EX_TYPE_DIRECT); //direct类型
$ex->setFlags(AMQP_DURABLE); //持久化
echo "Exchange Status:".$ex->declare()."n";
//创建队列
$q = new AMQPQueue($channel);
$q->setName($q_name);
$q->setFlags(AMQP_DURABLE); //持久化
echo "Message Total:".$q->declare()."n";
//绑定交换机与队列,并指定路由键
echo 'Queue Bind: '.$q->bind($e_name, $k_route)."n";
//阻塞模式接收消息
echo "Message:n";
while(True){
$q->consume('processMessage');
//$q->consume('processMessage', AMQP_AUTOACK); //自动ACK应答
}
$conn->disconnect();
/**
* 消费回调函数
* 处理消息
*/
function processMessage($envelope, $queue) {
$msg = $envelope->getBody();
echo $msg."n"; //处理消息
$queue->ack($envelope->getDeliveryTag()); //手动发送ACK应答
}
?>
2. 新建rabbit_publisher.php作为生产者
//配置信息
$conn_args = array(
'host' => '127.0.0.1',
'port' => '5672',
'login' => 'guest',
'password' => 'guest',
'vhost'=>'/'
);
$e_name = 'e_linvo'; //交换机名
//$q_name = 'q_linvo'; //无需队列名
$k_route = 'key_1'; //路由key
//创建连接和channel
$conn = new AMQPConnection($conn_args);
if (!$conn->connect()) {
die("Cannot connect to the broker!n");
}
$channel = new AMQPChannel($conn);
//创建交换机对象
$ex = new AMQPExchange($channel);
$ex->setName($e_name);
date_default_timezone_set("Asia/Shanghai");
//发送消息
//$channel->startTransaction(); //开始事务
for($i=0; $i<5; ++$i){
sleep(1);//休眠1秒
//消息内容
$message = "TEST MESSAGE!".date("h:i:sa");
echo "Send Message:".$ex->publish($message, $k_route)."n";
}
//$channel->commitTransaction(); //提交事务
$conn->disconnect();
?>
3. 运行生产者,输出结果
4. 运行消费者,输出结果
参考:
https://blog.csdn.net/liyuejin/article/details/78410586
https://www.cnblogs.com/zhming26/p/6141028.html
https://www.cnblogs.com/miketwais/p/RabbitMQ.html