要求:

不适用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. 利用ResultFilter实现asp.net mvc 页面静态化

    为了提高网站性能.和网站的负载能力,页面静态化是一种有效的方式,这里对于asp.net mvc3 构架下的网站,提供一种个人认为比较好的静态话方式. 实现原理是通过mvc提供的过滤器扩展点实现页面内容 ...

  2. 信步漫谈之Jenkins—集成环境搭建

    一.环境准备 1)Jenkins 部署 WAR 包:jenkins.war(2.164.2 版本,WAR 包官方下载路径:https://jenkins.io/download/)2)Tomcat 服 ...

  3. Oarcle之组函数

    组函数 avg 求20部门的平均薪水为多少? select avg(sal) avgsal from emp where deptno = 20:  sum 求20部门的员工的总薪水 select s ...

  4. 运用java反射机制获取实体方法报错,java.lang.NoSuchMethodException: int.<init>(java.lang.String)

    错误的原因是我的Student实体,成员变量数据类型,使用了int基本数据类型,改成Integer包装类型即可.

  5. window bat 切换目录并执行php文件

    新建一个 test.bat文件,输入一下命令并保存 cmd /k "cd /d D:\PHPWAMP_IN2\phpwamp\server\Nginx-PHPWNMP\htdocs\test ...

  6. 18B20驱动小经验

    在写命令时P14拉高在最后 在读命令时P14在拉低后拉高

  7. Oracle索引之Btree索引

    索引介绍 日常开发中,对于数据的查询如果需要优化,常听说要加个索引.但是为什么加了索引,数据的查询就快了呢?那是不是加了索引就一定会是有效或者有利的呢? Oracle中常见有BTREE索引,位图索引和 ...

  8. day16

    列表生成式语法[表达式 for in 遍历 if 条件]会从li一次去除所有值,进行判断 如果满足条件 就装到新的列表里 生成数据的函数函数体中又yield关键字yield 暂停函数的执行 还能返回一 ...

  9. Jenkins学习

    1.jenkins启动卡在密码初始化处不动的情况,参照: https://blog.csdn.net/lylload/article/details/82754101 https://blog.csd ...

  10. windows cannot find powershell.exe windows 7

    This can happen when the environment variables are missing an entry for Powershell. $env:path must i ...