mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-03 04:08:01 +08:00
96 lines
1.8 KiB
Markdown
96 lines
1.8 KiB
Markdown
|
# Nsq
|
||
|
|
||
|
Nsq 是一个开源、轻量级、高性能的分布式消息中间件, 使用 go 语言实现
|
||
|
|
||
|
## 使用
|
||
|
|
||
|
### 配置
|
||
|
|
||
|
```
|
||
|
return [
|
||
|
'default' => [
|
||
|
'host' => '127.0.0.1',
|
||
|
'port' => 4150,
|
||
|
'pool' => [
|
||
|
'min_connections' => 1,
|
||
|
'max_connections' => 10,
|
||
|
'connect_timeout' => 10.0,
|
||
|
'wait_timeout' => 3.0,
|
||
|
'heartbeat' => -1,
|
||
|
'max_idle_time' => 60.0,
|
||
|
],
|
||
|
],
|
||
|
];
|
||
|
```
|
||
|
|
||
|
### 创建消费者
|
||
|
|
||
|
```
|
||
|
$ php bin/hyperf.php gen:nsq-consumer DemoConsumer
|
||
|
```
|
||
|
|
||
|
使用 `\Hyperf\Nsq\Annotation\Consumer` 注解可以是设置 `topic / channel / name / nums`, 使用 `$pool` 属性可以切换不同连接
|
||
|
|
||
|
```php
|
||
|
<?php
|
||
|
|
||
|
declare(strict_types=1);
|
||
|
|
||
|
namespace App\Nsq\Consumer;
|
||
|
|
||
|
use Hyperf\Nsq\AbstractConsumer;
|
||
|
use Hyperf\Nsq\Annotation\Consumer;
|
||
|
use Hyperf\Nsq\Message;
|
||
|
use Hyperf\Nsq\Result;
|
||
|
|
||
|
/**
|
||
|
* @Consumer(
|
||
|
* topic="hyperf",
|
||
|
* channel="hyperf",
|
||
|
* name ="TestNsqConsumer",
|
||
|
* nums=1
|
||
|
* )
|
||
|
*/
|
||
|
class TestNsqConsumer extends AbstractConsumer
|
||
|
{
|
||
|
public function consume(Message $payload): string
|
||
|
{
|
||
|
var_dump($payload->getBody());
|
||
|
|
||
|
return Result::ACK;
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
### 投递消息
|
||
|
|
||
|
使用 `\Hyperf\Nsq\Nsq::publish()` 投递消息, 同样可以使用 `$pool` 属性来切换不同连接
|
||
|
|
||
|
```php
|
||
|
<?php
|
||
|
|
||
|
declare(strict_types=1);
|
||
|
|
||
|
namespace App\Command;
|
||
|
|
||
|
use Hyperf\Command\Command as HyperfCommand;
|
||
|
use Hyperf\Command\Annotation\Command;
|
||
|
use Hyperf\Nsq\Nsq;
|
||
|
|
||
|
/**
|
||
|
* @Command
|
||
|
*/
|
||
|
class NsqCommand extends HyperfCommand
|
||
|
{
|
||
|
protected $name = 'nsq:pub';
|
||
|
|
||
|
public function handle()
|
||
|
{
|
||
|
/** @var Nsq $nsq */
|
||
|
$nsq = make(Nsq::class); // 可以设置 `$pool` 属性
|
||
|
$nsq->publish('hyperf', 'test'. time());
|
||
|
|
||
|
$this->line('nsq pub success', 'info');
|
||
|
}
|
||
|
}
|
||
|
```
|