dubbo中的请求和响应是如何关联起来的呢?

每一个请求和响应共用同一个id。

public class DefaultFuture implements ResponseFuture {
    // invoke id.
    private final long                            id;
    private final Request                         request;
    private volatile Response                     response;
}

consumer创建请求的调用栈:

public class Request {
    private static final AtomicLong INVOKE_ID = new AtomicLong(0);
    private final long    mId;

    public Request() {
        mId = newId();
    }

    public long getId() {
        return mId;
    }
    private static long newId() {
        // getAndIncrement()增长到MAX_VALUE时,再增长会变为MIN_VALUE,负数也可以做为ID
        return INVOKE_ID.getAndIncrement();
    }
}

provider 创建响应:

//HeaderExchangeHandler类
Response handleRequest(ExchangeChannel channel, Request req) throws RemotingException {
    //使用请求的id,作为响应的id
    Response res = new Response(req.getId(), req.getVersion());
    if (req.isBroken()) {
        Object data = req.getData();

        String msg;
        if (data == null) msg = null;
        else if (data instanceof Throwable) msg = StringUtils.toString((Throwable) data);
        else msg = data.toString();
        res.setErrorMessage("Fail to decode request due to: " + msg);
        res.setStatus(Response.BAD_REQUEST);

        return res;
    }
    // find handler by message class.
    Object msg = req.getData();
    try {
        // handle data.
        Object result = handler.reply(channel, msg);
        res.setStatus(Response.OK);
        res.setResult(result);
    } catch (Throwable e) {
        res.setStatus(Response.SERVICE_ERROR);
        res.setErrorMessage(StringUtils.toString(e));
    }
    return res;
}

 dubbo的响应:

//省略其他代码
public class Response {
    public static final String HEARTBEAT_EVENT = null;
    public static final String READONLY_EVENT = "R";
    public static final byte OK                = 20;
    public static final byte CLIENT_TIMEOUT    = 30;
    public static final byte SERVER_TIMEOUT    = 31;
    public static final byte BAD_REQUEST       = 40;
    public static final byte BAD_RESPONSE      = 50;
    public static final byte SERVICE_NOT_FOUND = 60;
    public static final byte SERVICE_ERROR     = 70;
    public static final byte SERVER_ERROR      = 80;
    public static final byte CLIENT_ERROR      = 90;
    private long             mId               = 0;
    private String           mVersion;
    //响应的状态
    private byte             mStatus           = OK;
    private boolean          mEvent         = false;
    private String           mErrorMsg;
    // 真实类型是 RpcResult,存放provider返回的结果值或异常
    private Object           mResult;
}

public class RpcResult implements Result, Serializable {
    private static final long        serialVersionUID = -6925924956850004727L;
    //存放返回值。如果接口声明为void,则result为null
    private Object                   result;
    //异常
    private Throwable                exception;
    public RpcResult(){ }
}

请求-响应对示例:

posted on 2018-01-08 10:13  偶尔发呆  阅读(964)  评论(0编辑  收藏  举报