定制 mamatveev/yii2-rabbitmq-rpc 二次开发

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

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

mamatveev/yii2-rabbitmq-rpc

Composer 安装命令:

composer require mamatveev/yii2-rabbitmq-rpc

包简介

Yii2 rpc client and server classes

README 文档

README

The component is designed to quickly and easily start using the RabbitMQ queue server in the yii2 application. Parallel execution of tasks is the main goal of its creation.

INSTALLATION

composer require mamatveev/yii2-rabbitmq-rpc:@dev

Add a component configuration in the application config:

return [
    'components' => [
        'rpc' => [
            'class' => 'mamatveev\yii2rabbitmq\RabbitComponent',
            'host' => '127.0.0.1',
            'port' => 5672,
            'user' => 'guest',
            'password' => 'guest'
        ],
];

USAGE

To test the capabilities of a component, create a simple console controller

<?php

namespace app\commands;

use mamatveev\yii2rabbitmq\RabbitComponent;
use yii\console\Controller;

class RpcController extends Controller
{

    public function actionRabbitServer()
    {
        /** @var RabbitComponent $rpc */
        $rpc = \Yii::$app->rpc;

        $rpcServer = $rpc->initServer('exchange-name');

        $callback = function($msg){
            $result = "msg from client: " . print_r($msg, true);
            echo $result."\n";
            sleep(1);
            return $result;
        };

        $rpcServer->setCallback($callback);
        $rpcServer->start();
    }

    public function actionRabbitClient()
    {
        /** @var RabbitComponent $rpc */
        $rpc = \Yii::$app->rpc;

        // init a client
        $rpcClient = $rpc->initClient('exchange-name');

        // send a messages to exchange
        for ($i = 0; $i < 5; $i++) {
            $rpcClient->addRequest("message number {$i}, getReplies() test");
        }

        // get all responses from rpc server
        print_r($rpcClient->getReplies());

        for ($i = 0; $i < 5; $i++) {
            $rpcClient->addRequest("message number {$i}, getReplies() with callback");
        }

        // use callback for responses
        $rpcClient->getReplies(function($msg) {
            echo "server reply callback... response is {$msg}\n";
        });

        for ($i = 0; $i < 5; $i++) {
            $rpcClient->addRequest("message number {$i}, waitExecution() test");
        }

        // wait messages execution without getting any response
        $rpcClient->waitExecution();

        // any message object will be serialized
        $message = new \stdClass();
        $message->some_property = 2;
        $rpcClient->addRequest($message);
    }
}

To run RPC server execute bash command:

./yii rpc/rabbit-server

And then in other bash tab run client controller action

./yii rpc/rabbit-client

统计信息

  • 总下载量: 3.15k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-07-13