要求:

不适用nginx+fastcgi情况下,分布式系统之间如果通讯,如果不阻塞,能并发处理请求

环境:

luman/laravel:5.5

php:7.2

thrift -version :Thrift version 0.11.0

thrift文件模板:testServer.thrift

namespace php Rpc.Test

service Echop {
string Echop(1: string str) ,
}

  

生成RPC文件:

thrift -r --out ./app --gen php:server ./ThriftSource/testServer.thrift

  

服务端的实现:

安装第三方扩展包:

composer require sunlong/thrift

或者

https://github.com/sunlongv5/thrift.git

composer文件修改:

"autoload": {
"classmap": [
"app/Rpc" ],
"psr-4": {
"Rpc\\": "app/Rpc",
"Services\\": "app/services",
"Thrift\\": "vendor/sunlong/thrift/lib/php/lib/Thrift/"
}
},

  

  

新建Sevice文件夹创建文件EchopServie.php  实现thrift的Echop方法

<?php
namespace Services;
use Rpc\Test\EchopIf; class EchopServie implements EchopIf{
public function Echop($str){
\Log::info($str);
sleep(5);
return "RPC:".$str;
}
}

  

  

创建文件app\Console\Commands\RpcServer.php

此代码为thrift的server实现

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Thrift\Exception\TException;
use Thrift\Factory\TBinaryProtocolFactory;
use Thrift\Factory\TTransportFactory;
use Thrift\Server\TServerSocket;
use Thrift\Server\TSimpleServer;
use Thrift\Server\TForkingServer;
use Thrift\TMultiplexedProcessor;
use Rpc\Test\EchopClient;
use Rpc\Test\EchopProcessor;
use Thrift\Protocol\TBinaryProtocol;
use Thrift\Transport\TBufferedTransport; class RpcServer extends Command{
protected $signature = 'server:rpc'; /**
* 控制台命令说明。
*
* @var string
*/
protected $description = 'rpc 服务'; protected static $socketController; public function handle()
{
try { $handler = new \Services\EchopServie();
$processor = new EchopProcessor($handler);
// 将服务注册到TMultiplexedProcessor中
$tFactory = new TTransportFactory();
$pFactory = new TBinaryProtocolFactory(true, true);
$multiplexedProcessor = new TMultiplexedProcessor();
$multiplexedProcessor->registerProcessor("Echop", $processor);
// 监听开始
$transport = new TServerSocket('0.0.0.0', '');
$server = new TForkingServer($processor, $transport, $tFactory, $tFactory, $pFactory, $pFactory);
$server->serve();
} catch (TException $te) {
throw new \Exception($te->getMessage());
}
} }

RpcServer

app\Console\Kernel.php文件中添加

protected $commands = [
RpcServer::class,
];

  

客户端实现:

添加路由:

$router->get('/rpc/test', ['uses' => 'Controller@test']);

  

Controller文件新增test方法
public function test(){
try {
ini_set('memory_limit', '1024M');
// $socket = new THttpClient('http://192.168.1.188', 5201, '/rpc/test2');
$socket = new TSocket('192.168.1.188', '9998');
$socket->setRecvTimeout(50000);
$socket->setDebug(true);
$transport = new TBufferedTransport($socket, 1024, 1024);
$protocol = new TBinaryProtocol($transport);
$client = new \Rpc\Test\EchopClient($protocol);
$transport->open();
$result = $client->Echop('hello world !');
print_r($result);
$transport->close();
} catch (TException $tx) {
print_r($tx->getMessage());
}
}

  

启动服务端:

 /application/php7/bin/php  artisan server:rpc

  

模拟请求:

7878端口为nginx启动的客户端地址

同时刷新三个页面,发现每个页面都是5秒返回的结果,没有阻塞

采用python客户端测试:

/application/python3.6.4/bin/pip3  install thrift

生成pthon客户端thrift文件

thrift -out .. --gen py ./ThriftSource/testPyServer.thrift

编写python的客户端:

#! /usr/bin/env python
# -*- coding: utf-8 -*- from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from Rpc.Python.Echop import Client
import threading __HOST = '192.168.1.188'
__PORT = 9998 def send(data):
tsocket = TSocket.TSocket(__HOST, __PORT)
transport = TTransport.TBufferedTransport(tsocket)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = Client(protocol)
transport.open()
print(client.Echop(data)) if __name__ == '__main__':
threadl = []
t1 = threading.Thread(target=send,args=('python001',))
t2 = threading.Thread(target=send,args=('python002',))
t3 = threading.Thread(target=send,args=('python003',))
threadl.append(t1)
threadl.append(t2)
threadl.append(t3)
for x in threadl:
x.start()

  

