PHPackages                             xcl3721/dora-rpc - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. xcl3721/dora-rpc

ActiveLibrary[API Development](/categories/api)

xcl3721/dora-rpc
================

Dora RPC is an Basic Swoole Fixed Header TCP Proctol tiny RPC

0.4.15\_beta(9y ago)463159100[2 issues](https://github.com/xcl3721/Dora-RPC/issues)[1 PRs](https://github.com/xcl3721/Dora-RPC/pulls)1ApachePHPPHP &gt;=5.4.30

Since Jun 11Pushed 7y ago39 watchersCompare

[ Source](https://github.com/xcl3721/Dora-RPC)[ Packagist](https://packagist.org/packages/xcl3721/dora-rpc)[ Docs](https://github.com/xcl3721/dora-rpc)[ RSS](/packages/xcl3721-dora-rpc/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)DependenciesVersions (8)Used By (1)

Dora RPC
========

[](#dora-rpc)

[![Build Status](https://camo.githubusercontent.com/0e40d078c47a30663601895abe6574f08caec221d45c4414c53855639069438b/68747470733a2f2f7472617669732d63692e6f72672f78636c333732312f446f72612d5250432e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/xcl3721/Dora-RPC) [![Latest Stable Version](https://camo.githubusercontent.com/ee8f4da209ef1233d73bbf2ba20a313af32981806c34937ecc8eca807a9433cf/68747470733a2f2f706f7365722e707567782e6f72672f78636c333732312f646f72612d7270632f762f737461626c65)](https://packagist.org/packages/xcl3721/dora-rpc) [![Latest Unstable Version](https://camo.githubusercontent.com/cfef99026eca949622507c0e195f7ef27cc3829222bc85545966170f3654022a/68747470733a2f2f706f7365722e707567782e6f72672f78636c333732312f646f72612d7270632f762f756e737461626c65)](https://packagist.org/packages/xcl3721/dora-rpc) [![License](https://camo.githubusercontent.com/5edc68c5f32dc0733b5d1eb5cb0ec2e4637a191688037c44db96ab48237bfba9/68747470733a2f2f706f7365722e707567782e6f72672f78636c333732312f646f72612d7270632f6c6963656e7365)](https://packagist.org/packages/xcl3721/dora-rpc)

简介(Introduction)
----------------

[](#简介introduction)

Dora RPC 是一款基础于Swoole定长包头通讯协议的最精简的RPC, 用于复杂项目前后端分离，分离后项目都通过API工作可更好的跟踪、升级、维护及管理。

问题提交: [Issue](https://github.com/xcl3721/Dora-RPC/issues)

For complex projects separation, the project can be better maintained by the API project management.

> - Dora RPC is an Basic Swoole Fixed Header TCP Proctol tiny RPC
> - Now support an simple PHP version
> - If you find something wrong,please submit an issue
> - add the http protocol and KeepAlive for the other program language

设计思路(Design)
============

[](#设计思路design)

> - [http://blog.sina.com.cn/s/blog\_54ef39890102vs3h.html](http://blog.sina.com.cn/s/blog_54ef39890102vs3h.html) 架构设计图
> - [http://blog.sina.com.cn/s/blog\_54ef39890102w8ff.html](http://blog.sina.com.cn/s/blog_54ef39890102w8ff.html) 端午升级介绍
> -  Dora-RPC 思想介绍

功能支持(Function)
==============

[](#功能支持function)

> - 支持单API调用，多API并发调用
> - 支持同步调用，异步任务下发不等待结果，异步任务下发统一拿回结果
> - 其他相关知识请参考Swoole扩展
> - 客户端长链接，请求完毕后仍旧保留，减少握手消耗
> - guid收发一致性检测，避免发送和接收数据不一致
> - 基于redis制作的服务发现

> - Single API RPC \\ Multi API Concurrent RPC
> - Asynchronous，synchronization no need result， synchronization get result by manual
> - Please visit Swoole official for further infomation
> - keep the connection of client after the request finishe
> - check the guid when the send&lt;-&gt;recive
> - service discovery.
> - base on Redis. Service discovery for High available

请安装依赖(depend)
-------------

[](#请安装依赖depend)

> - Swoole 1.8.x+
> - PHP 5.4+
> - zlib for compress packet

\##Installation

```
composer require "xcl3721/dora-rpc"

```

文件功能简介(File)
------------

[](#文件功能简介file)

### dora-rpc/src/Client.php

[](#dora-rpcsrcclientphp)

> - 使用最简单的方式实现的客户端，通过这个框架可以轻松实现PHP的伪多线程，通过分布式加快接口响应速度及高可用
> - an simple client,it's easy adn simply to implement the multi fake thread,you can speed up you API by this distribute RPC

### dora-rpc/src/BackEndServer.php

[](#dora-rpcsrcbackendserverphp)

> - API服务端
> - 目前需要继承才能使用，继承后请实现dowork，这个函数是实际处理任务的函数参数为提交参数
> - 做这个只是为了减少大家启用RPC的开发时间
> - 开启服务发现功能，服务端在启动的时候，如果指定redis配置则会自动将当前服务器信息注册到Redis上
> - 返回结果是一个数组 分两部分，第一层是通讯状态code，第二层是处理状态 code

> - a powerful API server
> - you must extends the swserver and implement dowork function
> - it's use for decrease the dev cycle
> - when you setup the redis config the server will register this server to the redis for service discovery
> - the result will be a two-level arrayfirst is communicate state 'code field' ,second is dowork state

### dora-rpc/src/Monitor.php

[](#dora-rpcsrcmonitorphp)

> - 服务发现客户端，通过扫描Redis获取到所有可用后端服务列表，并生成配置到指定路径
> - an discovery controller client that:scan all the redis and get the list of available service and general config file to special path

### dora-rpc/src/groupclient.php (combined to client.php)

[](#dora-rpcsrcgroupclientphp-combined-to-clientphp)

> - 服务发现monitor进程产生的配置可以用这个客户端直接引用，请求时可以指定使用哪个组的服务
> - an client for service discovery （monitor general the config from redis） that you can use the config directly

使用方法(Example)
-------------

[](#使用方法example)

### 任务下发模式介绍(task deploy mode)

[](#任务下发模式介绍task-deploy-mode)

> - 0 sync wait result 同步下发任务阻塞等待结果返回
> - 1 async no need result 下发异步任务，下发成功返回下发成功提示，不等待任务处理结果
> - 2 async get result by getAsyncData function 下发异步任务，下发成功返回下发成功提示，可以在后续调用getAsyncData 获取所有下发的异步结果

### TCP客户端(TCP Client)

[](#tcp客户端tcp-client)

```
$config = include("client.conf.php");
//define the mode
$mode = array("type" => 1, "group" => "group1");

$maxrequest = 0;

//new obj
$obj = new \DoraRPC\Client($config);

//change connect mode
$obj->changeMode($mode);

for ($i = 0; $i < 10000; $i++) {
    //echo $i . PHP_EOL;

    //single
    $time = microtime(true);

    //single && sync
    $ret = $obj->singleAPI("/module_a/abc" . $i, array("mark" => 234, "foo" => $i), \DoraRPC\DoraConst::SW_MODE_WAITRESULT, 1);
    var_dump("single sync", $ret);

    //single call && async
    $ret = $obj->singleAPI("/module_b/abc" . $i, array("yes" => 21321, "foo" => $i), \DoraRPC\DoraConst::SW_MODE_NORESULT, 1);
    var_dump("single async", $ret);

    //single call && async
    $ret = $obj->singleAPI("/module_c/abd" . $i, array("yes" => 233, "foo" => $i), \DoraRPC\DoraConst::SW_MODE_ASYNCRESULT, 1);
    var_dump("single async result", $ret);

    //multi

    //multi && sync
    $data = array(
        "oak" => array("name" => "/module_c/dd" . $i, "param" => array("uid" => "ff")),
        "cd" => array("name" => "/module_f/ef" . $i, "param" => array("pathid" => "fds")),
    );
    $ret = $obj->multiAPI($data, \DoraRPC\DoraConst::SW_MODE_WAITRESULT, 1);
    var_dump("multi sync", $ret);

    //multi && async
    $data = array(
        "oak" => array("name" => "/module_d/oakdf" . $i, "param" => array("dsaf" => "32111321")),
        "cd" => array("name" => "/module_e/oakdfff" . $i, "param" => array("codo" => "f11ds")),
    );
    $ret = $obj->multiAPI($data, \DoraRPC\DoraConst::SW_MODE_NORESULT, 1);
    var_dump("multi async", $ret);

    //multi && async
    $data = array(
        "oak" => array("name" => "/module_a/oakdf" . $i, "param" => array("dsaf" => "11")),
        "cd" => array("name" => "/module_b/oakdfff" . $i, "param" => array("codo" => "f11ds")),
    );
    $ret = $obj->multiAPI($data, \DoraRPC\DoraConst::SW_MODE_ASYNCRESULT, 1);
    var_dump("multi async result", $ret);

    //get all the async result
    $data = $obj->getAsyncData();
    var_dump("allresult", $data);

    //compare each request
    $time = bcsub(microtime(true), $time, 5);
    if ($time > $maxrequest) {
        $maxrequest = $time;
    }
    echo $i . " cost:" . $time . PHP_EOL;
}
echo "max:" . $maxrequest . PHP_EOL;
```

### HTTP客户端(Http Client)

[](#http客户端http-client)

http protocol for the other language use performance is common.suggest used tcp client

```
for ($i = 0; $i < 10000; $i++) {
    $time = microtime(true);

    //mutil call sync wait result
    $data = array(
        "guid" => md5(mt_rand(1000000, 9999999) . mt_rand(1000000, 9999999) . microtime(true)),

        "api" => array(
            "oak" => array("name" => "/module_d/oakdf", "param" => array("dsaf" => "32111321")),
            "cd" => array("name" => "/module_e/oakdfff", "param" => array("codo" => "f11ds")),
        )
    ,
    );

    $data_string = "params=" . urlencode(json_encode($data));

    $ch = curl_init('http://127.0.0.1:9566/api/multisync');
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Connection: Keep-Alive',
            'Keep-Alive: 300',
        )
    );

    $result = curl_exec($ch);
    var_dump(json_decode($result, true));

    //multi call no wait result
    $data = array(
        "guid" => md5(mt_rand(1000000, 9999999) . mt_rand(1000000, 9999999) . microtime(true)),

        "api" => array(
            "oak" => array("name" => "/module_d/oakdf", "param" => array("dsaf" => "32111321")),
            "cd" => array("name" => "/module_e/oakdfff", "param" => array("codo" => "f11ds")),
        )
    ,
    );

    $data_string = "params=" . urlencode(json_encode($data));

    $ch = curl_init('http://127.0.0.1:9566/api/multinoresult');
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Connection: Keep-Alive',
            'Keep-Alive: 300',
        )
    );

    $result = curl_exec($ch);
    var_dump(json_decode($result, true));

    $time = bcsub(microtime(true), $time, 5);
    if ($time > $maxrequest) {
        $maxrequest = $time;
    }
    echo $i . " cost:" . $time . PHP_EOL;
    //var_dump($ret);
}
echo "max:" . $maxrequest . PHP_EOL;
```

### 服务端(Server)

[](#服务端server)

```
class Server extends DoraRPCServer {

    //all of this config for optimize performance
    //以下配置为优化服务性能用，请实际压测调试
    protected  $externalConfig = array(

        //to improve the accept performance ,suggest the number of cpu X 2
        //如果想提高请求接收能力，更改这个，推荐cpu个数x2
        'reactor_num' => 32,

        //packet decode process,change by condition
        //包处理进程，根据情况调整数量
        'worker_num' => 40,

        //the number of task logical process progcessor run you business code
        //实际业务处理进程，根据需要进行调整
        'task_worker_num' => 20,
    );

    function initServer($server){
        //the callback of the server init 附加服务初始化
        //such as swoole atomic table or buffer 可以放置swoole的计数器，table等
    }
    function doWork($param){
        //process you logical 业务实际处理代码仍这里
        //return the result 使用return返回处理结果
        return array("hehe"=>"ohyes");
    }

    function initTask($server, $worker_id){
        //require_once() 你要加载的处理方法函数等 what's you want load (such as framework init)
    }
}

$res = new Server();
```

\###客户端监控器(Client Local Monitor)

```
include "src/Doraconst.php";
include "src/Packet.php";
include "src/Monitor.php";

//redis for service discovery register
//when you on product env please prepare more redis to registe service for high available
$redisconfig = array(
    array(//first reporter
        "ip" => "127.0.0.1",
        "port" => "6379",
    ),
    array(//next reporter
        "ip" => "127.0.0.1",
        "port" => "6379",
    ),
);

//ok start server
$res = new \DoraRPC\Monitor("0.0.0.0", 9569, $redisconfig, "./client.conf.php");
//this server will auto get the node server list from redis and general the client config on special path
```

### 以上代码测试方法

[](#以上代码测试方法)

include以上两个文件，使用命令行启动即可（客户端支持在apache nginx fpm内执行，服务端只支持命令行启动）

> - php democlient.php
> - php demoserver.php

错误码及含义(Error Code)
------------------

[](#错误码及含义error-code)

> - 0 Success work
> - 100001 async task success
> - 100002 unknow task type
> - 100003 you must fill the api parameter on you request
> - 100005 Signed check error
> - 100006 Pack decode type wrong
> - 100007 socket error the recive packet length is wrong
> - 100008 the return guid wrong may be the socket trasfer wrong data
> - 100009 the recive wrong or timeout
> - 100010 there is no server can connect
> - 100011 unknow cmd of controlle
> - 100012 Get Async Result Fail: Client Closed.
> - 100099 unknow communicate mode have been set
> - 100100 guid wront please retry..

性能(Performance)
---------------

[](#性能performance)

> - Mac I7 Intel 2.2Mhz
> - Vagrant with Vm 1 Core
> - 1G Memory
> - with example code (loop forever)

### 测试结果Result

[](#测试结果result)

> - Network Cost:0.002~0.004/sec Per Request
> - CPU 10~25% 以上还有很大优化空间 There is still a lot of optimization space

### Optimize performance性能优化

[](#optimize-performance性能优化)

```
vim demoserver.php
to see $externalConfig var
and swoole offcial document

如果想优化性能请参考以上文件的$externalConfig配置

```

### Server Config Optimize

[](#server-config-optimize)

> -

### License授权

[](#license授权)

Apache

### QQ Group

[](#qq-group)

QQ Group:346840633

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity33

Limited adoption so far

Community28

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 81.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~113 days

Recently: every ~165 days

Total

7

Last Release

2947d ago

Major Versions

0.4.15\_beta → 2.0.x-dev2018-04-23

PHP version history (2 changes)0.4.6PHP &gt;=5.4.30

2.0.x-devPHP &gt;=7.1.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/7b2ed1ebdd56a1859252fbc7a0b2acbaffe6696883993ded27e724644df538eb?d=identicon)[xcl3721](/maintainers/xcl3721)

---

Top Contributors

[![137-rick](https://avatars.githubusercontent.com/u/1940307?v=4)](https://github.com/137-rick "137-rick (119 commits)")[![kaleesi](https://avatars.githubusercontent.com/u/8499651?v=4)](https://github.com/kaleesi "kaleesi (14 commits)")[![JanHuang](https://avatars.githubusercontent.com/u/7090871?v=4)](https://github.com/JanHuang "JanHuang (9 commits)")[![ppanphper](https://avatars.githubusercontent.com/u/6057923?v=4)](https://github.com/ppanphper "ppanphper (2 commits)")[![hehexianshi](https://avatars.githubusercontent.com/u/1439007?v=4)](https://github.com/hehexianshi "hehexianshi (1 commits)")[![ShiFengAway](https://avatars.githubusercontent.com/u/4715254?v=4)](https://github.com/ShiFengAway "ShiFengAway (1 commits)")

---

Tags

doradora-rpcphprpcservice-discoveryswooletcprpcdora-rpc

### Embed Badge

![Health badge](/badges/xcl3721-dora-rpc/health.svg)

```
[![Health](https://phpackages.com/badges/xcl3721-dora-rpc/health.svg)](https://phpackages.com/packages/xcl3721-dora-rpc)
```

###  Alternatives

[apache/thrift

Apache Thrift RPC system

10.9k2.1M41](/packages/apache-thrift)[grpc/grpc

gRPC library for PHP

506116.6M180](/packages/grpc-grpc)[sajya/server

Easy implementation of the JSON-RPC 2.0 server for the Laravel framework.

2391.9M4](/packages/sajya-server)[lstrojny/fxmlrpc

Fast and tiny XML/RPC client with bridges for various HTTP clients

1425.4M30](/packages/lstrojny-fxmlrpc)[jsonrpc/jsonrpc

JSON-RPC 2.0 client/server implementation

53407.1k4](/packages/jsonrpc-jsonrpc)[tivoka/tivoka

The universal JSON-RPC client/server library. JSON-RPC done right!

74159.0k3](/packages/tivoka-tivoka)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
