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(); }

结束

!!

最新文章

  1. 简单轮播js实现
  2. 基于HTML5的WebGL设计汉诺塔3D游戏
  3. php递归无限极分类实例
  4. Java 二进制与十六进制转换
  5. BusyBox
  6. jQuery时间轴插件:jQuery Timelinr
  7. dumpbin使用
  8. LeetCode Set Matrix Zeroes(技巧+逻辑)
  9. web-ylbtech-数据库备份-数据库设计
  10. Java Web编程的主要组件技术——Struts核心组件
  11. JDK1.8聚合操作
  12. 重装系统时,将MBR分区转为GPT 分区
  13. 1.1.4-学习Opencv与MFC混合编程之---画图工具 画椭圆
  14. 一个字母引发的血案 java.io.File中mkdir()和mkdirs()
  15. CSS border实现各个方向等腰直角三角
  16. httpd配置ResponseHeader
  17. java1.8--Null Object模式
  18. 解决Nginx出现403 forbidden (13: Permission denied)报错的四种方法
  19. 2018牛客网暑假ACM多校训练赛(第四场)E Skyline 线段树 扫描线
  20. 使用jQuery和CSS3实现一个数字时钟

热门文章

  1. -webkit新属性 image-set和srcset
  2. STL标准库-容器-set与multiset
  3. pixi之动画
  4. Jmeter-线程组执行顺序控制
  5. python的继承顺序
  6. .Net调用Java端带有WS-Security支持的Web Service各方案实战【转】
  7. StreamSets 设计Edge pipeline
  8. cratedb joins 原理(官方文档)
  9. 安装 Android Studio 2.3 详细过程及错误解决
  10. RabbitMQ内存爆出问题解决思路