前面七篇文章分析了Thrfit的方方面面,看到这里时应该对Thrift有了深入的理解。
Thrift源码分析(五)-- FrameBuffer类分析
Thrift源码分析(六)-- Transport传输层分析
下面通过一个实际可以运行的例子来跑一跑Thrift,结束这个主题
1. 通过IDL来定义接口: DemoService.thrift
namespace java com.thrift.test
service DemoService{
string sayHi(1:string name);
}
2. 根据IDL自动生成代码
thrift -r --gen java DemoService.thrift
3. 生成的代码供服务器和客户端调用
/**
* Autogenerated by Thrift Compiler (0.8.0)
*
* DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
* @generated
*/
package com.thrift.test;
import org.apache.thrift.scheme.IScheme;
import org.apache.thrift.scheme.SchemeFactory;
import org.apache.thrift.scheme.StandardScheme;
import org.apache.thrift.scheme.TupleScheme;
import org.apache.thrift.protocol.TTupleProtocol;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.EnumMap;
import java.util.Set;
import java.util.HashSet;
import java.util.EnumSet;
import java.util.Collections;
import java.util.BitSet;
import java.nio.ByteBuffer;
import java.util.Arrays;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DemoService {
public interface Iface {
public String sayHi(String name) throws org.apache.thrift.TException;
}
public interface AsyncIface {
public void sayHi(String name, org.apache.thrift.async.AsyncMethodCallback<AsyncClient.sayHi_call> resultHandler) throws org.apache.thrift.TException;
}
public static class Client extends org.apache.thrift.TServiceClient implements Iface {
public static class Factory implements org.apache.thrift.TServiceClientFactory<Client> {
public Factory() {}
public Client getClient(org.apache.thrift.protocol.TProtocol prot) {
return new Client(prot);
}
public Client getClient(org.apache.thrift.protocol.TProtocol iprot, org.apache.thrift.protocol.TProtocol oprot) {
return new Client(iprot, oprot);
}
}
public Client(org.apache.thrift.protocol.TProtocol prot)
{
super(prot, prot);
}
public Client(org.apache.thrift.protocol.TProtocol iprot, org.apache.thrift.protocol.TProtocol oprot) {
super(iprot, oprot);
}
public String sayHi(String name) throws org.apache.thrift.TException
{
send_sayHi(name);
return recv_sayHi();
}
public void send_sayHi(String name) throws org.apache.thrift.TException
{
sayHi_args args = new sayHi_args();
args.setName(name);
sendBase("sayHi", args);
}
public String recv_sayHi() throws org.apache.thrift.TException
{
sayHi_result result = new sayHi_result();
receiveBase(result, "sayHi");
if (result.isSetSuccess()) {
return result.success;
}
throw new org.apache.thrift.TApplicationException(org.apache.thrift.TApplicationException.MISSING_RESULT, "sayHi failed: unknown result");
}
}
public static class AsyncClient extends org.apache.thrift.async.TAsyncClient implements AsyncIface {
public static class Factory implements org.apache.thrift.async.TAsyncClientFactory<AsyncClient> {
private org.apache.thrift.async.TAsyncClientManager clientManager;
private org.apache.thrift.protocol.TProtocolFactory protocolFactory;
public Factory(org.apache.thrift.async.TAsyncClientManager clientManager, org.apache.thrift.protocol.TProtocolFactory protocolFactory) {
this.clientManager = clientManager;
this.protocolFactory = protocolFactory;
}
public AsyncClient getAsyncClient(org.apache.thrift.transport.TNonblockingTransport transport) {
return new AsyncClient(protocolFactory, clientManager, transport);
}
}
public AsyncClient(org.apache.thrift.protocol.TProtocolFactory protocolFactory, org.apache.thrift.async.TAsyncClientManager clientManager, org.apache.thrift.transport.TNonblockingTransport transport) {
super(protocolFactory, clientManager, transport);
}
public void sayHi(String name, org.apache.thrift.async.AsyncMethodCallback<sayHi_call> resultHandler) throws org.apache.thrift.TException {
checkReady();
sayHi_call method_call = new sayHi_call(name, resultHandler, this, ___protocolFactory, ___transport);
this.___currentMethod = method_call;
___manager.call(method_call);
}
public static class sayHi_call extends org.apache.thrift.async.TAsyncMethodCall {
private String name;
public sayHi_call(String name, org.apache.thrift.async.AsyncMethodCallback<sayHi_call> resultHandler, org.apache.thrift.async.TAsyncClient client, org.apache.thrift.protocol.TProtocolFactory protocolFactory, org.apache.thrift.transport.TNonblockingTransport transport) throws org.apache.thrift.TException {
super(client, protocolFactory, transport, resultHandler, false);
this.name = name;
}
public void write_args(org.apache.thrift.protocol.TProtocol prot) throws org.apache.thrift.TException {
prot.writeMessageBegin(new org.apache.thrift.protocol.TMessage("sayHi", org.apache.thrift.protocol.TMessageType.CALL, 0));
sayHi_args args = new sayHi_args();
args.setName(name);
args.write(prot);
prot.writeMessageEnd();
}
public String