多个线程会调用同一个serviceProxy的方法,一个serviceProxy由一个ThriftClientProxy产生,一个serviceProxy关联一个MTThriftMethodInterceptor, 一个MTThriftMethodInterceptor关联一个cluster。
1.ThriftClientProxy(Thrift客户端代理):
1)生成“serviceProxy(服务代理实例,当然对这个serviceProxy的方法调用都委托给了MTThriftMethodInterceptor)
2)创建ICluster实例(负责获取和保存servers信息)
2. ICluster定义:
public interface ICluster{
public List<ServerConn> getServerConnList();
public List<DegradeAction> getDegradeActions();
public void destroy();
public boolean isAsync();
public void updateServerConn(ServerConn serverConn);
}
3. ServerConn
public class ServerConn implements Serializable{
private Server server;
private GenericObjectPool.Config connPoolConf;
private ObjectPool objectPoll;
pirvate IChannelPool channelPool;
// 以下是些统计信息
private int _connectSuccess = 0;
private int _connectFailed = 0;
private long _invokeSuccessMills = 0;
private int _invokeSuccessNums = 0;
private int _invokeTimeoutNums = 0;
......
}
4. Server:
public class Server implements Serializable{private String ip;
private int port;
private double weight;
private String serverAppKey;
private Date startTime = new Date();
private float floating = 1.0f;
private int status;
private boolean unifiedProto = false;
private int socketNullNum = 0;
private ServerFeedback lastFeedback;
private String version;
private boolean nettyIOSupported;
private byte heartbeatSupport = 0;
......
}
5. DynamicAgentCluster
public class DynamicAgentCluster extends BaseCluster implements ICluster{
private volatile ConcurrentMap<Server, ServerConn> serversMap = new ConcurrentHashMap<Server, ServerConn>();
private int timeOut;
private MTThriftPoolConfig poolConfig;
private int initMinIdle;
private volatile List<ServerConn> serverConns = new ArrayList<ServerConn>();
prvate volatile List<ServerConn> serverConnsSameIDC = new ArrayList<ServerConn>();
private volatile List<ServerConn> serverConnsSameRegion = new ArrayList<ServerConn>();
private volatile List<ServerConn> serverConnsDiffRegion = new ArrayList<ServerConn>();
private volatile List<SGService> sgServiceList;
private volatile List<DegradeAction> degradeActions = new ArrayList<DegradeAction>();
private String localConfig;
private volatile Map<String, SGService> ipHostSGServiceMap = new HashMap();
private boolean serverDynamicWeight;
private Map<String, String> ipport2conf = new ConcurrentHashMap();
private MnsInvoker mnsInvoker;
private String appKey;
private String remoteAppKey;
private int remoteServerPort;
private boolean filterByServiceName =false;
private boolean isNettyIO;
private volatile boolean closed;
private boolean getServersWithoutRegion = false;
......
关注下构造函数:
}
6. DirectlyCluster
7. TTransport
传输数据, 可以往TTransport中写入数据,也可以从中读取数据。
public abstract class TTransport{
// 构造函数,略
public abstract boolean isOpen();
public abstract void open() throws TTransportException;
public abstract void close();
public abstract int read(byte[] var1, int var2, int var3) throws TTransportException;
public abstract void write(byte[] var1, int var2, int var3) throws TTransportException;
......
}
8.TIOStreamTransport
9. TSocket
10. TProtocol
写入或者读取数据的格式。
public abstract class TProtocol {
protected TTransport trans_;
public abstract void writeMessageBegin(TMessage msg) throws TException;
public abstract void writeMessageEnd() throws TException;
public abstract void writeStructBegin(TStruct struct) throws TException;
public abstract void writeStructEnd() throws TExcetion;
public abstract void writeFieldBegin(TField field) throws TException;
public abstract void writeFieldEnd() throws TException;
public abstract void writeFieldStop() throws TException;
public abstract void writeMapBegin(TMap map) thorws TException;
public abstract void writeMapEnd() throws TException;
public abstract void writeListBegin(TList list) throws TException;
public abstract void writeListEnd() throws TException;
public abstract void writeSetBegin(TSet tset) throws TException;
public abstract void writeSetEnd() thrwos TExcepiton;
public abstract void writeBool(boolean b) throws TException;
public abstract void writeByte(byte b) throws TException;
public abstract void writeI16(short s) throws TException;
public abstract void writeI32(int i) throws TException;
public abstract void writeI64(long l) throws TException;
public abstract void writeDouble(double d) throws TException;
public abstract void writeString(String s) throws TException;
public abstract void writeBinary(ByteBuffer b) throws TException;
// 以下是read方法
public abstract TMessage readMessageBegin() throws TException;
public abstract void readMessageEnd() throws TException;
......
}
11. TBinaryProtocol
public class TBinaryProtocol extends TProtocol {
12. ThriftCodecManager
codec:编解码器
13. ThriftCodec
public interface ThriftCodec<T>{
public ThriftType getType();
public T read(TProtocol protocol) throws Exception;
public void write(T value, TProtocol protocol) throws Exception;
}
13. ThriftCatalog
public class ThriftCatalog {
private final MetadataErrors.Monitor monitor;
private final ConcurrentMap<Type, ThriftStructMetadata> structs = new ConcurrentHashMap<>();
private final ConcurrentMap<Class<?>, ThriftEnumMetadata<?>> enmums = new ConcurrentHashMap<>();
.....
1. ThriftServiceMetadata(服务元数据):
服务的名称,服务由哪些方法组成,父服务都有哪些
private final String name ; // 服务的名称
private final Map<String, ThriftMethodMetadata> methods; // 服务所包含的方法,key是方法名,value是方法元数据
private final Map<String, ThriftMethodMetadata> declaredMethods; //
private final ImmutableList<ThriftServiceMetadate> parentServices; //
private final ImmutableList<String> documentation; //
2.ThriftMehtodMetadata(方法元数据):
private final String name; // 方法的名称
private final String qualifiedName; // 方法限定名
private final ThriftType returnType; // 方法返回值类型
private final List<ThriftFieldMetadata> parameters; // 方法入参列表
private final Method method; // 方法
private final ImmutableMap<Short, ThriftType> exceptions; // 方法抛出的异常
private final ImmutableList<String> documentation;
private final boolean oneway;
3. ThriftType(类型)
private final ThriftProtocolType protocolType;
private final Type javaType;
private final ThriftType keyType;
private final ThriftType valueType;
private final ThriftStructMetadata structMetadata;
private final ThriftEnumMetadata<?> enumMetadata;
private final ThriftType uncoercedType;
public static final ThriftType BOOL = new ThriftType(ThriftProtocolType.BOOL, boolean.class); public static final ThriftType BYTE = new ThriftType(ThriftProtocolType.BYTE, byte.class); public static final ThriftType DOUBLE = new ThriftType(ThriftProtocolType.DOUBLE, double.class); public static final ThriftType I16 = new ThriftType(ThriftProtocolType.I16, short.class); public static final ThriftType I32 = new ThriftType(ThriftProtocolType.I32, int.class); public static final ThriftType I64 = new ThriftType(ThriftProtocolType.I64, long.class); public static final ThriftType STRING = new ThriftType(ThriftProtocolType.STRING, String.class); public static final ThriftType BINARY = new ThriftType(ThriftProtocolType.BINARY, ByteBuffer.class); public static final ThriftType VOID = new ThriftType(ThriftProtocolType.STRUCT, void.class);
另外还有 struct, map, set ,list, array, enum, 全了吧
4. ThriftProtocolType(类型)
UNKNOWN((byte) 0), BOOL((byte) 2), BYTE((byte) 3), DOUBLE((byte) 4), I16((byte) 6), I32((byte) 8), I64((byte) 10), STRING((byte) 11), STRUCT((byte) 12), MAP((byte) 13), SET((byte) 14), LIST((byte) 15), ENUM((byte) 8), // same as I32 type BINARY((byte) 11); // same as STRING type
5.ThriftFieldMetadata(字段元数据):
private final short id;
private final ThriftType thriftType;
private final String name;
private final FieldKind fieldKind;
private final List<ThriftInjection> injections; //
private final Optional<ThriftConstructorInjection> constructorInjection;
private final Optional<ThriftMethodInjection> methodInjection;
private final Optional<ThriftExtraction> extraction;
private final Optional<TypeCoercion> coercion;
private final ImmutableList<String> documentation;
private final Requiredness requiredness;
6. FieldKind
public enum FieldKind { THRIFT_FIELD, THRIFT_UNION_ID; }
7. ThriftInjection
public interface ThriftInjection { short getId(); String getName(); FieldKind getFieldKind(); }
8. ThriftStructMetadata
public class ThriftStructMetadata{
public static enum MetadataType{
STRUCT, UNION;
}
private final String structName;
private final MetadataType metadataType;
private final Optional<ThriftMethodInjection> builderMethod;
private final ImmutableList<String> documentation;
private final SortedMap<Short, ThriftFieldMetadata> fields;
private final Optional<ThriftConstructorInjection> constructorInjection;
private final List<ThriftMethodInjection> methodInjections;
private final Type structType;
private final Type builderType;
9. ThriftMethodInjection
10. ThriftConstructorInjection