转载自:http://blog.csdn.net/u010887744/article/details/50984884
Thrift是什么,有什么优势?这里就不阐述了,百度即可。本文旨在于展现Thrift服务搭建和调用的过程,让初学者少走弯路。本文提供完整代码及所需jar和thrift-0.9.3.exe。点击此处下载完整工程:https://github.com/zxiaofan/OpenSource_Study/tree/master/Thrift
先来张目录结构图
1、下载所需jar和thrift-0.9.3.exe
xxxx.thrift以及thrift文件说明在压缩包中有说明文档和示例
2、解压thrift-0.9.3.exe到本地目录(如:E:\Thrift ),建议全英文目录,并修改名字为thrift.exe(方便敲命令)。
3、CMD定位到E:\Thrift ,执行命令
即可。
4、生成Java文件
接着在命令行输入:
- thrift.exe -r -gen java ./ThriftTest.thrift
,此时会在thrift.exe目录生成gen-java文件夹,里面的就是我们所需的ThriftHelloWorld.java文件了。
5、接下来就是编写服务端了。
①首先实现我们的接口,注意要实现Iface接口哦。
- package service;
-
- import thrift.ThriftHelloWorld.Iface;
-
-
-
-
- public classHelloServiceImpl implements Iface {
- public String sayHello(String username) {
- return "hello " + username;
- }
-
- public String getRandom() {
- return "random";
- }
- }
②在src目录下新建thrift包,将先前生成的ThriftHelloWorld.java拷贝进去;
③新建startServer用于启动服务,
- package service;
-
- import org.apache.thrift.protocol.TBinaryProtocol.Factory;
- 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.TTransportException;
-
- import thrift.ThriftHelloWorld;
- import thrift.ThriftHelloWorld.Processor;
-
-
-
-
-
- public classStartServer {
-
-
-
- public void startServer() {
-
- try {
-
- TServerSocket serverTransport = newTServerSocket(6789);
-
-
- Factory proFactory = newFactory(true, true);
-
-
- ThriftHelloWorld.Processor processor = new Processor(new HelloServiceImpl());
-
-
- Args args = new Args(serverTransport);
- args.processor(processor);
- args.protocolFactory(proFactory);
- TServer server = newTThreadPoolServer(args);
-
-
- server.serve();
- } catch (TTransportException e) {
- e.printStackTrace();
- }
- }
-
- public static void main(String[] args) {
- System.out.println("ServerStart!");
- StartServer server = new StartServer();
- server.startServer();
- }
- }
服务端就此完成,启动main函数,控制台输出Server Start!。
发布的时候只需将此工程导出为可执行jar或将工程稍作修改用tomcat发布。
6、编写客户端,完成服务调用
①同样在src目录下新建thrift包,将先前生成的ThriftHelloWorld.java拷贝进去;
②创建ClientTest类用于调用服务
- package client;
-
- 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 org.junit.Test;
-
- import thrift.ThriftHelloWorld;
-
-
-
-
-
- public classClientTest {
-
-
-
- @Test
- public void startClient() {
- try {
-
-
- TTransport transport = newTSocket("localhost", 6789);
- transport.open();
-
-
-
- TProtocol protocol = new TBinaryProtocol(transport);
- ThriftHelloWorld.Client client =new ThriftHelloWorld.Client(protocol);
-
- System.out.println(client.sayHello("zxiaofan"));
-
- transport.close();
- } catch (TTransportException e) {
- e.printStackTrace();
- } catch (TException e) {
- e.printStackTrace();
- }
- }
- }
启动startClient,控制台输出hello zxiaofan,至此客户端完成。
Thrift所需要的jar包和工具