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 Service
comment.
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:
In conjunction with the map view, we can say that at the provider end, expose a service process is as follows:
First, ServiceConfig
class reference implementation class ref external service provider (e.g. DubboUserServiceImpl
), then ProxyFactoty
a class that implements interface extension getInvoker()
method to generate a ref AbstractProxyInvoker
instance, this particular service is completed Invoker
conversion.
Next, the protocol Dubbo export()
method, will Invoker
be converted to Exporter
. Here, then, you will first start Netty Server
listening, 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 Invoker
object.
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 Invoker
target is based on a service implementation class generated AbstractProxyInvoker
instances. It eventually calls the wrapper.invokeMethod()
method. Here wrapper
classes by Javassist
generating a class in memory, so that the core length Method:
Dubbo
Each service provider will implement to generate a Wrapper
class. Upon receipt of the request of the consumer, according to the transmission method name and parameters Wrapper
class 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 Spring
credit, or a Spring FactoryBean
credit.
In Dubbo
the denoted the @Reference
interface is considered as a Factory Bean
, the Bean usually returns a proxy object to shield the underlying complex operations. For example Mybatis
in the xml file and associated mapper interfaces, Dubbo
network communication and the like.
We first take a look at the map through a specific process consumer side:
In conjunction with the map view, we summarize the process under the service reference:
Reference
Notes marked Dubbo
interface 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 Invoker
instance.
First of all, calling DubboProtocol
refer method returns DubboInvoker
an object. Here, the more important is to get the client instance. For example NettyClient
, Dubbo
to 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 JavassistProxyFactory
creating a proxy and return. Here, it's processor InvokerInvocationHandler
, which means that when we call a consumer at the end of Dubbo
the interface when, in fact, calls to the InvokerInvocationHandler.invoke()
method, there is Dubbo
complete 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 DubboInvoker
the 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;
}
}
复制代码