Thrift 实现 JAVA,PHP,C#,Python 互相通信
Thrift介绍 https://www.ibm.com/developerworks/cn/java/j-lo-apachethrift/index.html
首先需要下载 Thrift.exe 和Thrift的源码包,C# Thrift.dll java Thrift jar包
全部放在码云上面了 https://gitee.com/bandung/Allthrift.git
定义一个thrift文件
namespace java com.penngo
namespace php com.penngo
namespace py com.penngo
struct User { 定义的是一个结构体,用于同一的放回结果
: i64 id,
: string name,
: string password
} service LoginService{ 定义服务,服务先可以有多个方法,方法返回值为上面定义的结果User
User login(:string name, :string psw);
} service FaceService{ 方法的返回值为 string类型
string getFace(:string name, :string psw);
} service RegisterService{
User createUser(:string name, :string psw);
}
生成Python 版本的代码
.\thrift-0.9..exe -gen py .\test.thrift //python
.\thrift-0.9..exe -gen csharp.\test.thrift //C#
.\thrift-0.9..exe -gen java.\test.thrift //java
.\thrift-0.9..exe -gen php.\test.thrift //php
就生成了这些代码
就简单举拿JAVA 做服务端,其他都是客户端的例子把,启动之后监听了8848端口,多线程模式的,单线程IO阻塞的在git 里面有
try {
TServerSocket serverTransport = new TServerSocket(8848);
// 用户登录
LoginService.Processor loginProcessor = new LoginService.Processor(
new LoginServiceImpl());
//人脸识别
FaceService.Processor faceProcessor=new FaceService.Processor(new FaceServiceImpl());
// 用户注册
RegisterService.Processor registerProcessor = new RegisterService.Processor(
new RegisterServiceImpl()); TMultiplexedProcessor processor = new TMultiplexedProcessor(); processor.registerProcessor("LoginService", loginProcessor);
processor.registerProcessor("RegisterService", registerProcessor);
processor.registerProcessor("FaceService", faceProcessor);
TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(
serverTransport).processor(processor));
System.out.println("Starting server on port 8848 ...");
server.serve();
} catch (TTransportException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
PHP客户端连接
<?php
namespace com\penngo; require_once __DIR__.'/../../php/lib/Thrift/ClassLoader/ThriftClassLoader.php'; //按照自己的目录来,不能导入错了
//echo __DIR__.'/../../lib/Thrift/ClassLoader/ThriftClassLoader.php';
use Thrift\ClassLoader\ThriftClassLoader; $GEN_DIR = realpath(dirname(__FILE__)).'/../../../gen-php'; //按照自己的目录来,不能导入错了 $loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift', __DIR__ . '/../../php/lib'); //按照自己的目录来,不能导入错了,注册命名空间
//$loader->registerDefinition('shared', $GEN_DIR);
$loader->registerDefinition('com', $GEN_DIR);
$loader->register(); if (php_sapi_name() == 'cli') {
ini_set("display_errors", "stderr");
} use Thrift\Protocol\TBinaryProtocol;
use Thrift\Protocol\TMultiplexedProtocol;
use Thrift\Transport\TSocket;
use Thrift\Transport\THttpClient;
use Thrift\Transport\TBufferedTransport;
use Thrift\Exception\TException;
use com\penngo\RegisterServiceClient;
use com\penngo\LoginServiceClient; try { $socket = new TSocket('127.0.0.1', 8848);
$socket->setSendTimeout(100000); //设置超时时间
$socket->setRecvTimeout(100000); $transport = new TBufferedTransport($socket, 1024, 1024);
$protocol = new TBinaryProtocol($transport);
// $loginProtocol = new TMultiplexedProtocol($protocol, "LoginService");
$faceProtocol = new TMultiplexedProtocol($protocol, "FaceService");
// $registerProtocol = new TMultiplexedProtocol($protocol, "RegisterService");
$faceClient = new FaceServiceClient($faceProtocol);
// $registerClient = new RegisterServiceClient($registerProtocol);
$transport->open(); $faceinfo = $faceClient->getFace("123","asdasd"); print_r($faceinfo); $transport->close();
} catch (TException $tx) {
print 'TException: '.$tx->getMessage()."\n";
print 'TException: '.$tx->getTraceAsString()."\n";
}
Python 客户端
安装Python 插件
pip install thrift
# -*- coding:utf-8 -*-
import sys
sys.path.append('..') from thrift.TMultiplexedProcessor import TMultiplexedProcessor
from thrift.protocol.TMultiplexedProtocol import TMultiplexedProtocol
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
#根据实际的包结构去引入
import FaceService def client():
transport = TSocket.TSocket(host='localhost', port=8848)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
face_protocol = TMultiplexedProtocol(protocol, "FaceService") #如果服务端使用TMultiplexedProcessor接收处理,客户端必须用TMultiplexedProtocol并且指定serviceName和服务端的一致 face_client = FaceService.Client(face_protocol)#msg客户端 transport.open() print face_client.getFace("","啊实打实多") transport.close() if __name__ == '__main__':
client()
C# 客户端
https://gitee.com/bandung/Allthrift/raw/master/gen-csharp/gen-csharp/bin/Debug/Thrift.dll
dll 下载,然后引用
using Thrift.Protocol;
using Thrift.Server;
using Thrift.Transport; public void TMclient()
{ TSocket transport = new TSocket("127.0.0.1",); TBinaryProtocol protocol = new TBinaryProtocol(transport); TMultiplexedProtocol tprocessor = new TMultiplexedProtocol(protocol, "FaceService"); FaceService.Client faceclient = new FaceService.Client(tprocessor); transport.Open(); String info=faceclient.getFace("PHP","JAVA"); Console.WriteLine(info); transport.Close(); }
结束
!!
Thrift 实现 JAVA,PHP,C#,Python 互相通信的更多相关文章
- 《精通并发与Netty》学习笔记(07 - 基于Thrift实现Java与Python的RPC调用)
上节我们介绍了基于Thrift实现java与java的RPC调用,本节我们基于Thrift实现Java与Python的RPC调用 首先,修改data.thirft文件,将命名空间由java改为py n ...
- Apache Thrift with Java Quickstart(thrift入门及Java实例)
thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发.它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Python, PHP, Ruby, Erlang, Perl ...
- Atitit.http代理的实现 代码java php c# python
Atitit.http代理的实现 代码java php c# python 1. 代理服务器用途 代理服务器看成是一种扩展浏览器功能的途径.例如,在把数据发送给浏览器之前,可以用代理服务器压缩数据 调 ...
- Atitit.异步编程 java .net php python js 对照
Atitit.异步编程 java .net php python js 的比較 1. 1.异步任务,异步模式, APM模式,, EAP模式, TAP 1 1.1. APM模式: Beg ...
- 利用thrift rpc进行C++与Go的通信
一:什么是rpc rpc通俗来理解就是远程调用函数,相对于本地调用来说,只需要在主调函数中调用被掉函数即可,代码如下: void fun(int i) { cout << "fu ...
- Atitit.异步编程 java .net php python js 的比较
Atitit.异步编程 java .net php python js 的比较 1. 1.异步任务,异步模式, APM模式,, EAP模式, TAP 1 1.1. APM模式: Beg ...
- 我看不下去鸟。。。。Java和C#的socket通信真的简单吗?
这几天在博客园上看到好几个写Java和C#的socket通信的帖子.但是都为指出其中关键点. C# socket通信组件有很多,在vs 使用nuget搜索socket组件有很多类似的.本人使用的是自己 ...
- Thrift-java实例
➠更多技术干货请戳:听云博客 Thrift实例1 功能描述:客户端与服务器端分别是两个应用,先启动服务器端,再启动客户端,实现执行客户端运行服务器端的加法方法. 源码截图(源码在附件中): 客户端: ...
- 为什么用 Java:一个 Python 程序员告诉你
这篇文章专门给程序员写的,普通读者慎入.原作者:Kevin Sookocheff 译者:Celia Zhen,原文点击文末链接. 每当我告诉别人我一直在用Java工作时,大家的反应都是: “纳尼!Ja ...
随机推荐
- MySQL 服务正在启动 .MySQL 服务无法启动。系统出错。发生系统错误 1067。进程意外终止。
MySQL 服务正在启动 .MySQL 服务无法启动.系统出错.发生系统错误 1067.进程意外终止. 检查了一个晚上才发现是---配置问题 #Path to installation directo ...
- CentOS 6&7安装ffmpeg
CentOS 6和7安装方法是不一样的,下面分别说明: 安装前都需要先安装epel扩展源 yum -y install epel-release CentOS 6比较简单,安装yum源之后直接安装即可 ...
- USB学习笔记-总结
1. # ls /sys/bus/usb/devices/解析:1-0:1.0 1-1 1-1:1.0 2-0:1.0 2-1 2-1:1.0 2-2 2-2.1 2-2:1.0 2-2.1:1.0 ...
- streamsets record header 属性
record 的header 属性可以在pipeline 逻辑中使用. 有写stages 会为了特殊目录创建reord header 属性,比如(cdc)需要进行crud 操作类型的区分 你可以使用一 ...
- cratedb json 数据导入
基本环境的搭建,可以参考相关文档,或者直接使用docker 安装 docker run -d -p 4200:4200 crate 导出mongodb数据(可选,同时使用工具进行数据类型转换) mon ...
- yarn workspaces基本试用
初始化项目 yarn init -y 添加workspaces 支持 修改package.json { "name": "second", "vers ...
- pthread线程初始化(pthread_once)
pthread_once 语法 int pthread_once(pthread_once_t *once_control, void (*init_routine)(void)); #include ...
- 【转】VC++10(VS2010)IDE各种使用技巧
原文网址:http://www.cnblogs.com/sunrisezhang/articles/2802397.html 一个好的coder,他首先必须是一个熟练工.对于C++程序员来说,只有掌握 ...
- ZedGraph控件的使用 --归类(转帖)
在我们编写程序的时候,有时候是要做一些统计的,为了达到一目了然的效果,饼状图,曲线图,柱状图都是很好的表现统计的直观形式.这个时候,ZedGraph控件给我们带来了极大的方便. 1.下载ZedGrap ...
- SQL中利用脚本创建database mail.
SQL中利用脚本创建database mail 编写人:CC阿爸 2014-6-14 多话不讲,请参考以下脚本 use