用于复杂项目前后端分离,分离后项目都通过API工作可更好维护管理。
For complex projects separation, the project can be better maintained by the API project management.
http://blog.sina.com.cn/s/blog_54ef39890102vs3h.html
基于redis制作的服务发现
Single API RPC / Multi API Concurrent RPC
pecl install swoole vim composer.json added: { "name": "xx", "description": "xxx", "require": { "xcl3721/dora-rpc": ">=0.3.4" } } :wq composer update
返回结果是一个数组 分两部分,第一层是通讯状态code,第二层是处理状态 code
a simple server
$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 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;
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();
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内执行,服务端只支持命令行启动)
vim demoserver.php to see $externalConfig var and swoole offcial document 如果想优化性能请参考以上文件的$externalConfig配置
Apache
QQ Group:346840633