composer require panus/php-thrift-swoole
- 生成服务端代码
thrift --gen php:server,psr4 order.thrift
- 业务层自行去实现生成的接口
$service = new OrderServiceImpl();
$processor = new OrderServiceProcessor($service);
//swooler_server 里的配置选项参数,worker_num根据实际调用情况调节
$setting = [
'worker_num' => 4,
'log_file' => __DIR__.'/swoole.log',
'pid_file' => __DIR__.'/thrift.pid',
];
$socket_tranport = new \SwooleThrift\TSwooleServerTransport('0.0.0.0', 8192, $setting);
$out_factory = $in_factory = new \Thrift\Factory\TTransportFactory();
$out_protocol = $in_protocol = new \Thrift\Factory\TBinaryProtocolFactory();
$server = new \SwooleThrift\TSwooleServer($processor, $socket_tranport, $in_factory, $out_factory, $in_protocol, $out_protocol);
$server->serve();
$socket = new \Thrift\Transport\TSocket('192.168.0.101', 8100);
$transport = new \Thrift\Transport\TFramedTransport($socket);
$protocol = new \Thrift\Protocol\TBinaryProtocol($transport);
$transport->open();
$client = new OrderServiceClient($protocol);
$client->implMethod(...);
$transport->close();
cd /thrift_root/lib/php/src/ext/thrift_protocol
/php_path/bin/phpize
./configure --with-php-config=/php_path/bin/php-config
make
make install
echo "extension=thrift_protocol.so" >> /path/php.ini
- nginx从1.9.0后引入模块ngx_stream_core_module,模块默认是没有开启的,编译时开启 --with-stream
http {
...
}
stream {
upstream thrift {
server 127.0.0.1:8192 weight=1;
#server backend1:9000 weight=5;
}
server {
listen 8100;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass thrift;
}
}
- 关闭服务器:
kill -TERM {masterPid}
- 重启Worker进程:
kill -USR1 {masterPid}
- 官方的额外说明
平滑重启只对onWorkerStart或onReceive等在Worker进程中include/require的PHP文件有效,Server启动前就已经include/require的PHP文件,不能通过平滑重启重新加载 对于Server的配置即$serv->set()中传入的参数设置,必须关闭/重启整个Server才可以重新加载
- 默认绑定的本地回环地址,端口为8090,可在setting 里设置
http_server_host
和http_server_port
,不建绑在公网ip地址上 - 响应如下,也就是
swoole_server->stats
{
"start_time": 1522580115,
"connection_num": 2,
"accept_count": 2,
"close_count": 0,
"tasking_num": 0,
"request_count": 0,
"worker_request_count": 0
}
- 由于传输层是用
TFramedTransport
,所以对应的客户端也是要采用该传输层