Thrift是一个由Facebook开发,跨语言的RPC框架,使用IDL(接口定义语言)描述。支持多种通信协议:TCompactProtocol(压缩)、TBinaryProtocol(二进制)和TJSONProtocol(json)。
一,需使用接口定义语言描述接口,Test.thrift使用命令生成文件,见下图。由于本人的thrift版本为0.8.0,生成的java代码,有些代码在java1.7有问题,做此修改,将@override去掉,catch到某个异常,直接调用printSatck打印,而非throw new xxxException。
namespace java com.thrift.demo
service HelloWorldService {
string sayHello(1:string name)
}
$thrift -gen java Test.thrift
二、新建一个maven项目,新建包名com.thrift.demo,将生成的java文件拷贝到此下。其中pom.xml文件加入依赖如下
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.8.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
</dependency>
新建客户端,helloClient,代码如下,代码都很简单,都是thrift的模块格式
package com.thrift.demo;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
public class helloClient {
public static final String SERVER_IP = "127.0.0.1";
public static final int SERVER_PORT = 9090;
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);
HelloWorldService.Client client = new HelloWorldService.Client(protocol);
transport.open();
String result = client.sayHello(userName);
System.out.println("thrift remote call : " + result);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != transport) {
transport.close();
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("thrift client main executing...");
helloClient client = new helloClient();
client.startClient("THRIFT");
}
}
将此客户端打包成jar,打包方法见本人另一篇博客
三、新建另一个maven工程,同理客户端,拷贝thrift生成的接口文件,新建包,再新建类服务端,代码如下:
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
public class helloServer {
public static final int SERVER_PORT = 9090;
public void startServer() {
try {
System.out.println("server start ....");
TProcessor tprocessor = new HelloWorldService.Processor(new HelloWorldImpl());
TServerSocket serverTransport = new TServerSocket(SERVER_PORT);
TServer.Args tArgs = new TServer.Args(serverTransport);
tArgs.processor(tprocessor);
tArgs.protocolFactory(new TBinaryProtocol.Factory());
TServer server = new TSimpleServer(tArgs);
server.serve();
} catch (Exception e) {
System.out.println("Server happened error!!!");
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("thrift server executing...");
helloServer server = new helloServer();
server.startServer();
}
}
新建Iface的实现类代码
public class HelloWorldImpl implements HelloWorldService.Iface {
@Override
public String sayHello(String name) {
System.out.println("client name:" + name);
return name;
}
}
将jar上传到linux下,直接使用下述命令启动客户端和服务器端。
$java -jar xxx.jar
参考文献
https://www.cnblogs.com/xxxteam/archive/2013/04/25/3042760.html
https://blog.csdn.net/mn960mn/article/details/50476759