Dubbo服务消费者调用过程

  • 同步 需要返回值:使用ExchangeClient.request()方法,返回一个ResponseFuture,一直阻塞到服务端返回响应结果;

案例介绍

====

先看一个简单的客户端引用服务的例子,HelloServicedubbo配置如下:

<dubbo:application name=“consumer-of-helloService” />

<dubbo:registry protocol=“zookeeper” address=“127.0.0.1:2181” />

<dubbo:protocol name=“dubbo” port=“20880” />

<dubbo:reference id=“helloService” interface=“com.demo.dubbo.service.HelloService” />

  • 使用Zookeeper作为注册中心

  • 引用远程的HelloService接口服务

根据之前的介绍,在Spring启动的时候,根据<dubbo:reference>配置会创建一个ReferenceBean,该bean又实现了Spring的FactoryBean接口,所以我们如下方式使用时:

@Autowired

private HelloService helloService;

使用的不是ReferenceBean对象,而是ReferenceBean的getObject()方法返回的对象,该对象通过代理实现了HelloService接口,所以要看服务引用的整个过程就需要从ReferenceBean.getObject()方法开始入手。

服务引用过程


将ReferenceConfig.init()中的内容拆成具体的步骤,如下:

第一步:收集配置参数


methods=hello,

timestamp=1443695417847,

dubbo=2.5.3

application=consumer-of-helloService

side=consumer

pid=7748

interface=com.demo.dubbo.service.HelloService

第二步:从注册中心获取服务地址,返回Invoker对象


如果是单个注册中心,代码如下:

Protocol refprotocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();

invoker = refprotocol.refer(interfaceClass, url);

上述url的内容如下:

registry://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?

application=consumer-of-helloService&

dubbo=2.5.6&

pid=8292&

registry=zookeeper&

timestamp=1443707173909&

refer=

application=consumer-of-helloService&

dubbo=2.5.6&

interface=com.demo.dubbo.service.HelloService&

methods=hello&

pid=8292&

side=consumer&

timestamp=1443707173884&

第三步:使用ProxyFactory创建出Invoker的代理对象


ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();

proxyFactory.getProxy(invoker);

下面就详细说明下上述提到的几个概念:Protocol、Invoker、ProxyFactory

概念介绍


Invoker


Invoker是一个可执行对象,有三种类型的Invoker:

  1. 本地执行的Invoker(服务端使用)

  2. 远程通信执行的Invoker(客户端使用)

  3. 多个类型2的Invoker聚合成的集群版Invoker(客户端使用)

Invoker的实现情况如下:

这里写图片描述

先来看服务引用的第2个步骤,返回Invoker对象

对于客户端来说,Invoker应该是2、3这两种类型。先来说第2种类型,即远程通信的Invoker,看DubboInvoker的源码,调用过程AbstractInvoker.invoke()->doInvoke():

protected Result doInvoke(final Invocation invocation) throws Throwable {

RpcInvocation inv = (RpcInvocation) invocation;

final String methodName = RpcUtils.getMethodName(invocation);

inv.setAttachment(Constants.PATH_KEY, getUrl().getPath());

inv.setAttachment(Constants.VERSION_KEY, version);

ExchangeClient currentClient;

if (clients.length == 1) {

currentClient = clients[0];

} else {

currentClient = clients[index.getAndIncrement() % clients.length];

}

try {

boolean isAsync = RpcUtils.isAsync(getUrl(), invocation);

boolean isOneway = RpcUtils.isOneway(getUrl(), invocation);

int timeout = getUrl().getMethodParameter(methodName, Constants.TIMEOUT_KEY,Constants.DEFAULT_TIMEOUT);

if (isOneway) {

boolean isSent = getUrl().getMethodParameter(methodName, Constants.SENT_KEY, false);

currentClient.send(inv, isSent);

RpcContext.getContext().setFuture(null);

return new RpcResult();

} else if (isAsync) {

ResponseFuture future = currentClient.request(inv, timeout) ;

RpcContext.getContext().setFuture(new FutureAdapter(future));

return new RpcResult();

} else {

RpcContext.getContext().setFuture(null);

return (Result) currentClient.request(inv, timeout).get();

}

} catch (TimeoutException e) {

throw new RpcException(RpcException.TIMEOUT_EXCEPTION, "Invoke remote method timeout. method: " + invocation.getMethodName() + ", provider: " + getUrl() + ", cause: " + e.getMessage(), e);

} catch (RemotingException e) {

throw new RpcException(RpcException.NETWORK_EXCEPTION, "Failed to invoke remote method: " + invocation.getMethodName() + ", provider: " + getUrl() + ", cause: " + e.getMessage(), e);

}

}

大致内容就是:

将通过远程通信将Invocation信息传递给服务器端,服务器端接收到该Invocation信息后,找到对应的本地Invoker,然后通过反射执行相应的方法,将方法的返回值再通过远程通信将结果传递给客户端。

这里分3种情况:

  • 执行方法不需要返回值:直接调用ExchangeClient.send()方法。

  • 执行方法的结果需要异步返回:使用ExchangeClient.request()方法返回一个ResponseFuture对象,通过RpcContext中的ThreadLocal使ResponseFuture和当前线程绑定,未等服务端响应结果就直接返回,然后服务端通过ProtocolFilterWrapper.buildInvokerChain()方法会调用Filter.invoke()方法,即FutureFilter.invoker()->asyncCallback(),会获取RpcContextResponseFuture对象,异步返回结果。

  • 执行方法的结果需要同步返回:使用ExchangeClient.request()方法,返回一个ResponseFuture,一直阻塞到服务端返回响应结果

Protocol
使ResponseFuture和当前线程绑定,未等服务端响应结果就直接返回,然后服务端通过ProtocolFilterWrapper.buildInvokerChain()方法会调用Filter.invoke()方法,即FutureFilter.invoker()->asyncCallback(),会获取RpcContextResponseFuture对象,异步返回结果。

  • 执行方法的结果需要同步返回:使用ExchangeClient.request()方法,返回一个ResponseFuture,一直阻塞到服务端返回响应结果

Protocol

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值