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();
}
public static void main(String args[]) {

Server srv = new Server();

srv.TMstart();
 
}
 

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 互相通信的更多相关文章

  1. 《精通并发与Netty》学习笔记(07 - 基于Thrift实现Java与Python的RPC调用)

    上节我们介绍了基于Thrift实现java与java的RPC调用,本节我们基于Thrift实现Java与Python的RPC调用 首先,修改data.thirft文件,将命名空间由java改为py n ...

  2. Apache Thrift with Java Quickstart(thrift入门及Java实例)

    thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发.它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Python, PHP, Ruby, Erlang, Perl ...

  3. Atitit.http代理的实现 代码java php c# python

    Atitit.http代理的实现 代码java php c# python 1. 代理服务器用途 代理服务器看成是一种扩展浏览器功能的途径.例如,在把数据发送给浏览器之前,可以用代理服务器压缩数据 调 ...

  4. Atitit.异步编程 java .net php python js 对照

    Atitit.异步编程 java .net php python js 的比較 1. 1.异步任务,异步模式,  APM模式,,  EAP模式, TAP 1 1.1.       APM模式: Beg ...

  5. 利用thrift rpc进行C++与Go的通信

    一:什么是rpc rpc通俗来理解就是远程调用函数,相对于本地调用来说,只需要在主调函数中调用被掉函数即可,代码如下: void fun(int i) { cout << "fu ...

  6. Atitit.异步编程 java .net php python js 的比较

    Atitit.异步编程 java .net php python js 的比较 1. 1.异步任务,异步模式,  APM模式,,  EAP模式, TAP 1 1.1.       APM模式: Beg ...

  7. 我看不下去鸟。。。。Java和C#的socket通信真的简单吗?

    这几天在博客园上看到好几个写Java和C#的socket通信的帖子.但是都为指出其中关键点. C# socket通信组件有很多,在vs 使用nuget搜索socket组件有很多类似的.本人使用的是自己 ...

  8. Thrift-java实例

    ➠更多技术干货请戳:听云博客 Thrift实例1 功能描述:客户端与服务器端分别是两个应用,先启动服务器端,再启动客户端,实现执行客户端运行服务器端的加法方法. 源码截图(源码在附件中): 客户端: ...

  9. 为什么用 Java:一个 Python 程序员告诉你

    这篇文章专门给程序员写的,普通读者慎入.原作者:Kevin Sookocheff 译者:Celia Zhen,原文点击文末链接. 每当我告诉别人我一直在用Java工作时,大家的反应都是: “纳尼!Ja ...

随机推荐

  1. 严重: Dispatcher initialization failed java.lang.RuntimeException: java.lang.reflect.InvocationT

    严重: Dispatcher initialization failed java.lang.RuntimeException: java.lang.reflect.InvocationT (2012 ...

  2. win32程序显示网页

    说是win32,实际程序中使用了atl的东西.所以新建win32程序时,记得勾选使用atl: 代码如下: #include "stdafx.h" #include "Wi ...

  3. 折半查找(C语言)

    一.二分查找 在C和C++里,二分查找是针对有序数组所用的一种快速查找元素的方法. 二.二分查找的条件以及优缺点 条件:针对有序数组(元素从小到大或从大到小) 优点:查询速度较快,时间复杂度为O(n) ...

  4. HDU4261 Estimation

    题意 Estimation Time Limit: 40000/15000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  5. QLoo graphql engine 学习三 架构

    一张官方的参考图 说明 Storage Layer API 参考了kubernetes 的设计 qloo 组成 qloo 有qloo 服务以及envoy proxy 组合而成,envoy proxy ...

  6. timescaledb 几个方便的api

    timescaledb 提供了内置的api 操作,方便我们进行操作控制 hypertable 控制api add_dimension 向hypertable添加一个额外的分片方式,可以做为分片列有时间 ...

  7. FastAdmin 前台会员分组的权限分析

    FastAdmin 前台会员分组的权限分析 之前有用户反馈前台用户组权限无法选中无子节点的节点 1. 其实这个理解是不对的,是无法选中菜单可视的节点. 从这里 UserRule.php 可能可以看出. ...

  8. Java多线程编程核心技术,第三章

    1,notify的同步块完了,才会运行wait的同步块 2,interrupt()不是静态方法,用在wait的线程上会有InteruptException,锁也会被释放 3,notify()唤醒的线程 ...

  9. js中caller和callee属性详解

    一.caller属性返回一个对函数的引用,该函数调用了当前函数.functionName.callerfunctionName 对象是所执行函数的名称.说明对于函数来说,caller 属性只有在函数执 ...

  10. ZedGraph右键菜单怎样禁止它弹出(转)

    private void ZGC_ContextMenuBuilder( ZedGraphControl sender,                     ContextMenuStrip me ...