一般情况,consumer发送请求时,创建一个DefaultFuture对象,然后阻塞并等待响应。DefaultFuture类,封装了请求和响应:

// 省略其他代码
public class DefaultFuture implements ResponseFuture {
private final Lock lock = new ReentrantLock();
private final Condition done = lock.newCondition();
//waiting map
private static final Map<Long, DefaultFuture> FUTURES
                = new ConcurrentHashMap<Long, DefaultFuture>();
private final Request request;
private volatile Response response;
}

1. consumer发送请求,阻塞等待响应。

调用代码:

HelloService helloService = (HelloService) appCtx.getBean("hello");
String hello = helloService.sayHello();

调用代理对象的sayHello方法,代理类proxy0由javassist动态生成,代码大致如下:

class com.alibaba.dubbo.common.bytecode.proxy0 implements
com.alibaba.dubbo.rpc.service.EchoService, com.zhang.HelloService {
public <init>(java.lang.reflect.InvocationHandler arg0){
handler=$1;
}
public static java.lang.reflect.Method[] methods;
private java.lang.reflect.InvocationHandler handler; public java.lang.String sayHello(){
Object[] args = new Object[0];
//handler是InvokerInvocationHandler对象
Object ret = handler.invoke(this, methods[0], args);
return (java.lang.String)ret;
}
public java.lang.Object $echo(java.lang.Object arg0){
Object[] args = new Object[1];
args[0] = ($w)$1;
Object ret = handler.invoke(this, methods[1], args);
return (java.lang.Object)ret;
}
}

进InvokerInvocationHandler

//InvokerInvocationHandler类
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String methodName = method.getName();
Class<?>[] parameterTypes = method.getParameterTypes();
if (method.getDeclaringClass() == Object.class) {
return method.invoke(invoker, args);
}
if ("toString".equals(methodName) && parameterTypes.length == 0) {
return invoker.toString();
}
if ("hashCode".equals(methodName) && parameterTypes.length == 0) {
return invoker.hashCode();
}
if ("equals".equals(methodName) && parameterTypes.length == 1) {
return invoker.equals(args[0]);
}
//invoker是MockClusterInvoker
return invoker.invoke(new RpcInvocation(method, args)).recreate();
}

跳过中间步骤,进到DubboInvoker的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<Object>(future));
return new RpcResult();
} else {
//currentClient对象是 ReferenceCountExchangeClient/HeaderExchangeClient/HeaderExchangeChannel
//拆分来看:ResponseFuture responseFuture = currentClient.request(inv, timeout);
// responseFuture.get(); 调用get导致调用线程阻塞
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);
}
}

进HeaderExchangeChannel

public ResponseFuture request(Object request, int timeout) throws RemotingException {
if (closed) {
throw new RemotingException(this.getLocalAddress(), null,
      "Failed to send request " + request + ", cause: The channel " + this + " is closed!");
}
// create request.
Request req = new Request();
req.setVersion("2.0.0");
req.setTwoWay(true);
req.setData(request);
DefaultFuture future = new DefaultFuture(channel, req, timeout);
try{
channel.send(req);
}catch (RemotingException e) {
future.cancel();
throw e;
}
return future;
}

进DefaultFuture

