一. Thrift简介
Apache Thrift是跨语言服务访问的框架。最早由Facebook 开发,贡献给了Apache。
图中前三部分分别是 通过Thrift 脚本文件生成的代码,根据生成代码创建的服务器客户端代码和两端的结果.
协议:
TBinaryProtocol: 二进制编码格式
TCompactProtocol: 使用Variable-Length Quantity (VLQ) 编码对数据进行压缩
TJSONProtocol: 使用JSON的数据编码协议进行数据传输
TSimpleJSONProtocol – 这种节约只提供JSON只写的协议,适用于通过脚本语言解析
TDebugProtocol – 在开发的过程中帮助开发人员调试用的,以文本的形式展现方便阅读
传输层
TSocket- 使用堵塞式I/O进行传输,也是最常见的模式。
TFramedTransport- 使用非阻塞方式,按块的大进行传输,类似于Java中的NIO。
TFileTransport- 按照文件的方式进程传输,虽然这种方式不提供Java的实现,但是实现起来非常简单。
TMemoryTransport- 使用内存I/O,就好比Java中的ByteArrayOutputStream实现。
TZlibTransport- 使用执行zlib压缩,不提供Java的实现。
服务端类型
TSimpleServer - 单线程服务器端使用标准的堵塞式I/O。
TThreadPoolServer - 多线程服务器端使用标准的堵塞式I/O。
TNonblockingServer – 多线程服务器端使用非堵塞式I/O,并且实现了Java中的NIO通道。
二. Thrift 安装配置
1)sudo apt-get install libboost-dev libboost-test-dev libboost-program-options-dev libboost-system-dev libboost-filesystem-dev libevent-dev automake libtool flex bison pkg-config g++ libssl-dev
2)下载
www.apache.org/dyn/closer.cgi?path=/thrift/0.9.2/thrift-0.9.2.tar.gz
3)解压后进入根目录
./configure
sudo make
sudo make install
hello.thrift
namespace java com.test
service HelloWorldService {
string sayHello(1:string username)
}
bye.thrift
namespace java com.test2
service GoodByeService {
string sayBye(1:string username)
}
thrift -r –gen java hello.thrift
thrift -r –gen java bye.thrift
GoodByeImpl.java
package com.abc;
import com.test2.*;
import org.apache.thrift.TException;
public class GoodByeImpl implements GoodByeService.Iface {
public GoodByeImpl() {
}
@Override
public String sayBye(String username) throws TException {
otherServer client = new otherServer();
String result = client.getClientInfo("Lyn");
return result + "\n Bye-bye, " + username ;
}
}
HelloWorldImpl.java
package com.abc;
import com.test.*;
import org.apache.thrift.TException;
public class HelloWorldImpl implements HelloWorldService.Iface {
public HelloWorldImpl() {
}
@Override
public String sayHello(String username) throws TException {
return "hello, " + username +":"+ HelloServer.SERVER_PORT ;
}
}
HelloServer.java
package com.abc;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;
import com.test.HelloWorldService;
public class HelloServer {
public static final int SERVER_PORT = 9090;
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());
TServer server = new TSimpleServer(tArgs);
server.serve();
} catch (Exception e) {
System.out.println("Server start error!!!");
e.printStackTrace();
}
}
public static void main(String[] args) {
HelloServer server = new HelloServer();
server.startServer();
}
}
otherServer.java
package com.abc;
import javax.print.attribute.standard.MediaSize.Other;
import org.apache.thrift.TException;
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;
import org.apache.thrift.transport.TTransportException;
import com.test.HelloWorldService;
import com.test2.GoodByeService;
public class otherServer {
public static final int PORT2 = 8090;
public static final int SERVER_PORT = 9090;
public static final int TIMEOUT = 30000;
public void startServer() {
try {
System.out.println("Another TSimpleServer start ....");
TProcessor tprocessor = new GoodByeService.Processor<GoodByeService.Iface>(
new GoodByeImpl());
TServerSocket serverTransport = new TServerSocket(PORT2);
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("Another Server start error!!!");
e.printStackTrace();
}
}
public String getClientInfo(String userName) {
TTransport transport = null;
String result = "";
try {
transport = new TSocket("localhost", 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();
result = client.sayHello(userName);
System.out.println("Another thrift client result =: " + result);
} catch (TTransportException e) {
e.printStackTrace();
} catch (TException e) {
e.printStackTrace();
} finally {
if (null != transport) {
transport.close();
}
}
return result;
}
public static void main(String[] args) {
// HelloClient client = new HelloClient();
otherServer server = new otherServer();
// client.startClient("Hello client in other server");
// client2.startClient("other server");
server.startServer();
}
}
HelloClient.java
package com.abc;
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.test2.*;
public class HelloClient {
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);
GoodByeService.Client client = new GoodByeService.Client(protocol);
transport.open();
// String result = client.sayHello(userName);
// System.out.println("Thrift client result =: " + result);
String result = client.sayBye(userName);
System.out.println("Thrift 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) {
HelloClient client = new HelloClient();
client.startClient("Jack");
}
}