dubbo_rpc包中thrift协议的使用过程
来源:互联网 发布:旺旺号是不是淘宝昵称 编辑:程序博客网 时间:2024/05/27 19:27
今天学习的是rpc包中的thrift协议的使用过程,以下是简单的调用步骤,在这里记录一下并做了点简单的说明。
1、首先初始化Protocol类
Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getExtension( ThriftProtocol.NAME );
2、以服务的接口类和URL为参数,调用protocol的refer()方法,返回一个具体协议的Invoker对象,此处以thrift举例,故返回ThriftInvoker对象实例;
public <T> Invoker<T> refer( Class<T> type, URL url ) throws RpcException { ThriftInvoker<T> invoker = new ThriftInvoker<T>(type, url, getClients(url), invokers); invokers.add(invoker); return invoker; }
3、RpcInvocation类继承自Invocation类,主要是对回调方法的一些方法名、参数及类型等一些参数的设置,然后作为以上invoker对象调用invoke方法时的参数,来返回结果。
RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName( "echoString" ); invocation.setParameterTypes( new Class<?>[]{ String.class } ); String arg = "Hello, World!"; invocation.setArguments( new Object[] { arg } ); Result result = invoker.invoke( invocation );
4、invoke方法是在AbstractInvoker抽象类中进行了实现,主要还是对RpcInvocation的一些参数进行设置,具体的回调操作doInvoke(invocation)由具体协议实现
public Result invoke(Invocation inv) throws RpcException { if(destroyed) { throw new RpcException("Rpc invoker for service " + this + " on consumer " + NetUtils.getLocalHost() + " use dubbo version " + Version.getVersion() + " is DESTROYED, can not be invoked any more!"); } RpcInvocation invocation = (RpcInvocation) inv; invocation.setInvoker(this); if (attachment != null && attachment.size() > 0) { invocation.addAttachmentsIfAbsent(attachment); } Map<String, String> context = RpcContext.getContext().getAttachments(); if (context != null) { invocation.addAttachmentsIfAbsent(context); } if (getUrl().getMethodParameter(invocation.getMethodName(), Constants.ASYNC_KEY, false)){ invocation.setAttachment(Constants.ASYNC_KEY, Boolean.TRUE.toString()); } RpcUtils.attachInvocationIdIfAsync(getUrl(), invocation); try { <span style="background-color: rgb(255, 255, 102);">return doInvoke(invocation);//由具体协议实现</span> } catch (InvocationTargetException e) { // biz exception Throwable te = e.getTargetException(); if (te == null) { return new RpcResult(e); } else { if (te instanceof RpcException) { ((RpcException) te).setCode(RpcException.BIZ_EXCEPTION); } return new RpcResult(te); } } catch (RpcException e) { if (e.isBiz()) { return new RpcResult(e); } else { throw e; } } catch (Throwable e) { return new RpcResult(e); } }
5、在doInvoker()中主要是随机选择一个ExchangeClient对象进行异步回调,得到相应的结果。
protected Result doInvoke( Invocation invocation ) throws Throwable { RpcInvocation inv = (RpcInvocation) invocation; final String methodName; methodName = invocation.getMethodName(); inv.setAttachment( Constants.PATH_KEY, getUrl().getPath() ); // for thrift codec inv.setAttachment( ThriftCodec.PARAMETER_CLASS_NAME_GENERATOR, getUrl().getParameter( ThriftCodec.PARAMETER_CLASS_NAME_GENERATOR, DubboClassNameGenerator.NAME ) ); ExchangeClient currentClient; if (clients.length == 1) { currentClient = clients[0]; } else { currentClient = clients[index.getAndIncrement() % clients.length];//随机选择一个Client } try { int timeout = getUrl().getMethodParameter( methodName, Constants.TIMEOUT_KEY,Constants.DEFAULT_TIMEOUT); RpcContext.getContext().setFuture(null); return (Result) currentClient.request(inv, timeout).get();//进行方法异步回调 } catch (TimeoutException e) { throw new RpcException(RpcException.TIMEOUT_EXCEPTION, e.getMessage(), e); } catch (RemotingException e) { throw new RpcException(RpcException.NETWORK_EXCEPTION, e.getMessage(), e); } }
0 0
- dubbo_rpc包中thrift协议的使用过程
- thrift安装过程中遇到的错误
- thrift的编译过程
- thrift使用传输协议TCompactProtocol
- thrift使用过程中的问题
- thrift使用过程中的问题
- thrift使用过程中的问题
- thrift使用过程中的问题
- thrift使用过程中的问题
- thrift的binaryprotocol协议分析
- Thrift协议的服务模型
- Thrift的TJsonProtocol协议分析
- Jmeter中Websocket协议支持包的使用
- Jmeter中Websocket协议支持包的使用
- Jmeter中Websocket协议支持包的使用
- 关于maven使用的过程中jar包找不到咋办
- MO_GLOBAL包中一些过程和函数的使用
- MO_GLOBAL包中一些过程和函数的使用(转)
- 三个枪手问题
- 中转inline hook,不需要恢复首字节的hook
- 报错Syntax error on token "int", Dimensions expected after this token
- 大数据社区研讨会 (第一届)
- 学习C语言第一天
- dubbo_rpc包中thrift协议的使用过程
- IOS 宏定义
- Android WebView网页自适应手机,可放大缩小设置代码
- iOS蓝牙4.0开发流程
- 操作系统学习笔记(3)——进程描述与进程状态变化
- cannot resolve symbol 'XXXX'
- 黑马程序员——java学习日记二
- SystemUi->虚拟按键
- 标准模板库STL(Standard Template Library)