参考:
http://www.ibm.com/developerworks/cn/java/j-lo-apachethrift/
http://blog.csdn.net/m13321169565/article/details/7836006
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.9.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
</dependency>
IDL文件
编写IDL文件 EchoService.thrift
namespace java service.demo
service EchoService {
string echo(1:string para)
}
编译
thrift -o <output directory> -gen java EchoService.thrift
实现接口
public class EchoServiceImpl implements EchoService.Iface {
public String echo(String para) throws TException {
return para;
}
}
服务端
public class HelloServiceServer {
public static void main(String arge[]) {
try {
// 设置服务端口为 7911
TServerSocket serverTransport = new TServerSocket(7911);
// 设置协议工厂为 TBinaryProtocol.Factory
TBinaryProtocol.Factory proFactory = new TBinaryProtocol.Factory();
// 关联处理器与 Hello 服务的实现
TProcessor processor = new HelloService.Processor(new HelloServiceImpl());
TServer server = new TThreadPoolServer(
new TThreadPoolServer.Args(serverTransport)
.protocolFactory(proFactory)
.processor(processor)
);
System.out.println("Start server on port 7911...");
server.serve();
} catch (TTransportException e) {
e.printStackTrace();
}
}
}
客户端
public class HelloServiceClient {
public static void main(String args[]) {
try {
// 设置调用的服务地址为本地,端口为 7911
TTransport transport = new TSocket("localhost", 7911);
transport.open();
// 设置传输协议为 TBinaryProtocol
TProtocol protocol = new TBinaryProtocol(transport);
HelloService.Client client = new HelloService.Client(protocol);
// 调用服务的 helloVoid 方法
System.out.print(client.echo("hello"));
transport.close();
} catch (TTransportException e) {
e.printStackTrace();
} catch (TException e) {
e.printStackTrace();
}
}
}
异步IO
服务端
public static void main(String[] args) {
try {
//传输通道 - 非阻塞方式
TNonblockingServerSocket serverTransport = new TNonblockingServerSocket(7911);
//异步IO,需要使用TFramedTransport,它将分块缓存读取。
TTransportFactory transportFactory = new TFramedTransport.Factory();
//使用高密度二进制协议
TProtocolFactory proFactory = new TCompactProtocol.Factory();
//设置处理器 HelloImpl
TProcessor processor = new Hello.Processor(new HelloImpl());
//创建服务器
TServer server = new TThreadedSelectorServer(
new Args(serverTransport)
.protocolFactory(proFactory)
.transportFactory(transportFactory)
.processor(processor)
);
System.out.println("Start server on port 7911...");
server.serve();
} catch (Exception e) {
e.printStackTrace();
}
}
客户端
/** 调用[非阻塞IO]服务,异步 */
public static void main(String[] args) {
try {
//异步调用管理器
TAsyncClientManager clientManager = new TAsyncClientManager();
//设置传输通道,调用非阻塞IO。
final TNonblockingTransport transport = new TNonblockingSocket("localhost", 7911);
//设置协议
TProtocolFactory protocol = new TCompactProtocol.Factory();
//创建Client
final Hello.AsyncClient client = new Hello.AsyncClient(protocol, clientManager, transport);
// 调用服务
System.out.println("开始:" + System.currentTimeMillis());
client.helloBoolean(false, new AsyncMethodCallback<Hello.AsyncClient.helloBoolean_call>() {
public void onError(Exception exception) {
System.out.println("错误1: " + System.currentTimeMillis());
}
public void onComplete(helloBoolean_call response) {
System.out.println("完成1: " + System.currentTimeMillis());
try {
client.helloBoolean(false, new AsyncMethodCallback<Hello.AsyncClient.helloBoolean_call>() {
public void onError(Exception exception) {
System.out.println("错误2: " + System.currentTimeMillis());
}
public void onComplete(helloBoolean_call response) {
System.out.println("完成2: " + System.currentTimeMillis());
transport.close();
}
});
} catch (TException e) {
e.printStackTrace();
}
}
});
System.out.println("结束:" + System.currentTimeMillis());
Thread.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
}
}
跨语言调用
reference:
http://itindex.net/detail/52661-thrift-%E8%AF%AD%E8%A8%80-%E5%BC%80%E5%8F%91
例如本文中使用c++调用对应的java服务。服务端和客户端的请求调用是通过网络完成的,因此需要保证,请求协议、编码格式以及服务调用的格式都保持一致。
因此针对跨语言的调用,只要生成对应的服务接口,按照不同语言提供的客户端服务接口编写对应的代码。保持协议和编码合适一致即可。
安全协议
使用SSL安全传输协议
服务端
TSSLTransportParameters parameters = new TSSLTransportParameters();
params.setKeyStore("../.keystore", "thrift" , null, null);
TServerTransport serverTransport = TSSLTransportFactory.getServerSocket(9091, 0, null, param);
客户端
TSSLTransportParameters parameters = new TSSLTransportParameters();
params.setKeyStore("../.keystore", "thrift" , "SunX509", "JKS");
TServerTransport serverTransport = TSSLTransportFactory.getServerSocket("localhost", 9091, 0, param);