要学习Thrift,它的官网上有资料介绍以及实例,可以到官网:http://thrift.apache.org/上查看。
在官网下载thrift的资源包,安装编译生成相应语言下的jar包,具体在windows下或者ubuntu下的安装参见:
Thrift 学习笔记2——Windows环境下Thrift的安装、编译以及测试
Thrift 学习笔记1——Ubuntu环境下Thrift的安装、编译以及测试
我主要是利用java语言完成Thrift的实例。因为感觉安装起来比较麻烦,所以不想去安装thrift,我直接使用别人已经编译好的jar包,将这些jar包直接添加到我的Myeclipse工程下就可以编写Thrift的项目了。此外,thrift文件自动生成java代码需要使用thrift-0.9.0.exe,以上文件可以到这里去下载:
下面通过几个实例来说明Thrift的使用方法:
1、我们将下载的thrift-0.9.0.exe放置在目录D:\Thrift下
2、编写Hello.thrift文件
service Hello{
string helloString(1:string para)
i32 helloInt(1:i32 para)
bool helloBoolean(1:bool para)
void helloVoid()
string helloNull()
}
这是使用IDL描述性语言编写的Thrift文件,包括了5个方法,每个方法包含一个方法名,参数列表和返回类型。每个参数包括参数序号,参数类型以及参数名。 Thrift 是对 IDL(Interface Definition Language) 描述性语言的一种具体实现。
3、自动生成java代码
将Hello.thrift文件和thrift-0.9.0.exe放置到相同目录下,即D:\Thrift,运行cmd,打开窗口命令行,定位到D:\Thrift
执行命令:
D:\Thrift>thrift-0.9.0.exe -gen java Hello.thrift
此时会在D:\Thrift下生成一个目录gen-java,里面有Hello.java
4、创建Java工程
打开Eclipse或者Myeclipse,创建一个Java工程:Hello,导入刚才生成的Hello.java文件,同时新建一个自由文件夹,Thrift工程需要的jar包以及libthrift-0.9.0.jar放置到文件夹下,同时在Java Build Path中添加引用。
5、编写接口
import org.apache.thrift.TException;
public class HelloServiceImpl implements Hello.Iface{
public boolean helloBoolean(boolean para) throws TException{
return para;
}
public int helloInt(int para) throws TException{
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return para;
}
public String helloNull() throws TException{
return null;
}
public String helloString(String para) throws TException{
return para;
}
public void helloVoid() throws TException{
System.out.println("Hello World!");
}
}
接口实现Thrift定义文件中的服务。
6、编写服务器端
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TBinaryProtocol.Factory;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
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 org.apache.thrift.transport.TTransportException;
public class HelloServiceServer {
/**
* 启动thrift服务器
* @param args
*/
public static void main(String[] args) {
try{
//设置服务器端口为7911
TServerSocket serverTransport = new TServerSocket(7911);
//设置协议工厂为TBinaryProtocol.Factory
Factory proFactory = new TBinaryProtocol.Factory();
//关联处理器与Hello服务的实现
TProcessor processor = new Hello.Processor<Hello.Iface>(new HelloServiceImpl());
TServer.Args tArgs = new TServer.Args(serverTransport);
tArgs.processor(processor);
tArgs.protocolFactory(proFactory);
//使用TSimpleServer
TServer server = new TSimpleServer(tArgs);
System.out.println("Start server on port 7911....");
server.serve();
}catch(TTransportException e){
e.printStackTrace();
}
/*
try{
//设置服务器端口为7911
TServerSocket serverTransport = new TServerSocket(7911);
//设置协议工厂为TBinaryProtocol.Factory
Factory proFactory = new TBinaryProtocol.Factory();
//关联处理器与Hello服务的实现
TProcessor processor = new Hello.Processor<Hello.Iface>(new HelloServiceImpl());
Args tArgs = new Args(serverTransport);
tArgs.processor(processor);
tArgs.protocolFactory(proFactory);
TServer server = new TThreadPoolServer(tArgs);
System.out.println("Start server on port 7911....");
server.serve();
TServerTransport s = new TServerSocket(11);
}catch(TTransportException e){
e.printStackTrace();
}
*/
}
}
7、编写客户端
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;
public class HelloServiceClient {
/**
* 调用Hello服务
* @param args
*/
public static void main(String[] args) {
try {
//设置调用的服务器为本地,端口为7911
TTransport transport = new TSocket("localhost", 7911);
transport.open();
//设置传输协议为TBinaryProtocol
TProtocol protocol = new TBinaryProtocol(transport);
Hello.Client client = new Hello.Client(protocol);
client.helloVoid();
transport.close();
} catch (TTransportException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
8、运行
先运行服务器端,再运行客户端。
得到:
Start server on port 7911....
Hello World!