thrift for java的使用

来源:互联网 发布:软件开发质量管理体系 编辑:程序博客网 时间:2024/05/18 21:07

thrift是一套RPC协议和相关工具,有点像以前百度inf自己写的infpack。
thrift在java语言环境,ubuntu 13.04下使用是这样的。

1.安装依赖库
apt-get install libboost-dev libboost-test-dev libboost-program-options-dev libevent-dev automake libtool flex bison pkg-config g++ libssl-dev
这个命令在ucloud云主机的ubuntu 13.04下会出错,所以改成以下的命令
apt-get build-dep thrift-compiler -y
安装jdk和jre环境

2.通过git安装thrift 0.9.1
git clone -b 0.9.1 https://github.com/apache/thrift thrift-0.9.1

3.编译安装thrift
./bootstrap.sh
./configure
make
make install

4.开始尝试写一个简单的thrift文件add.thrift

namespace java com.luoyan.sample.thrift.server
typedef i32 int
service AdditionService {
int add(1:int n1, 2:int n2),
}

5.写一个消息的实际处理函数AdditionServiceHandler.java

package com.luoyan.sample.thrift.server;
import org.apache.thrift.TException;
public class AdditionServiceHandler implements AdditionService.Iface {
@Override
public int add(int n1, int n2) throws TException {
return n1 + n2;
}
}
6.写一个消息的server端MyServer.java

package com.luoyan.sample.thrift.server;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TServer.Args;
import org.apache.thrift.server.TSimpleServer;
public class MyServer {
public static void StartsimpleServer(AdditionService.Processor<AdditionServiceHandler> processor) {
try {
TServerTransport serverTransport = new TServerSocket(9090);
TServer server = new TSimpleServer(
new Args(serverTransport).processor(processor));
System.out.println(“Starting the simple server …”);
server.serve();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
StartsimpleServer(new AdditionService.Processor<AdditionServiceHandler>(new AdditionServiceHandler()));
}
}

7.写一个消息的客户端AdditionClient.java
package com.luoyan.sample.thrift.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 com.luoyan.sample.thrift.server.AdditionService;

public class AdditionClient {
public static void main(String[] args) {
try {
TTransport transport;
transport = new TSocket(“localhost”, 9090);
transport.open();

TProtocol protocol = new TBinaryProtocol(transport);
AdditionService.Client client = new AdditionService.Client(protocol);

System.out.println(client.add(100, 100));

transport.close();

} catch (TTransportException e) {
e.printStackTrace();
} catch (TException e) {
e.printStackTrace();
}
}
}
包装了参数100和100,用thrift序列化,发给MyServer。在MyServer计算结果,再通过thrift返回给AdditionClient

8.编译方式使用ant,类似于C++的make
build.xml这样写:
<project name=”add” default=”add” basedir=”.”>
<description>Thrift Java add</description>

<property name=”src” location=”src” />
<property name=”gen” location=”gen-java” />
<property name=”build” location=”build” />
<path id=”libs.classpath”>
<fileset dir=”/usr/local/lib”>
<include name=”*.jar” />
</fileset>
<fileset dir=”../../thrift-0.9.1/lib/java/build/lib”>
<include name=”*.jar” />
</fileset>
</path>
<path id=”build.classpath”>
<path refid=”libs.classpath” />
<pathelement path=”${gen}” />
</path>
<path id=”add.classpath”>
<path refid=”build.classpath” />
<pathelement path=”${build}” />
<pathelement path=”add.jar” />
</path>
<target name=”init”>
<tstamp />
<mkdir dir=”${build}”/>
<mkdir dir=”${build}/log”/>
</target>
<target name=”generate”>
<!– Generate the thrift gen-java source –>
<exec executable=”/usr/local/bin/thrift” failonerror=”true”>
<arg line=”–gen java -r add.thrift”/>
</exec>
</target>
<target name=”compile” depends=”init, generate”>
<javac srcdir=”${gen}” destdir=”${build}” classpathref=”libs.classpath” />
<javac srcdir=”${src}” destdir=”${build}” classpathref=”build.classpath” />
</target>
<target name=”add” description=”Run the add” depends=”compile”>
<jar jarfile=”add.jar” basedir=”${build}”/>
<parallel>
<java classname=”com.luoyan.sample.thrift.server.MyServer” fork=”true” timeout=”10000″
classpathref=”add.classpath” failonerror=”false” output=”${build}/log/tutorial.log”>
</java>
<sequential>
<sleep seconds=”2″/>
<echo>add client simple:</echo>
<java classname=”com.luoyan.sample.thrift.client.AdditionClient”
classpathref=”add.classpath” failonerror=”true”>
<arg line=”simple”/>
</java>
<echo>add client secure:</echo>
<java classname=”com.luoyan.sample.thrift.client.AdditionClient”
classpathref=”add.classpath” failonerror=”true”>
<arg line=”secure”/>
</java>
</sequential>
</parallel>
</target>
<target name=”clean”>
<delete dir=”${build}” />
<delete dir=”${gen}”/>
<delete file=”add.jar” />
</target>
</project>

然后调用ant 它会自动处理依赖关系,编译成add.jar
9.server.sh的脚本(其实应该也可以写到build.xml中)
java -classpath add.jar:/usr/local/lib/libthrift-0.9.1.jar:/usr/local/lib/slf4j-api-1.5.8.jar:/usr/local/lib/slf4j-log4j12-1.5.8.jar:/usr/share/ja
va/log4j-1.2.jar com.luoyan.sample.thrift.server.MyServer
client.sh的脚本(其实应该也可以写到build.xml中)
java -classpath add.jar:/usr/local/lib/libthrift-0.9.1.jar:/usr/local/lib/slf4j-api-1.5.8.jar:/usr/local/lib/slf4j-log4j12-1.5.8.jar:/usr/share/java/log4j-1.2.jar com.luoyan.sample.thrift.client.AdditionClient

先启动server.sh再启动client.sh就可以看到client收到200的结果了。

0 0