Thrift是什么,看这里:http://thrift.apache.org/
1.从官网下载thrift
Thrift官网:http://thrift.apache.org/,Windows 和 Linux请分别下载不同的版本。 在Windows上,将下载的压缩文件解压后,放到一个文件夹下,并为其配置环境变量,以便以后可以直接从命令行启用它。注意:在网上找找,将libthrift-0.9.0.jar和slfj-api-1.7.2.jar一起放入我们项目的lib文件夹下,并添加到项目的buildpath中,因为thrift生成的java文件中,将会引用到这两个包中的类。
2.编写thrift文件
thrift文件是一个文本文件,文件名自定义,后缀也自定义。但是为了方便理解,建议后缀写为:.thrift。一个thrift文件(Person.thrift)像下面这样:
1
2
3
4
5
6
7
8
9
10
11
|
namespace java com.abc.gen
struct Person{
1:i32 id;
2:string name;
3:int age;
4:bool married;
}
service PersonService {
Person getPersonById(1:i32 id) ,
bool deletePersonById(1:i32 id )
}
|
这个文件中,namespace描述了生成的类的包名;struct用于定义一种结构体(在Java中叫做对象),thrift将生成这个Person对象,Person对象中定义了几个基本的属性。service用于定义“服务”,这些“服务”将以接口的方式生成在类中。我们需要做的,就是待thrift为我们生成相应的文件之后,去实现这些接口。
3.用thrift生成自定义的类
使用命令:thrift -r -gen java Person.thrift 来生成我们需要的类。
其中-r表示递归,如果这个Person.thrift文件中又引用了其他thrift文件中定义的对象,那么在生成的时候,将会把引用的对象的thrift文件中定义的所有对象一起生成。
-gen Java 表示生成的是java语言的文件,最后一个参数表示定义thrift对象和服务的文件。
4.实现生成的接口
根据以上的thrift文件,生成之后的文件有Person.java和PersonService.java,其中PersonService.java类中有一个Iface接口,这个接口就是我们需要实现的。我们把这两个生成的Java类复制到我们的项目包中待用。现在,新建一个类PersonServiceImpl,这个类实现了PersonService.Iface。这里以方法getPersonById为例,为了简单起见,直接在PersonServiceImpl的getPersonById中new一个Person对象,设置相关的属性并返回。如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
import
org.apache.thrift.TException;
import
com.abc.gen.Person;
import
com.abc.gen.PersonService;
public
class
PersonServiceImpl
implements
PersonService.Iface {
@Override
public
Person getPersonById(
int
id)
throws
TException {
Person p =
new
Person();
p.setId(id);
p.setAge(
20
);
p.setName(
"name"
);
p.isMarried(
false
);
return
p;
}
@Override
public
boolean
deletePersonById(
int
id)
throws
TException {
// TODO Auto-generated method stub
return
false
;
}
}
|
其中这两个复写的接口,就是我们在Person.thrift中定义的service。
5.编写ThriftServer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
import
org.apache.thrift.server.TServer;
import
org.apache.thrift.server.TThreadPoolServer;
import
org.apache.thrift.server.TThreadPoolServer.Args;
import
org.apache.thrift.transport.TServerSocket;
import
org.apache.thrift.transport.TServerTransport;
import
com.abc.gen.PersonService;
import
com.abc.impl.PersonServiceImpl;
public
class
ThriftServer {
public
static
void
main(String[] args) {
PersonService.Processor processor =
new
PersonService.Processor(
new
PersonServiceImpl());
try
{
TServerTransport serverTransport =
new
TServerSocket(
new
InetSocketAddress(
"127.0.0.1"
,
9999
));
Args trArgs=
new
Args(serverTransport);
trArgs.processor(processor);
TServer server =
new
TThreadPoolServer(trArgs);
System.out.println(
"Thrift服务已开启..."
);
server.serve();
server.stop();
System.out.println(
"Thrift服务已停止..."
);
}
catch
(Exception e){
throw
new
RuntimeException(
"thrift服务启动失败"
+
"\n"
+e.getMessage());
}
}
}
|
6.编写ThriftClient
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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
com.abc.gen.Person;
import
com.abc.gen.PersonService;
public
class
ThriftClient {
public
static
void
main(String[] args)
throws
TException {
TTransport transport =
new
TSocket(
"127.0.0.1"
,
9999
);
long
start=System.currentTimeMillis();
TProtocol protocol =
new
TBinaryProtocol(transport);
PersonService.Client client=
new
PersonService.Client(protocol);
transport.open();
System.out.println(
"调用开始..."
);
Person person = client.getPersonById(
1
);
System.out.println(
"调用结果:"
+ person.toString());
transport.close();
System.out.println(
"消耗时间:"
+ (System.currentTimeMillis()-start) +
"ms"
);
}
}
|
7.调用
先启动ThriftServer,在启动ThriftClient,这时会看到以下内容:
1
2
3
4
|
Thrift服务已开启...
调用开始...
调用结果:Person(id:1, name:name, age:20, married:
false
)
消耗时间:115ms
|