91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

  • 大小: 70KB
    文件類型: .gz
    金幣: 1
    下載: 0 次
    發布日期: 2021-05-24
  • 語言: 其他
  • 標簽: Thrift;??rpc??

資源簡介

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“);

}

}




評論

共有 條評論