生产者:

正常的生产消息,不过再消息头部设置了一个参数 表示消息的尝试次数

$msg = new AMQPMessage('Hello World');
$headers = new \PhpAmqpLib\Wire\AMQPTable([
"retry_nums"=>0
]);
$msg->set('application_headers', $headers);
$channel->basic_publish($msg,$exchange,$queue);



<?php
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;


$queue = "cache_queue";
$exchange = "cache_exchange";



//获取连接
$connection = new AMQPStreamConnection('192.168.2.245', 5672, 'guest', 'guest');
//从连接中创建通道
$channel = $connection->channel();


$channel->exchange_declare($exchange, 'direct',false,true,false);



$channel->queue_declare($queue,false,true,false,false,false);
$channel->queue_bind($queue, $exchange,$queue);



$msg = new AMQPMessage('Hello World');
$headers = new \PhpAmqpLib\Wire\AMQPTable([
"retry_nums"=>0
]);
$msg->set('application_headers', $headers);
$channel->basic_publish($msg,$exchange,$queue);
echo date('Y-m-d H:i:s')." [x] Sent 'Hello World!' ".PHP_EOL;




//while ($wait) {
// $channel->wait();
//}

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



消费者:

消费者假如消费异常或者失败,则把消息放入一个临时队列  暂且命名为

rety_cache_exchange
rety_cache_queue_10


临时队列rety_cache_queue_10 设置一个ttl过期时间 ,然后绑定一个死信队列(死信队列为原始生产消息队列);这样当消息过期了,消息自动进入死信队列(原始队列);就实现了 消息尝试的机制
设置头部参数
retry_nums++  的目的 就是为了得到消息已经消费了多少次  ;可以当消息达到消费5次后 就告警 入库db操作
<?php
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;


$queue = "cache_queue";
$exchange = "cache_exchange";

$rety_queue = "rety_cache_queue_10";
$rety_exchange = "rety_cache_exchange";


//获取连接
$connection = new AMQPStreamConnection('192.168.2.245', 5672, 'guest', 'guest');
//从连接中创建通道
$channel = $connection->channel();

$channel->exchange_declare($exchange, 'direct',false,true,false);

$channel->queue_declare($queue,false,true,false,false,false);
$channel->queue_bind($queue, $exchange,$queue);


$channel2 = $connection->channel();

$channel2->exchange_declare($rety_exchange, 'direct',false,true,false);
$tale = new \PhpAmqpLib\Wire\AMQPTable();
$tale->set('x-dead-letter-exchange', $exchange);
$tale->set('x-dead-letter-routing-key',$queue);
$tale->set('x-message-ttl',50000);
$channel2->queue_declare($rety_queue,false,true,false,false,false,$tale);
$channel2->queue_bind($rety_queue, $rety_exchange,$rety_queue);








$channel->basic_qos(null, 1, null);
$channel->basic_consume($queue, '', false, false, false, false, function(AMQPMessage $message) use ($rety_exchange,$rety_queue,$channel2){
echo $message->body,PHP_EOL;
$msg_headers = $message->get('application_headers')->getNativeData();
var_dump($msg_headers);
$message->delivery_info['channel']->basic_ack($message->delivery_info['delivery_tag']);
$msg = new AMQPMessage($message->body);
$headers = new \PhpAmqpLib\Wire\AMQPTable([
"retry_nums"=>intval($msg_headers['retry_nums'])+1
]);
$msg->set('application_headers', $headers);
$channel2->basic_publish($msg,$rety_exchange,$rety_queue);


});

function shutdown($channel, $connection)
{
$channel->close();
$connection->close();
}
register_shutdown_function('shutdown', $channel, $connection);

while (count($channel->callbacks)) {
$channel->wait();
}