dudashuang/php-queue

a php client for message queue

v1.2.0 2018-07-09 09:02 UTC

This package is auto-updated.

Last update: 2024-11-27 23:14:21 UTC


README

A php client for message queue which is one of RabbitMQ, Kafka and Redis.

Requirement

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