<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post//</link>
<title><![CDATA[Dora RPC 虚拟机下实测性能]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Php/Js/Shell/Go]]></category>
<pubDate>Mon, 14 Sep 2015 03:31:01 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	背景：关于rpc，即远程过程调用，这个技术最早是由sun公司发明出来，后来在linux上默认有rpc服务，该功能我最早见在新浪的企业邮箱里有这样一个运用，在用户登录这块用到rpc，其服务端实现了与mysql长连接的技术，进而减少了重次重新连接，提高了高并发时的性能，而后来在鸟哥的一个php框架里实现了类似的php rpc框架，也是用c写的，但究其根源，还是来自于xdr这样一种数据结构，其可以跨平台使用，进而它在做一些高并发这块的的确确有较好的效果，最近rango兄弟的swoole里发布后，也有兄弟基于它做成了Dora Rpc，值得了解，应该性能还成，可能试用并使用，毕竟简单、效率、快才是王道。<br/>Dora RPC<br/>简介(Introduction)<br/><br/>用于复杂项目前后端分离，分离后项目都通过API工作可更好维护管理。<br/><br/>是一款基础于Swoole定长包头通讯协议的最精简的RPC<br/>目前只提供PHP语言代码<br/>后续有什么bug或者问题请提交Issue<br/><br/>功能支持(Function)<br/>支持单API调用，多API并发调用<br/>支持同步调用，异步任务下发<br/>其他相关知识请参考Swoole扩展<br/>客户端长链接，请求完毕后仍旧保留，减少握手消耗<br/>guid收发一致性检测，避免发送和接收数据不一致<br/><br/>dora-rpc/server.php<br/><br/>使用最简单的方式实现的服务端<br/>目前需要继承才能使用，继承后请实现dowork，这个函数是实际处理任务的函数参数为提交参数<br/>做这个只是为了减少大家启用RPC的开发时间<br/>返回结果是一个数组 分两部分，第一层是通讯状态（code），第二层是处理状态（code）<br/><br/><br/>使用方法(Example)<br/><br/>客户端(Client)<br/><textarea name="code" class="C" rows="15" cols="100">
include &quot;dora-rpc/client.php&quot;;

//app server config 
$config = array(
&nbsp;&nbsp;&nbsp;&nbsp;array(&quot;ip&quot;=&gt;&quot;127.0.0.1&quot;,&quot;port&quot;=&gt;9567),
&nbsp;&nbsp;&nbsp;&nbsp;//array(&quot;ip&quot;=&gt;&quot;127.0.0.1&quot;,&quot;port&quot;=&gt;9567), you can set more ,the client will random select one,to increase High availability
);

$obj = new DoraRPCClient($config);
for ($i = 0; $i &lt; 100000; $i++) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;//single &amp;&amp; sync
&nbsp;&nbsp;&nbsp;&nbsp;$ret = $obj-&gt;singleAPI(&quot;abc&quot;, array(234, $i), false,1);
&nbsp;&nbsp;&nbsp;&nbsp;var_dump($ret);

&nbsp;&nbsp;&nbsp;&nbsp;//multi &amp;&amp; rsync
&nbsp;&nbsp;&nbsp;&nbsp;$data = array(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;oak&quot; =&gt; array(&quot;name&quot; =&gt; &quot;oakdf&quot;, &quot;param&quot; =&gt; array(&quot;dsaf&quot; =&gt; &quot;321321&quot;)),
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;cd&quot; =&gt; array(&quot;name&quot; =&gt; &quot;oakdfff&quot;, &quot;param&quot; =&gt; array(&quot;codo&quot; =&gt; &quot;fds&quot;)),
&nbsp;&nbsp;&nbsp;&nbsp;);
&nbsp;&nbsp;&nbsp;&nbsp;$ret = $obj-&gt;multiAPI($data, true,1);
&nbsp;&nbsp;&nbsp;&nbsp;var_dump($ret);
&#125;
</textarea><br/><br/>服务端(Server)<br/><textarea name="code" class="php" rows="15" cols="100">
include &quot;dora-rpc/server.php&quot;;

class Server extends DoraRPCServer &#123;

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

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

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//packet decode process,change by condition
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//包处理进程，根据情况调整数量
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;worker_num&#039; =&gt; 40,

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

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

&nbsp;&nbsp;&nbsp;&nbsp;function initTask($server, $worker_id)&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//require_once() 你要加载的处理方法函数等 what&#039;s you want load (such as framework init)
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&#125;

$res = new Server();
</textarea><br/><br/><br/>_____________________________________________________________________________<br/>经过24小时持续压力测试，目前接口仍旧工作正常<br/><br/>使用的vagrant虚拟进行压测的分配了1G内存和1核CPU（Mac 2.2 GHz Intel Core i7）<br/> <br/><br/>压测进程：目前只开了10个php进程疯狂发送请求<br/>并发性能：TPS 2100上下（比直接使用curl快很多）<br/>响应时间：0.02～0.04s 偶尔出现0.4s<br/>后端代码为：查询一次数据库后返回结果<br/>CPU使用：10～25%<br/>内存使用：一个PHP task 16M 目前开了30个进程<br/>PHP版本：5.4.41<br/>压测时使用端口个数：10个（长连接）<br/><br/>测试代码使用的使用客户端示范程序无限循环，服务端直接返回一个数组。<br/>每次接口会请求一次api接口调用后再下发一个请求内含两个并发任务<br/><br/>其他资源情况如下：<br/><br/>Dora &lt;wbr&gt;RPC &lt;wbr&gt;虚拟机下实测性能<br/><br/>此开源使用Swoole特性制作<br/>客户端使用长链接，处理请求结束后连接也不会断开，再次使用的时候会自动找回<br/>服务端自动管理task及进程通讯<br/>通过task处理业务<br/>如果使用更高速的序列化函数取代serialize会更快一些<br/>支持单api请求，多api并发请求，此功能可取代发展越来越怪的gearman<br/>如果有持久化请求需求，可以考虑在此基础上自行封装下（会降性能的哦）<br/><br/>过几天增加个中间件，可以检测后端服务压力状态自动负载均衡～<br/><br/>github地址<br/>https://github.com/xcl3721/Dora-RPC<br/><br/><br/>有一个哥们测试了一下，地址：http://blog.sina.com.cn/s/blog_54ef39890102vkgh.html
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] Dora RPC 虚拟机下实测性能]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>