dudashuang / php-queue
a php client for message queue
Fund package maintenance!
Patreon
dudashuang.com
Requires
- php: >=7.0.0
- kwn/php-rdkafka-stubs: ^1.2
- php-amqplib/php-amqplib: ^2.7
- predis/predis: ^1.1
README
A php client for message queue which is one of RabbitMQ, Kafka and Redis.
Requirement
-
redis
sudo apt-get install redis-server
-
RabbitMQ
-
Kafka
Install
-
composer
composer require dudashuang/php-queue
Base Usage
-
examples
-
create a driver
-
redis:
<?php require __DIR__ . '/vendor/autoload.php'; $connector = new Lily\Connectors\RedisConnector([ 'scheme' => 'tcp', 'host' => '127.0.0.1', 'port' => 6379, 'read_write_timeout' => 0, ]); $driver = new Lily\Drivers\Redis($connector);
-
kafka:
<?php require __DIR__ . '/vendor/autoload.php'; $connector = new Lily\Connectors\KafkaConnector([ 'brokers' => [ ['host' => 'localhost', 'port' => 9092], ], ]); $driver = new Lily\Drivers\Kafka($connector);
-
rabbitmq:
<?php require __DIR__ . '/vendor/autoload.php'; $connector = new Lily\Connectors\RabbitMQConnector([ 'host' => 'localhost', 'port' => 5672, 'username' => 'guest', 'password' => 'guest', ]); $driver = new Lily\Drivers\RabbitMQ($connector);
-
-
create a application
<?php require __DIR__ . '/vendor/autoload.php'; $application = new Lily\Application($driver, [ 'deafult_queue' => 'default-queue', 'failed_queue' => 'failed-queue', ]);
-
dispatch a job
-
default queue
for ($i=0; $i<10; $i++) { $application->dispatch(new TestJob('hello', new LilyTest\TestModel(1, 2))); }
-
other queue
$application->dispatch((new TestJob(...))->set_queue($queue_name));
-
-
dispatch a event
$application->dispatch(new TestEvent(...));
-
create a consumer
$application−>consume($queue_name);
-
create a listener
$application->listen('LilyTest\Listeners\SendListener', ['TestEvent', 'TestEvent1']);
TODO
-
add RocketMQ driver
-
add delay queue
Additional
you can use supervisor to manage consumer
-
install
sudo apt-get install supervisor
-
supervisor config
sudo vim /etc/supervisor/conf.d/guopika.config
[program:guopika-worker] process_name=%(program_name)s_%(process_num)02d command=php /path_to_listen_queue/cli_func autostart=true autorestart=true user=www-data numprocs=4 redirect_stderr=true stdout_logfile=/path/worker.log