-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRabbitMQClient.php
More file actions
394 lines (368 loc) · 11.5 KB
/
RabbitMQClient.php
File metadata and controls
394 lines (368 loc) · 11.5 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<?php
namespace module\lib;
/**
* RabbitMQClient PHP原生客户端实现方式工具 (AMQP协议)
*
* ####################################### 使用示例 ##############################################
* $result = [];
* //生产者
* $message = ['id' => 11111, 'status' => 1, 'data' => ['user_code' => 'W12334', 'user_name' => '张三']];
* $rabbitMQClient = new RabbitMQClient();
* $exchangeName = 'eee';
* $routeKey = 'rrr';
* $queueName = 'qqq';
* // $result = $rabbitMQClient->sendMessage($message, $exchangeName, $queueName, $routeKey);
* // $result = $rabbitMQClient->sendRouteMessage($message, $exchangeName, $routeKey);
* // $result = $rabbitMQClient->sendFanOutMessage($message, $exchangeName, $queueName);
* // $result = $rabbitMQClient->getOneMessage($exchangeName, $queueName);
* // if ($result === false) {
* // return $rabbitMQClient->getErrorMessage();
* // }
* //消费者
* $callback = function ($body) {
* echo date('[Y-m-d H:i:s]') . print_r($body, true) . PHP_EOL;
* sleep(1);
* return true;
* };
* try {
* $rabbitMQClient->consume($callback, $queueName);
* } catch (Exception $e) {
*
* }
* return $result;
* ##########################################################################################
* Class RabbitMQClient
*/
class RabbitMQClient
{
protected $mqConfig = [];
/**
* @var \AMQPConnection
*/
protected $connection;
/**
* @var \AMQPChannel
*/
protected $channel;
/**
* @var \AMQPExchange
*/
protected $exchange;
/**
* @var string 默认交换机
*/
protected $exchangeName;
protected $exchangeType = AMQP_EX_TYPE_DIRECT;
protected $routeKey;
/**
* @var \AMQPQueue
*/
protected $queue;
protected $queueName;
/**
* 消费队列,可绑定多个Key
* @var string
*/
protected $bindKey;
/**
* @var string
*/
protected $errorMessage;
/**
* @var int
*/
private $messageCount;
/**
* @var callable
*/
private $callback;
/**
* RabbitMQClient constructor.
* @param array $config
* @throws \Exception
*/
public function __construct($config = [])
{
$this->setMqConfig($config);
$this->connect();
}
/**
* 设置RabbitMQ连接信息,或直接配置
* @param array $mqConfig
* @return $this
*/
public function setMqConfig($mqConfig = [])
{
$this->mqConfig = [
'host' => $mqConfig['host'] ?? '192.168.71.91',
'vhost' => $mqConfig['vhost'] ?? '/',
'port' => $mqConfig['port'] ?? '5672',
'login' => $mqConfig['login'] ?? 'admin',
'password' => $mqConfig['password'] ?? 'admin123.',
'connect_timeout' => $mqConfig['password'] ?? 30,
'heartbeat' => $mqConfig['heartbeat'] ?? 30,
];
return $this;
}
/**
* @throws \Exception
*/
protected function connect()
{
try {
$this->connection = new \AMQPConnection($this->mqConfig);
$this->connection->connect();
$this->channel = new \AMQPChannel($this->connection);
} catch (\AMQPConnectionException $e) {
throw new \Exception('rabbitmq连接失败:' . $e->getMessage());
}
}
/**
* @param $exchangeName
* @return $this
*/
public function setExchangeName($exchangeName)
{
$this->exchangeName = $exchangeName;
return $this;
}
/**
* @param $exchangeType
* @return $this
*/
public function setExchangeType($exchangeType)
{
$this->exchangeType = $exchangeType;
return $this;
}
/**
* @param $queueName
* @return $this
*/
public function setQueueName($queueName)
{
$this->queueName = $queueName;
return $this;
}
/**
* @param $bindKey
* @return $this
*/
public function setBindKey($bindKey)
{
$this->bindKey = $bindKey;
return $this;
}
/**
* @param $routeKey
* @return $this
*/
public function setRouteKey($routeKey)
{
$this->routeKey = $routeKey;
return $this;
}
/**
* 声明交换机
* @param bool $autoCreate 不存在是否自动创建(持久化)
* @throws \AMQPChannelException
* @throws \AMQPConnectionException
* @throws \AMQPExchangeException
*/
private function handleExchange($autoCreate = false)
{
if (!$this->connection->isConnected()) {
throw new \AMQPConnectionException('RabbitMQ is not connected.');
}
$this->exchange = new \AMQPExchange($this->channel);
$this->exchange->setName($this->exchangeName);
$this->exchange->setType($this->exchangeType);
$flags = $autoCreate ? AMQP_DURABLE : AMQP_PASSIVE;
$this->exchange->setFlags($flags);
$this->exchange->declareExchange();
}
/**
* 声明队列
* @param bool $autoCreate 不存在是否自动创建(持久化)
* @return $this
* @throws \AMQPChannelException
* @throws \AMQPConnectionException
* @throws \AMQPQueueException
*/
private function handleQueue($autoCreate = false)
{
if (empty($this->queueName)) {
return $this;
}
$this->queue = new \AMQPQueue($this->channel);
$this->queue->setName($this->queueName);
$flags = $autoCreate ? AMQP_DURABLE : AMQP_PASSIVE;
$this->queue->setFlags($flags);
$this->queue->declareQueue();
return $this;
}
private function queueBind()
{
if ($this->queue === null) {
return $this;
}
$this->queue->bind($this->exchangeName, $this->routeKey);
return $this;
}
//生产者 点对点发送指定队列消息
//生产者发送消息
public function sendMessage($message, $exchangeName, $queueName = null, $routeKey = null)
{
try {
$this->setExchangeName($exchangeName)
->setExchangeType(AMQP_EX_TYPE_DIRECT)
->setQueueName($queueName)
->setRouteKey($routeKey);
$this->handleExchange(true);
$this->handleQueue(true);
$this->queueBind();
$message = (is_array($message) || is_object($message)) ? json_encode($message, JSON_UNESCAPED_UNICODE) : $message;
$result = $this->exchange->publish($message, $this->routeKey);
return $result;
} catch (\Exception $e) {
$this->errorMessage = $e->getMessage();
return false;
}
}
//发送消息 direct-route 模式
public function sendRouteMessage($message, $exchangeName, $routeKey = null)
{
return $this->sendMessage($message, $exchangeName, null, $routeKey);
}
/**
* 发送广播消息。忽略路由,绑定此交换机的队列都能接收到此消息
* 一个交换机只能有一种类型
* @param $message
* @param null $exchangeName
* @param null $queueName 有传队列,则进行持久化,发送可不传
* @return bool
*/
public function sendFanOutMessage($message, $exchangeName, $queueName = null)
{
try {
$this->setExchangeName($exchangeName)
->setExchangeType(AMQP_EX_TYPE_FANOUT)
->setQueueName($queueName);
$this->handleExchange(true);
$this->handleQueue(true);
$this->queueBind();
$message = (is_array($message) || is_object($message)) ? json_encode($message, JSON_UNESCAPED_UNICODE) : $message;
$result = $this->exchange->publish($message, '', AMQP_MANDATORY, ['delivery_mode' => 2]);
return $result;
} catch (\Exception $e) {
$this->errorMessage = $e->getMessage();
return false;
}
}
/**
* 消费者消费消息(阻塞模式),消费端其实只要知道队列名即可,如需要初始化交换机及绑定路由,传入exchangeName,routeKey
* @param callable $callback
* @param $queueName
* @param string $exchangeName
* @param string $exchangeType
* @param string $routeKey
* @throws \AMQPChannelException
* @throws \AMQPConnectionException
* @throws \AMQPEnvelopeException
* @throws \AMQPExchangeException
* @throws \AMQPQueueException
*/
public function consume(callable $callback, $queueName, $exchangeName = null, $exchangeType = AMQP_EX_TYPE_DIRECT, $routeKey = null)
{
$this->setCallback($callback)->setQueueName($queueName)->setExchangeName($exchangeName)->setExchangeType($exchangeType)->setRouteKey($routeKey);
if (!empty($exchangeName)) {
$this->handleExchange(true);
}
$this->handleQueue(true);
if (!empty($exchangeName)) {
$this->queueBind();
}
//阻塞消费
$this->queue->consume(function (\AMQPEnvelope $AMQPEnvelope, \AMQPQueue $AMQPQueue) {
if (!is_callable($this->callback)) {
throw new \Exception('not a callback function.');
}
$result = call_user_func($this->callback, $AMQPEnvelope->getBody());
if ($result === true) {
$AMQPQueue->ack($AMQPEnvelope->getDeliveryTag());
} else {
$AMQPQueue->nack($AMQPEnvelope->getDeliveryTag());
}
});
}
/**
* 消费者获取消息(非阻塞)
* @param $exchangeName
* @param $queueName
* @param bool $autoAck
* @return string
* @throws \AMQPChannelException
* @throws \AMQPConnectionException
* @throws \AMQPExchangeException
* @throws \AMQPQueueException
*/
public function getOneMessage($exchangeName, $queueName, $autoAck = true)
{
$this->setExchangeName($exchangeName)->setQueueName($queueName);
$this->handleExchange(false);
$this->handleQueue(true);
$this->queueBind();
if ($autoAck) {
$envelope = $this->queue->get(AMQP_AUTOACK);
} else {
$envelope = $this->queue->get();
}
return $envelope instanceof \AMQPEnvelope ? null : $envelope->getBody();
}
/**
* 获取队列消息数量
* AMQP_DURABLE [队列持久]
* AMQP_PASSIVE [返回消息计数]
* AMQP_EXCLUSIVE [只被一个连接(connection)使用,而且当连接关闭后队列即被删除]
* AMQP_AUTODELETE [当最后一个消费者退订后即被删除]
* @param $queueName
* @return int
*/
public function getMessageCount($queueName)
{
try {
$this->queue = new \AMQPQueue($this->channel);
$this->queue->setName($queueName);
$this->queue->setFlags(AMQP_PASSIVE);
$this->messageCount = $this->queue->declareQueue();
} catch (\Exception $e) {
$this->messageCount = 0;
}
return $this->messageCount;
}
public function setCallback(callable $callback)
{
$this->callback = $callback;
return $this;
}
/**
* @return string
*/
public function getErrorMessage()
{
return $this->errorMessage;
}
private function close()
{
if ($this->connection) {
$this->connection->disconnect();
}
if ($this->channel) {
$this->channel->close();
}
}
public function __destruct()
{
$this->close();
}
}