public Object get() throws RemotingException {
return get(timeout);
} public Object get(int timeout) throws RemotingException {
if (timeout <= 0) {
timeout = Constants.DEFAULT_TIMEOUT;
}
if (! isDone()) {
long start = System.currentTimeMillis();
lock.lock();
try {
while (!isDone()) {
//调用线程等待直到超时,或者被DubboClientHandler线程唤醒
done.await(timeout, TimeUnit.MILLISECONDS);
if (isDone() || System.currentTimeMillis() - start > timeout) {
break;
}
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
lock.unlock();
}
if (! isDone()) {
throw new TimeoutException(sent > 0, channel, getTimeoutMessage(false));
}
}
return returnFromResponse();
} private Object returnFromResponse() throws RemotingException {
Response res = response;
if (res == null) {
throw new IllegalStateException("response cannot be null");
}
if (res.getStatus() == Response.OK) {
return res.getResult();
}
if (res.getStatus() == Response.CLIENT_TIMEOUT || res.getStatus() == Response.SERVER_TIMEOUT) {
throw new TimeoutException(res.getStatus() == Response.SERVER_TIMEOUT,
    channel, res.getErrorMessage());
}
throw new RemotingException(channel, res.getErrorMessage());
}

2. 在DubboClientHandler线程中,consumer接收响应,唤醒调用线程。

从ChannelEventRunnable进

//ChannelEventRunnable类
public void run() {
//省略其他代码
switch (state) {
case RECEIVED:
handler.received(channel, message);break;
default:
logger.warn("unknown state: " + state + ", message is " + message);
}
}

进DecodeHandler

public void received(Channel channel, Object message) throws RemotingException {
if (message instanceof Decodeable) {
decode(message);
} if (message instanceof Request) {
decode(((Request)message).getData());
} if (message instanceof Response) {
decode( ((Response)message).getResult());
} handler.received(channel, message);
}

进HeaderExchangeHandler

public void received(Channel channel, Object message) throws RemotingException {
channel.setAttribute(KEY_READ_TIMESTAMP, System.currentTimeMillis());
ExchangeChannel exchangeChannel = HeaderExchangeChannel.getOrAddChannel(channel);
try {
if (message instanceof Request) {
// handle request.
Request request = (Request) message;
if (request.isEvent()) {
handlerEvent(channel, request);
} else {
if (request.isTwoWay()) {
Response response = handleRequest(exchangeChannel, request);
channel.send(response);
} else {
handler.received(exchangeChannel, request.getData());
}
}
} else if (message instanceof Response) {
handleResponse(channel, (Response) message);
} else if (message instanceof String) {
if (isClientSide(channel)) {
Exception e = new Exception("Dubbo client can not supported string message: "
               + message + " in channel: " + channel + ", url: " + channel.getUrl());
logger.error(e.getMessage(), e);
} else {
String echo = handler.telnet(channel, (String) message);
if (echo != null && echo.length() > 0) {
channel.send(echo);
}
}
} else {
handler.received(exchangeChannel, message);
}
} finally {
HeaderExchangeChannel.removeChannelIfDisconnected(channel);
}
} static void handleResponse(Channel channel, Response response) throws RemotingException {
if (response != null && !response.isHeartbeat()) {
DefaultFuture.received(channel, response);
}
}

进DefaultFuture

public static void received(Channel channel, Response response) {
try {
//收到响应删除future
DefaultFuture future = FUTURES.remove(response.getId());
if (future != null) {
future.doReceived(response);
} else {
logger.warn("The timeout response finally returned at "
+ (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()))
+ ", response " + response
+ (channel == null ? "" : ", channel: " + channel.getLocalAddress()
+ " -> " + channel.getRemoteAddress()));
}
} finally {
CHANNELS.remove(response.getId());
}
} private void doReceived(Response res) {
lock.lock();
try {
//给响应赋值
response = res;
if (done != null) {
//DubboClientHandler线程唤醒调用线程
done.signal();
}
} finally {
lock.unlock();
}
if (callback != null) {
invokeCallback(callback);
}
}

可以看到实际是两个线程在通信,main线程发送调用请求,并阻塞,DubboClientHandler线程接收到响应,并唤醒主线程。

DubboClientHandler是一个线程池,它执行工作队列中的任务(即ChannelEventRunnable对象)。

又是谁把ChannelEventRunnable对象放到DubboClientHandler线程池的工作队列中的呢?答案是New I/O client worker

//AllChannelHandler类
public void received(Channel channel, Object message) throws RemotingException {
//DubboClientHandler线程池
ExecutorService cexecutor = getExecutorService();
try {
cexecutor.execute(new ChannelEventRunnable(channel, handler, ChannelState.RECEIVED, message));
} catch (Throwable t) {
throw new ExecutionException(message, channel, getClass() +
                  " error when process received event .", t);
}
}

如果一个方法没有返回值,声明为:public void sayHello();
但配置不是oneway,那么cosumer还是会返回RpcResult,但是RpcResult中的内容为空。

