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 { 定义的是一个结构体,用于同一的放回结果 1: i64 id, 2: string name, 3: string password } service LoginService{ 定义服务,服务先可以有多个方法,方法返回值为上面定义的结果User User login(1:string name, 2:string psw); } service FaceService{ 方法的返回值为 string类型 string getFace(1:string name, 2:string psw); } service RegisterService{ User createUser(1:string name, 2:string psw); }
生成Python 版本的代码
. hrift-0.9.3.exe -gen py . est.thrift //python . hrift-0.9.3.exe -gen csharp. est.thrift //C# . hrift-0.9.3.exe -gen java. est.thrift //java . hrift-0.9.3.exe -gen php. est.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 compenngo; require_once __DIR__.'/../../php/lib/Thrift/ClassLoader/ThriftClassLoader.php'; //按照自己的目录来,不能导入错了 //echo __DIR__.'/../../lib/Thrift/ClassLoader/ThriftClassLoader.php'; use ThriftClassLoaderThriftClassLoader; $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 ThriftProtocolTBinaryProtocol; use ThriftProtocolTMultiplexedProtocol; use ThriftTransportTSocket; use ThriftTransportTHttpClient; use ThriftTransportTBufferedTransport; use ThriftExceptionTException; use compenngoRegisterServiceClient; use compenngoLoginServiceClient; 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()." "; print 'TException: '.$tx->getTraceAsString()." "; }
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("123","啊实打实多") 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",8848); 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(); }
结束
!!