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 ...
随机推荐
- istringstream、ostringstream、stringstream类介绍
本文转载自: http://www.cnblogs.com/gamesky/archive/2013/01/09/2852356.html 一.C++的输入输出分为三种: (1)基于控制台的I/O ( ...
- L194
For many of us, asking for help is it difficult concept. We may feel as if we are admitting a weakne ...
- react native遇到的坑
1.模拟器报错no bundle url present https://github.com/facebook/react-native/issues/12754 http://www.cnblog ...
- axios请求requestBody和formData
前言 在vue的后台管理开发中,应需求,需要对信息做一个校验,需要将参数传递两份过去,一份防止在body中,一份防止在formdata中,axios请求会默认将参数放在formdata中进行发送. 对 ...
- Linux 中同名进程的查杀
长久一段时间没有做任何工作总结了,如果用工作忙来敷衍那是欺骗自己,承认这一段时间拒绝进步了. 在系统运维中,有许多同名进程需要kill是常有的事情, 数一下battle这个进程的数量 [root@HD ...
- 离散数学:用C语言来判断集合存在的二元关系
用C语言来判断是否满足自反,反自反,非自反,对称,反对称,非对称和传递性 也不知道写的对不对.没有大量验证,但是随便找的一些关系测试的没毛病,如果错了,欢迎各位大佬留言 #include<bit ...
- 《DSP using MATLAB》示例 Example 9.11
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- 微信后端服务架构及其过载控制系统DAGOR
微信架构介绍 眼下的微信后端包含3000多个移动服务,包括即时消息.社交网络.移动支付和第三方授权.该平台每天收到的外部请求在10 ^10个至10^11个.每个这样的请求都会触发多得多的内部微服务 ...
- Android自动化测试-UiAutomator2环境搭建
Android自动化测试-UiAutomator环境搭建(QQ交流群:490451176) 一.环境准备 1. 安装android sdk,并配置环境变量 2. 安装android studio,国内 ...
- GNU Radio: USRP2 and N2x0 Series
Comparative features list 相对性能清单 Hardware Capabilities: 1 transceiver card slot External PPS referen ...