色叔叔 發表於 2019-12-9 21:21:00

Swoole 是 PHP 中的 Node.js?

<p>一想到那些可以使用 Node 的同事,一些 PHP 开发者的脸都嫉妒绿了。异步 Node 系统可以在不同协议间共享代码库,并在代码之外提供服务。这真的想让一个人转 Node 开发。实际上 PHP 中也有类似于 Node 的存在,并被列入了 PHP 拓展,叫做 Swoole。</p>
<p class="ztext-empty-paragraph">&nbsp;</p>
<h3>PHP 中的 Node ?Swoole 到底是什么?</h3>
<p>我先从&nbsp;官方文档&nbsp;中引用下 Swoole 的定义:</p>
<blockquote>Swoole:面向生产环境的 PHP 异步网络通信引擎。<br>使 PHP 开发人员可以编写高性能、可拓展的异步并发 TCP、UDP、Unix Socket、HTTP,WebSocket 服务,而无需深入了解非阻塞 I/O 编程和初级 Linux 内核。</blockquote>
<p>Swoole 使用 C 语言编写,作为 PHP 的&nbsp;基本扩展&nbsp;存在。听起来可还行,是吧?用 PHP 来运行 HTTP 服务?用 PHP 实现 Websockets ?还有其他的可能性,是不是很风骚?而且所有的这些都会保持极高的性能,我们来看看吧!</p>
<p class="ztext-empty-paragraph">&nbsp;</p>
<h3>如何让它运行?</h3>
<p>不同平台的安装方法有差异。</p>
<p>对于 Linux 来说,只需要运行一条 PECL 命令:</p>
<blockquote>pecl install swoole</blockquote>
<p>MacOS 用户可以使用 brew 命令:</p>
<blockquote>brew install swoole<br>brew install homebrew/php/php72-swoole<br>译者注:截止翻译时,Brew 官方已经移除了所有 PHP 扩展,请使用 PECL 安装。</blockquote>
<p>暂时不支持在 Windows 上的安装,但是可以使用 Docker 的方式。</p>
<p class="ztext-empty-paragraph">&nbsp;</p>
<h3>使用 Docker 运行 Swoole</h3>
<p>毫无疑问,运行 PHP + Swoole 的最佳方案便是 Docker。让我们来看看如何创建一个包含 Swoole 的容器。首先,我们需要创建一个 Dockerfile。</p>
<blockquote>FROM php:latest\<br>RUN pecl install swoole\<br>ADD php.ini /usr/local/etc/php\<br>RUN usermod -u 1000 www-data</blockquote>
<p>这看起来十分直接。基于 PHP 官方 Docker 镜像,使用 PECL 安装 Swoole,接着复制 php.ini 到镜像内 —— 搞定。最后一行是 MacOS 的 Docker 一个常规的权限修复命令。</p>
<p>至于被复制的 php.ini 配置文件,它只需一行:</p>
<blockquote>extension=swoole.so</blockquote>
<p class="ztext-empty-paragraph">&nbsp;</p>
<h3>Swoole 可以做什么?</h3>
<p>Swoole 有许多功能,大部分是异步执行。以下是其中最让人感兴趣的部分(其他的可以在 Swoole&nbsp;官方文档&nbsp;中找到):</p>
<ul>
<li>TCP/UDP 服务端与客户端,</li>
<li>HTTP 服务端与客户端,</li>
<li>Websocket 服务端与客户端,</li>
<li>基于 Redis 协议的服务端与客户端,</li>
<li>MySQL 客户端,</li>
<li>原子性,</li>
<li>文件系统。</li>


