rabbitmq 異步(workerman)和同步的生產者和消費者

2.0.x-dev
版本
2025-01-09
版本更新時間
281
安裝
4
star
簡介
rabbitmq 是一個異步(workerman)和同步的PHP客戶端,用于異步(workerman)和同步的生產者和消費者。
封裝了workerman/rabbitmq, 有類似webman/redis-queue的分組消費者, 也有按文件的單個worker
安裝
composer require roiwk/rabbitmq
使用
Config Demo
// 配置格式
$config = [
'host' => '127.0.0.1',
'port' => 5672,
'vhost' => '/',
'mechanism' => 'AMQPLAIN',
'user' => 'username',
'password' => 'password',
'timeout' => 10,
'heartbeat' => 60,
'heartbeat_callback' => function(){},
'error_callback' => null,
];
一. webman中自定義進程--消費者
1.process.php
'hello-rabbitmq' => [
'handler' => app\queue\rabbitmq\Hello::class,
'count' => 1,
'constructor' => [
'rabbitmqConfig' => $config,
//'logger' => Log::channel('hello'),
],
]
2.app\queue\rabbitmq\Hello.php
namespace app\queue\rabbitmq;
use Roiwk\Rabbitmq\AbstractConsumer;
use Roiwk\Rabbitmq\Producer;
use Bunny\Channel;
use Bunny\Message;
use Bunny\AbstractClient;
class Hello extends AbstractConsumer
{
protected bool $async = true;
protected string $queue = 'hello';
protected array $consume = [
'noAck' => true,
];
public function consume(Message $message, Channel $channel, AbstractClient $client)
{
echo " [x] Received ", $message->content, "\n";
}
}
二.webman中自定義進程--分組消費者
類似webman-queue插件, 分組將消費者放同一個文件夾下, 使用同一個worker, 多個進程數(shù)處理
1.process.php
'hello-rabbitmq' => [
'handler' => Roiwk\Rabbitmq\GroupConsumers::class,
'count' => 2,
'constructor' => [
'consumer_dir' => app_path().'/queue/rabbimq',
'rabbitmqConfig' => $config,
//'logger' => Log::channel('hello'),
],
]
2.在 app_path().'/queue/rabbimq'
目錄下創(chuàng)建php文件, 繼承Roiwk\Rabbitmq\AbstractConsumer
即可, 同上app\queue\rabbitmq\Hello.php