执行客户端

python3 ./client.py

  

 结果同一时刻返回三条记录

thrift之php,python使用TServerSocket并发 处理请求的更多相关文章

  1. Python使用grequests并发发送请求

    目录 前言 grequests简单使用 grequests和requests性能对比 异常处理 前言 requests是Python发送接口请求非常好用的一个三方库,由K神编写,简单,方便上手快.但是 ...

  2. thrift安装及python和c++版本调试

    一.安装过程 1.安装依赖库 ]# yum install boost-devel-static libboost-dev libboost-test-dev libboost-program-opt ...

  3. Python中的并发编程

    简介 我们将一个正在运行的程序称为进程.每个进程都有它自己的系统状态,包含内存状态.打开文件列表.追踪指令执行情况的程序指针以及一个保存局部变量的调用栈.通常情况下,一个进程依照一个单序列控制流顺序执 ...

  4. Python开源异步并发框架

    Python开源异步并发框架的未来 2014年3月30日,由全球最大的中文IT社区CSDN主办的“开源技术大会·” (Open Source Technology Conference ,简称OSTC ...

  5. Python几种并发实现方案的性能比较

    http://blog.csdn.net/permike/article/details/54846831 Python几种并发实现方案的性能比较 2017-02-03 14:33 1541人阅读 评 ...

  6. 用 Python 理解 Web 并发模型

    用 Python 理解 Web 并发模型 http://www.jianshu.com/users/1b1fde012122/latest_articles   来源:MountainKing 链接: ...

  7. python学习之并发编程

    目录 一.并发编程之多进程 1.multiprocessing模块介绍 2.Process类的介绍 3.Process类的使用 3.1 创建开启子进程的两种方式 3.2 获取进程pid 3.3验证进程 ...

  8. 百万年薪python之路 -- 并发编程之 多线程 二

    1. 死锁现象与递归锁 进程也有死锁与递归锁,进程的死锁和递归锁与线程的死锁递归锁同理. 所谓死锁: 是指两个或两个以上的进程或线程在执行过程中,因为争夺资源而造成的一种互相等待的现象,在无外力的作用 ...

  9. 使用dispatch_group实现并封装分组并发网络请求

    在实际开发中我们通常会遇到这样一种需求:某个页面加载时通过网络请求获得相应的数据,再做某些操作.有时候加载的内容需要通过好几个请求的数据组合而成,比如有两个请求A和B,我们通常为了省事,会将B请求放在 ...

随机推荐

  1. centos7基于SVN+Apache+IF.svnadmin实现SVN的web管理

    一.介绍 本文介绍的是CentOS7上搭建基于Apache.SVN Server.iF.svnadmin实现web后台可视化管理SVN. iF.SVNAdmin应用程序是Subversion授权文件基 ...

  2. ansible批量管理

    编写批量安装脚本 [root@m01 scripts]# vim install.sh for ip in $* do echo "=======start install to $ip = ...

  3. c#进阶之泛型

    好久没用写博了,感觉工作的越久就越发的懒了,啦啦啦!德玛西亚! 感觉最近食欲不正,便想写写组织下自己的学习路程: 泛型,可能很多朋友在学习这个东西的时候都源于面向对象,当然我也不例外:从一个实体继承另 ...

  4. 【转】git示意图

  5. Linux中LAMP构架的实现

    LAMP:Linux+Apache+Mysql+Perl/PHP/Python一组常用来搭建动态网站或者服务器的开源软件,本身都是各自独立的程序,但是因为常被放在一起使用,拥有了越来越高的兼容度共同组 ...

  6. javascript声明变量

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. 使用特性将数据库返回的datatable转换成对象列表

    public class ColumnMapAttribute : Attribute { private readonly string _name; public ColumnMapAttribu ...

  8. CentOS 7安装mysql(rpm)

    1.检查是否安装了mysql rpm -qa|grep -i mysql centos7默认是安装的mariadb,而安装mysql的话会和mariadb的文件冲突,所以需要先卸载掉mariadb 2 ...

  9. sehll 小脚本的编写{基础}

    1.模拟linnux登录shell #/bin/bash echo -n "login:" read name echo -n "password:" read ...

  10. vue-自主研发非父子关系组件之间通信的问题

    相信很多人都知道解决组件间通信:vuex,今天的主角不是它. element-ui里解决组件间通信的思路:emitter.js ,但是如果你拿来你会发现它解决的是父子组件之间的通信问题.如果我们通信的 ...