consumer发送请求,接收响应的更多相关文章

  1. 使用HttpClient发送请求接收响应

    1.一般需要如下几步:(1) 创建HttpClient对象.(2)创建请求方法的实例,并指定请求URL.如果需要发送GET请求,创建HttpGet对象:如果需要发送POST请求,创建HttpPost对 ...

  2. fiddler模拟发送请求和响应

    iddler模拟发送请求和响应 一.fiddler模拟发送请求 1.fiddler模拟发送get请求 1)例如:访问博客园https://www.cnblogs.com/,并且登录输入密码账号登录,再 ...

  3. Postman使用手册1——导入导出和发送请求查看响应

    导读: 现在的web和移动开发,常常会调用服务器提供restful接口进行数据请求,为了调试,一般会先用工具进行测试,通过测试后才开始在开发中使用.这里介绍一下如何在chrome浏览器利用postma ...

  4. 发送请求获取响应内容(c#)

    C#请求url,获取响应的脚本 public string ResultOfApi(string url) { //发送请求 HttpWebRequest request = null; HttpWe ...

  5. XMLHttpRequest发送请求

    *open(method,url,async) *send(string)//在使用get请求的时候,是没有主体的,所有的信息都会拼在url当中,所以使用send的时候括号里的string可以为空!如 ...

  6. Android HTTP实例 发送请求和接收响应

    Android HTTP实例 发送请求和接收响应 Android Http连接 实例:发送请求和接收响应 添加权限 首先要在manifest中加上访问网络的权限: <manifest ... & ...

  7. 使用HttpClient发送请求、接收响应

    使用HttpClient发送请求.接收响应很简单,只要如下几步即可. 1.创建HttpClient对象.  CloseableHttpClient httpclient = HttpClients.c ...

  8. javascript实现select菜单/级联菜单(用Rails.ajax实现发送请求,接收响应)

    在购物网站,填写收货地址的时候,会出现XX省XX市XX区的下拉菜单,如何实现此功能?思路是什么? 功能设置: 当选择省select菜单后,市的select菜单为这个省的城市列. 当选择市菜单后,区菜单 ...

  9. JQuery Ajax 发送请求成功后却接收不到任何响应数据问题

    问题描述 使用 JQuery Ajax 向后端服务器发送请求,服务器也收到请求返回了响应数据,但是 Ajax 却收不到任何响应数据. 举例如下: $.ajax({ type: "post&q ...

随机推荐

  1. 关于sqlite使用场景

    对于sqlite,实际中从来没有用过,也几乎没有考虑过其使用场景,更不要说专门去研究它了,今天看最新的数据库流行度排行榜的时候,发现sqlite的长期趋势好像一直在第十位左右徘徊,特地搜索了下其使用场 ...

  2. 03: Memcached

    目录: 1.1 Memcached简介与安装 1.2 python-memcached模块天生支持集群 1.3 Memcached基本操作 1.1 Memcached简介与安装返回顶部 1.Memca ...

  3. zabbix监控之自定义item

    zabbix安装完成后,当需要使用自定义脚本构建自定义item必须注意以下几点: 1.首先使用zabbix_get手动在zabbix-server服务端获取监控的默认的item值,如下: [root@ ...

  4. 《Python程序设计(第3版)》[美] 约翰·策勒(John Zelle) 第 3 章 答案

    判断对错 1.由计算机存储和操作的信息称为数据.2.由于浮点数是非常准确的,所以通常应该使用它们,而不是int.3.像加法和减法这样的操作在mAth库中定义.4.n 项的可能排列的数目等于 n!.5. ...

  5. ad各层

    mechanical              机械层    keepout layer           禁止布线层    top overlay              顶层丝印层    bo ...

  6. cogs 330. [NOI2003] 文本编辑器

    ★★★   输入文件:editor2003.in   输出文件:editor2003.out   简单对比 时间限制:2 s   内存限制:128 MB [问题描述] 很久很久以前,DOS3.x的程序 ...

  7. P4303 [AHOI2006]基因匹配 未完成

    题目 luogu 暴力60pts部分 显然如果没有出现次数==5的条件 显然是\(N_{2}\)的求lcs的模板 但是加点条件就完全不同了 思路 这个题短小精悍,不想数据结构那么傻逼无脑 我们考虑一下 ...

  8. JavaScript:正则表达式 分组

    在现在的我看来,带小挂号的就是分组,嗯. 代码: var reg=/(abc)/; var str="abcdabcdeabcdef"; console.dir(reg.exec( ...

  9. RHEL7--linux系统启动流程与故障排除

    一.Linux启动过程 MBR保存着系统的主引导程序(grub 446字节,分区表64字节),启动过程就是把内核加载到内存. 启动的顺序: 1.BIOS: 2.BIOS激活MBR: 3.MBR中的引导 ...

  10. github上的markdown如何换行

    https://gist.github.com/shaunlebron/746476e6e7a4d698b373 1.普通的换行 在文本结束后面,加2个空格 2.段落之间的换行 使用反斜杠\