1. Go to this page and download the library: Download easyswoole/nsq library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
easyswoole / nsq example snippets
composer
namespace EasySwoole\EasySwoole;
use App\Producer\Process as ProducerProcess;
use App\Consumer\Process as ConsumerProcess;
use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\Http\Request;
use EasySwoole\Http\Response;
class EasySwooleEvent implements Event
{
public static function initialize()
{
// TODO: Implement initialize() method.
date_default_timezone_set('Asia/Shanghai');
}
public static function mainServerCreate(EventRegister $register)
{
// TODO: Implement mainServerCreate() method.
// 生产者
ServerManager::getInstance()->getSwooleServer()->addProcess((new ProducerProcess())->getProcess());
// 消费者
ServerManager::getInstance()->getSwooleServer()->addProcess((new ConsumerProcess())->getProcess());
}
......
}
namespace App\Producer;
use EasySwoole\Component\Process\AbstractProcess;
class Process extends AbstractProcess
{
protected function run($arg)
{
go(function () {
$config = new \EasySwoole\Nsq\Config();
$topic = "topic.test";
$hosts = $config->getNsqdUrl();
foreach ($hosts as $host) {
$nsq = new \EasySwoole\Nsq\Nsq();
for ($i = 0; $i < 10; $i++) {
$msg = new \EasySwoole\Nsq\Message\Message();
$msg->setPayload("test$i");
$nsq->push(
new \EasySwoole\Nsq\Connection\Producer($host, $config),
$topic,
$msg
);
}
}
});
}
}
namespace App\Consumer;
use EasySwoole\Component\Process\AbstractProcess;
class Process extends AbstractProcess
{
protected function run($arg)
{
go(function () {
$topic = "topic.test";
$channel = "test.consuming";
$config = new \EasySwoole\Nsq\Config();
$nsqlookup = new \EasySwoole\Nsq\Lookup\Nsqlookupd($config->getNsqlookupUrl());
$hosts = $nsqlookup->lookupHosts($topic);
foreach ($hosts as $host) {
$nsq = new \EasySwoole\Nsq\Nsq();
$nsq->subscribe(
new \EasySwoole\Nsq\Connection\Consumer($host, $config, $topic, $channel),
function ($item) {
var_dump($item['message']);
}
);
}
});
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.