thrift之php,python使用TServerSocket并发 处理请求
要求:
不适用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并发 处理请求的更多相关文章
- Python使用grequests并发发送请求
目录 前言 grequests简单使用 grequests和requests性能对比 异常处理 前言 requests是Python发送接口请求非常好用的一个三方库,由K神编写,简单,方便上手快.但是 ...
- thrift安装及python和c++版本调试
一.安装过程 1.安装依赖库 ]# yum install boost-devel-static libboost-dev libboost-test-dev libboost-program-opt ...
- Python中的并发编程
简介 我们将一个正在运行的程序称为进程.每个进程都有它自己的系统状态,包含内存状态.打开文件列表.追踪指令执行情况的程序指针以及一个保存局部变量的调用栈.通常情况下,一个进程依照一个单序列控制流顺序执 ...
- Python开源异步并发框架
Python开源异步并发框架的未来 2014年3月30日,由全球最大的中文IT社区CSDN主办的“开源技术大会·” (Open Source Technology Conference ,简称OSTC ...
- Python几种并发实现方案的性能比较
http://blog.csdn.net/permike/article/details/54846831 Python几种并发实现方案的性能比较 2017-02-03 14:33 1541人阅读 评 ...
- 用 Python 理解 Web 并发模型
用 Python 理解 Web 并发模型 http://www.jianshu.com/users/1b1fde012122/latest_articles 来源:MountainKing 链接: ...
- python学习之并发编程
目录 一.并发编程之多进程 1.multiprocessing模块介绍 2.Process类的介绍 3.Process类的使用 3.1 创建开启子进程的两种方式 3.2 获取进程pid 3.3验证进程 ...
- 百万年薪python之路 -- 并发编程之 多线程 二
1. 死锁现象与递归锁 进程也有死锁与递归锁,进程的死锁和递归锁与线程的死锁递归锁同理. 所谓死锁: 是指两个或两个以上的进程或线程在执行过程中,因为争夺资源而造成的一种互相等待的现象,在无外力的作用 ...
- 使用dispatch_group实现并封装分组并发网络请求
在实际开发中我们通常会遇到这样一种需求:某个页面加载时通过网络请求获得相应的数据,再做某些操作.有时候加载的内容需要通过好几个请求的数据组合而成,比如有两个请求A和B,我们通常为了省事,会将B请求放在 ...
随机推荐
- (转)sql 违反了 PRIMARY KEY 约束,不能在对象 中插入重复键
说明你的数据里面有重复记录 两种情况 1.已存在的表中和要导入数据之间的重复 这个时候可以通过在两个表之间建立关联,将主键级联找出重复记录 2.要导入的表中存在重复记录 可通过类似如下的语句将表中的重 ...
- 基于PLC1850平台的UDP报文接收与发送
一.UDP报文格式 源端口(2个字节):发送报文的进程的16位端口号. 目的端口(2个字节):目的设备上的接收进程的16位端口号. 长度(2个字节):整个UDP数据报的长度,包括首都和数据字段. 校验 ...
- 用JavaScript+css制作下拉式菜单
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- Java 数据返回接口封装
enum StatusCode package com.lee.utils; public enum StatusCode { SUCCESS(20000, "成功"), FALL ...
- Python爬虫与一汽项目【综述】
项目来源 这个爬虫项目是 去年实验室去一汽后的第一个项目(基本交工,现在处于更新维护阶段).内容大概是,获取到全国31个省份政府的关于汽车的招标公告,再用图形界面的方式展示爬虫内容.在完成政府招标采购 ...
- Java8-Map
1.Staff实体 public class Staff { private String name; private int age; private String address; public ...
- 原创《分享(Angular 和 Vue)按需加载的项目实践优化方案》
针对前端优化的点有很多,例如:图片压缩,雪碧图,js/css/html 文件的压缩合并, cdn缓存, 减少重定向, 按需加载 等等 最近有心想针对 ionic项目 和 vue项目,做一个比较大的优 ...
- CentOS 7编译OpenWRT
安装必要的依赖 yum install subversion binutils bzip2 gcc gcc-c++ gawk gettext flex ncurses-devel zlib-devel ...
- Bugku-CTF之速度要快
Day21 速度要快 速度要快!!!!!! http://123.206.87.240:8002/web6/
- 立即执行函数(自执行函数) IIFE
// 最常用的两种写法 (function(){ /* code */ }()); // 老道推荐写法 (function(){ /* code */ })(); // 当然这种也可以 // 括号和J ...