</ul>
<p>我们来看下其中的 HTTP 服务、Websocket 服务、文件系统怎么使用。在我看来这是最重要的几个功能。</p>
<p class="ztext-empty-paragraph">&nbsp;</p>
<h3>基于 Swoole 实现 HTTP 服务</h3>
<p>基于 Swoole 仅需少量代码即可实现一个简易的异步 HTTP 服务。以下是一份示例代码,该例子使用异步文件系统来读取&nbsp;<code>index.html</code>&nbsp;文件并作为响应返回给它处理的每条请求。</p>
<div class="highlight">
<div class="cnblogs_code">
<pre>&lt;?<span style="color: rgba(0, 0, 0, 1)">php
chdir(__DIR__);
$http </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> swoole_http_server(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">php</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 128, 1)">8080</span><span style="color: rgba(0, 0, 0, 1)">);
$http</span>-&gt;on(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">start</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">, function ($server) {
    echo </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Server has been started!\n</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
});
$http</span>-&gt;on(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">request</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">, function ($request, $response) {
    swoole_async_readfile(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">index.html</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">, function($filename, $content) use ($response) {
      $response</span>-&gt;header(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Content-Type</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">text/html</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
      $response</span>-&gt;<span style="color: rgba(0, 0, 0, 1)">end($content);
    });
});
$http</span>-&gt;start();</pre>
</div>
<p>&nbsp;</p>
</div>
<p>如你所见,这段代码看起来有点像 Node.js 的风格。</p>
<p>首先,我们创建一个类似 HTTP 服务的&nbsp;<code>swoole_http_server</code>&nbsp;对象。接着,绑定两个异步回调函数到以下事件:一个用于启动,将会在服务启动时被调用;另一个用于请求,将会在收到每次请求时被调用,它带有&nbsp;<code>$request</code>&nbsp;和&nbsp;<code>$response</code>&nbsp;两个参数。</p>
<p><code>$request</code>&nbsp;对象包含了所有与请求相关的数据:请求路径(<code>Path</code>)、头信息(<code>Headers</code>)等等。而&nbsp;<code>$response</code>&nbsp;被用来提供输出、设置响应头等。值得一提的是,以上两个对象都不符合 PSR 标准,而是 Swoole 自定义的。<br>在请求事件中,异步请求文件系统用于从文件加载数据。 一旦数据可用,就会在数据加载完成后触发回调。然后将此数据加载到响应体并关闭比此次响应。 这将会把数据有效地发送回浏览器。</p>
<p>这样看起来很简洁,最重要的是 --- 能运行起来。 来看下它的性能如何呢?</p>
<p class="ztext-empty-paragraph">&nbsp;</p>
<h3>HTTP Server 标准</h3>
<p>为了使用 Swoole 测试 HTTP 服务器的性能,我在 Node 中创建了一个应用程序 --- 它可以与 Swoole 中的应用程序完全相同 - 还有一个 服务器,它将提供 index.html 作为静态文件。 全部运行在 3 个独立的容器中。</p>
<p>然后,我用 wrk 工具给这些容器进行压力测试。 结果令人震惊。</p>
<p class="ztext-empty-paragraph">&nbsp;</p>
<p class="ztext-empty-paragraph">&nbsp;</p>
<p>
<img class="origin_image zh-lightbox-thumb lazy lazyload" alt="" width="631" data-caption="" data-size="normal" data-rawwidth="631" data-rawheight="356" data-original="https://pic3.zhimg.com/v2-a96f97fd6f3a8c268e1aebb88f2d2c6e_r.jpg" data-actualsrc="https://pic3.zhimg.com/v2-a96f97fd6f3a8c268e1aebb88f2d2c6e_b.jpg" data-lazy-status="ok" data-src="https://pic3.zhimg.com/80/v2-a96f97fd6f3a8c268e1aebb88f2d2c6e_hd.jpg"></p>
<p class="ztext-empty-paragraph">&nbsp;</p>
<p class="ztext-empty-paragraph">&nbsp;</p>
<p>Swoole 的工作性能要比预期的好很多!</p>
<p>这令人惊讶。 我没想到 Swoole 会超越 Nginx ,但它确实做到了!这也远远超过了 Node 。 这个扩展的原始功能确实令人印象深刻,但它在请求中完成了更多工作后逐渐消失。 不幸的是, Swoole 有两个小缺点,使这些缺点和原始标准有些偏差。 我们稍后会找到他们。</p>
<p class="ztext-empty-paragraph">&nbsp;</p>
<h3>在 Websocket 服务中使用 Swoole</h3>
<p>如前所述, Swoole 提供了一种创建 websocket 服务器的方法。 它以异步方式来进行工作,遵循与 HTTP 协议并和 Swoole 部分方法功能相同。 在我看来,它是最重要的 Swoole 组件之一。 来吧,在 PHP 运行中的 websockets 会是怎么样。让我们看看它的结果。</p>
<div class="highlight">
<div class="cnblogs_code">
<pre>&lt;?<span style="color: rgba(0, 0, 0, 1)">php
$server </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> swoole_websocket_server(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">php</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 128, 1)">9501</span><span style="color: rgba(0, 0, 0, 1)">);
$server</span>-&gt;on(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">start</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">, function (swoole_websocket_server $server) {
    echo </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Server has been started!\n</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
});
$server</span>-&gt;on(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">open</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">, function (swoole_websocket_server $server, $request) {
    echo </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">websocket: new connection, id: {$request-&gt;fd}\n</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
});
$server</span>-&gt;on(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">message</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">, function (swoole_websocket_server $server, $frame) {
    echo </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">websocket: {$frame-&gt;fd}:{$frame-&gt;data},opcode:{$frame-&gt;opcode},fin:{$frame-&gt;finish}\n</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
    $server</span>-&gt;push($frame-&gt;fd, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Replying, you sent </span><span style="color: rgba(128, 0, 0, 1)">"</span> . $frame-&gt;<span style="color: rgba(0, 0, 0, 1)">data);
});
$server</span>-&gt;on(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">close</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">, function (swoole_websocket_server $server, $fd) {
    echo </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">websocket: connection with id {$fd} has been closed\n</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
});
$server</span>-&gt;start();</pre>
</div>
<p>&nbsp;</p>
</div>
<p>看起来类似于 HTTP 服务器的示例。</p>
<p>首先,我们创建类似于 websocket 服务器的 swoole_websocket_server 对象。 然后,我们将 4 个匿名函数绑定到 4 个事件。 第一个启动事件,它将像 HTTP 服务器的启动事件一样工作。 第二个运行事件,它会在连接另一个 websocket 后执行。 第三个消息事件将在 websocket 向服务器发送消息时执行。最后 --- 关闭时间会在 websocket 断开连接时执行。</p>
<p>ID 是作为 Websocket 连接到服务器的唯一标识,该 ID 随每个新的 websockets 进行递增。</p>
<p class="ztext-empty-paragraph">&nbsp;</p>
<h3>使用 Swoole 时遇到的问题</h3>
<p>到目前为止,这一切都运行良好,但在使用 Swoole 测试某些解决方案时遇到了两个问题。 我将它列出来:</p>
<ul>
<li>HTTP 服务器中没有真正的支持 HTTPS,</li>
<li>脚本中不支持全局变量。</li>
</ul>
<p>第一问题个很容易解决。 我们只需要使用 Nginx 或任何负载均衡设备设置反向代理,就完成了。 但通过这样做,我们就失去了 Swoole 提供的极端性能。</p>
<p>第二个问题更棘手。 Swoole 生成用于处理 HTTP 请求的工作进程,这意味着如果我们创建一个全局变量,它的值在线程之间是独立的,并且它不能工作。下面这段代码是显示问题所在之处。</p>
<div class="highlight">
<div class="cnblogs_code">
<pre>&lt;?<span style="color: rgba(0, 0, 0, 1)">php
$server </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> swoole_websocket_server(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">php</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 128, 1)">9501</span><span style="color: rgba(0, 0, 0, 1)">);
$server</span>-&gt;on(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">start</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">, function (swoole_websocket_server $server) {
    echo </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Server has been started!\n</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
});
$server</span>-&gt;on(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">open</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">, function (swoole_websocket_server $server, $request) {
    echo </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">websocket: new connection, id: {$request-&gt;fd}\n</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
});
$server</span>-&gt;on(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">message</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">, function (swoole_websocket_server $server, $frame) {
    echo </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">websocket: {$frame-&gt;fd}:{$frame-&gt;data},opcode:{$frame-&gt;opcode},fin:{$frame-&gt;finish}\n</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
    $server</span>-&gt;push($frame-&gt;fd, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Replying, you sent </span><span style="color: rgba(128, 0, 0, 1)">"</span> . $frame-&gt;<span style="color: rgba(0, 0, 0, 1)">data);
});
$server</span>-&gt;on(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">close</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">, function (swoole_websocket_server $server, $fd) {
    echo </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">websocket: connection with id {$fd} has been closed\n</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
});
$server</span>-&gt;start();</pre>
</div>
<p>&nbsp;</p>
</div>
<p>预期中响应的信息将返回 0 ,然后返回 1, 2 , 3 等等,但它总是返回 0 。</p>
<p>我找到了 Swoole 的作者来检查它是否是一个 bug ,但事实并非如此。 为了获得我们期望的行为,我们可以在配置中设置 worker_num = 1 ,但这会降低部分性能。</p>
<p class="ztext-empty-paragraph">&nbsp;</p>
<h3>结论</h3>
<p>总的来说,Swoole 有明亮的侧面也有黑暗的角落。我认为将异步编程引入 PHP 仍然是一个好主意。 它可用于各种情况,包括快速设计原型,简洁且责任单一的微服务,低延迟游戏服务器以及作为大型框架的后端服务器。 确实有前途。</p>
<p><strong>以上内容希望帮助到大家</strong></p>
<div>
<div>很多PHPer在进阶的时候总会遇到一些问题和瓶颈,业务代码写多了没有方向感,不知道该从那里入手去提升,对此我整理了一些资料,包括但不限于:分布式架构、高可扩展、高性能、高并发、服务器性能调优、TP6,laravel,YII2,Redis,Swoole、Swoft、Kafka、Mysql优化、shell脚本、Docker、微服务、Nginx等多个知识点高级进阶干货需要的可以免费分享给大家<strong>,需要的phper可以加入我的官方群点击此处</strong></div>
</div>
<p class="ztext-empty-paragraph"><span style="font-size: 18pt">&nbsp;推荐阅读:</span></p>
<p class="ztext-empty-paragraph"><span style="font-size: 18pt">PHP 当Swoole&nbsp;遇上 ThinkPHP5</span></p>
<p class="ztext-empty-paragraph">&nbsp;</p><br><br>
来源:https://www.cnblogs.com/a609251438/p/12013539.html
頁: [1]
查看完整版本: Swoole 是 PHP 中的 Node.js?