一、服务端:

项目服务使用lumen5.8+Swoole作为运行环境,.env配置文件中需要定义每个服务端服务id,方便调试和做特殊的逻辑处理

如:

SERVICE_ID=10001

1、composer中引入

"cecd/thrift": "dev-master"

并执行composer update

2、创建启动脚本

<?php
namespace Cecd\Sdk\Rpc;

use Thrift\CeCd\Sdk\Core\TSwooleServerTransport;
use Thrift\CeCd\Sdk\RpcServiceProcessor;
use Thrift\Factory\TTransportFactory;
use Thrift\Factory\TBinaryProtocolFactory;

/**
 * Class Command.
 */
class Command extends \Thrift\CeCd\Sdk\Core\Command\Command
{

    /**
     * Run up the server.
     *
     * @param string $argv
     *
     * @throws \Exception
     */
    public function run($host, $port)
    {
        require ROOT_PATH .'vendor/autoload.php';


         $this->serverOptions['pid_file'] = '/var/tmp/server_rpc.pid';

        if (file_exists($this->serverOptions['pid_file'])) {
            $pid = file_get_contents($this->serverOptions['pid_file']);
            if (posix_getpgid($pid)) {
                echo "server is running\n";
                return;
            } else {
                //意外退出后再次启动
                unlink($this->serverOptions['pid_file']);
            }
        }
        $this->serverOptions['worker_num'] = 4;
        $handler = new RpcServiceHandle();
        $processor = new RpcServiceProcessor($handler);

        $socket_tranport = new TSwooleServerTransport();
        $out_factory = $in_factory = new TTransportFactory();
        $out_protocol = $in_protocol = new TBinaryProtocolFactory();
        $server = new SwooleServer($processor, $socket_tranport, $in_factory, $out_factory, $in_protocol, $out_protocol);
        $server->options($this->serverOptions);
        $server->setServer($host, $port);
        $server->serve();
    }
}

以上仅为示例代码,启动脚本需要继承 \Thrift\CeCd\Sdk\Core\Command\Command 类

 

3、实现自己的服务器处理接口

 

 

 

 

 

 

 

问题:

今天写java通信php的rpc,遇到个很尴尬的问题,java方法参数是强类型,参数如果是map list set,php只能传array

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