您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
workerman异步消息队列组件-rabbitmq
发布时间:2021-12-31 21:42:25编辑:雪饮阅读()
[root@localhost workerman]# composer require workerman/rabbitmq
Using version ^1.0 for workerman/rabbitmq
./composer.json has been updated
Running composer update workerman/rabbitmq
Loading composer repositories with package information
Updating dependencies
Lock file operations: 4 installs, 0 updates, 0 removals
- Locking bunny/bunny (v0.5.1)
- Locking react/event-loop (v1.2.0)
- Locking react/promise (v2.8.0)
- Locking workerman/rabbitmq (v1.0.2)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 4 installs, 0 updates, 0 removals
- Downloading react/event-loop (v1.2.0)
- Downloading react/promise (v2.8.0)
- Downloading bunny/bunny (v0.5.1)
- Downloading workerman/rabbitmq (v1.0.2)
- Installing react/event-loop (v1.2.0): Extracting archive
- Installing react/promise (v2.8.0): Extracting archive
- Installing bunny/bunny (v0.5.1): Extracting archive
- Installing workerman/rabbitmq (v1.0.2): Extracting archive
2 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating autoload files
2 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
实例-接收者:
<?php
use Bunny\Channel;
use Bunny\Message;
use Workerman\Worker;
use Workerman\RabbitMQ\Client;
require __DIR__ . '/vendor/autoload.php';
$worker = new Worker();
$worker->onWorkerStart = function() {
(new Client())->connect()->then(function (Client $client) {
//连接成功返回频道
return $client->channel();
})->then(function (Channel $channel) {
//订阅频道队列名
//个人理解:这里其实可以直接只传一个队列名就行了,后面四个参数本来就是默认false
return $channel->queueDeclare('hello', false, false, false, false)->then(function () use ($channel) {
return $channel;
});
})->then(function (Channel $channel) {
echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";
//消费上面的hello队列
//这里感觉也可以只传前面两个参数,即回调参数和队列名,因为后面三个和consume对应参数默认值相同
$channel->consume(
function (Message $message, Channel $channel, Client $client) {
echo " [x] Received ", $message->content, "\n";
},
'hello',
'',
false,
true
);
});
};
Worker::runAll();
实例-发送者:
<?php
use Bunny\Channel;
use Bunny\Message;
use Workerman\Worker;
use Workerman\RabbitMQ\Client;
require __DIR__ . '/vendor/autoload.php';
$worker = new Worker();
$worker->onWorkerStart = function() {
(new Client())->connect()->then(function (Client $client) {
//连接成功返回频道
return $client->channel();
})->then(function (Channel $channel) {
//频道订阅队列名(订阅)
return $channel->queueDeclare('hello', false, false, false, false)->then(function () use ($channel) {
return $channel;
});
})->then(function (Channel $channel) {
echo " [x] Sending 'Hello World!'\n";
//向队列发布消息(生产者),第二第三个参数为默认值
return $channel->publish('Hello World!', [], '', 'hello')->then(function () use ($channel) {
return $channel;
});
})->then(function (Channel $channel) {
//发送完成返回客户端实例
echo " [x] Sent 'Hello World!'\n";
$client = $channel->getClient();
return $channel->close()->then(function () use ($client) {
return $client;
});
})->then(function (Client $client) {
//拿到客户端实例后,安全断开当前客户端
$client->disconnect();
});
};
Worker::runAll();
实例接收者运行时(有rabbitmq服务启动时并有发送者发送了消息时)如:
[root@localhost workerman]# php receive.php start
Workerman[receive.php] start in DEBUG mode
------------------------------------- WORKERMAN --------------------------------------
Workerman version:4.0.26 PHP version:7.3.31
-------------------------------------- WORKERS ---------------------------------------
proto user worker listen processes status
tcp root none none 1 [OK]
--------------------------------------------------------------------------------------
Press Ctrl+C to stop. Start success.
[*] Waiting for messages. To exit press CTRL+C
[x] Received Hello World!
实例发送者运行时并有rabbitmq启动时如:
[root@localhost workerman]# php send.php start
Workerman[send.php] start in DEBUG mode
------------------------------------- WORKERMAN --------------------------------------
Workerman version:4.0.26 PHP version:7.3.31
-------------------------------------- WORKERS ---------------------------------------
proto user worker listen processes status
tcp root none none 1 [OK]
--------------------------------------------------------------------------------------
Press Ctrl+C to stop. Start success.
[x] Sending 'Hello World!'
[x] Sent 'Hello World!'
关键字词:workerman,rabbitmq,异步,消息队列
上一篇:rabbitmq安装