今天小编来讲解一下如何通过PHP来实现RabbitMQ的消息发送与接收。本文代码不采用任何类库,后续会写一篇文章来讲解php-amqplib的使用
1.消息发布rabbit_publisher.php
<?php
//connect的配置信息
$conn_args = [
'host' => '127.0.0.1',
'port' => '5672',
'login' => 'guest',
'password' => 'guest',
'vhost'=>'/'
];
$ex_name = 'e_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($ex_name);
date_default_timezone_set("Asia/Shanghai");
//发送消息
for($i=0; $i<5; ++$i){
sleep(1);//休眠1秒
//消息内容
$message = "TEST MESSAGE!" . date("h:i:sa");
echo "Send Message:" . $ex->publish($message, $k_route) . PHP_EOL;
}
$conn->disconnect();
2.消息消费rabbit_consumer.php
<?php
//配置信息
$conn_args = array(
'host' => '127.0.0.1',
'port' => '5672',
'login' => 'guest',
'password' => 'guest',
'vhost'=>'/'
);
$ex_name = 'e_linvo'; //交换机名
$queue_name = 'q_linvo'; //队列名
$k_route = 'key_1'; //路由key
//创建连接和channel
$conn = new AMQPConnection($conn_args);
if (!$conn->connect()) {
die("Cannot connect to the broker!" . PHP_EOL);
}
$channel = new AMQPChannel($conn);
//创建交换机
$ex = new AMQPExchange($channel);
$ex->setName($ex_name);
$ex->setType(AMQP_EX_TYPE_DIRECT); //direct类型 如果 routing key 匹配,那么 Message 就会被传递到相应的 queue
$ex->setFlags(AMQP_DURABLE); //持久化
echo "Exchange Status:" . $ex->declareExchange() . PHP_EOL;
//创建队列
$q = new AMQPQueue($channel);
$q->setName($queue_name);
$q->setFlags(AMQP_DURABLE); //持久化
echo "Message Total:".$q->declareQueue() . PHP_EOL;
//绑定交换机与队列,并指定路由键
echo 'Queue Bind: '.$q->bind($ex_name, $k_route) . PHP_EOL;
//阻塞模式接收消息
echo "Message:" . PHP_EOL;
while(True){
$q->consume('processMessage');
//$q->consume('processMessage', AMQP_AUTOACK); //自动ACK应答
}
$conn->disconnect();
/**
* 消费回调函数
* 处理消息
*/
function processMessage($envelope, $queue) {
$msg = $envelope->getBody();
echo $msg . PHP_EOL; //处理消息
$queue->ack($envelope->getDeliveryTag()); //手动发送ACK应答
}
processMessage中的$envelope
是一个对象
AMQPEnvelope Object
(
[content_type:AMQPBasicProperties:private] => text/plain
[content_encoding:AMQPBasicProperties:private] =>
[headers:AMQPBasicProperties:private] => Array
(
)
[delivery_mode:AMQPBasicProperties:private] => 1
[priority:AMQPBasicProperties:private] => 0
[correlation_id:AMQPBasicProperties:private] =>
[reply_to:AMQPBasicProperties:private] =>
[expiration:AMQPBasicProperties:private] =>
[message_id:AMQPBasicProperties:private] =>
[timestamp:AMQPBasicProperties:private] => 0
[type:AMQPBasicProperties:private] =>
[user_id:AMQPBasicProperties:private] =>
[app_id:AMQPBasicProperties:private] =>
[cluster_id:AMQPBasicProperties:private] =>
[body:AMQPEnvelope:private] => TEST MESSAGE!10:11:57am
[consumer_tag:AMQPEnvelope:private] => amq.ctag-6OFjCh0xTtUGPBE3sgp7Tw
[delivery_tag:AMQPEnvelope:private] => 1
[is_redelivery:AMQPEnvelope:private] => 1
[exchange_name:AMQPEnvelope:private] => e_linvo
[routing_key:AMQPEnvelope:private] => key_1
)
具体的说明可以查看php.net的官方文档AMQPQueue::consume
3.打开2个终端分别执行消费和发布文件
php rabbit_consumer.php
php rabbit_publisher.php
你将看到
发布端显示
Send Message:1
Send Message:1
Send Message:1
Send Message:1
Send Message:1
消费端显示
Exchange Status:1
Message Total:5
Queue Bind: 1
Message:
TEST MESSAGE!10:27:34
TEST MESSAGE!10:27:35
TEST MESSAGE!10:27:36
TEST MESSAGE!10:27:37
TEST MESSAGE!10:27:38
网友评论