资源简介
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上位机程序
相关资源
- ICSharpCode.SharpZipLib改过后的dll 不乱码
- 在安装Exchange2003服务器时Windows Serve
- Netty4.1实战-手写RPC框架.pdf
- RPCdemo
- 自己动手写基于动态代理,使用ZooK
- gRPC+官方文档中文版_1.0
- ICSharpCode.SharpZipLib.dll下载55485
- 实现了异常管理流程QtSharpCore.zip
- RPC.rar
- libtirpc-devel-0.2.4-0.6.el7.x86_64
- 不同矿物掺合料对RPC抗氯离子渗透性
- ICSharpCode.SharpZipLib.dll(版本号0.86.0.
- 服务治理中间件dubbo原理解析
- ICSharpCode.SharpZipLib
- 基于netty的手写rpc框架
- libtirpc-devel-0.2.4-0.10.el7.x86_64
- ICSharpCode.SharpZipLib.dll 多文件打包压缩
- icsharpcode.sharpziplib.dll各版本包含 0.8
- ICSharpCode.SharpZipLib .net 4.0
- ICSharpCode.SharpZipLib.dll
- 深入浅出gRPC-李林峰.zip
- PowerPC 的 MMU 和 TLB 机制
- AltiumDesignerPCB封装库
- unity读取excel文件的dll Excel.dll ICSha
- ARM、PowerPC、MIPS反汇编工具V1.23.03
- 深入浅出gRPC-李林峰 文档
- SharpCompressDll0.24.zip
- rpcbind如果linux没有portmap或rpcbind服务的
- RPC ,RPC
- PowerPC Architecture Specification -- Book III
评论
共有 条评论