tzmudz/rabbitmq_rpc
Composer 安装命令:
composer require tzmudz/rabbitmq_rpc
包简介
Very simple RabbitMQ RPC message handler. Based on RPC example.
README 文档
README
This package makes ready to use implementation of RabbitMQ RPC tutorial published on: https://www.rabbitmq.com/tutorials/tutorial-six-php.html It implements both server side and client.
Thanks to this package you will be able to send a message to RabbtiMQ, then receive this message (on the server side) handle it and finally send the response to the client (Producer).
Unlike tutorial this package is set to save the queue and messages after RabbtiMQ server reboot.
Installation
Installation of the package by Composer:
composer require tzmudz/rabbitmq_rpc
Package uses an Environment Variables delivered by vlucas/phpdotenv package.
You have to create an .env file in the root folder containing variables:
RABBTIMQ_PORT=5672 RABBTIMQ_HOST=localhost RABBTIMQ_USERNAME=guest RABBTIMQ_PASSWORD=guest
Thus if you are using e.g. Laravel all you have to do is to put this variables in your .env
app file.
Notice If your application doesn’t use .env files, don't forget to load them by executing the code:
$dotenv = Dotenv\Dotenv::create(__DIR__); $dotenv->load();
You can find more information here: https://github.com/vlucas/phpdotenv
Usage
Package comes with two classes: Producer and Consumer.
Producer
The Producer class is responsible for delivering messages to RabbtiMQ.
In your application
create a new instance of Tzm\Rpc\Producer(). As a parameter you can set the queue name
(by default the name is default):
$producer = new Tzm\Rpc\Producer('long_task_queue_name');
Otherwise you can set the queue name by calling the
setQueueName().
$producer = new Tzm\Rpc\Producer(); $producer->setQueueName('long_task_queue_name');
Finally you have to call the method call() with message as parameter:
$producer = new Tzm\Rpc\Producer(); $producer->setQueueName('long_task_queue_name')->call($message);
Where $message is a string of data that should be send to the RabbtiMQ server.
Actually RPC sends a message and waits for the response given from server. But, if you don't
want to wait for server’s response you can use withoutWaiting():
$producet = new Tzm\Rpc\Producer(); $producer->withoutWaiting()->call($message);
Consumer
To process the queued message you need Consumer class. This class is abstract.
You have to create a new class that inherits from Consumer abstract class. Then you have to implement
several methods.
handleMessage()
This method is responsible for process the message. It accepts $message as
a parameter e.g.
public function handleMessage($message) { echo $message; }
handleError()
Generally it's not necessary, but it handles the error. If the method handleMessage() thrown an
error it will be passed to the handleError() method. So that you can handle the error however you want.
e.g. in Laravel you can log the error message:
public function handleError(\Exception $e) { Log::error($e->getMessage()); }
setResults()
After the message is handled RabbitMQ returns a response message. Initially it returns true. But if you want
you can use setResult() method to set the data you want to return. E.g.:
public function handleMessage($message) { $this->setResult('this_will_be_returned_to_Producer'); }
Run Consumer
Your inherited class of consumer of Consumer should look like this:
namespace Tzm\Rpc; class EchoMessage extends Consumer { protected function handleMessage($message) { echo $message; } protected function handleError(\Exception $e) { \Log::error($e->getMessage()); // In Laravel Log exception message } }
Then you can run the Consumer:
$consumer = new EchoMessage(); $consumer->run();
Just like a Producer class this one also has a setQueueName() method. So you can set the queue name
when new instance is being created or later by using method.
Console information
Additionally there are two methods:
consoleMessage($req)
It's used to display message in a console. It has one parameter $req - containing all the info about the recievied
message. By default this method returns string: New request so if a new message will be delivered
the console should print:
[04.11 11:03:49] [NEW] New request
[04.11 11:03:49] [OK] New request
Whole message could be changed by overriding consoleInfo() method.
ATTENTION!
Don't forget to run Consumer class first (it will create the queue on RabbitMQ server)
License
MIT
tzmudz/rabbitmq_rpc 适用场景与选型建议
tzmudz/rabbitmq_rpc 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 56 次下载、GitHub Stars 达 4, 最近一次更新时间为 2019 年 11 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「queue」 「rpc」 「rabbitmq」 「message」 「broker」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tzmudz/rabbitmq_rpc 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tzmudz/rabbitmq_rpc 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tzmudz/rabbitmq_rpc 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Work with RabbitMQ in Laravel.
Microservice RPC through message queues.
PHP AMQP Binding Library
A Laravel package to monitor queue jobs.
Integrates php-amqplib with Symfony & RabbitMq. Formerly oldsound/rabbitmq-bundle.
Control Nginx as a reverse proxy through plinker RPC
统计信息
- 总下载量: 56
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-11-05