Dubbo Analysis-netty realize synchronous communication

Source: Internet
Author: User
Tags unique id
Description

Dubbo The default protocol is DUBBOPROTOCOL, the default remote communication is Netty (not netty4), and the default codec2 is hessian2.

Here we analyze the details of the Dubbo implementation of Netty asynchronous synchronization.

# Exchangechannel

We said in the last article that Dubbo's upper layer communication is encapsulated in ExchangeServer and exchangeclient. View the source found that the channel sent the message is Exchangechannel, below to see the implementation of Headerexchangechannel.

Public responsefuture Request (Object request, int timeout) throws RemotingException {
    if (closed) {
        throw new Remo Tingexception (this.getlocaladdress (), NULL, "Failed to send Request" + request + ", cause:the Channel ' + This + ' is CL Osed! ");
    }
    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;
}

Through the code above, we find that a defaultfuture result is actually generated. Call Future.get directly to synchronize the wait results.

Public Object get (int timeout) throws RemotingException {
    if (timeout <= 0) {
        timeout = Constants.default_timeo UT;
    }
    if (!isdone ()) {
        Long start = System.currenttimemillis ();
        Lock.lock ();
        try {
            while (!isdone ()) {
                done.await (timeout, timeunit.milliseconds);
                if (Isdone () | | System.currenttimemillis ()-Start > Timeout {break
                    ;}}}
        catch (interruptedexception e) { C14/>throw new RuntimeException (e);
        } finally {
            lock.unlock ();
        }
        if (!isdone ()) {
            throw new TimeoutException (Sent > 0, Channel, Gettimeoutmessage (false));
        }
    return Returnfromresponse ();
}

Blocking main use

Private Final lock lock = new Reentrantlock ();
Private final Condition done = Lock.newcondition ();

Call Done.signal when a message arrives ()

Specifically, invoking the Exchangechannel request method generates the Request object Req,s here every
Req has a unique ID. Then Defaultfuture has the following Qin two static variables, saving the
Reqid to the defaultfuture mapping.

Private static final Map<long, channel> channels = new Concurrenthashmap<long, channel> ();

Private static final Map<long, defaultfuture> futures = new Concurrenthashmap<long, defaultfuture> ();

When Netty receives a message, the com.alibaba.dubbo.remoting.exchange.support.defaultfuture#received static method is invoked.

public static void received (Channel Channel, Response Response) {
    try {
        defaultfuture future = Futures.remove (res Ponse.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.getlocaladd Ress ()
                    + "->" + channel.getremoteaddress ()));
        }
    finally {
        channels.remove (Response.getid ());
    }
}

Because Req and response all have unique IDs, it's easy to turn them into sync. Debug Dubbo Source demo using the multicast registry, encountered the provider-side failure to start the error as follows:

Java.net.ConnectException: Network unreachable (connect failed) at Java.net.PlainSocketImpl.socketConnect (Native method) at Java . Net. Abstractplainsocketimpl.doconnect (abstractplainsocketimpl.java:350) at Java.net.AbstractPlainSocketImpl.connectToAddress (abstractplainsocketimpl.java:206) at Java.net.AbstractPlainSocketImpl.connect (abstractplainsocketimpl.java:188) at Java.net.SocksSocketImpl.connect ( sockssocketimpl.java:392) at Java.net.Socket.connect (socket.java:589) at com.alibaba.dubbo.config.ServiceConfig.fi Ndconfigedhosts (serviceconfig.java:577) at Com.alibaba.dubbo.config.ServiceConfig.doExportUrlsFor1Protocol ( serviceconfig.java:469) at Com.alibaba.dubbo.config.ServiceConfig.doExportUrls (serviceconfig.java:357) at Com.alib Aba.dubbo.config.ServiceConfig.doExport (serviceconfig.java:316) at Com.alibaba.dubbo.config.ServiceConfig.export
    (serviceconfig.java:215) at Com.alibaba.dubbo.config.spring.ServiceBean.onApplicationEvent (servicebean.java:121)At Com.alibaba.dubbo.config.spring.ServiceBean.onApplicationEvent (servicebean.java:50)
 

Debug found Findconfigedhosts hosttobind = Inetaddress.getlocalhost (). gethostaddress (); This line gets hosttobind is 127.0.0.1 so can't broadcast.

Reference articles

Http://dubbo.apache.org/books/dubbo-user-book/references/registry/multicast.html
Http://www.cnblogs.com/ghj1976/p/5276452.html
https://blog.csdn.net/raintungli/article/details/8191701

Solution:

I'm using the Ubuntu system,
sudo vi/etc/hosts

127.0.0.1       localhost
 #127.0.1.1       tao-tm1604
 192.168.3.3      tao-tm1604

Note the second line adds the third line 192.168.3. It's me. Lan IP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.