安装Apache Thrift
ubuntu linux运行:
#!/bin/bash #下载 wget http://mirrors.cnnic.cn/apache/thrift/0.9.1/thrift-0.9.1.tar.gz tar zxvf thrift-0.9.1.tar.gz cd thrift-0.9.1.tar.gz ./configure make make install #编译java依赖包 cd lib/java ant
安装ubuntu依赖
sudo apt-get install libboost-dev libboost-test-dev libboost-program-options-dev libevent-dev automake libtool flex bison pkg-config g++ libssl-dev
编写thrift文件hello.thrift
namespace java com.micmiu.thrift.demo service HelloWorldService { string sayHello(1:string username) }
thrift -gen java hello.thrift
将在同级目录下生成gen-java/com/micmiu/thrift/demo/HelloWorldService.java文件
使用maven构建
pom.xml文件
<?xml version="1.0" encoding="UTF-8"?> <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.micmiu.thrift.demo</groupId> <artifactId>thrift-test</artifactId> <version>0.1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.apache.thrift</groupId> <artifactId>libthrift</artifactId> <version>0.9.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.5.8</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.2-beta-5</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.6</source> <target>1.6</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project>
HelloClientDemo.java
package com.micmiu.thrift.demo; import org.apache.thrift.TException; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.protocol.TCompactProtocol; import org.apache.thrift.protocol.TJSONProtocol; import org.apache.thrift.protocol.TProtocol; import org.apache.thrift.transport.TSocket; import org.apache.thrift.transport.TTransport; import org.apache.thrift.transport.TTransportException; /** * blog http://www.micmiu.com * * @author Michael * */ public class HelloClientDemo { public static final String SERVER_IP = "localhost"; public static final int SERVER_PORT = 8090; public static final int TIMEOUT = 30000; /** * * @param userName */ 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(); } } } /** * @param args */ public static void main(String[] args) { HelloClientDemo client = new HelloClientDemo(); client.startClient("Michael"); } }HelloServerDemo.java
package com.micmiu.thrift.demo; 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.protocol.TSimpleJSONProtocol; import org.apache.thrift.server.TServer; import org.apache.thrift.server.TSimpleServer; import org.apache.thrift.transport.TServerSocket; /** * blog http://www.micmiu.com * * @author Michael * */ 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()); // HelloWorldService.Processor<HelloWorldService.Iface> 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(); } } /** * @param args */ public static void main(String[] args) { HelloServerDemo server = new HelloServerDemo(); server.startServer(); } }
HelloClientDemo.java
HelloServerDemo.java
HelloWorldImpl.java
HelloWorldService.java
其中HelloWorldService.java文件是用上文的thrift命令生成的。
执行测试程序
使用maven编译
mvn package assembly:assembly
运行
在target目录下,运行时需要指定main class
java -cp thrift-test-0.1.0-SNAPSHOT-jar-with-dependencies.jar com.micmiu.thrift.demo.HelloServerDemo
java -cp thrift-test-0.1.0-SNAPSHOT-jar-with-dependencies.jar com.micmiu.thrift.demo.HelloClientDemo
参考
本文中的代码来自:http://www.micmiu.com/soa/rpc/thrift-sample/
IBM Developer有篇写Apache Thrift-可伸缩的跨语言服务开发框架:http://www.ibm.com/developerworks/cn/java/j-lo-apachethrift/
百度文库中thrift下java客户端服务器开发指南:http://wenku.baidu.com/view/f77bc1a6f524ccbff1218461.html?qq-pf-to=pcqq.c2c
Apache thrift官方文档:http://thrift.apache.org/docs/