安装rabbitmq客户端
- 安装rabbitmq-c库
yum install librabbitmq -y
- 安装rabbitmq-c
wget https://github.com/alanxz/rabbitmq-c/archive/v0.10.0.tar.gz
- 解压编译安装
tar -zxvf v0.10.0.tar.gz && cd rabbitmq-c-0.10.0 && mkdir build && cd build && cmake -DCMAKE_INSTALL_PREFIX=/usr/local/rabbitmq-c .. && cmake --build . --target install
安装php的amqp扩展
- 安装扩展支持
yum install librabbitmq-devel && wget http://pecl.php.net/get/amqp-1.10.2.tgz && tar -zxvf amqp-1.10.2.tgz && cd amqp-1.10.2 && phpize && ./configure --with-php-config=/usr/local/php/bin/php-config --with-amqp --with-librabbitmq-dir=/usr/local/rabbitmq-c && make && make install
- 添加扩展支持
编辑php.ini添加:extension=amqp.so
- 重启php
发送消息(send.php)
<?php
//创建连接和channel
$amqpConnection = new AMQPConnection([
'host' => '127.0.0.1',
'port' => '5672',
'login' => 'guest',
'password' => 'guest',
'vhost' => '/'
]);
//交换机名
$exchange_name = 'test_exchange_name';
//队列名
$queue_name = 'test_queue_name';
//路由key
$route_key = 'test_route_key';
//创建连接和channel
if (!$amqpConnection->connect()) {
die("Cannot connect to the broker!\n");
}
$amqpChannel = new AMQPChannel($amqpConnection);
//创建交换机
$amqpExchange = new AMQPExchange($amqpChannel);
$amqpExchange->setName($exchange_name);
$amqpExchange->setType(AMQP_EX_TYPE_DIRECT); //direct类型
$amqpExchange->setFlags(AMQP_DURABLE); //持久化
echo "Exchange Status:" . $amqpExchange->declareExchange() . PHP_EOL;
echo "Send Message:" . $amqpExchange->publish(json_encode(['user_id' => 1,'time' => time()]),$route_key). PHP_EOL;
接收消息(receive.php)
<?php
//创建 RabbitMQ 连接
$amqpConnection = new AMQPConnection([
'host' => '127.0.0.1',
'port' => '5672',
'login' => 'guest',
'password' => 'guest',
'vhost' => '/'
]);
//交换机名
$exchange_name = 'test_exchange_name';
//队列名
$queue_name = 'test_queue_name';
//路由key
$route_key = 'test_route_key';
//创建连接和channel
if (!$amqpConnection->connect()) {
die("Cannot connect to the broker!\n");
}
$amqpChannel= new AMQPChannel($amqpConnection);
//创建交换机
$amqpExchange= new AMQPExchange($amqpChannel);
$amqpExchange->setName($exchange_name);
$amqpExchange->setType(AMQP_EX_TYPE_DIRECT); //direct类型
$amqpExchange->setFlags(AMQP_DURABLE); //持久化
echo "Exchange Status:" . $amqpExchange->declareExchange() . "\n";
//创建队列
$amqpQueue= new AMQPQueue($amqpChannel);
$amqpQueue->setName($queue_name);
$amqpQueue->setFlags(AMQP_DURABLE); //持久化
echo "Message Total:" . $amqpQueue->declareQueue() . "\n";
//绑定交换机与队列,并指定路由键
echo 'Queue Bind: ' . $amqpQueue->bind($exchange_name, $route_key) . "\n";
//阻塞模式接收消息
echo "Message:\n";
while (true) {
$amqpQueue->consume(function ($envelope, $queue) {
/**
* @var AMQPEnvelope $envelope
* @var AMQPQueue $queue
*/
$msg = $envelope->getBody();
echo $msg . "\n"; //处理消息
$queue->ack($envelope->getDeliveryTag()); //手动ACK应答确认
});
// 自动ACK应答确认
// $amqpQueue->consume(function ($envelope, $queue) {
// /**
// * @var AMQPEnvelope $envelope
// * @var AMQPQueue $queue
// */
// $msg = $envelope->getBody();
// echo $msg . "\n"; //处理消息
// },AMQP_AUTOACK);
}
$amqpConnection->disconnect();
测试
- 通过命令行执行:
php receive.php
- 通过浏览器访问:
http://localhost/send.php
以上信息是本人翻阅了好多例子后实践整理的结果,本地已经可以跑通,各位看官和有需要的朋友如果觉得好请给个赞啊!
相关资料整理
从rabbitmq服务端安装到使用,包含rabbitmq management安装
redis发布订阅模式下实现消息队列和rabbitmq的对比