1.下载window下thrift的编译工具exe
去apache官网(http://archive.apache.org/dist/thrift/)下载一个thrift的编译工具,我下载的是thrift-0.9.1.exe(最新版是thrift-0.9.1.exe),然后拷贝到E:\thrift下(位置自己随便放)
(可以把E:\thrift加入到path下面去,重名名thrift-0.9.1.exe为thrift.exe ,这样任何目录就可以使用thrift这个命令了,我并没有这么做)
2. 编写thrift文件
文件名为:demoHello.thrift
namespace java com.kedacom.demo.thriftDemo service HelloWorldService { string sayHello(1:string username) }
3. 编译thrift文件生成java文件
打开windows下的命令行窗口,进入E:/thrift目录下面执行如下命令编译thrift文件
thrift-0.9.1.exe --gen java demoHello.thrift
可以看到生成如下文件夹和文件:
可以看到生成了HelloWorldService.java文件了。
4. 在eclipse里面新建maven的java项目,引入libthrift-0.9.1.jar库,pom.xml内容如下
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.kedacom.demo</groupId> <artifactId>thriftDemo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>thriftDemo</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.thrift</groupId> <artifactId>libthrift</artifactId> <version>0.9.1</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.2</version> </dependency> </dependencies> </project>
然后将我们刚才生成的HelloWorldService.java拷贝到我们的项目中
最后我们的目录结果如下:
5. 编写接口Iface实现代码
HelloWorldImpl.java:
package com.kedacom.demo.thriftDemo; import org.apache.thrift.TException; public class HelloWorldImpl implements HelloWorldService.Iface { public HelloWorldImpl() { } public String sayHello(String username) throws TException { // TODO Auto-generated method stub return "Hi," + username + " welcome to my blog www.micmiu.com"; } }
6. 编写server代码
HelloServerDemo:
package com.kedacom.demo.thriftDemo; 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.server.TServer; import org.apache.thrift.server.TSimpleServer; import org.apache.thrift.transport.TServerSocket; 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<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(); } } public static void main(String[] args) { HelloServerDemo server = new HelloServerDemo(); server.startServer(); } }
7.编写client代码
HelloClientDemo.java代码
package com.kedacom.demo.thriftDemo; 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 HelloClientDemo { public static final String SERVER_IP = "localhost"; public static final int SERVER_PORT = 8090; public static final int TIMEOUT = 30000; 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(); } } } public static void main(String[] args) { HelloClientDemo client = new HelloClientDemo(); client.startClient("Michael"); } }
8.先运行server端,控制台输出如下:
9再运行client端,控制台输出如下:
至此客户端调用服务器的函数成功。