資源簡介
Thrift RPC客戶端的服務化框架代碼,
代碼片段和文件信息
package?com.lighting.rpc.core.client;
import?java.io.IOException;
import?java.lang.reflect.Constructor;
import?java.lang.reflect.InvocationHandler;
import?java.lang.reflect.InvocationTargetException;
import?java.lang.reflect.Method;
import?java.lang.reflect.Proxy;
import?java.net.ConnectException;
import?java.util.List;
import?org.apache.thrift.protocol.TBinaryProtocol;
import?org.apache.thrift.protocol.TProtocol;
import?org.apache.thrift.transport.TSocket;
import?org.apache.thrift.transport.TTransportException;
import?com.lighting.rpc.core.configure.RpcServerConfiguration;
import?com.lighting.rpc.core.model.ServerNode;
public?class?DynamicClientProxy?implements?InvocationHandler?{
private?Class?ts?=?null;
private?RpcServerConfiguration?configuration?=?null;
public?object?createProxy(Class?ts?RpcServerConfiguration?configuration)?{
this.ts?=?ts;
this.configuration?=?configuration;
return?Proxy.newProxyInstance(ts.getClassLoader()?ts.getInterfaces()?this);
}
@Override
public?object?invoke(object?proxy?Method?method?object[]?args)
throws?Throwable?{
List?serverNodes?=?configuration.getServerNodes();
int?timeout?=?configuration.getTimeout();
for?(?ServerNode?serverNode?:?serverNodes?)?{
String?ip?=?serverNode.getIp();
int?port?=?serverNode.getPort();
TSocket?tsocket?=?null;
try?{
tsocket?=?new?TSocket(ip?port);
tsocket.setTimeout(timeout);
TProtocol?protocol?=?new??TBinaryProtocol(tsocket);
Class[]?argsClass?=?new?Class[]?{
TProtocol.class
};
Constructor?cons?=?(Constructor)?ts.getConstructor(argsClass);
T?client?=?(T)cons.newInstance(protocol);
tsocket.open();
return?method.invoke(client?args);
}?catch?(TTransportException?e)?{
Throwable?te?=?e.getCause();
if?(?te?!=?null?&&?(te?instanceof?ConnectException?||?te?instanceof?IOException)?)?{
//?DO?nothing
}?else?{
throw?e;
}
}?catch?(InvocationTargetException?e)?{
throw?e.getTargetException();
}?catch?(Throwable?e)?{
throw?e;
}?finally?{
if?(?tsocket?!=?null?)?{
tsocket.close();
}
}
}
throw?new?Exception(“Server?is?not?avaiable“);
}
}
- 上一篇:wireshark omci 解析插件
- 下一篇:溫度控制labview上位機程序
評論
共有 條評論