Dubbo notes②: Architecture overview

I. Introduction

This series is a personal Dubbo study note, the content comes from "In-depth analysis of Apache Dubbo core technology insider", only for personal note recording. The analysis of this article is based on Dubbo 2.7.0 version. Due to the limitations of personal understanding, if there is an error in the article, thank you for your correction.


This article is purely theoretical, and the content comes from "In-Depth Analysis of Apache Dubbo Core Technology Insider". The purpose of this article is to be the beginning of a follow-up series of articles.

Two, an overview of the layered architecture

Insert picture description here

  • Service and Config layer : API interface layer to allow Dubbo users to easily publish services and reference services; for service providers, it is necessary to implement service interfaces, and then use ServiceConfig API to publish the service; for service consumers Need to use ReferenceConfig to proxy the service interface. Dubbo service publishing and referencing parties can directly initialize the configuration class, or automatically generate the configuration class through Spring configuration.

  • The other layers are all SPI (Service Provider Interface) layers. SPI means that the following layers are all componentized and can be replaced. This is one of the better designs of Dubbo. Dubbo enhances the standard SPI functions provided in the JDK. In Dubbo, except for the Service and Config layers, all other layers provide services by implementing the extension point interface; Dubbo's enhanced SPI adds support for the extension point IoC and AOP, An extension point can directly use the setter() method to inject other extension points, and will not instantiate all implementation classes of the extension point at one time, which avoids the time-consuming initialization of the extension point implementation class, but it has not been used yet The function of loading and instantiation is still a waste of resources; the enhanced SPI is to instantiate a specific implementation class when a specific implementation class is used. The implementation principle of Dubbo's enhanced SPI will be explained in detail later.

  • Proxy service proxy layer : This layer is mainly to proxy the interface used by the service consumer, and transparently convert local calls to remote calls; in addition, proxy the service implementation classes of the service provider and convert the service implementation classes to Wrapper classes. This is to reduce the call of reflection, which will be explained in detail later. The SPI extension interface of the Proxy layer is ProxyFactory. The implementation classes provided by Dubbo mainly include JavassistProxyFactory (used by default) and JdkProxyFactory. Users can implement the ProxyFactory SPI interface and customize the implementation of the proxy service layer.

  • Registry service registry layer : when the service provider starts, it will register the service to the service registry, and when the consumer starts, it will go to the service registry to obtain the address list of the service provider. The main function of the Registry layer is to encapsulate the registration and discovery logic of the service address , The extensions corresponding to the extension interface Registry are implemented as ZookeeperRegistry, RedisRegistry, MulticastRegistry, DubboRegistry, etc. Extended interface RegistryFactory The corresponding extended interface is implemented as DubboRegistryFactory, DubboRegistryFactory, RedisRegistryFactory, ZookeeperRegistryFactory. In addition, the implementation classes of this layer's extended interface Directory include RegistryDirectory and StaticDirectory, which are used to transparently convert the Invoker list into an Invoker; users can implement a series of extended interfaces of this layer and customize the service implementation of this layer.

  • Cluster routing layer : encapsulates the implementation of routing rules, load balancing, cluster fault tolerance of multiple service providers, and bridges the service registry; the implementation classes corresponding to the extended interface Cluster include FailoverCluster (failure retry), FailbackCluster (failure automatic recovery), FailfastCluster (fast failure), FailsafeCluster (failure safety), ForkingCluster (parallel call), etc.; the implementation classes corresponding to the load balancing extension interface LoadBalance are RandomLoadBalance (random), RoundRobinLoadBalance (polling), LeastActiveLoadBalance (minimum active number), ConsistentHashLoadBalance (consistent) Sex Hash) and so on. Users can implement a series of extended interfaces on this layer, and customize cluster fault tolerance and load balancing strategies.

  • Monitor monitoring layer : used to count the number of RPC calls and the time-consuming calls, the extension interface is MonitorFactory, and the corresponding implementation class is DubboMonitorFactroy. Users can implement the MonitorFactory extension interface of this layer and implement custom monitoring statistics strategies.

  • Protocol remote call layer : encapsulates the RPC call logic, the extended interface is Protocol, and the corresponding implementations include RegistryProtocol, DubboProtocol, InjvmProtocol, etc.

  • Exchange information exchange layer : encapsulates the request response mode, synchronous to asynchronous, the extension interface is Exchanger, and the corresponding extension implementation includes HeaderExchanger and so on.

  • Transport network transport layer : Mina and Netty are abstracted as unified interfaces. The extended interface is Channel, and the corresponding implementations include NettyChannel (default), MinaChannel, etc.; the implementation classes corresponding to the extended interface Transporter are GrizzlyTransporter, MinaTransporter, NettyTransporter (default implementation); the implementation classes corresponding to the extended interface Codec2 include DubboCodec, ThriftCodec, etc.

  • Serialize data serialization layer : Provides some tools that can be reused. The extended interface is Serialization. The corresponding extended implementations include DubboSerialization, FastJsonSerialization, Hessian2Serialization, JavaSerialization, etc., and the corresponding extended implementations of the extended interface ThreadPool include FixedThreadPool, CachedThreadPool, LimitedThreadPool, etc.

Three, remote call overview

1. Overview of Service Provider Exposure

Insert picture description here

  1. Ref -> Invoker: The ServiceConfig class references the implementation class ref (such as GreetingServiceImpl) that provides external services, and then uses the ref to generate an AbstractProxyInvoker instance through the ProxyFactory#getInvoker method. At this step, the conversion of the specific service to the Invoker is completed. .
  2. Invoker -> Export: The conversion of Dubbo protocol Invoker to Exporter occurs in the Protocol#export method. The key to Dubbo handling service exposure is in the process of converting Invoker to Exporter. In this process, Netty Server will be started to monitor the service connection, and then Register the service to the service registry.

2. Overview of service consumer consumption services

Insert picture description here

  1. ReferenceConfig First, the ReferenceConfig#init method calls the Protocol#refer method to generate an Invoker instance, which is the key to service consumption. Next, convert the Invoker to the interface required by the client (such as GreetingService).
  2. The Invoker of the Dubbo protocol is converted to the interface required by the client, which occurs in the getProxy method of the extended implementation class of the ProxyFactory interface. It mainly uses the proxy to convert the call to the service interface to the call to the Invoker.

Above: For the content, refer to
"In-Depth Analysis of Apache Dubbo Core Technology Insider".
If there is any intrusion, please contact and delete it. The content is only used for self-record learning. If there is any error, please correct me

Guess you like

Origin blog.csdn.net/qq_36882793/article/details/114591565