<?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[[实践OK]swoole4.4.1协程和通道实现并发访问三个网站。]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Swoole专题研究]]></category>
<pubDate>Mon, 22 Jul 2019 09:03:23 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	背景：关于Go的一协程和通道，PHP也是可以的。<br/><br/>实践中发现swoole去获取微信授权，在单队列网卡和多队列（2个物理核心，8逻辑CPU）的一个情况：<br/>单队列：<br/>8CPU8G：1000并发。<br/>两队列：<br/>8CPU8G：10000并发1W的TPS。<br/><br/>#ls /sys/class/net/enp0s3/queues/<br/>rx-0&nbsp;&nbsp;tx-0<br/><br/>Top的Cache有点多：<br/>7111104 buff/cache<br/><br/>队列来自：https://jackxiang.com/post/10079/<br/><br/><br/>技术：<br/>也就是说三次访问以最长那个，在一些微信请求时以PHP-FPM进行阻塞时，80进程也就400个请求，用这个swoole的请求也就不存在这个问题，可以实现同时发出curl请求，而不是卡在那儿。<br/><br/>也即使是Mysql的访问，用协程，如果一次卡20秒，那么影响也就那一个请求，而不是所有的后面请求就排队等待，这也就是它的优势。<br/><br/>其实现方式，应该是用socket的句柄结合Epoll的事件驱动经过reactor的挂起和唤醒，用到系统的内核进行通知，性能是很不错的。也就是说：<br/>1）必须开端口。<br/>2）那个句柄数要设置大一些才行，一般设置个10万左右。<br/>3）协程对内存的要求较高，内存大一些好。<br/><br/>系统三大杀手，一是内存拷贝（PHP内核支持写时拷贝）、二是系统调用、进程线程切换，而协程经过测试PHP切换一次150纳秒，而Go得160纳秒。<br/><br/>========================================================================================<br/>#cat&nbsp;&nbsp;curl.php&nbsp;&nbsp; <br/><br/><textarea name="code" class="php" rows="15" cols="100">&nbsp;&nbsp; 
&lt;?php
$server = new Swoole&#92;Http&#92;Server(&#039;127.0.0.1&#039;,8000);
$server-&gt;on(&quot;Request&quot;,function($req,$resp)&#123;
&nbsp;&nbsp;&nbsp;&nbsp;$chan = new Chan();
&nbsp;&nbsp;&nbsp;&nbsp;go(function () use ($chan)&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $c = new Co&#92;Http&#92;Client(&quot;www.baidu.com&quot;,443,true);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $c-&gt;get(&#039;/index.php&#039;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $chan-&gt;push($c-&gt;body);
&nbsp;&nbsp;&nbsp;&nbsp;&#125;);
&nbsp;&nbsp;&nbsp;&nbsp;go(function () use ($chan)&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $c = new Co&#92;Http&#92;Client(&quot;www.taobao.com&quot;,443,true);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $c-&gt;get(&#039;/index.php&#039;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $chan-&gt;push($c-&gt;body);
&nbsp;&nbsp;&nbsp;&nbsp;&#125;);
&nbsp;&nbsp;&nbsp;&nbsp;go(function () use ($chan)&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $c = new Co&#92;Http&#92;Client(&quot;www.sina.com.cn&quot;,443,true);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $c-&gt;get(&#039;/index.php&#039;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $chan-&gt;push($c-&gt;body);
&nbsp;&nbsp;&nbsp;&nbsp;&#125;);
&nbsp;&nbsp;&nbsp;&nbsp;for($i = 0; $i &lt; 3;$i++)&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$resp-&gt;write($chan-&gt;pop());
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;$resp-&gt;end();
&#125;);

$server-&gt;start();
</textarea><br/><br/>php curl.php<br/>PHP Fatal error:&nbsp;&nbsp;Swoole&#92;Coroutine&#92;Http&#92;Client::__construct(): need to use `--enable-openssl` to support ssl when compiling swoole. in /data/www/coding.jackxiang.com/curl.php on line 6<br/>[2019-07-22 16:53:17 $4830.0]&nbsp;&nbsp; WARNING swManager_check_exit_status: worker#0 abnormal exit, status=255, signal=0<br/><br/>curl 127.0.0.1:8000<br/>curl: (52) Empty reply from server<br/><br/>重新编译：<br/><textarea name="code" class="php" rows="15" cols="100">
phpize
./configure --with-php-config=/usr/local/php/bin/php-config --enable-coroutine --enable-openssl&nbsp;&nbsp;
make &amp;&amp; make install
</textarea><br/><br/><br/>configure: WARNING: unrecognized options: --enable-coroutine ，最新版本好像把这个给去了。<br/><br/>/usr/local/src/swoole-src-4.4.1/include/swoole.h:591:25: fatal error: openssl/ssl.h: No such file or directory<br/> #include &lt;openssl/ssl.h&gt;<br/><br/>还是不行，参考了一下：<br/>http://www.21yunwei.com/archives/5196<br/><br/>重新phpize再来一次就好了，估计是和phpize生成的configure有关吧。<br/><br/>mv&nbsp;&nbsp;/usr/local/php/lib/php/extensions/no-debug-non-zts-20170718/swoole.so /usr/local/php/ext/.<br/>mv: overwrite ‘/usr/local/php/ext/./swoole.so’? y<br/>启动服务端：<br/>#php curl.php <br/>strace -ff -o /tmp/s.txt -tt php curl.php <br/><br/>连接测试：<br/>curl 127.0.0.1:8000<br/><br/>得到三个网站的返回。<br/><br/><br/>
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [实践OK]swoole4.4.1协程和通道实现并发访问三个网站。]]></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>