定制 devyk/yii2-amqp 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

devyk/yii2-amqp

最新稳定版本:v1.0.5

Composer 安装命令:

composer require devyk/yii2-amqp

包简介

Yii 2 extension wrapper to communicate with RabbitMQ server via AMQP. Based on videlalvaro/php-amqplib.

README 文档

README

AMQP extension wrapper to communicate with RabbitMQ server. Based on php-amqplib/php-amqplib. This fork provides an alternative list of the parameters to be able to listen on multiple queues with one worker.

Installation

The preferred way to install this extension is through composer.

Add the following lines

...
"repositories":[
    {
      "type":"git",
      "url":"https://github.com/M0nsterLabs/yii2-migration-aware-module"
    },
],
...
"devyk/yii2-amqp": "1.0.2"
  

to your composer.json file.

Add the following in your console config:

return [
    ...
    'components' => [
        ...
        'amqp' => [
            'class' => 'devyk\amqp\components\Amqp',
            'host' => '127.0.0.1',
            'port' => 5672,
            'user' => 'your_login',
            'password' => 'your_password',
            'vhost' => '/',
        ],
        ...
    ],
    ...
    'controllerMap' => [
        ...
        'rabbit' => [
            'class' => 'devyk\amqp\controllers\AmqpListenerController',
            'interpreters' => [
                'my-exchange' => 'app\components\RabbitInterpreter', // interpreters for each exchange
            ],
        ],
        ...
    ],
    ...
];

Add messages interpreter class @app/components/RabbitInterpreter with your handlers for different routing keys:

<?php

namespace app\components;

use devyk\amqp\components\AmqpInterpreter;
use app\services\ExampleServiceInterface;


class RabbitInterpreter extends AmqpInterpreter
{
    protected $service;
    
    /**
     * Example of passing custom service as dependency for the AmqpInterpreter
     */
    public function __construct(ExampleServiceInterface $exampleService)
    {
        $this->service = $exampleService;
        parent::__construct();
    }
    
    /**
     * Interprets AMQP message with routing key 'hello_world'.
     *
     * @param array $message
     */
    public function readHelloWorld($message)
    {
        // todo: write message handler
        $this->log(print_r($message, true));
    }
}

Usage

Just run command

$ php yii rabbit

to listen queue on specified exchange

$ php yii rabbit <queue_name> --exchange=<exchange_name>

to listen multiple queues with one worker

$ php yii rabbit <queue_name>, <queue_name_1> --exchange=<exchange_name>

to enable auto acknowledge

$ php yii rabbit <queue_name>, <queue_name_1> --noAck=true

Also you can create controllers for your needs. Just use for your web controllers class devyk\amqp\controllers\AmqpConsoleController instead of yii\web\Controller and for your console controllers class devyk\amqp\controllers\AmqpConsoleController instead of yii\console\Controller. AMQP connection will be available with property connection. AMQP channel will be available with property channel.

统计信息

  • 总下载量: 36.55k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 40
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2019-03-14