1下载:http://www.apache.org/dyn/closer.cgi?path=/thrift/0.9.2/thrift-0.9.2.exe
2 创建 :demoHello.thrift
namespace java xdg.luozhonghua.thrift.demo
service HelloWorldService {
string sayHello(1:string username)
}
3:命令: E:\thrift>thrift -r -gen java ./demoHello.thrift
4:结果: gen-java
目录:
[一]、概述
Thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发。它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, and OCaml 等等编程语言间无缝结合的、高效的服务。
Thrift最初由facebook开发,07年四月开放源码,08年5月进入apache孵化器。thrift允许你定义一个简单的定义文件中的数据类型和服务接口。以作为输入文件,编译器生成代码用来方便地生成RPC客户端和服务器通信的无缝跨编程语言。
官网地址:thrift.apache.org
推荐值得一看的文章:
[二]、下载配置
到官网下载最新版本,截止今日(2012-06-11)最新版本为0.8.0.
1. 如果是Maven构建项目的,直接在pom.xml 中添加如下内容:
1
2
3
4
5
|
|
2.如果自己编译lib包,把下载的压缩包解压到X:盘,然后在X:\thrift-0.8.0\lib\java 目录下运行ant进行自动编译,会在X:\thrift-0.8.0\lib\java\build\ 目录下看到编译好的lib包:libthrift-0.8.0.jar
[三]、基本概念
1.数据类型
2.服务端编码基本步骤:
3.客户端编码基本步骤:
4.数据传输协议
tips:客户端和服务端的协议要一致
[四]、实例演示
1. thrift生成代码
创建Thrift文件:G:\test\thrift\demoHello.thrift ,内容如下:
1
2
3
4
5
|
namespace
java
com
.
micmiu
.
thrift
.
demo
service
HelloWorldService
{
string
sayHello
(
1
:
string
username
)
}
|
目录结构如下:
1
2
3
4
5
6
7
8
9
|
G
:
\
test
\
thrift
&
gt
;
tree
/
F
卷
other
的文件夹
PATH
列表
卷序列号为
D238
-
BE47
G
:
.
demoHello
.
thrift
demouser
.
thrift
thrift
-
0.8.0.exe
没有子文件夹
|
thrift-0.8.0.exe 是官网提供的windows下编译工具,运用这个工具生成相关代码:
1
|
thrift
-
0.8.0.exe
-
r
-
gen
java
.
/
demoHello
.
thrift
|
生成后的目录结构如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
G
:
\
test
\
thrift
&
gt
;
tree
/
F
卷
other
的文件夹
PATH
列表
卷序列号为
D238
-
BE47
G
:
.
│
demoHello
.
thrift
│
demouser
.
thrift
│
thrift
-
0.8.0.exe
│
└─
gen
-
java
└─
com
└─
micmiu
└─
thrift
└─
demo
HelloWorldService
.
java
|
将生成的HelloWorldService.java 文件copy到自己测试的工程中,我的工程是用maven构建的,故在pom.xml中增加如下内容:
1
2
3
4
5
6
7
8
9
10
|
|
2. 实现接口Iface
java代码:HelloWorldImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package
com
.
micmiu
.
thrift
.
demo
;
import
org
.
apache
.
thrift
.
TException
;
/**
* blog http://www.micmiu.com
*
* @author Michael
*
*/
public
class
HelloWorldImpl
implements
HelloWorldService
.
Iface
{
public
HelloWorldImpl
(
)
{
}
@
Override
public
String
sayHello
(
String
username
)
throws
TException
{
return
"Hi,"
+
username
+
" welcome to my blog www.micmiu.com"
;
}
}
|
3.TSimpleServer服务端
简单的单线程服务模型,一般用于测试。
编写服务端server代码:HelloServerDemo.java
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
package
com
.
micmiu
.
thrift
.
demo
;
import
org
.
apache
.
thrift
.
TProcessor
;
import
org
.
apache
.
thrift
.
protocol
.
TBinaryProtocol
;
import
org
.
apache
.
thrift
.
protocol
.
TCompactProtocol
;
import
org
.
apache
.
thrift
.
protocol
.
TJSONProtocol
;
import
org
.
apache
.
thrift
.
protocol
.
TSimpleJSONProtocol
;
import
org
.
apache
.
thrift
.
server
.
TServer
;
import
org
.
apache
.
thrift
.
server
.
TSimpleServer
;
import
org
.
apache
.
thrift
.
transport
.
TServerSocket
;
/**
* blog http://www.micmiu.com
*
* @author Michael
*
*/
public
class
HelloServerDemo
{
public
static
final
int
SERVER_PORT
=
8090
;
public
void
startServer
(
)
{
try
{
System
.
out
.
println
(
"HelloWorld TSimpleServer start ...."
)
;
TProcessor
tprocessor
=
new
HelloWorldService
.
Processor
&
lt
;
HelloWorldService
.
Iface
&
gt
;
(
new
HelloWorldImpl
(
)
)
;
// HelloWorldService.Processor<HelloWorldService.Iface> tprocessor =
// new HelloWorldService.Processor<HelloWorldService.Iface>(
// new HelloWorldImpl());
// 简单的单线程服务模型,一般用于测试
TServerSocket
serverTransport
=
new
TServerSocket
(
SERVER_PORT
)
;
TServer
.
Args
tArgs
=
new
TServer
.
Args
(
serverTransport
)
;
tArgs
.
processor
(
tprocessor
)
;
tArgs
.
protocolFactory
(
new
TBinaryProtocol
.
Factory
(
)
)
;
// tArgs.protocolFactory(new TCompactProtocol.Factory());
// tArgs.protocolFactory(new TJSONProtocol.Factory());
TServer
server
=
new
TSimpleServer
(
tArgs
)
;
server
.
serve
(
)
;
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
"Server start error!!!"
)
;
e
.
printStackTrace
(
)
;
}
}
/**
* @param args
*/
public
static
void
main
(
String
[
]
args
)
{
HelloServerDemo
server
=
new
HelloServerDemo
(
)
;
server
.
startServer
(
)
;
}
}
|
编写客户端Client代码:HelloClientDemo.java
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
package
com
.
micmiu
.
thrift
.
demo
;
import
org
.
apache
.
thrift
.
TException
;
import
org
.
apache
.
thrift
.
protocol
.
TBinaryProtocol
;
import
org
.
apache
.
thrift
.
protocol
.
TCompactProtocol
;
import
org
.
apache
.
thrift
.
protocol
.
TJSONProtocol
;
import
org
.
apache
.
thrift
.
protocol
.
TProtocol
;
import
org
.
apache
.
thrift
.
transport
.
TSocket
;
import
org
.
apache
.
thrift
.
transport
.
TTransport
;
import
org
.
apache
.
thrift
.
transport
.
TTransportException
;
/**
* blog http://www.micmiu.com
*
* @author Michael
*
*/
public
class
HelloClientDemo
{
public
static
final
String
SERVER_IP
=
"localhost"
;
public
static
final
int
SERVER_PORT
=
8090
;
public
static
final
int
TIMEOUT
=
30000
;
/**
*
* @param userName
*/
public
void
startClient
(
String
userName
)
{
TTransport
transport
=
null
;
try
{
transport
=
new
TSocket
(
SERVER_IP
,
SERVER_PORT
,
TIMEOUT
)
;
// 协议要和服务端一致
TProtocol
protocol
=
new
TBinaryProtocol
(
transport
)
;
// TProtocol protocol = new TCompactProtocol(transport);
// TProtocol protocol = new TJSONProtocol(transport);
HelloWorldService
.
Client
client
=
new
HelloWorldService
.
Client
(
protocol
)
;
transport
.
open
(
)
;
String
result
=
client
.
sayHello
(
userName
)
;
System
.
out
.
println
(
"Thrify client result =: "
+
result
)
;
}
catch
(
TTransportException
e
)
{
e
.
printStackTrace
(
)
;
}
catch
(
TException
e
)
{
e
.
printStackTrace
(
)
;
}
finally
{
if
(
null
!=
transport
)
{
transport
.
close
(
)
;
}
}
}
/**
* @param args
*/
public
static
void
main
(
String
[
]
args
)
{
HelloClientDemo
client
=
new
HelloClientDemo
(
)
;
client
.
startClient
(
"Michael"
)
;
}
}
|
先运行服务端程序,日志如下:
1
|
HelloWorld
TSimpleServer
start
.
.
.
.
|
再运行客户端调用程序,日志如下:
1
|
Thrify
client
result
=
:
Hi
,
Michael
welcome
to
my
blog
www
.
micmiu
.
com
|
测试成功,和预期的返回信息一致。
4.TThreadPoolServer 服务模型
线程池服务模型,使用标准的阻塞式IO,预先创建一组线程处理请求。
编写服务端代码:HelloServerDemo.java
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
42
43
44
45
46
47
48
49
|
package
com
.
micmiu
.
thrift
.
demo
;
import
org
.
apache
.
thrift
.
TProcessor
;
import
org
.
apache
.
thrift
.
protocol
.
TBinaryProtocol
;
import
org
.
apache
.
thrift
.
server
.
TServer
;
import
org
.
apache
.
thrift
.
server
.
TThreadPoolServer
;
import
org
.
apache
.
thrift
.
transport
.
TServerSocket
;
/**
* blog http://www.micmiu.com
*
* @author Michael
*
*/
public
class
HelloServerDemo
{
public
static
final
int
SERVER_PORT
=
8090
;
public
void
startServer
(
)
{
try
{
System
.
out
.
println
(
"HelloWorld TThreadPoolServer start ...."
)
;
TProcessor
tprocessor
=
new
HelloWorldService
.
Processor
&
lt
;
HelloWorldService
.
Iface
&
gt
;
(
new
HelloWorldImpl
(
)
)
;
TServerSocket
serverTransport
=
new
TServerSocket
(
SERVER_PORT
)
;
TThreadPoolServer
.
Args
ttpsArgs
=
new
TThreadPoolServer
.
Args
(
serverTransport
)
;
ttpsArgs
.
processor
(
tprocessor
)
;
ttpsArgs
.
protocolFactory
(
new
TBinaryProtocol
.
Factory
(
)
)
;
// 线程池服务模型,使用标准的阻塞式IO,预先创建一组线程处理请求。
TServer
server
=
new
TThreadPoolServer
(
ttpsArgs
)
;
server
.
serve
(
)
;
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
"Server start error!!!"
)
;
e
.
printStackTrace
(
)
;
}
}
/**
* @param args
*/
public
static
void
main
(
String
[
]
args
)
{
HelloServerDemo
server
=
new
HelloServerDemo
(
)
;
server
.
startServer
(
)
;
}
}
|
客户端Client代码和之前的一样,只要数据传输的协议一致即可,客户端测试成功,结果如下:
1
|
Thrify
client
result
=
:
Hi
,
Michael
welcome
to
my
blog
www
.
micmiu
.
com
|
5.TNonblockingServer 服务模型
使用非阻塞式IO,服务端和客户端需要指定 TFramedTransport 数据传输的方式。
编写服务端代码:HelloServerDemo.java
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
42
43
44
45
46
47
48
49
50
51
52
|
package
com
.
micmiu
.
thrift
.
demo
;
import
org
.
apache
.
thrift
.
TProcessor
;
import
org
.
apache
.
thrift
.
protocol
.
TCompactProtocol
;
import
org
.
apache
.
thrift
.
server
.
TNonblockingServer
;
import
org
.
apache
.
thrift
.
server
.
TServer
;
import
org
.
apache
.
thrift
.
transport
.
TFramedTransport
;
import
org
.
apache
.
thrift
.
transport
.
TNonblockingServerSocket
;
/**
* blog http://www.micmiu.com
*
* @author Michael
*
*/
public
class
HelloServerDemo
{
public
static
final
int
SERVER_PORT
=
8090
;
public
void
startServer
(
)
{
try
{
System
.
out
.
println
(
"HelloWorld TNonblockingServer start ...."
)
;
TProcessor
tprocessor
=
new
HelloWorldService
.
Processor
&
lt
;
HelloWorldService
.
Iface
&
gt
;
(
new
HelloWorldImpl
(
)
)
;
TNonblockingServerSocket
tnbSocketTransport
=
new
TNonblockingServerSocket
(
SERVER_PORT
)
;
TNonblockingServer
.
Args
tnbArgs
=
new
TNonblockingServer
.
Args
(
tnbSocketTransport
)
;
tnbArgs
.
processor
(
tprocessor
)
;
tnbArgs
.
transportFactory
(
new
TFramedTransport
.
Factory
(
)
)
;
tnbArgs
.
protocolFactory
(
new
TCompactProtocol
.
Factory
(
)
)
;
// 使用非阻塞式IO,服务端和客户端需要指定TFramedTransport数据传输的方式
TServer
server
=
new
TNonblockingServer
(
tnbArgs
)
;
server
.
serve
(
)
;
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
"Server start error!!!"
)
;
e
.
printStackTrace
(
)
;
}
}
/**
* @param args
*/
public
static
void
main
(
String
[
]
args
)
{
HelloServerDemo
server
=
new
HelloServerDemo
(
)
;
server
.
startServer
(
)
;
}
}
|
编写客户端代码:HelloClientDemo.java
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
package
com
.
micmiu
.
thrift
.
demo
;
import
org
.
apache
.
thrift
.
TException
;
import
org
.
apache
.
thrift
.
protocol
.
TCompactProtocol
;
import
org
.
apache
.
thrift
.
protocol
.
TProtocol
;
import
org
.
apache
.
thrift
.
transport
.
TFramedTransport
;
import
org
.
apache
.
thrift
.
transport
.
TSocket
;
import
org
.
apache
.
thrift
.
transport
.
TTransport
;
import
org
.
apache
.
thrift
.
transport
.
TTransportException
;
/**
* blog http://www.micmiu.com
*
* @author Michael
*
*/
public
class
HelloClientDemo
{
public
static
final
String
SERVER_IP
=
"localhost"
;
public
static
final
int
SERVER_PORT
=
8090
;
public
static
final
int
TIMEOUT
=
30000
;
/**
|