定制 so-php/rpc 二次开发

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

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

so-php/rpc

Composer 安装命令:

composer require so-php/rpc

包简介

RPC pattern via php-amqplib

README 文档

README

RPC Pattern via php-amqplib

Avoiding Technology Lockout

One of the goals of ths implementation is to prevent locking out other technologies like python, ruby and java from producing or consuming requests. This is not a terribly difficult task--it just means we need to use a non-proprietary message queue (php-amqplib + rabbitmq) and a message format that isn't language specific. Hence we choose to use a json serialized data structure rather than a PHP serialized string.

That being said, there are no enforced restrictions on the content of the PRC params. It is up to developers to keep vigilant and be sure not to stuff anything in an RPC that is PHP or platform specific.

Message Structure

As stated above, the message is a plain Json string. The structure is an object with three keys (only) at the top level.

  • method the method name
  • params an array to hold rpc params. No imposed limits other than technical feasibility and reasonableness. Params should be present even if empty []. Params are ordered, and will be provided to the RP in the same order they are received.
{
    "name": "sayHelloTo",
    "params": [
        "bob"
    ]
}

Usage

Using RPC is pretty straight forward. Words in uppercase are values that need to be supplied/configured.

Server

First we need to start an RPC Server queue and bind it a class/method.

$conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
$ch = $conn->channel();

$server = new Server($ch);
$server->serve(function($name){ return "Hello $name"; }, 'greet');
// or more commonly:
// $rpc->serve(new Greeter()); // where greeter has a greet method.

echo "listening for RPCs @ " . $server->getQueueName();
while(true){
    $server->wait();
}

Output might look something like:

`Listening for RPCs @ amq.gen-v1ac3`

Client

The client looks a bit like this:

// need a channel to work with
$conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
$ch = $conn->channel();

// we have to tell the client what queue to use
$rpc = new Client($ch, 'amq.gen-v1ac3');

$greeting = $rpc->greet('bob');
echo $greeting;

Output might look like:

`Hello bob`

Wait, wait, wait...

"You mean I have to somehow orchestrate configuring my RPC client with the correct queue for the server?" I hear you asking. The answer is, unapologetically, yes. But you shouldn't do that, you should use a service registry pattern for that. Try so-php/service-registry.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2014-06-03

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固