# producer php bin/hyperf.php gen:amqp-producer DemoProducer # consumer php bin/hyperf.php gen:amqp-consumer DemoConsumer # 使用 command 盜用 DemoProducer 進行驗證 php bin/hyperf.php gen:command TestCommand
producer 發個消息:
- 設置 command 的名字:
parent::__construct('t');
- 使用
@Inject()
註解注入 - 發消息, 一行搞定:
$this->producer->produce(new DemoProducer('test'. date('Y-m-d H:i:s')));
<?php declare(strict_types=1); namespace App\Command; use App\Amqp\Producer\DemoProducer; use Hyperf\Amqp\Producer; use Hyperf\Command\Command as HyperfCommand; use Hyperf\Command\Annotation\Command; use Hyperf\Di\Annotation\Inject; use Psr\Container\ContainerInterface; /** * @Command */ class TestCommand extends HyperfCommand { /** * @var ContainerInterface */ protected $container; /** * @Inject() * @var Producer */ protected $producer; public function __construct(ContainerInterface $container) { $this->container = $container; parent::__construct('t'); } public function configure() { $this->setDescription('Hyperf Demo Command'); } public function handle() { $this->producer->produce(new DemoProducer('test'. date('Y-m-d H:i:s'))); } }
愉快的玩耍起來:
# produce
php bin/hyperf.php t
# consume
php bin/hyperf.php start # 會使用 swoole process 啓動 DemoConsumer
# 也可以訪問 rabbitmq admin 控制檯
http://localhost:15672
擼一擼 rabbitmq 官網 tutorial
跟着 rabbitmq 官網 tutorial, 見識一下 hyperf 中的 amqp 有多簡單
// consumer /** * @Consumer() */ class DemoConsumer extends ConsumerMessage { protected $exchange = 'hello'; protected $type = Type::FANOUT; protected $queue = 'hello'; public function consume($data): string { var_dump($data); return Result::ACK; } } // producer /** * @Producer() */ class DemoProducer extends ProducerMessage { protected $exchange = 'hello'; protected $type = Type::FANOUT; protected $routingKey = 'hello'; public function __construct($data) { $this->payload = $data; } } work queue 設置一下 nums 參數, 就可以多進程. // Consumer /** * @Consumer(nums=2) */ class DemoConsumer extends ConsumerMessage { protected $exchange = 'task'; protected $type = Type::FANOUT; protected $queue = 'task'; public function consume($data): string { var_dump($data); return Result::ACK; } } // producer /** * @Producer() */ class DemoProducer extends ProducerMessage { protected $exchange = 'task'; protected $type = Type::FANOUT; protected $routingKey = 'task'; public function __construct($data) { $this->payload = $data; } } pub/sub 和上面的 hello world 一致 routing 終於看到 routing_key 的作用了 // consumer /** * @Consumer() */ class DemoConsumer extends ConsumerMessage { protected $exchange = 'routing'; protected $type = Type::DIRECT; // 這個 consumer 只消費 error 級別的日誌 protected $queue = 'routing.error'; protected $routingKey = 'error'; public function consume($data): string { var_dump($data); return Result::ACK; } } /** * @Consumer() */ class Demo2Consumer extends ConsumerMessage { protected $exchange = 'routing'; protected $type = Type::DIRECT; // 這個 consumer 消費所有級別的日誌 protected $queue = 'routing.all'; protected $routingKey = [ 'info', 'warning', 'error', ]; public function consume($data): string { var_dump($data); return Result::ACK; } } // producer /** * @Producer() */ class DemoProducer extends ProducerMessage { protected $exchange = 'routing'; protected $type = Type::DIRECT; public function __construct($data, $routingKey) { $this->routingKey = $routingKey; $this->payload = $data; } } // produce $this->producer->produce(new DemoProducer('info'. date('Y-m-d H:i:s'), 'info')); $this->producer->produce(new DemoProducer('warning'. date('Y-m-d H:i:s'), 'warning')); $this->producer->produce(new DemoProducer('error'. date('Y-m-d H:i:s'), 'error')); var_dump('done'); topics 和的, 和上面的 routing 差不多 // consume /** * @Consumer() */ class DemoConsumer extends ConsumerMessage { protected $exchange = 'topics'; protected $type = Type::TOPIC; protected $queue = 'topics.t1'; // protected $routingKey = '#'; // all // protected $routingKey = 'kern.*'; // protected $routingKey = '*.critical'; // protected $routingKey = 'kern.critical'; protected $routingKey = [ 'kern.*', '*.critical', ]; public function consume($data): string { var_dump($data); return Result::ACK; } } // produce /** * @Producer() */ class DemoProducer extends ProducerMessage { protected $exchange = 'topics'; protected $type = Type::TOPIC; public function __construct($data, $routingKey) { $this->routingKey = $routingKey; $this->payload = $data; } }