1.下載window下thrift的編譯工具exejava
去apache官網(http://archive.apache.org/dist/thrift/)下載一個thrift的編譯工具,我下載的是thrift-0.9.1.exe(最新版是thrift-0.9.1.exe),而後拷貝到E:\thrift下(位置本身隨便放)apache
(能夠把E:\thrift加入到path下面去,重名名thrift-0.9.1.exe爲thrift.exe ,這樣任何目錄就可使用thrift這個命令了,我並無這麼作)windows
2. 編寫thrift文件api
文件名爲:demoHello.thrift 服務器
namespace java com.kedacom.demo.thriftDemo service HelloWorldService { string sayHello(1:string username) }
3. 編譯thrift文件生成java文件eclipse
打開windows下的命令行窗口,進入E:/thrift目錄下面執行以下命令編譯thrift文件maven
thrift-0.9.1.exe --gen java demoHello.thrift函數
能夠看到生成以下文件夾和文件:工具
能夠看到生成了HelloWorldService.java文件了。ui
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端,控制檯輸出以下:
至此客戶端調用服務器的函數成功。