转载自 https://www.cnblogs.com/newboys/p/9366762.html
什么是thrift?
简单来说,是Facebook公布的一款开源跨语言的RPC框架.
那么问题来了.
什么是RPC框架?
RPC全称为Remote Procedure Call,意为远程过程调用.
假设有两台服务器A,B.A服务器上部署着一个应用a,B服务器上部署着一个应用b,现在a希望能够调用b应用的某个函数(方法),但是二者不在同一个进程内,不能直接调用,就需要通过网络传输,在AB服务器之间建一条网络传输通道,a把参数传过去,b接收到参数调用自己的方法,得到结果,再通过网络传回给a,简单讲就是A通过网络来调用B的过程.这个过程要涉及的东西很多,比如多线程,Socket,序列化反序列化,网络I/O,很复杂,于是牛掰的程序员把这些封装起来做成一套框架,供大家使用,就是RPC框架.
thrift的跨语言特型
thrift通过一个中间语言IDL(接口定义语言)来定义RPC的数据类型和接口,这些内容写在以.thrift结尾的文件中,然后通过特殊的编译器来生成不同语言的代码,以满足不同需要的开发者,比如java开发者,就可以生成java代码,c++开发者可以生成c++代码,生成的代码中不但包含目标语言的接口定义,方法,数据类型,还包含有RPC协议层和传输层的实现代码.
thrift的协议栈结构

thrift是一种c/s的架构体系.在最上层是用户自行实现的业务逻辑代码.第二层是由thrift编译器自动生成的代码,主要用于结构化数据的解析,发送和接收。TServer主要任务是高效的接受客户端请求,并将请求转发给Processor处理。Processor负责对客户端的请求做出响应,包括RPC请求转发,调用参数解析和用户逻辑调用,返回值写回等处理。从TProtocol以下部分是thirft的传输协议和底层I/O通信。TProtocol是用于数据类型解析的,将结构化数据转化为字节流给TTransport进行传输。TTransport是与底层数据传输密切相关的传输层,负责以字节流方式接收和发送消息体,不关注是什么数据类型。底层IO负责实际的数据传输,包括socket、文件和压缩数据流等。
1 首先,在官网下载安装包http://thrift.apache.org/download , windows下下载Thrift compiler for Windows (thrift-0.11.0.exe),放到某个目录下,修改名称为 thrift.exe 。
2 添加thrift.exe 目录路径到path 环境变量
3 使用。
3.1 定义thrift的接口文件,文件名称为Apple.thrift
1
2
3
4
5
6
|
namespace java service.demo
service Apple{
string appleString( 1 :string para);
i32 appleAdd( 1 :i32 para);
i32 appleMult( 2 :i32 para1,i32 para2);
}
|
3.2 编译Apple.thrift文件,生成接口文件,
命令: thrift -r -gen java Apple.thrift,会生成一个Apple.java 文件,把文件放入项目中,
3.3 新建mavne 项目,

<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.10.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>

3.4 新建接口实现类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
package service.demo;
import org.apache.thrift.TException;
public class AppleServiceImpl implements Apple.Iface{
@Override
public String appleString(String para) throws TException {
return "apple print hello " + para;
}
@Override
public int appleAdd( int para) throws TException {
// TODO Auto-generated method stub
return para+ 10 ;
}
@Override
public int appleMult( int para1, int para2) throws TException {
// TODO Auto-generated method stub
return para1-para2;
}
}
|
3.5 服务端
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package service.demo;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TTransportException;
public class AppleServiceServer {
public static void main(String[] args) throws TTransportException {
System.out.println( "apple 服务端开启。。" );
TProcessor tprocessor = new Apple.Processor<Apple.Iface>( new AppleServiceImpl());
TServerSocket serverTransport = new TServerSocket( 9000 );
TServer.Args tArgs = new TServer.Args(serverTransport);
tArgs.processor(tprocessor);
tArgs.protocolFactory( new TBinaryProtocol.Factory());
TServer server = new TSimpleServer(tArgs);
server.serve();
}
}
|
3.6 客户端
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
package service.demo;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import service.demo.Apple.Client;
public class AppleServiceClient {
public static void main(String[] args) {
System.out.println( "客户端开始。。" );
TTransport transport = null ;
try {
transport = new TSocket( "localhost" , 9000 , 3000 );
TProtocol protocol = new TBinaryProtocol(transport);
Client client = new Apple.Client(protocol);
transport.open();
String result = client.appleString( "abc" );
System.out.println( "服务端返回 。 " + result);
int a = client.appleAdd( 8 );
int b = client.appleMult( 29 , 3 );
System.out.println( "a= " + a + " b=" +b);
} catch (TTransportException e){
e.printStackTrace();
} catch (TException e){
e.printStackTrace();
} finally {
if ( null !=transport){
transport.close();
}
}
}
}
|
3.7 整个项目
3.8 启动服务端和客户端代码,
执行结果
客户端开始。。
Received 1
服务端返回 。 apple print hello abc
Received 2
Received 3
a= 18 b=26