戒腥的猫 發表於 2019-11-7 13:58:00

PHP laravel+thrift+swoole打造微服务框架

<blockquote>
<p>Laravel作为最受欢迎的php web框架一直广受广大互联网公司的喜爱。</p>
<p>笔者也参与过一些由laravel开发的项目。虽然laravel的性能广受诟病但是业界也有一些比较好的解决方案,比如堆机器,比如使用swoole进行加速。</p>
<p>一个项目立项到开发上线,随着时间和需求的不断激增,会越来越复杂,变成一个大项目,如果前期项目架构没设计的不好,代码会越来越臃肿,难以维护,后期的每次产品迭代上线都会牵一发而动全身。项目微服务化,松耦合模块间的关系,是一个很好的选择,随然增加了维护成本,但是还是很值得的。</p>
<p>那么有什么办法使一个laravel项目改造成微服务呢?</p>
</blockquote>
<p class="ztext-empty-paragraph">&nbsp;</p>
<p>最近研究thrift的时候发现thrift对php之城非常好,那么可不可以使用使用thrift作为rpc框架,使用swoole来实现异步TCP服务,打造一个微服务框架呢。</p>
<p class="ztext-empty-paragraph">&nbsp;</p>
<p>心动不如行动我们开始尝试一下吧。首先我们创建一个laravel的项目,笔者使用的laravel官方提供的homestead的环境。</p>
<div class="highlight">
<pre><code class="language-text">laravel new laravel-thrift-app </code></pre>
</div>
<p>安装laravel-s <span style="text-decoration: underline"><span class="invisible">https://<span class="visible">github.com/hhxsv5/larav<span class="invisible">el-s/blob/master/README-CN.md</span></span></span></span></p>
<div class="highlight">
<div class="cnblogs_code">
<pre>composer <span style="color: rgba(0, 0, 255, 1)">require</span> "hhxsv5/laravel-s:~3.5.0" -vvv </pre>
</div>
<p>laravel-s是一个由swoole写的laravel扩展,赋予laravel更好的性能,具体使用方法参看官方文档。</p>
</div>
<p>在项目的根目录下新建一个thrift的目录,然后在该子目录下创建 Thrift IDL 文件 user.thrift,用于定义和用户相关的服务接口。</p>
<div class="highlight">
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> namespace php App.Thrift.<span style="color: rgba(0, 0, 0, 1)">User
</span><span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 定义用户接口 </span>
<span style="color: rgba(0, 128, 128, 1)">3</span> <span style="color: rgba(0, 0, 0, 1)">service User {   
</span><span style="color: rgba(0, 128, 128, 1)">4</span> <span style="color: rgba(0, 0, 255, 1)">string</span> getInfo(1:<span style="color: rgba(0, 0, 0, 1)">i32 id)
</span><span style="color: rgba(0, 128, 128, 1)">5</span>}</pre>
</div>
<p>&nbsp;</p>
</div>
<p>这里我们定义了一个接口,接着在项目根目录下运行如下命令,根据上述 IDL 文件生成相关的服务代码:</p>
<div class="highlight">
<div class="cnblogs_code">
<pre>thrift -r --gen php:server -out ./ thrift/user.thrift </pre>
</div>
<p>&nbsp;</p>
</div>
<p>查看文件这时候我们会发现在App\Thrift\User`目录下生成对应的服务代码。</p>
<p class="ztext-empty-paragraph">&nbsp;</p>
<p>通过 Composer 安装 Thrift PHP 依赖包:</p>
<div class="highlight">
<div class="cnblogs_code">
<pre>composer <span style="color: rgba(0, 0, 255, 1)">require</span> apache/thrift </pre>
</div>
<p>&nbsp;</p>
</div>
<p>编写服务代码,在 app目录下新建一个 Services/Server 子目录,然后在该目录下创建服务接口类 UserService,该类实现自 `App\Thrift\User\UserIf` 接口:</p>
<div class="highlight">
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> &lt;?<span style="color: rgba(0, 0, 0, 1)">php
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span> <span style="color: rgba(0, 0, 0, 1)">namespace App\Services\Server;
</span><span style="color: rgba(0, 128, 128, 1)"> 3</span>
<span style="color: rgba(0, 128, 128, 1)"> 4</span>
<span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(0, 0, 255, 1)">use</span><span style="color: rgba(0, 0, 0, 1)"> App\Thrift\User\UserIf;
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span>
<span style="color: rgba(0, 128, 128, 1)"> 7</span> <span style="color: rgba(0, 0, 255, 1)">class</span> UserService <span style="color: rgba(0, 0, 255, 1)">implements</span><span style="color: rgba(0, 0, 0, 1)"> UserIf
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span> <span style="color: rgba(0, 0, 0, 1)">{
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span>   <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">function</span> getInfo(<span style="color: rgba(128, 0, 128, 1)">$id</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)">10</span> <span style="color: rgba(0, 0, 0, 1)">    {
</span><span style="color: rgba(0, 128, 128, 1)">11</span>         <span style="color: rgba(0, 0, 255, 1)">return</span> "chenSi".<span style="color: rgba(128, 0, 128, 1)">$id</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">12</span> <span style="color: rgba(0, 0, 0, 1)">    }
</span><span style="color: rgba(0, 128, 128, 1)">13</span> }</pre>
</div>
<p>&nbsp;</p>
</div>
<p>在 app 目录下新建一个 Sockets目录用于存放 Swoole 相关代码,首先我们创建一个 ServerTransport.php用来存放服务端代理类,并编写代码如下:</p>
<div class="highlight">
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> &lt;?<span style="color: rgba(0, 0, 0, 1)">php
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span> <span style="color: rgba(0, 0, 0, 1)">namespace App\Sockets;
</span><span style="color: rgba(0, 128, 128, 1)"> 3</span>
<span style="color: rgba(0, 128, 128, 1)"> 4</span>
<span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(0, 0, 255, 1)">use</span><span style="color: rgba(0, 0, 0, 1)"> Thrift\Server\TServerTransport;
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span>
<span style="color: rgba(0, 128, 128, 1)"> 7</span> <span style="color: rgba(0, 0, 255, 1)">class</span> ServerTransport <span style="color: rgba(0, 0, 255, 1)">extends</span><span style="color: rgba(0, 0, 0, 1)"> TServerTransport
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span> <span style="color: rgba(0, 0, 0, 1)">{
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span>   <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
</span><span style="color: rgba(0, 128, 128, 1)">10</span> <span style="color: rgba(0, 128, 0, 1)">   * @var array 服务器选项
</span><span style="color: rgba(0, 128, 128, 1)">11</span>      <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">12</span>   <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(128, 0, 128, 1)">$options</span> =<span style="color: rgba(0, 0, 0, 1)"> [
</span><span style="color: rgba(0, 128, 128, 1)">13</span>         'dispatch_mode'         =&gt; 1, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1: 轮循, 3: 争抢</span>
<span style="color: rgba(0, 128, 128, 1)">14</span>         'open_length_check'   =&gt; <span style="color: rgba(0, 0, 255, 1)">true</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">打开包长检测</span>
<span style="color: rgba(0, 128, 128, 1)">15</span>         'package_max_length'    =&gt; 8192000, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">最大的请求包长度,8M</span>
<span style="color: rgba(0, 128, 128, 1)">16</span>         'package_length_type'   =&gt; 'N', <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">长度的类型,参见PHP的pack函数</span>
<span style="color: rgba(0, 128, 128, 1)">17</span>         'package_length_offset' =&gt; 0,   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">第N个字节是包长度的值</span>
<span style="color: rgba(0, 128, 128, 1)">18</span>         'package_body_offset'   =&gt; 4,   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">从第几个字节计算长度</span>
<span style="color: rgba(0, 128, 128, 1)">19</span> <span style="color: rgba(0, 0, 0, 1)">    ];
</span><span style="color: rgba(0, 128, 128, 1)">20</span>
<span style="color: rgba(0, 128, 128, 1)">21</span>   <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
</span><span style="color: rgba(0, 128, 128, 1)">22</span> <span style="color: rgba(0, 128, 0, 1)">   * @var SwooleServer
</span><span style="color: rgba(0, 128, 128, 1)">23</span>      <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">24</span>   <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(128, 0, 128, 1)">$server</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">25</span>   <span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(128, 0, 128, 1)">$host</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">26</span>   <span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(128, 0, 128, 1)">$port</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">27</span>   <span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(128, 0, 128, 1)">$sockType</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">28</span>
<span style="color: rgba(0, 128, 128, 1)">29</span>
<span style="color: rgba(0, 128, 128, 1)">30</span>   <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">function</span> __construct(<span style="color: rgba(128, 0, 128, 1)">$swoole</span>, <span style="color: rgba(128, 0, 128, 1)">$host</span>, <span style="color: rgba(128, 0, 128, 1)">$port</span> = 9999, <span style="color: rgba(128, 0, 128, 1)">$sockType</span> = SWOOLE_SOCK_TCP, <span style="color: rgba(128, 0, 128, 1)">$options</span> =<span style="color: rgba(0, 0, 0, 1)"> [])
</span><span style="color: rgba(0, 128, 128, 1)">31</span> <span style="color: rgba(0, 0, 0, 1)">    {
</span><span style="color: rgba(0, 128, 128, 1)">32</span>         <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;server = <span style="color: rgba(128, 0, 128, 1)">$swoole</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">33</span>         <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;host   = <span style="color: rgba(128, 0, 128, 1)">$host</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">34</span>         <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;port   = <span style="color: rgba(128, 0, 128, 1)">$port</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">35</span>         <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;sockType = <span style="color: rgba(128, 0, 128, 1)">$sockType</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">36</span>         <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;options = <span style="color: rgba(0, 128, 128, 1)">array_merge</span>(<span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;options,<span style="color: rgba(128, 0, 128, 1)">$options</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">37</span>
<span style="color: rgba(0, 128, 128, 1)">38</span> <span style="color: rgba(0, 0, 0, 1)">    }
</span><span style="color: rgba(0, 128, 128, 1)">39</span>
<span style="color: rgba(0, 128, 128, 1)">40</span>
<span style="color: rgba(0, 128, 128, 1)">41</span>   <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> listen()
</span><span style="color: rgba(0, 128, 128, 1)">42</span> <span style="color: rgba(0, 0, 0, 1)">    {
</span><span style="color: rgba(0, 128, 128, 1)">43</span>         <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;server =<span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;server-&gt;addlistener(<span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;host,<span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;port,<span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;<span style="color: rgba(0, 0, 0, 1)">sockType);
</span><span style="color: rgba(0, 128, 128, 1)">44</span>         <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;server-&gt;set(<span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;<span style="color: rgba(0, 0, 0, 1)">options);
</span><span style="color: rgba(0, 128, 128, 1)">45</span>         <span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">46</span> <span style="color: rgba(0, 0, 0, 1)">    }
</span><span style="color: rgba(0, 128, 128, 1)">47</span>
<span style="color: rgba(0, 128, 128, 1)">48</span>
<span style="color: rgba(0, 128, 128, 1)">49</span>   <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> close()
</span><span style="color: rgba(0, 128, 128, 1)">50</span> <span style="color: rgba(0, 0, 0, 1)">    {
</span><span style="color: rgba(0, 128, 128, 1)">51</span>         <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">$this-&gt;server-&gt;shutdown();</span>
<span style="color: rgba(0, 128, 128, 1)">52</span>         <span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">53</span> <span style="color: rgba(0, 0, 0, 1)">    }
</span><span style="color: rgba(0, 128, 128, 1)">54</span>
<span style="color: rgba(0, 128, 128, 1)">55</span>
<span style="color: rgba(0, 128, 128, 1)">56</span>   <span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> acceptImpl()
</span><span style="color: rgba(0, 128, 128, 1)">57</span> <span style="color: rgba(0, 0, 0, 1)">    {
</span><span style="color: rgba(0, 128, 128, 1)">58</span>         <span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">59</span> <span style="color: rgba(0, 0, 0, 1)">    }
</span><span style="color: rgba(0, 128, 128, 1)">60</span> }</pre>
</div>
<p>&nbsp;</p>
</div>
<p>我们在代理类的构造函数中初始化 Swoole TCP 服务器参数,由于我们使用的是laravel-s然后在该类中定义 listen 方法启动这个swoole增加监听的端口并监听客户端请求。</p>
<p>我们在 app/Sockets目录下创建 Transport.php文件用于存放基于 Swoole 的传输层实现代码:</p>
<div class="highlight">
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> &lt;?<span style="color: rgba(0, 0, 0, 1)">php
</span><span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
</span><span style="color: rgba(0, 128, 128, 1)">3</span> <span style="color: rgba(0, 128, 0, 1)"> * Created by PhpStorm.
</span><span style="color: rgba(0, 128, 128, 1)">4</span> <span style="color: rgba(0, 128, 0, 1)"> * User: 74100
</span><span style="color: rgba(0, 128, 128, 1)">5</span> <span style="color: rgba(0, 128, 0, 1)"> * Date: 2019/10/21
</span><span style="color: rgba(0, 128, 128, 1)">6</span> <span style="color: rgba(0, 128, 0, 1)"> * Time: 2:22
</span><span style="color: rgba(0, 128, 128, 1)">7</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">8</span> <span style="color: rgba(0, 0, 0, 1)">namespace App\Sockets;
</span><span style="color: rgba(0, 128, 128, 1)">9</span>
<span style="color: rgba(0, 128, 128, 1)"> 10</span> <span style="color: rgba(0, 0, 255, 1)">use</span> Swoole\Server <span style="color: rgba(0, 0, 255, 1)">as</span><span style="color: rgba(0, 0, 0, 1)"> SwooleServer;
</span><span style="color: rgba(0, 128, 128, 1)"> 11</span> <span style="color: rgba(0, 0, 255, 1)">use</span> Thrift\<span style="color: rgba(0, 0, 255, 1)">Exception</span><span style="color: rgba(0, 0, 0, 1)">\TTransportException;
</span><span style="color: rgba(0, 128, 128, 1)"> 12</span> <span style="color: rgba(0, 0, 255, 1)">use</span><span style="color: rgba(0, 0, 0, 1)"> Thrift\Transport\TTransport;
</span><span style="color: rgba(0, 128, 128, 1)"> 13</span>
<span style="color: rgba(0, 128, 128, 1)"> 14</span> <span style="color: rgba(0, 0, 255, 1)">class</span> Transport <span style="color: rgba(0, 0, 255, 1)">extends</span><span style="color: rgba(0, 0, 0, 1)"> TTransport
</span><span style="color: rgba(0, 128, 128, 1)"> 15</span> <span style="color: rgba(0, 0, 0, 1)">{
</span><span style="color: rgba(0, 128, 128, 1)"> 16</span>   <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
</span><span style="color: rgba(0, 128, 128, 1)"> 17</span> <span style="color: rgba(0, 128, 0, 1)">   * @var swoole服务器实例
</span><span style="color: rgba(0, 128, 128, 1)"> 18</span>      <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)"> 19</span>   <span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(128, 0, 128, 1)">$server</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 20</span>   <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
</span><span style="color: rgba(0, 128, 128, 1)"> 21</span> <span style="color: rgba(0, 128, 0, 1)">   * @var int 客户端连接描述符
</span><span style="color: rgba(0, 128, 128, 1)"> 22</span>      <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)"> 23</span>   <span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(128, 0, 128, 1)">$fd</span> = -1<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 24</span>   <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
</span><span style="color: rgba(0, 128, 128, 1)"> 25</span> <span style="color: rgba(0, 128, 0, 1)">   * @var string 数据
</span><span style="color: rgba(0, 128, 128, 1)"> 26</span>      <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)"> 27</span>   <span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(128, 0, 128, 1)">$data</span> = ''<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 28</span>   <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
</span><span style="color: rgba(0, 128, 128, 1)"> 29</span> <span style="color: rgba(0, 128, 0, 1)">   * @var int 数据读取指针
</span><span style="color: rgba(0, 128, 128, 1)"> 30</span>      <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)"> 31</span>   <span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(128, 0, 128, 1)">$offset</span> = 0<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 32</span>
<span style="color: rgba(0, 128, 128, 1)"> 33</span>   <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
</span><span style="color: rgba(0, 128, 128, 1)"> 34</span> <span style="color: rgba(0, 128, 0, 1)">   * SwooleTransport constructor.
</span><span style="color: rgba(0, 128, 128, 1)"> 35</span> <span style="color: rgba(0, 128, 0, 1)">   * @param SwooleServer $server
</span><span style="color: rgba(0, 128, 128, 1)"> 36</span> <span style="color: rgba(0, 128, 0, 1)">   * @param int $fd
</span><span style="color: rgba(0, 128, 128, 1)"> 37</span> <span style="color: rgba(0, 128, 0, 1)">   * @param string $data
</span><span style="color: rgba(0, 128, 128, 1)"> 38</span>      <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)"> 39</span>   <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">function</span> __construct(SwooleServer <span style="color: rgba(128, 0, 128, 1)">$server</span>, <span style="color: rgba(128, 0, 128, 1)">$fd</span>, <span style="color: rgba(128, 0, 128, 1)">$data</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)"> 40</span> <span style="color: rgba(0, 0, 0, 1)">    {
</span><span style="color: rgba(0, 128, 128, 1)"> 41</span>         <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;server = <span style="color: rgba(128, 0, 128, 1)">$server</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 42</span>         <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;fd = <span style="color: rgba(128, 0, 128, 1)">$fd</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 43</span>         <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;data = <span style="color: rgba(128, 0, 128, 1)">$data</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 44</span> <span style="color: rgba(0, 0, 0, 1)">    }
</span><span style="color: rgba(0, 128, 128, 1)"> 45</span>
<span style="color: rgba(0, 128, 128, 1)"> 46</span>   <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
</span><span style="color: rgba(0, 128, 128, 1)"> 47</span> <span style="color: rgba(0, 128, 0, 1)">   * Whether this transport is open.
</span><span style="color: rgba(0, 128, 128, 1)"> 48</span> <span style="color: rgba(0, 128, 0, 1)">   *
</span><span style="color: rgba(0, 128, 128, 1)"> 49</span> <span style="color: rgba(0, 128, 0, 1)">   * @return boolean true if open
</span><span style="color: rgba(0, 128, 128, 1)"> 50</span>      <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)"> 51</span>   <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> isOpen()
</span><span style="color: rgba(0, 128, 128, 1)"> 52</span> <span style="color: rgba(0, 0, 0, 1)">    {
</span><span style="color: rgba(0, 128, 128, 1)"> 53</span>         <span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;fd &gt; -1<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 54</span> <span style="color: rgba(0, 0, 0, 1)">    }
</span><span style="color: rgba(0, 128, 128, 1)"> 55</span>
<span style="color: rgba(0, 128, 128, 1)"> 56</span>   <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
</span><span style="color: rgba(0, 128, 128, 1)"> 57</span> <span style="color: rgba(0, 128, 0, 1)">   * Open the transport for reading/writing
</span><span style="color: rgba(0, 128, 128, 1)"> 58</span> <span style="color: rgba(0, 128, 0, 1)">   *
</span><span style="color: rgba(0, 128, 128, 1)"> 59</span> <span style="color: rgba(0, 128, 0, 1)">   * @throws TTransportException if cannot open
</span><span style="color: rgba(0, 128, 128, 1)"> 60</span>      <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)"> 61</span>   <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> open()
</span><span style="color: rgba(0, 128, 128, 1)"> 62</span> <span style="color: rgba(0, 0, 0, 1)">    {
</span><span style="color: rgba(0, 128, 128, 1)"> 63</span>         <span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;<span style="color: rgba(0, 0, 0, 1)">isOpen()) {
</span><span style="color: rgba(0, 128, 128, 1)"> 64</span>             <span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> TTransportException('Swoole Transport already connected.', TTransportException::<span style="color: rgba(0, 0, 0, 1)">ALREADY_OPEN);
</span><span style="color: rgba(0, 128, 128, 1)"> 65</span> <span style="color: rgba(0, 0, 0, 1)">      }
</span><span style="color: rgba(0, 128, 128, 1)"> 66</span> <span style="color: rgba(0, 0, 0, 1)">    }
</span><span style="color: rgba(0, 128, 128, 1)"> 67</span>
<span style="color: rgba(0, 128, 128, 1)"> 68</span>   <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
</span><span style="color: rgba(0, 128, 128, 1)"> 69</span> <span style="color: rgba(0, 128, 0, 1)">   * Close the transport.
</span><span style="color: rgba(0, 128, 128, 1)"> 70</span> <span style="color: rgba(0, 128, 0, 1)">   * @throws TTransportException
</span><span style="color: rgba(0, 128, 128, 1)"> 71</span>      <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)"> 72</span>   <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> close()
</span><span style="color: rgba(0, 128, 128, 1)"> 73</span> <span style="color: rgba(0, 0, 0, 1)">    {
</span><span style="color: rgba(0, 128, 128, 1)"> 74</span>         <span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;<span style="color: rgba(0, 0, 0, 1)">isOpen()) {
</span><span style="color: rgba(0, 128, 128, 1)"> 75</span>             <span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> TTransportException('Swoole Transport not open.', TTransportException::<span style="color: rgba(0, 0, 0, 1)">NOT_OPEN);
</span><span style="color: rgba(0, 128, 128, 1)"> 76</span> <span style="color: rgba(0, 0, 0, 1)">      }
</span><span style="color: rgba(0, 128, 128, 1)"> 77</span>         <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;server-&gt;close(<span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;fd, <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)"> 78</span>         <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;fd = -1<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 79</span> <span style="color: rgba(0, 0, 0, 1)">    }
</span><span style="color: rgba(0, 128, 128, 1)"> 80</span>
<span style="color: rgba(0, 128, 128, 1)"> 81</span>   <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
</span><span style="color: rgba(0, 128, 128, 1)"> 82</span> <span style="color: rgba(0, 128, 0, 1)">   * Read some data into the array.
</span><span style="color: rgba(0, 128, 128, 1)"> 83</span> <span style="color: rgba(0, 128, 0, 1)">   *
</span><span style="color: rgba(0, 128, 128, 1)"> 84</span> <span style="color: rgba(0, 128, 0, 1)">   * @param int $len How much to read
</span><span style="color: rgba(0, 128, 128, 1)"> 85</span> <span style="color: rgba(0, 128, 0, 1)">   * @return string The data that has been read
</span><span style="color: rgba(0, 128, 128, 1)"> 86</span> <span style="color: rgba(0, 128, 0, 1)">   * @throws TTransportException if cannot read any more data
</span><span style="color: rgba(0, 128, 128, 1)"> 87</span>      <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)"> 88</span>   <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">function</span> read(<span style="color: rgba(128, 0, 128, 1)">$len</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)"> 89</span> <span style="color: rgba(0, 0, 0, 1)">    {
</span><span style="color: rgba(0, 128, 128, 1)"> 90</span>         <span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(0, 128, 128, 1)">strlen</span>(<span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;data) - <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;offset &lt; <span style="color: rgba(128, 0, 128, 1)">$len</span><span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 128, 128, 1)"> 91</span>             <span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> TTransportException('Swoole Transport[' . <span style="color: rgba(0, 128, 128, 1)">strlen</span>(<span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;data) . '] read ' . <span style="color: rgba(128, 0, 128, 1)">$len</span> . ' bytes failed.'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)"> 92</span> <span style="color: rgba(0, 0, 0, 1)">      }
</span><span style="color: rgba(0, 128, 128, 1)"> 93</span>         <span style="color: rgba(128, 0, 128, 1)">$data</span> = <span style="color: rgba(0, 128, 128, 1)">substr</span>(<span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;data, <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;offset, <span style="color: rgba(128, 0, 128, 1)">$len</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)"> 94</span>         <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;offset += <span style="color: rgba(128, 0, 128, 1)">$len</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 95</span>         <span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 128, 1)">$data</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 96</span> <span style="color: rgba(0, 0, 0, 1)">    }
</span><span style="color: rgba(0, 128, 128, 1)"> 97</span>
<span style="color: rgba(0, 128, 128, 1)"> 98</span>   <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
</span><span style="color: rgba(0, 128, 128, 1)"> 99</span> <span style="color: rgba(0, 128, 0, 1)">   * Writes the given data out.
</span><span style="color: rgba(0, 128, 128, 1)">100</span> <span style="color: rgba(0, 128, 0, 1)">   *
</span><span style="color: rgba(0, 128, 128, 1)">101</span> <span style="color: rgba(0, 128, 0, 1)">   * @param string $buf The data to write
</span><span style="color: rgba(0, 128, 128, 1)">102</span> <span style="color: rgba(0, 128, 0, 1)">   * @throws TTransportException if writing fails
</span><span style="color: rgba(0, 128, 128, 1)">103</span>      <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">104</span>   <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">function</span> write(<span style="color: rgba(128, 0, 128, 1)">$buf</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)">105</span> <span style="color: rgba(0, 0, 0, 1)">    {
</span><span style="color: rgba(0, 128, 128, 1)">106</span>         <span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;<span style="color: rgba(0, 0, 0, 1)">isOpen()) {
</span><span style="color: rgba(0, 128, 128, 1)">107</span>             <span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> TTransportException('Swoole Transport not open.', TTransportException::<span style="color: rgba(0, 0, 0, 1)">NOT_OPEN);
</span><span style="color: rgba(0, 128, 128, 1)">108</span> <span style="color: rgba(0, 0, 0, 1)">      }
</span><span style="color: rgba(0, 128, 128, 1)">109</span>         <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;server-&gt;send(<span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;fd, <span style="color: rgba(128, 0, 128, 1)">$buf</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">110</span> <span style="color: rgba(0, 0, 0, 1)">    }
</span><span style="color: rgba(0, 128, 128, 1)">111</span> }</pre>
</div>
<p>&nbsp;</p>
</div>
<p>Transport类主要用于从传输层写入或读取数据,最后我们创建 Server.php 文件,用于存放基于 Swoole 的 RPC 服务器类:</p>
<div class="highlight">
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> &lt;?<span style="color: rgba(0, 0, 0, 1)">php
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span> <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
</span><span style="color: rgba(0, 128, 128, 1)"> 3</span> <span style="color: rgba(0, 128, 0, 1)"> * Created by PhpStorm.
</span><span style="color: rgba(0, 128, 128, 1)"> 4</span> <span style="color: rgba(0, 128, 0, 1)"> * User: 74100
</span><span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(0, 128, 0, 1)"> * Date: 2019/10/21
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span> <span style="color: rgba(0, 128, 0, 1)"> * Time: 2:24
</span><span style="color: rgba(0, 128, 128, 1)"> 7</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)"> 8</span> <span style="color: rgba(0, 0, 0, 1)">namespace App\Sockets;
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span>
<span style="color: rgba(0, 128, 128, 1)">10</span> <span style="color: rgba(0, 0, 255, 1)">use</span> Swoole\Server <span style="color: rgba(0, 0, 255, 1)">as</span><span style="color: rgba(0, 0, 0, 1)"> SwooleServer;
</span><span style="color: rgba(0, 128, 128, 1)">11</span> <span style="color: rgba(0, 0, 255, 1)">use</span><span style="color: rgba(0, 0, 0, 1)"> Thrift\Server\TServer;
</span><span style="color: rgba(0, 128, 128, 1)">12</span>
<span style="color: rgba(0, 128, 128, 1)">13</span> <span style="color: rgba(0, 0, 255, 1)">class</span> Server <span style="color: rgba(0, 0, 255, 1)">extends</span><span style="color: rgba(0, 0, 0, 1)"> TServer
</span><span style="color: rgba(0, 128, 128, 1)">14</span> <span style="color: rgba(0, 0, 0, 1)">{
</span><span style="color: rgba(0, 128, 128, 1)">15</span>   <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> serve()
</span><span style="color: rgba(0, 128, 128, 1)">16</span> <span style="color: rgba(0, 0, 0, 1)">    {
</span><span style="color: rgba(0, 128, 128, 1)">17</span>
<span style="color: rgba(0, 128, 128, 1)">18</span>         <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;transport_-&gt;server-&gt;on('receive', [<span style="color: rgba(128, 0, 128, 1)">$this</span>, 'handleReceive'<span style="color: rgba(0, 0, 0, 1)">]);
</span><span style="color: rgba(0, 128, 128, 1)">19</span>         <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;transport_-&gt;<span style="color: rgba(0, 0, 0, 1)">listen();
</span><span style="color: rgba(0, 128, 128, 1)">20</span>
<span style="color: rgba(0, 128, 128, 1)">21</span> <span style="color: rgba(0, 0, 0, 1)">    }
</span><span style="color: rgba(0, 128, 128, 1)">22</span>
<span style="color: rgba(0, 128, 128, 1)">23</span>   <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> stop()
</span><span style="color: rgba(0, 128, 128, 1)">24</span> <span style="color: rgba(0, 0, 0, 1)">    {
</span><span style="color: rgba(0, 128, 128, 1)">25</span>         <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;transport_-&gt;<span style="color: rgba(0, 0, 0, 1)">close();
</span><span style="color: rgba(0, 128, 128, 1)">26</span> <span style="color: rgba(0, 0, 0, 1)">    }
</span><span style="color: rgba(0, 128, 128, 1)">27</span>
<span style="color: rgba(0, 128, 128, 1)">28</span>   <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
</span><span style="color: rgba(0, 128, 128, 1)">29</span> <span style="color: rgba(0, 128, 0, 1)">   * 处理RPC请求
</span><span style="color: rgba(0, 128, 128, 1)">30</span> <span style="color: rgba(0, 128, 0, 1)">   * @param Server $server
</span><span style="color: rgba(0, 128, 128, 1)">31</span> <span style="color: rgba(0, 128, 0, 1)">   * @param int $fd
</span><span style="color: rgba(0, 128, 128, 1)">32</span> <span style="color: rgba(0, 128, 0, 1)">   * @param int $fromId
</span><span style="color: rgba(0, 128, 128, 1)">33</span> <span style="color: rgba(0, 128, 0, 1)">   * @param string $data
</span><span style="color: rgba(0, 128, 128, 1)">34</span>      <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">35</span>   <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">function</span> handleReceive(SwooleServer <span style="color: rgba(128, 0, 128, 1)">$server</span>, <span style="color: rgba(128, 0, 128, 1)">$fd</span>, <span style="color: rgba(128, 0, 128, 1)">$fromId</span>, <span style="color: rgba(128, 0, 128, 1)">$data</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)">36</span> <span style="color: rgba(0, 0, 0, 1)">    {
</span><span style="color: rgba(0, 128, 128, 1)">37</span>         <span style="color: rgba(128, 0, 128, 1)">$transport</span> = <span style="color: rgba(0, 0, 255, 1)">new</span> Transport(<span style="color: rgba(128, 0, 128, 1)">$server</span>, <span style="color: rgba(128, 0, 128, 1)">$fd</span>, <span style="color: rgba(128, 0, 128, 1)">$data</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">38</span>         <span style="color: rgba(128, 0, 128, 1)">$inputTransport</span> = <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;inputTransportFactory_-&gt;getTransport(<span style="color: rgba(128, 0, 128, 1)">$transport</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">39</span>         <span style="color: rgba(128, 0, 128, 1)">$outputTransport</span> = <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;outputTransportFactory_-&gt;getTransport(<span style="color: rgba(128, 0, 128, 1)">$transport</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">40</span>         <span style="color: rgba(128, 0, 128, 1)">$inputProtocol</span> = <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;inputProtocolFactory_-&gt;getProtocol(<span style="color: rgba(128, 0, 128, 1)">$inputTransport</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">41</span>         <span style="color: rgba(128, 0, 128, 1)">$outputProtocol</span> = <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;outputProtocolFactory_-&gt;getProtocol(<span style="color: rgba(128, 0, 128, 1)">$outputTransport</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">42</span>         <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;processor_-&gt;process(<span style="color: rgba(128, 0, 128, 1)">$inputProtocol</span>, <span style="color: rgba(128, 0, 128, 1)">$outputProtocol</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">43</span> <span style="color: rgba(0, 0, 0, 1)">    }
</span><span style="color: rgba(0, 128, 128, 1)">44</span> }</pre>
</div>
<p>&nbsp;</p>
</div>
<p class="ztext-empty-paragraph">&nbsp;</p>
<p>该类继承自 Thrift\Server\TServer,在子类中需要实现 serve` 和 `stop`方法,分别定义服务器启动和关闭逻辑,这里我们在 serve方法中定义了 Swoole TCP 服务器收到请求时的回调处理函数,其中 $this-&gt;transport 指向 App\Swoole\ServerTransport 实例,回调函数 handleReceive中我们会将请求数据传入传输层处理类 Transport进行初始化,然后再通过一系列转化通过处理器对请求进行处理,该方法中 `$this` 指针指向的属性都是在外部启动 RPC 服务器时传入的,后面我们会看到。定义好请求回调后,即可通过 `$this-&gt;transport_-&gt;listen()` 启动服务器并监听请求。</p>
<p class="ztext-empty-paragraph">&nbsp;</p>
<p>最后我们使用laravel-s的事件回调。</p>
<p>在laravel-s的配置文件新增Master进程启动时的事件。</p>
<div class="highlight">
<div class="cnblogs_code">
<pre>'event_handlers'         =&gt; [   'ServerStart' =&gt; \App\Events\ServerStartEvent::<span style="color: rgba(0, 0, 255, 1)">class</span>, ], </pre>
</div>
<p>&nbsp;</p>
</div>
<p>编写ServerStartEvent类。</p>
<div class="highlight">
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> &lt;?<span style="color: rgba(0, 0, 0, 1)">php
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span> <span style="color: rgba(0, 0, 0, 1)">namespace App\Events;
</span><span style="color: rgba(0, 128, 128, 1)"> 3</span> <span style="color: rgba(0, 0, 255, 1)">use</span><span style="color: rgba(0, 0, 0, 1)"> App\Sockets\ServerTransport;
</span><span style="color: rgba(0, 128, 128, 1)"> 4</span> <span style="color: rgba(0, 0, 255, 1)">use</span><span style="color: rgba(0, 0, 0, 1)"> Hhxsv5\LaravelS\Swoole\Events\ServerStartInterface;
</span><span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(0, 0, 255, 1)">use</span><span style="color: rgba(0, 0, 0, 1)"> App\Services\Server\UserService;
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span> <span style="color: rgba(0, 0, 255, 1)">use</span><span style="color: rgba(0, 0, 0, 1)"> App\Sockets\TFramedTransportFactory;
</span><span style="color: rgba(0, 128, 128, 1)"> 7</span> <span style="color: rgba(0, 0, 255, 1)">use</span><span style="color: rgba(0, 0, 0, 1)"> App\Thrift\User\UserProcessor;
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span> <span style="color: rgba(0, 0, 255, 1)">use</span><span style="color: rgba(0, 0, 0, 1)"> Thrift\Factory\TBinaryProtocolFactory;
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span> <span style="color: rgba(0, 0, 255, 1)">use</span><span style="color: rgba(0, 0, 0, 1)"> Swoole\Http\Server;
</span><span style="color: rgba(0, 128, 128, 1)">10</span> <span style="color: rgba(0, 0, 255, 1)">use</span> App\Sockets\Server <span style="color: rgba(0, 0, 255, 1)">as</span><span style="color: rgba(0, 0, 0, 1)"> TServer;
</span><span style="color: rgba(0, 128, 128, 1)">11</span>
<span style="color: rgba(0, 128, 128, 1)">12</span>
<span style="color: rgba(0, 128, 128, 1)">13</span> <span style="color: rgba(0, 0, 255, 1)">class</span> ServerStartEvent <span style="color: rgba(0, 0, 255, 1)">implements</span><span style="color: rgba(0, 0, 0, 1)"> ServerStartInterface
</span><span style="color: rgba(0, 128, 128, 1)">14</span> <span style="color: rgba(0, 0, 0, 1)">{
</span><span style="color: rgba(0, 128, 128, 1)">15</span>   <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> __construct()
</span><span style="color: rgba(0, 128, 128, 1)">16</span> <span style="color: rgba(0, 0, 0, 1)">    {
</span><span style="color: rgba(0, 128, 128, 1)">17</span> <span style="color: rgba(0, 0, 0, 1)">    }
</span><span style="color: rgba(0, 128, 128, 1)">18</span>   <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">function</span> handle(Server <span style="color: rgba(128, 0, 128, 1)">$server</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)">19</span> <span style="color: rgba(0, 0, 0, 1)">    {
</span><span style="color: rgba(0, 128, 128, 1)">20</span>         <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 初始化thrift</span>
<span style="color: rgba(0, 128, 128, 1)">21</span>         <span style="color: rgba(128, 0, 128, 1)">$processor</span> = <span style="color: rgba(0, 0, 255, 1)">new</span> UserProcessor(<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> UserService());
</span><span style="color: rgba(0, 128, 128, 1)">22</span>         <span style="color: rgba(128, 0, 128, 1)">$tFactory</span> = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TFramedTransportFactory();
</span><span style="color: rgba(0, 128, 128, 1)">23</span>         <span style="color: rgba(128, 0, 128, 1)">$pFactory</span> = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TBinaryProtocolFactory();
</span><span style="color: rgba(0, 128, 128, 1)">24</span>         <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 监听本地 9999 端口,等待客户端连接请求</span>
<span style="color: rgba(0, 128, 128, 1)">25</span>         <span style="color: rgba(128, 0, 128, 1)">$transport</span> = <span style="color: rgba(0, 0, 255, 1)">new</span> ServerTransport(<span style="color: rgba(128, 0, 128, 1)">$server</span>,'127.0.0.1', 9999<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">26</span>         <span style="color: rgba(128, 0, 128, 1)">$server</span> = <span style="color: rgba(0, 0, 255, 1)">new</span> TServer(<span style="color: rgba(128, 0, 128, 1)">$processor</span>, <span style="color: rgba(128, 0, 128, 1)">$transport</span>, <span style="color: rgba(128, 0, 128, 1)">$tFactory</span>, <span style="color: rgba(128, 0, 128, 1)">$tFactory</span>, <span style="color: rgba(128, 0, 128, 1)">$pFactory</span>, <span style="color: rgba(128, 0, 128, 1)">$pFactory</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">27</span>         <span style="color: rgba(128, 0, 128, 1)">$server</span>-&gt;<span style="color: rgba(0, 0, 0, 1)">serve();
</span><span style="color: rgba(0, 128, 128, 1)">28</span> <span style="color: rgba(0, 0, 0, 1)">    }
</span><span style="color: rgba(0, 128, 128, 1)">29</span> }</pre>
</div>
<p>&nbsp;</p>
</div>
<p>这时候我们服务端的代码已经写完。开始写客户端的代码。</p>
<p>接下来,我们来修改客户端请求服务端远程接口的代码,在此之前在 app/Sockets目录下新建一个 ClientTransport.php 来存放客户端与服务端通信的传输层实现代码:</p>
<div class="highlight">
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> &lt;?<span style="color: rgba(0, 0, 0, 1)">php
</span><span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(0, 0, 0, 1)">namespace App\Sockets;
</span><span style="color: rgba(0, 128, 128, 1)">3</span>   <span style="color: rgba(0, 0, 255, 1)">use</span><span style="color: rgba(0, 0, 0, 1)"> Swoole\Client;
</span><span style="color: rgba(0, 128, 128, 1)">4</span>   <span style="color: rgba(0, 0, 255, 1)">use</span> Thrift\<span style="color: rgba(0, 0, 255, 1)">Exception</span><span style="color: rgba(0, 0, 0, 1)">\TTransportException;
</span><span style="color: rgba(0, 128, 128, 1)">5</span>   <span style="color: rgba(0, 0, 255, 1)">use</span><span style="color: rgba(0, 0, 0, 1)"> Thrift\Transport\TTransport;
</span><span style="color: rgba(0, 128, 128, 1)">6</span>
<span style="color: rgba(0, 128, 128, 1)">7</span>   <span style="color: rgba(0, 0, 255, 1)">class</span> ClientTransport <span style="color: rgba(0, 0, 255, 1)">extends</span><span style="color: rgba(0, 0, 0, 1)"> TTransport
</span><span style="color: rgba(0, 128, 128, 1)">8</span> <span style="color: rgba(0, 0, 0, 1)">    {
</span><span style="color: rgba(0, 128, 128, 1)">9</span>         <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
</span><span style="color: rgba(0, 128, 128, 1)"> 10</span> <span style="color: rgba(0, 128, 0, 1)">         * @var string 连接地址
</span><span style="color: rgba(0, 128, 128, 1)"> 11</span>          <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)"> 12</span>         <span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(128, 0, 128, 1)">$host</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 13</span>         <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
</span><span style="color: rgba(0, 128, 128, 1)"> 14</span> <span style="color: rgba(0, 128, 0, 1)">         * @var int 连接端口
</span><span style="color: rgba(0, 128, 128, 1)"> 15</span>          <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)"> 16</span>         <span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(128, 0, 128, 1)">$port</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 17</span>         <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
</span><span style="color: rgba(0, 128, 128, 1)"> 18</span> <span style="color: rgba(0, 128, 0, 1)">         * @var Client
</span><span style="color: rgba(0, 128, 128, 1)"> 19</span>          <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)"> 20</span>         <span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(128, 0, 128, 1)">$client</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 21</span>
<span style="color: rgba(0, 128, 128, 1)"> 22</span>         <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
</span><span style="color: rgba(0, 128, 128, 1)"> 23</span> <span style="color: rgba(0, 128, 0, 1)">         * ClientTransport constructor.
</span><span style="color: rgba(0, 128, 128, 1)"> 24</span> <span style="color: rgba(0, 128, 0, 1)">         * @param string $host
</span><span style="color: rgba(0, 128, 128, 1)"> 25</span> <span style="color: rgba(0, 128, 0, 1)">         * @param int $port
</span><span style="color: rgba(0, 128, 128, 1)"> 26</span>          <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)"> 27</span>         <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">function</span> __construct(<span style="color: rgba(128, 0, 128, 1)">$host</span>, <span style="color: rgba(128, 0, 128, 1)">$port</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)"> 28</span> <span style="color: rgba(0, 0, 0, 1)">      {
</span><span style="color: rgba(0, 128, 128, 1)"> 29</span>             <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;host = <span style="color: rgba(128, 0, 128, 1)">$host</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 30</span>             <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;port = <span style="color: rgba(128, 0, 128, 1)">$port</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 31</span>             <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;client = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Client(SWOOLE_SOCK_TCP);
</span><span style="color: rgba(0, 128, 128, 1)"> 32</span> <span style="color: rgba(0, 0, 0, 1)">      }
</span><span style="color: rgba(0, 128, 128, 1)"> 33</span>
<span style="color: rgba(0, 128, 128, 1)"> 34</span>         <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
</span><span style="color: rgba(0, 128, 128, 1)"> 35</span> <span style="color: rgba(0, 128, 0, 1)">         * Whether this transport is open.
</span><span style="color: rgba(0, 128, 128, 1)"> 36</span> <span style="color: rgba(0, 128, 0, 1)">         *
</span><span style="color: rgba(0, 128, 128, 1)"> 37</span> <span style="color: rgba(0, 128, 0, 1)">         * @return boolean true if open
</span><span style="color: rgba(0, 128, 128, 1)"> 38</span>          <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)"> 39</span>         <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> isOpen()
</span><span style="color: rgba(0, 128, 128, 1)"> 40</span> <span style="color: rgba(0, 0, 0, 1)">      {
</span><span style="color: rgba(0, 128, 128, 1)"> 41</span>             <span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;client-&gt;sock &gt; 0<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 42</span> <span style="color: rgba(0, 0, 0, 1)">      }
</span><span style="color: rgba(0, 128, 128, 1)"> 43</span>
<span style="color: rgba(0, 128, 128, 1)"> 44</span>         <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
</span><span style="color: rgba(0, 128, 128, 1)"> 45</span> <span style="color: rgba(0, 128, 0, 1)">         * Open the transport for reading/writing
</span><span style="color: rgba(0, 128, 128, 1)"> 46</span> <span style="color: rgba(0, 128, 0, 1)">         *
</span><span style="color: rgba(0, 128, 128, 1)"> 47</span> <span style="color: rgba(0, 128, 0, 1)">         * @throws TTransportException if cannot open
</span><span style="color: rgba(0, 128, 128, 1)"> 48</span>          <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)"> 49</span>         <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> open()
</span><span style="color: rgba(0, 128, 128, 1)"> 50</span> <span style="color: rgba(0, 0, 0, 1)">      {
</span><span style="color: rgba(0, 128, 128, 1)"> 51</span>             <span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;<span style="color: rgba(0, 0, 0, 1)">isOpen()) {
</span><span style="color: rgba(0, 128, 128, 1)"> 52</span>               <span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> TTransportException('ClientTransport already open.', TTransportException::<span style="color: rgba(0, 0, 0, 1)">ALREADY_OPEN);
</span><span style="color: rgba(0, 128, 128, 1)"> 53</span> <span style="color: rgba(0, 0, 0, 1)">            }
</span><span style="color: rgba(0, 128, 128, 1)"> 54</span>             <span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;client-&gt;connect(<span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;host, <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;<span style="color: rgba(0, 0, 0, 1)">port)) {
</span><span style="color: rgba(0, 128, 128, 1)"> 55</span>               <span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TTransportException(
</span><span style="color: rgba(0, 128, 128, 1)"> 56</span>                     'ClientTransport could not open:' . <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;client-&gt;errCode,
<span style="color: rgba(0, 128, 128, 1)"> 57</span>                     TTransportException::<span style="color: rgba(0, 0, 0, 1)">UNKNOWN
</span><span style="color: rgba(0, 128, 128, 1)"> 58</span> <span style="color: rgba(0, 0, 0, 1)">                );
</span><span style="color: rgba(0, 128, 128, 1)"> 59</span> <span style="color: rgba(0, 0, 0, 1)">            }
</span><span style="color: rgba(0, 128, 128, 1)"> 60</span> <span style="color: rgba(0, 0, 0, 1)">      }
</span><span style="color: rgba(0, 128, 128, 1)"> 61</span>
<span style="color: rgba(0, 128, 128, 1)"> 62</span>         <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
</span><span style="color: rgba(0, 128, 128, 1)"> 63</span> <span style="color: rgba(0, 128, 0, 1)">         * Close the transport.
</span><span style="color: rgba(0, 128, 128, 1)"> 64</span> <span style="color: rgba(0, 128, 0, 1)">         * @throws TTransportException
</span><span style="color: rgba(0, 128, 128, 1)"> 65</span>          <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)"> 66</span>         <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> close()
</span><span style="color: rgba(0, 128, 128, 1)"> 67</span> <span style="color: rgba(0, 0, 0, 1)">      {
</span><span style="color: rgba(0, 128, 128, 1)"> 68</span>             <span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;<span style="color: rgba(0, 0, 0, 1)">isOpen()) {
</span><span style="color: rgba(0, 128, 128, 1)"> 69</span>               <span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> TTransportException('ClientTransport not open.', TTransportException::<span style="color: rgba(0, 0, 0, 1)">NOT_OPEN);
</span><span style="color: rgba(0, 128, 128, 1)"> 70</span> <span style="color: rgba(0, 0, 0, 1)">            }
</span><span style="color: rgba(0, 128, 128, 1)"> 71</span>             <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;client-&gt;<span style="color: rgba(0, 0, 0, 1)">close();
</span><span style="color: rgba(0, 128, 128, 1)"> 72</span> <span style="color: rgba(0, 0, 0, 1)">      }
</span><span style="color: rgba(0, 128, 128, 1)"> 73</span>
<span style="color: rgba(0, 128, 128, 1)"> 74</span>         <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
</span><span style="color: rgba(0, 128, 128, 1)"> 75</span> <span style="color: rgba(0, 128, 0, 1)">         * Read some data into the array.
</span><span style="color: rgba(0, 128, 128, 1)"> 76</span> <span style="color: rgba(0, 128, 0, 1)">         *
</span><span style="color: rgba(0, 128, 128, 1)"> 77</span> <span style="color: rgba(0, 128, 0, 1)">         * @param int $len How much to read
</span><span style="color: rgba(0, 128, 128, 1)"> 78</span> <span style="color: rgba(0, 128, 0, 1)">         * @return string The data that has been read
</span><span style="color: rgba(0, 128, 128, 1)"> 79</span> <span style="color: rgba(0, 128, 0, 1)">         * @throws TTransportException if cannot read any more data
</span><span style="color: rgba(0, 128, 128, 1)"> 80</span>          <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)"> 81</span>         <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">function</span> read(<span style="color: rgba(128, 0, 128, 1)">$len</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)"> 82</span> <span style="color: rgba(0, 0, 0, 1)">      {
</span><span style="color: rgba(0, 128, 128, 1)"> 83</span>             <span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;<span style="color: rgba(0, 0, 0, 1)">isOpen()) {
</span><span style="color: rgba(0, 128, 128, 1)"> 84</span>               <span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> TTransportException('ClientTransport not open.', TTransportException::<span style="color: rgba(0, 0, 0, 1)">NOT_OPEN);
</span><span style="color: rgba(0, 128, 128, 1)"> 85</span> <span style="color: rgba(0, 0, 0, 1)">            }
</span><span style="color: rgba(0, 128, 128, 1)"> 86</span>             <span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;client-&gt;recv(<span style="color: rgba(128, 0, 128, 1)">$len</span>, <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)"> 87</span> <span style="color: rgba(0, 0, 0, 1)">      }
</span><span style="color: rgba(0, 128, 128, 1)"> 88</span>
<span style="color: rgba(0, 128, 128, 1)"> 89</span>         <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
</span><span style="color: rgba(0, 128, 128, 1)"> 90</span> <span style="color: rgba(0, 128, 0, 1)">         * Writes the given data out.
</span><span style="color: rgba(0, 128, 128, 1)"> 91</span> <span style="color: rgba(0, 128, 0, 1)">         *
</span><span style="color: rgba(0, 128, 128, 1)"> 92</span> <span style="color: rgba(0, 128, 0, 1)">         * @param string $buf The data to write
</span><span style="color: rgba(0, 128, 128, 1)"> 93</span> <span style="color: rgba(0, 128, 0, 1)">         * @throws TTransportException if writing fails
</span><span style="color: rgba(0, 128, 128, 1)"> 94</span>          <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)"> 95</span>         <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">function</span> write(<span style="color: rgba(128, 0, 128, 1)">$buf</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)"> 96</span> <span style="color: rgba(0, 0, 0, 1)">      {
</span><span style="color: rgba(0, 128, 128, 1)"> 97</span>             <span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;<span style="color: rgba(0, 0, 0, 1)">isOpen()) {
</span><span style="color: rgba(0, 128, 128, 1)"> 98</span>               <span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> TTransportException('ClientTransport not open.', TTransportException::<span style="color: rgba(0, 0, 0, 1)">NOT_OPEN);
</span><span style="color: rgba(0, 128, 128, 1)"> 99</span> <span style="color: rgba(0, 0, 0, 1)">            }
</span><span style="color: rgba(0, 128, 128, 1)">100</span>             <span style="color: rgba(128, 0, 128, 1)">$this</span>-&gt;client-&gt;send(<span style="color: rgba(128, 0, 128, 1)">$buf</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">101</span> <span style="color: rgba(0, 0, 0, 1)">      }
</span><span style="color: rgba(0, 128, 128, 1)">102</span>   }</pre>
</div>
<p>&nbsp;</p>
</div>
<p>我们在 app/Services/Client 目录下创建 UserService.php,用于存放 RPC 客户端连接与请求服务接口方法:</p>
<div class="highlight">
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> &lt;?<span style="color: rgba(0, 0, 0, 1)">php
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span> <span style="color: rgba(0, 0, 0, 1)">    namespace App\Services\Client;
</span><span style="color: rgba(0, 128, 128, 1)"> 3</span>
<span style="color: rgba(0, 128, 128, 1)"> 4</span>   <span style="color: rgba(0, 0, 255, 1)">use</span><span style="color: rgba(0, 0, 0, 1)"> App\Sockets\ClientTransport;
</span><span style="color: rgba(0, 128, 128, 1)"> 5</span>   <span style="color: rgba(0, 0, 255, 1)">use</span><span style="color: rgba(0, 0, 0, 1)"> App\Thrift\User\UserClient;
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span>   <span style="color: rgba(0, 0, 255, 1)">use</span> Thrift\<span style="color: rgba(0, 0, 255, 1)">Exception</span><span style="color: rgba(0, 0, 0, 1)">\TException;
</span><span style="color: rgba(0, 128, 128, 1)"> 7</span>   <span style="color: rgba(0, 0, 255, 1)">use</span><span style="color: rgba(0, 0, 0, 1)"> Thrift\Protocol\TBinaryProtocol;
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span>   <span style="color: rgba(0, 0, 255, 1)">use</span><span style="color: rgba(0, 0, 0, 1)"> Thrift\Protocol\TMultiplexedProtocol;
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span>   <span style="color: rgba(0, 0, 255, 1)">use</span><span style="color: rgba(0, 0, 0, 1)"> Thrift\Transport\TBufferedTransport;
</span><span style="color: rgba(0, 128, 128, 1)">10</span>   <span style="color: rgba(0, 0, 255, 1)">use</span><span style="color: rgba(0, 0, 0, 1)"> Thrift\Transport\TFramedTransport;
</span><span style="color: rgba(0, 128, 128, 1)">11</span>   <span style="color: rgba(0, 0, 255, 1)">use</span><span style="color: rgba(0, 0, 0, 1)"> Thrift\Transport\TSocket;
</span><span style="color: rgba(0, 128, 128, 1)">12</span>
<span style="color: rgba(0, 128, 128, 1)">13</span>   <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> UserService
</span><span style="color: rgba(0, 128, 128, 1)">14</span> <span style="color: rgba(0, 0, 0, 1)">    {
</span><span style="color: rgba(0, 128, 128, 1)">15</span>         <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">function</span> getUserInfoViaSwoole(int <span style="color: rgba(128, 0, 128, 1)">$id</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)">16</span> <span style="color: rgba(0, 0, 0, 1)">      {
</span><span style="color: rgba(0, 128, 128, 1)">17</span>             <span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 128, 1)">18</span>               <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 建立与 SwooleServer 的连接</span>
<span style="color: rgba(0, 128, 128, 1)">19</span>               <span style="color: rgba(128, 0, 128, 1)">$socket</span> = <span style="color: rgba(0, 0, 255, 1)">new</span> ClientTransport("127.0.0.1", 9999<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">20</span>
<span style="color: rgba(0, 128, 128, 1)">21</span>               <span style="color: rgba(128, 0, 128, 1)">$transport</span> = <span style="color: rgba(0, 0, 255, 1)">new</span> TFramedTransport(<span style="color: rgba(128, 0, 128, 1)">$socket</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">22</span>               <span style="color: rgba(128, 0, 128, 1)">$protocol</span> = <span style="color: rgba(0, 0, 255, 1)">new</span> TBinaryProtocol(<span style="color: rgba(128, 0, 128, 1)">$transport</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">23</span>               <span style="color: rgba(128, 0, 128, 1)">$client</span> = <span style="color: rgba(0, 0, 255, 1)">new</span> UserClient(<span style="color: rgba(128, 0, 128, 1)">$protocol</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">24</span>               <span style="color: rgba(128, 0, 128, 1)">$transport</span>-&gt;<span style="color: rgba(0, 0, 0, 1)">open();
</span><span style="color: rgba(0, 128, 128, 1)">25</span>
<span style="color: rgba(0, 128, 128, 1)">26</span>               <span style="color: rgba(128, 0, 128, 1)">$result</span> = <span style="color: rgba(128, 0, 128, 1)">$client</span>-&gt;getInfo(<span style="color: rgba(128, 0, 128, 1)">$id</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">27</span>
<span style="color: rgba(0, 128, 128, 1)">28</span>               <span style="color: rgba(128, 0, 128, 1)">$transport</span>-&gt;<span style="color: rgba(0, 0, 0, 1)">close();
</span><span style="color: rgba(0, 128, 128, 1)">29</span>               <span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 128, 1)">$result</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">30</span>             } <span style="color: rgba(0, 0, 255, 1)">catch</span> (TException <span style="color: rgba(128, 0, 128, 1)">$TException</span><span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 128, 128, 1)">31</span>               dd(<span style="color: rgba(128, 0, 128, 1)">$TException</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">32</span> <span style="color: rgba(0, 0, 0, 1)">            }
</span><span style="color: rgba(0, 128, 128, 1)">33</span> <span style="color: rgba(0, 0, 0, 1)">      }
</span><span style="color: rgba(0, 128, 128, 1)">34</span>   }</pre>
</div>
<p>&nbsp;</p>
</div>
<p>测试,新增一个路由。</p>
<div class="highlight">
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> Route::get('/user/{id}', <span style="color: rgba(0, 0, 255, 1)">function</span>(<span style="color: rgba(128, 0, 128, 1)">$id</span><span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 128, 128, 1)">2</span>   <span style="color: rgba(128, 0, 128, 1)">$userService</span> = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> UserService();
</span><span style="color: rgba(0, 128, 128, 1)">3</span>   <span style="color: rgba(128, 0, 128, 1)">$user</span> = <span style="color: rgba(128, 0, 128, 1)">$userService</span>-&gt;getUserInfoViaSwoole(<span style="color: rgba(128, 0, 128, 1)">$id</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">4</span>   <span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 128, 1)">$user</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">5</span> });</pre>
</div>
<p>&nbsp;</p>
</div>
<p>启动laravel-s。</p>
<div class="highlight">
<div class="cnblogs_code">
<pre>php bin/laravels start </pre>
</div>
<p>&nbsp;</p>
</div>
<p>在浏览器中输入 <span style="text-decoration: underline"><span class="invisible">http://<span class="visible">192.168.10.100:5200/use<span class="invisible">r/2</span></span></span></span> (192.168.10.100为我homestead设置的地址,5200为laravel-s设置的端口号)</p>
<p>这时候我们就会发现浏览器上面出现chensi2几个大字。一个由larave+thrift+swoole搭建的微服务框架就这样完成了。端口号固定9999也可以使用consul做服务发现。</p>
<p class="ztext-empty-paragraph">&nbsp;</p>
<p>当然了有兴趣的可以写一个package自己去实现而不用laravels这个扩展。</p>
<ul>
<li>很<strong>多PHPer在进阶的时候总会遇到一些问题和瓶颈,业务代码写多了没有方向感,不知道该从那里入手去提升,对此我整理了一些资料,包括但不限于:分布式架构、高可扩展、高性能、高并发、服务器性能调优、TP6,laravel,YII2,Redis,Swoole、Swoft、Kafka、Mysql优化、shell脚本、Docker、微服务、Nginx等多个知识点高级进阶干货需要的可以免费分享给大家,需要的加群(点击→)677079770</strong></li>
</ul><br><br>
来源:https://www.cnblogs.com/a609251438/p/11811665.html
頁: [1]
查看完整版本: PHP laravel+thrift+swoole打造微服务框架