2019-11-01 13:54:42 +08:00
|
|
|
# NATS
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
NATS 是一個開源、輕量級、高效能的分散式訊息中介軟體,實現了高可伸縮性和優雅的 `Publish` / `Subscribe` 模型,使用 `Golang` 語言開發。NATS 的開發哲學認為高質量的 QoS 應該在客戶端構建,故只建立了 `Request-Reply`,不提供 1. 持久化 2. 事務處理 3. 增強的交付模式 4. 企業級佇列。
|
2019-11-01 13:54:42 +08:00
|
|
|
|
2020-03-19 12:00:23 +08:00
|
|
|
## 安裝
|
|
|
|
|
|
|
|
```bash
|
2020-04-09 16:51:05 +08:00
|
|
|
composer require hyperf/nats
|
2020-03-19 12:00:23 +08:00
|
|
|
```
|
|
|
|
|
2019-11-01 13:54:42 +08:00
|
|
|
## 使用
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
### 建立消費者
|
2019-11-01 13:54:42 +08:00
|
|
|
|
|
|
|
```
|
2020-03-19 12:00:23 +08:00
|
|
|
php bin/hyperf.php gen:nats-consumer DemoConsumer
|
2019-11-01 13:54:42 +08:00
|
|
|
```
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
如果設定了 `queue`,則相同的 `subject` 只會被一個 `queue` 消費。若不設定 `queue`,則每個消費者都會受到訊息。
|
2019-11-01 13:54:42 +08:00
|
|
|
|
|
|
|
```php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace App\Nats\Consumer;
|
|
|
|
|
|
|
|
use Hyperf\Nats\AbstractConsumer;
|
|
|
|
use Hyperf\Nats\Annotation\Consumer;
|
|
|
|
use Hyperf\Nats\Message;
|
|
|
|
|
2021-12-01 16:19:47 +08:00
|
|
|
#[Consumer(subject: 'hyperf.demo', queue: 'hyperf.demo', name: 'DemoConsumer', nums: 1)]
|
2019-11-01 13:54:42 +08:00
|
|
|
class DemoConsumer extends AbstractConsumer
|
|
|
|
{
|
|
|
|
public function consume(Message $payload)
|
|
|
|
{
|
|
|
|
// Do something...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
### 投遞訊息
|
2019-11-01 13:54:42 +08:00
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
使用 publish 投遞訊息。
|
2019-11-01 13:54:42 +08:00
|
|
|
|
|
|
|
```php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
|
|
|
use Hyperf\Di\Annotation\Inject;
|
|
|
|
use Hyperf\HttpServer\Annotation\AutoController;
|
|
|
|
use Hyperf\Nats\Driver\DriverInterface;
|
|
|
|
|
2021-12-01 16:19:47 +08:00
|
|
|
#[AutoController(prefix: "nats")]
|
2021-02-19 12:53:50 +08:00
|
|
|
class NatsController extends AbstractController
|
2019-11-01 13:54:42 +08:00
|
|
|
{
|
2021-12-01 16:19:47 +08:00
|
|
|
#[Inject]
|
|
|
|
protected DriverInterface $nats;
|
2019-11-01 13:54:42 +08:00
|
|
|
|
|
|
|
public function publish()
|
|
|
|
{
|
|
|
|
$res = $this->nats->publish('hyperf.demo', [
|
|
|
|
'id' => 'Hyperf',
|
|
|
|
]);
|
|
|
|
|
|
|
|
return $this->response->success($res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
使用 request 投遞訊息。
|
2019-11-01 13:54:42 +08:00
|
|
|
|
|
|
|
```php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
|
|
|
use Hyperf\Di\Annotation\Inject;
|
|
|
|
use Hyperf\HttpServer\Annotation\AutoController;
|
|
|
|
use Hyperf\Nats\Driver\DriverInterface;
|
|
|
|
use Hyperf\Nats\Message;
|
|
|
|
|
2021-12-01 16:19:47 +08:00
|
|
|
#[AutoController(prefix: "nats")]
|
2021-02-19 12:53:50 +08:00
|
|
|
class NatsController extends AbstractController
|
2019-11-01 13:54:42 +08:00
|
|
|
{
|
2021-12-01 16:19:47 +08:00
|
|
|
#[Inject]
|
|
|
|
protected DriverInterface $nats;
|
2019-11-01 13:54:42 +08:00
|
|
|
|
|
|
|
public function request()
|
|
|
|
{
|
|
|
|
$res = $this->nats->request('hyperf.reply', [
|
|
|
|
'id' => 'limx',
|
|
|
|
], function (Message $payload) {
|
|
|
|
var_dump($payload->getBody());
|
|
|
|
});
|
|
|
|
|
|
|
|
return $this->response->success($res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
使用 requestSync 投遞訊息。
|
2019-11-01 13:54:42 +08:00
|
|
|
|
|
|
|
```php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
|
|
|
use Hyperf\Di\Annotation\Inject;
|
|
|
|
use Hyperf\HttpServer\Annotation\AutoController;
|
|
|
|
use Hyperf\Nats\Driver\DriverInterface;
|
|
|
|
use Hyperf\Nats\Message;
|
|
|
|
|
2021-12-01 16:19:47 +08:00
|
|
|
#[AutoController(prefix: "nats")]
|
2021-02-19 12:53:50 +08:00
|
|
|
class NatsController extends AbstractController
|
2019-11-01 13:54:42 +08:00
|
|
|
{
|
2021-12-01 16:19:47 +08:00
|
|
|
#[Inject]
|
|
|
|
protected DriverInterface $nats;
|
2019-11-01 13:54:42 +08:00
|
|
|
|
|
|
|
public function sync()
|
|
|
|
{
|
|
|
|
/** @var Message $message */
|
|
|
|
$message = $this->nats->requestSync('hyperf.reply', [
|
|
|
|
'id' => 'limx',
|
|
|
|
]);
|
|
|
|
|
|
|
|
return $this->response->success($message->getBody());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-01 16:19:47 +08:00
|
|
|
```
|