Dubbo call Process at a Glance

Foreword

Apache Dubbo as a high-performance Java RPC framework, the evolution of the domestic service system played a very important role, it is widely used in a large number of companies.

Close of the year, there may be little opportunity to find a new partner with the idea, then it might often see such a problem during the interview:

Dubbo do you know? Can you talk about it the call flow.

For the purposes of the Friends of the basin do not understand, no doubt will reduce the brownie points; will be used only if, in fact, is not enough, at least we have to understand its basic principles.

This paper attempts to Dubbo from the user's perspective, the key code combination flow chart and the corresponding knowledge together, we answer the above questions.

First, the service provider

From the perspective of the program developer's perspective, we must first have a service provider. Usually, we marked Dubbo on the achievement of specific interfaces Servicecomment.

package com.viewscenes.producer.dubbo;
import org.apache.dubbo.config.annotation.Service;
import com.viewscenes.common.service.DubboUserService;
@Service
public class DubboUserServiceImpl implements DubboUserService {
}
复制代码

The implementation class in Dubbo corresponding analytical class ServiceBean, which is responsible for the realization of foreign exposed as a service. Process is as follows:

Dubbo service exposure process

In conjunction with the map view, we can say that at the provider end, expose a service process is as follows:

First, ServiceConfigclass reference implementation class ref external service provider (e.g. DubboUserServiceImpl), then ProxyFactotya class that implements interface extension getInvoker()method to generate a ref AbstractProxyInvokerinstance, this particular service is completed Invokerconversion.

Next, the protocol Dubbo export()method, will Invokerbe converted to Exporter. Here, then, you will first start Netty Serverlistening, and then registered with the service registry service.

Here, we must note that, as a service provider end, has opened a TCP listening port through Netty. So, when consumers call, through a series of Netty Handler processor, it will call into DubboProtocol > ExchangeHandler.reply().

In this method, the process is a reverse thrust. Through the service interface name to be called, found Exporter, and then get to the Invokerobject.

Invoker<?> getInvoker(Channel channel, Invocation inv) throws RemotingException {
    int port = channel.getLocalAddress().getPort();
    String path = inv.getAttachments().get(PATH_KEY);
    String serviceKey = serviceKey(port, path, inv.getAttachments().get(VERSION_KEY),  inv.getAttachments().get(GROUP_KEY));
    DubboExporter<?> exporter = (DubboExporter<?>) exporterMap.get(serviceKey);
    return exporter.getInvoker();
}
复制代码

From the above analysis, we already know where the Invokertarget is based on a service implementation class generated AbstractProxyInvokerinstances. It eventually calls the wrapper.invokeMethod()method. Here wrapperclasses by Javassistgenerating a class in memory, so that the core length Method:

DubboEach service provider will implement to generate a Wrapperclass. Upon receipt of the request of the consumer, according to the transmission method name and parameters Wrapperclass calls the service provider interface class can be implemented. The main purpose of this call is to reduce reflections.

Second, the service consumer

In the service consumer side, we have a direct reference to an interface.

@Reference
DubboUserService userService;
复制代码

Perhaps you may want to ask, why so only inject a common interface, you can call to the remote service it?

We think about how SQL Mybatis the Dao interface and the XML document is to establish a relationship? This problem, how they associate it?

To put it bluntly or Springcredit, or a Spring FactoryBeancredit.

In Dubbothe denoted the @Referenceinterface is considered as a Factory Bean, the Bean usually returns a proxy object to shield the underlying complex operations. For example Mybatisin the xml file and associated mapper interfaces, Dubbonetwork communication and the like.

We first take a look at the map through a specific process consumer side:

Service Reference

In conjunction with the map view, we summarize the process under the service reference:

ReferenceNotes marked Dubbointerface will be registered as FactoryBean, and ultimately return a proxy object.

In the process of creating agents in call other methods to build and merge Invokerinstance.

First of all, calling DubboProtocolrefer method returns DubboInvokeran object. Here, the more important is to get the client instance. For example NettyClient, Dubboto rely on it for network communication.

Then, you need multiple services provider instance combined into one, which is to achieve fault tolerance clusters.

Finally, by JavassistProxyFactorycreating a proxy and return. Here, it's processor InvokerInvocationHandler, which means that when we call a consumer at the end of Dubbothe interface when, in fact, calls to the InvokerInvocationHandler.invoke()method, there is Dubbocomplete such as cluster fault tolerance, load balancing, remote method invocation of a series action.

When the consumer sends the request of Dubbo, eventually calls to DubboInvokerthe method. Here, we handle the details of the request logic, such as data transmission request.

final class HeaderExchangeChannel implements ExchangeChannel {
    //创建请求消息对象
    Request req = new Request();
    req.setVersion(Version.getProtocolVersion());
    req.setTwoWay(true);
    req.setData(request);
    
    //创建Future,用于获取返回结果
    DefaultFuture future = DefaultFuture.newFuture(this.channel, req, timeout, executor);
    try {
        //通过Netty客户端发送数据
        this.channel.send(req);
        return future;
    } catch (RemotingException var7) {
    	future.cancel();
    	throw var7;
    }
}
复制代码

Third, the request - response process

Request response process Dubbo

Guess you like

Origin juejin.im/post/5e148151f265da5d45542791