Home

Awesome

Dora RPC

Build Status Latest Stable Version Latest Unstable Version License

简介(Introduction)

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

问题提交: Issue

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

设计思路(Design)

功能支持(Function)

请安装依赖(depend)

##Installation

composer require "xcl3721/dora-rpc"

文件功能简介(File)

dora-rpc/src/Client.php

dora-rpc/src/BackEndServer.php

dora-rpc/src/Monitor.php

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

使用方法(Example)

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

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 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)


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内执行,服务端只支持命令行启动)

错误码及含义(Error Code)

性能(Performance)

测试结果Result

Optimize performance性能优化

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

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

Server Config Optimize

License授权

Apache

QQ Group

QQ Group:346840633