2019-03-30 15:00:10 +08:00
|
|
|
# 异步队列
|
|
|
|
|
2019-07-04 20:10:57 +08:00
|
|
|
异步队列区别于 `RabbitMQ` `Kafka` 等消息队列,它只提供一种 `异步处理` 和 `异步延时处理` 的能力,并不能严格地保证消息的持久化和支持 `ACK 应答机制`。
|
2019-03-30 15:00:10 +08:00
|
|
|
|
2019-03-30 22:53:32 +08:00
|
|
|
## 安装
|
|
|
|
|
|
|
|
```bash
|
2019-04-10 14:28:15 +08:00
|
|
|
composer require hyperf/async-queue
|
2019-03-30 22:53:32 +08:00
|
|
|
```
|
|
|
|
|
2019-03-30 15:00:10 +08:00
|
|
|
## 配置
|
|
|
|
|
2019-07-04 20:10:57 +08:00
|
|
|
配置文件位于 `config/autoload/async_queue.php`,如文件不存在可自行创建。
|
|
|
|
|
|
|
|
> 暂时只支持 `Redis Driver` 驱动。
|
2019-03-30 15:00:10 +08:00
|
|
|
|
2019-04-10 14:28:15 +08:00
|
|
|
| 配置 | 类型 | 默认值 | 备注 |
|
|
|
|
|:-------------:|:------:|:-------------------------------------------:|:------------------:|
|
|
|
|
| driver | string | Hyperf\AsyncQueue\Driver\RedisDriver::class | 无 |
|
|
|
|
| channel | string | queue | 队列前缀 |
|
|
|
|
| retry_seconds | int | 5 | 失败后重新尝试间隔 |
|
|
|
|
| processes | int | 1 | 消费进程数 |
|
2019-03-30 15:00:10 +08:00
|
|
|
|
|
|
|
```php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
return [
|
|
|
|
'default' => [
|
2019-04-10 14:28:15 +08:00
|
|
|
'driver' => Hyperf\AsyncQueue\Driver\RedisDriver::class,
|
2019-03-30 15:00:10 +08:00
|
|
|
'channel' => 'queue',
|
|
|
|
'retry_seconds' => 5,
|
|
|
|
'processes' => 1,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
## 使用
|
|
|
|
|
|
|
|
### 消费消息
|
|
|
|
|
2019-07-25 02:05:31 +08:00
|
|
|
组件已经提供了默认子进程,只需要将它配置到 `processes.php` 中即可。
|
2019-03-30 15:00:10 +08:00
|
|
|
|
|
|
|
```php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
return [
|
2019-04-10 14:28:15 +08:00
|
|
|
Hyperf\AsyncQueue\Process\ConsumerProcess::class,
|
2019-03-30 15:00:10 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
```
|
|
|
|
|
2019-07-25 10:05:33 +08:00
|
|
|
当然,您也可以将以下 `Process` 添加到自己的项目中。
|
|
|
|
|
|
|
|
```php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace App\Process;
|
|
|
|
|
|
|
|
use Hyperf\AsyncQueue\Process\ConsumerProcess;
|
|
|
|
use Hyperf\Process\Annotation\Process;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Process(name="async-queue")
|
|
|
|
*/
|
|
|
|
class AsyncQueueConsumer extends ConsumerProcess
|
|
|
|
{
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2019-03-30 15:00:10 +08:00
|
|
|
### 发布消息
|
|
|
|
|
|
|
|
首先我们定义一个消息,如下
|
|
|
|
|
|
|
|
```php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-07-25 10:05:33 +08:00
|
|
|
namespace App\Job;
|
2019-03-30 15:00:10 +08:00
|
|
|
|
2019-04-10 14:28:15 +08:00
|
|
|
use Hyperf\AsyncQueue\Job;
|
2019-03-30 15:00:10 +08:00
|
|
|
|
|
|
|
class ExampleJob extends Job
|
|
|
|
{
|
2019-07-25 10:05:33 +08:00
|
|
|
public $params;
|
2019-07-25 02:05:31 +08:00
|
|
|
|
2019-07-25 10:05:33 +08:00
|
|
|
public function __construct($params)
|
2019-07-25 02:05:31 +08:00
|
|
|
{
|
2019-07-25 10:05:33 +08:00
|
|
|
// 这里最好是普通数据,不要使用携带 IO 的对象,比如 PDO 对象
|
|
|
|
$this->params = $params;
|
2019-07-25 02:05:31 +08:00
|
|
|
}
|
2019-07-25 10:05:33 +08:00
|
|
|
|
2019-03-30 15:00:10 +08:00
|
|
|
public function handle()
|
|
|
|
{
|
2019-07-25 10:05:33 +08:00
|
|
|
// 根据参数处理具体逻辑
|
2019-07-25 02:05:31 +08:00
|
|
|
var_dump($this->params);
|
2019-03-30 15:00:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
发布消息
|
|
|
|
|
|
|
|
```php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-07-25 10:05:33 +08:00
|
|
|
namespace App\Service;
|
|
|
|
|
|
|
|
use App\Job\ExampleJob;
|
2019-04-10 14:28:15 +08:00
|
|
|
use Hyperf\AsyncQueue\Driver\DriverFactory;
|
2019-07-04 17:41:44 +08:00
|
|
|
use Hyperf\AsyncQueue\Driver\DriverInterface;
|
2019-03-30 15:00:10 +08:00
|
|
|
|
2019-07-25 10:05:33 +08:00
|
|
|
class QueueService
|
2019-03-30 15:00:10 +08:00
|
|
|
{
|
2019-07-04 17:41:44 +08:00
|
|
|
/**
|
|
|
|
* @var DriverInterface
|
|
|
|
*/
|
2019-03-30 15:00:10 +08:00
|
|
|
protected $driver;
|
|
|
|
|
2019-05-13 02:25:03 +08:00
|
|
|
public function __construct(DriverFactory $driverFactory)
|
2019-03-30 15:00:10 +08:00
|
|
|
{
|
2019-05-13 02:25:03 +08:00
|
|
|
$this->driver = $driverFactory->get('default');
|
2019-03-30 15:00:10 +08:00
|
|
|
}
|
|
|
|
|
2019-07-25 10:05:33 +08:00
|
|
|
/**
|
|
|
|
* 投递消息.
|
|
|
|
* @param $params 数据
|
|
|
|
* @param int $delay 延时时间 单位秒
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function push($params, $delay = 0)
|
2019-07-04 17:41:44 +08:00
|
|
|
{
|
2019-07-25 10:05:33 +08:00
|
|
|
// 这里的 `ExampleJob` 会被序列化存到 Redis 中,所以内部变量最好只传入普通数据
|
|
|
|
// 同理,如果内部使用了注解 @Value 会把对应对象一起序列化,导致消息体变大。
|
2019-07-25 10:25:50 +08:00
|
|
|
// 所以这里也不推荐使用 `make` 方法来创建 `Job` 对象。
|
2019-07-25 10:05:33 +08:00
|
|
|
return $this->driver->push(new ExampleJob($params), $delay);
|
2019-07-25 02:05:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
根据实际业务场景,动态投递消息到异步队列执行,我们演示在控制器动态投递消息,如下:
|
|
|
|
|
|
|
|
```php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-07-25 10:05:33 +08:00
|
|
|
namespace App\Controller;
|
2019-07-25 02:05:31 +08:00
|
|
|
|
2019-07-25 10:05:33 +08:00
|
|
|
use App\Service\QueueService;
|
|
|
|
use Hyperf\Di\Annotation\Inject;
|
|
|
|
use Hyperf\HttpServer\Annotation\AutoController;
|
2019-07-25 02:05:31 +08:00
|
|
|
|
2019-07-25 10:05:33 +08:00
|
|
|
/**
|
|
|
|
* @AutoController
|
|
|
|
*/
|
|
|
|
class QueueController extends Controller
|
2019-07-25 02:05:31 +08:00
|
|
|
{
|
2019-07-25 10:05:33 +08:00
|
|
|
/**
|
|
|
|
* @Inject
|
|
|
|
* @var QueueService
|
|
|
|
*/
|
|
|
|
protected $service;
|
2019-07-25 02:05:31 +08:00
|
|
|
|
2019-07-25 10:05:33 +08:00
|
|
|
public function index()
|
2019-07-25 02:05:31 +08:00
|
|
|
{
|
2019-07-25 10:05:33 +08:00
|
|
|
$this->service->push([
|
|
|
|
'group@hyperf.io',
|
|
|
|
'https://doc.hyperf.io',
|
|
|
|
'https://www.hyperf.io',
|
|
|
|
]);
|
2019-07-25 02:05:31 +08:00
|
|
|
|
2019-07-25 10:05:33 +08:00
|
|
|
return 'success';
|
2019-03-30 15:00:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-04 20:10:57 +08:00
|
|
|
```
|