前言
之前有写过死信队列的使用场景以及通过管控台创建死信。这次就通过代码实现死信队列的创建,同时也分享一下RabbitMQ封装的类。
准备
- 先准备一个死信队列(最后用来消费)的参数配置,包括虚拟机,交换机,队列,有效时间等,如下。
- 按照上面在RabbitMQ中创建虚拟机和交换机,死信队列。并让交换机与死信队列绑定,操作方法前面有介绍。
- 这里就直接提供rabbitMQ操作的基本封装的类,包括一个基类,生产者类,消费者类。
3.1. 基类。
<?php
namespace rabbitmq;
/** Member
* AMQPChannel
* AMQPConnection
* AMQPEnvelope
* AMQPExchange
* AMQPQueue
* Class BaseMQ
* @package rabbitMQ
*/
class BaseMQ
{
/** MQ Channel
* @var \AMQPChannel
*/
public $AMQPChannel ;
/** MQ Link
* @var \AMQPConnection
*/
public $AMQPConnection ;
/** MQ Envelope
* @var \AMQPEnvelope
*/
public $AMQPEnvelope ;
/** MQ Exchange
* @var \AMQPExchange
*/
public $AMQPExchange ;
/** MQ Queue
* @var \AMQPQueue
*/
public $AMQPQueue ;
/** conf
* @var
*/
public $conf ;
/** exchange
* @var
*/
public $exchange ;
/**
* queue
* @var
*/
public $queue;
/**
* routes
* @var
*/
public $route;
/**
* queue_args
* @var
*/
public $queueArgs;
/** link
* BaseMQ constructor.
* @throws \AMQPConnectionException
*/
public function __construct($host,$options,$args = [])
{
$config = include 'config/config.php';
if (!$config)
throw new \AMQPConnectionException('config error!');
$this->host = array_merge($config,$host);
isset($options['vhost']) && $this->host['vhost'] = $options['vhost'];
$this->exchange = $options['exchange'];
$this->queue = $options['queue'];
$this->route = $options['route'];
$this->queueArgs = $args;
$this->AMQPConnection = new \AMQPConnection($this->host);
if (!$this->AMQPConnection->connect())
throw new \AMQPConnectionException("Cannot connect to the broker!\n");
}
/**
* close link
*/
public function close()
{
$this->AMQPConnection->disconnect();
}
/** Channel
* @return \AMQPChannel
* @throws \AMQPConnectionException
*/
public function channel()
{
if (!$this->AMQPChannel) {
$this->AMQPChannel = new \AMQPChannel($this->AMQPConnection);
}
return $this->AMQPChannel;
}
/** Exchange
* @return \AMQPExchange
* @throws \AMQPConnectionException
* @throws \AMQPExchangeException
*/
public function exchange()
{
if (!$this->AMQPExchange) {
$this->AMQPExchange = new \AMQPExchange($this->channel());
$this->AMQPExchange->setName($this->exchange);
}
return $this->AMQPExchange ;
}
/** queue
* @return \AMQPQueue
* @throws \AMQPConnectionException
* @throws \AMQPQueueException
*/
public function queue()
{
if (!$this->AMQPQueue) {
$this->AMQPQueue = new \AMQPQueue($this->channel());
}
return $this->AMQPQueue ;
}
/** Envelope
* @return \AMQPEnvelope
*/
public function envelope()
{
if (!$this->AMQPEnvelope) {
$this->AMQPEnvelope = new \AMQPEnvelope();
}
return $this->AMQPEnvelope;
}
}
- 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.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
3.2. 生产者类。
<?php
//生产
namespace rabbitmq;
class ProductMQ extends BaseMQ
{
/** 只控制发送成功 不接受消费者是否收到
* @throws \AMQPChannelException
* @throws \AMQPConnectionException
* @throws \AMQPExchangeException
*/
public function publish($message)
{
$message = is_array($message)?json_encode($message):$message;
//频道
$channel = $this->channel();
//创建交换机对象
$ex = $this->exchange();
return $ex->publish($message, $this->route, AMQP_NOPARAM, array('delivery_mode' => 2));
}
}
- 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.
3.3. 消费者。
<?php
namespace rabbitmq;
class ConsumerMQ extends BaseMQ
{
/** 接受消息 如果终止 重连时会有消息
* @throws \AMQPChannelException
* @throws \AMQPConnectionException
* @throws \AMQPExchangeException
* @throws \AMQPQueueException
*/
public function run($processMessage)
{
// 创建交换机
$ex = $this->exchange();
// direct类型
$ex->setType(AMQP_EX_TYPE_DIRECT);
// 持久化
$ex->setFlags(AMQP_DURABLE);
// 不存在就发布
$ex->declareExchange();
// 创建队列
$q = $this->queue();
// 设置队列名称
$q->setName($this->queue);
// 持久化
$q->setFlags(AMQP_DURABLE);
// 队列参数
is_array($this->queueArgs) && $q->setArguments($this->queueArgs);
//echo "Message Total:".$q->declareQueue()."\n";
$q->declareQueue();
//绑定交换机与队列,并指定路由键
// echo 'Queue Bind: '.$q->bind($this->exchange, $this->route)."\n";
$q->bind($this->exchange, $this->route);
//阻塞模式接收消息
// echo "Message:\n";
if (!is_null($processMessage)) {
while (True) {
$q->consume($processMessage);
}
}
$this->close();
}
}
- 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.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
编码
上面的死信队列已经创建好了,接下来主要就是通过代码创建一个用于直接生产消息的普通队列,但是这个队列需要设置三个参数。
1. 代码部分:
public function addToDlx()
{
$host = [
'host' => '127.0.0.1',
'port' => '5672',
'login' => 'guest',
'password' => 'guest',
'vhost' => 'report',
'heartbeat' => 60
];
// 普通队列
$normal = [
'vhost' => 'report', // 虚拟机
'exchange' => 'normal', // 交换机
'route' => 'normal_route', // 路由key - 用于交换机与队列进行绑定
'queue' => 'normal_queue', // 队列
'expire' => 1000*60, // 有效时间单位:毫秒 - 1分钟
];
// 死信队列
$normal_dlx = [
'vhost' => 'report',
'exchange' => 'normal_dlx',
'route' => 'normal_dlx_route',
'queue' => 'normal_dlx_queue'
];
// 给普通队列关联死信队列,携带的参数
$dlx_args = [
'x-dead-letter-exchange' => $normal_dlx['exchange'],
'x-dead-letter-routing-key' => $normal_dlx['route'],
'x-message-ttl' => $normal['expire'],
];
通过消费者方式创建死信队列/
$dlx_mq = new ConsumerMQ($host,$normal,$dlx_args);
$dlx_mq->run(null);
将消息放入普通队列/
$mq = new ProductMQ($host, $normal);
$param = json_encode([
'name' => 'test',
'id' => 11568,
'remark' => '测试一下'
]);
$mq->publish($param);
$mq->close();
}
- 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.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 测试结果:
通过postman点击上面接口,控制台就可以看出多出了一个normal队列,并且队列的 Features 为“ D TTL DLX DLK ”,$param的消息也会首先进入“normal”队列。
- 1分钟后(自己设置的),normal的消息会失效,进而开始添加到了死信队列“normal_dxl”,可以点击死信查看最新的消息信息。