Understanding Dubbo and RPC

Pay attention to Wang Youzhi , a man who shares hard-core Java technology with mutual gold and fishing.
Join the group of Java people who run around with buckets : Java people who are rich together

Open a new pit and learn Dubbo 3.X with everyone . We learn in an order from shallow to deep, starting with using Dubbo, and then digging into the core principles of Dubbo.

Today we start with getting to know Dubbo, the overall content can be divided into 3 parts:

  • What is Dubbo

  • What is RPC

  • Dubbo's architecture

Before the official start, I will make a statement. Usually, many materials on the Internet refer to RPC as a protocol, and compare RPC with HTTP. At present, this has become a "not correct" but mainstream statement. Personally, I am a fundamentalist, and I prefer to use the original interpretation of RPC, so it may be different from some of the articles you have seen. In addition, due to limited personal ability, if there are mistakes, I hope everyone will not hesitate to enlighten me.

Tips : The RPC chapter mainly refers to the paper "[Implementing Remote Procedure Calls] (https://flowus.cn/chang/share/6ec29968-8c6b-465e-bbdd-63fd29ad6825) published by Andrew D. Birrell and Bruce Jay Nelson in 1984

【FlowUs flow】Implementing Remote Procedure Calls.pdf)", this article is generally considered to be the origin of "modern" RPC (in fact, there were literatures that began to discuss RPC in 1976).

What is Dubbo?

Let's see how the Apache Dubbo community describes Dubbo:

Apache Dubbo is an RPC service development framework , which is used to solve service governance and communication problems under the microservice architecture . It officially provides multi-language SDK implementations such as Java and Golang. The microservices developed using Dubbo have native remote address discovery and communication capabilities between each other. Using the rich service governance features provided by Dubbo, service governance demands such as service discovery, load balancing, and traffic scheduling can be realized. Dubbo is designed to be highly scalable, and users can easily implement various custom logics for traffic interception and location selection.

Dubbo is an RPC framework with high performance and scalability . In addition, Dubbo also provides service governance capabilities.
insert image description here
Dubbo's "ambition" is not only to provide a complete RPC call and service governance framework, but also to untie Dubbo from programming languages ​​and provide versions of most mainstream languages.

Tips : This picture is taken from " Quick Understanding of Apache Dubbo in 5 Minutes " published by the Apache Dubbo community on station B.

What are RPCs?

Since the essence of Dubbo is an RPC framework, it is necessary for us to understand what RPC is before continuing to study Dubbo in depth.

RPC (Remote Procedure Call), that is, remote procedure call . "Implementing Remote Procedure Calls" is explained in this way:

The idea of remote procedure calls (hereinafter called RPC) is quite simple. It is based on the observation that procedure calls are a well-known and well-understood mechanism for transfer of control and data within a program running on a single computer.Therefore, it is proposed that this same mechanism be
extended to provide for transfer of control and data across a communication network.

The idea of ​​RPC is based on observations of procedure calls for transferring and processing data in stand-alone programs, and suggests the results of extending the same mechanism to remote network communications.

Is it a bit difficult to understand? It doesn't matter, let's put it in a simpler way and look at Sahn Lam's explanation in the YouTube video " What is RPC? gRPC Introduction ". In the video, he explains by comparing local procedure calls with remote procedure calls:

A local procedure call is a function call within a process to execute some code.A remote procedure call enables one machine to invoke some code on another machine as if it is a local fuction call from a user’s perspective.

This explanation is very clear. The core of RPC is to hope that remote calls can be as simple as local function calls . Based on this goal, Birrell and Nelson gave a design reference for RPC services:
insert image description here

The design of Birrell and Nelson is based on the concept of stub (stub, that is, User-stub and Server-stub in the figure). The whole system consists of five parts:

  • client, service caller;

  • User-side stub, which saves function declarations, and is responsible for packaging request parameters and unpacking response parameters;

  • RPC Runtime, select the appropriate method (protocol) to transmit data;

  • The server-side stub, which saves the function declaration, is responsible for unpacking the request parameters and packaging the response parameters;

  • server, service provider.

Developers on the client side and server side only need to obtain and call the target function from the stub, without considering the address of the server where the target function is located and the way of transmitting data, which is very suitable for the vision of "remote calling can be as simple as local function calling". of.

Well, here we have an overall understanding of the "fundamentalist" RPC, now to answer a less "serious" question: Why do we need RPC since we have HTTP?

This is a very common misunderstanding for beginners, equating RPC and HTTP. First of all, RPC is an idea (I think it is more like the goal of simplifying remote service calls), and HTTP is the transmission protocol of the application layer. The "two" RPC Runtimes in the above figure can use HTTP when transmitting data, or other methods that can The way the data transfer is done. Secondly, the theory of "modern" RPC was born in 1984, and HTTP was initiated in 1989, so it seems a little reasonable to ask this question in reverse. Finally, the purpose of the birth of HTTP is to receive and publish HTML pages, that is, data transmission between the browser and the server, rather than the application of data transmission between the two servers.

Tips

  • Sahn Lam and Alex Xu are the managers of ByteByteGo, a YouTube channel with 430,000 fans, and they are also the authors of "System Design Interview";

  • The RPC system design diagram is taken from "Implementing Remote Procedure Calls";

  • In actual projects, there is no strict distinction between client and server, and services can provide external interfaces or use external service interfaces.

Dubbo's architecture

Starting from Dubbo 3.0, Dubbo's official documentation uses a new abstract architecture:

insert image description here

Dubbo is divided into two layers from the whole:

  • Dubbo data plane : provides the core part of the RPC function, communicates through the RPC protocol, defines the calling specification, and completes the encoding and decoding functions of data interaction;

  • Service governance control plane : the abstraction of service governance, including registration center, traffic control strategy, Dubbo Admin console, etc.

Before Dubbo 3.0, the official gave a very complicated design drawing of Dubbo 2.X ( the following part is the official text ):

insert image description here

illustration

  • The one with the light blue background on the left in the figure is the interface used by the service consumer, the one with the light green background on the right is the interface used by the service provider, and the one on the central axis is the interface used by both parties;

  • The figure is divided into ten layers from bottom to top. Each layer is one-way dependent. The black arrow on the right represents the dependency relationship between layers. Each layer can be reused by stripping the upper layer. Among them, the Service and Config layers are APIs. All other layers are SPI;

  • The small green block in the figure is the extension interface, and the small blue block is the implementation class. Only the implementation class used to associate each layer is shown in the figure;

  • The blue dotted line in the figure is the initialization process, that is, the assembly chain at startup, the red solid line is the method call process, that is, the runtime timing chain, and the purple triangle arrow is inheritance. You can regard the subclass as the same node of the parent class, and the line The text above is the method to call.

Dubbo provides a very rich interface, these are the extension points of Dubbo that can be customized by users. Dubbo itself also adopts the Microkernel+Plugin (microkernel + extension) model, and Microkernel is only responsible for assembling Dubbo's default implementation of Plugin.

Description of each layer

  • config configuration layer : external configuration interface, centered on ServiceConfig, ReferenceConfigcan directly initialize configuration classes, or generate configuration classes through Spring parsing configuration

  • Proxy service proxy layer : service interface transparent proxy, generating service client Stub and server Skeleton, ServiceProxycentered on the center, and the extended interface isProxyFactory

  • Registry registration center layer : Encapsulates the registration and discovery of service addresses, centered on service URLs, and the extension interface is RegistryFactory, Registry,RegistryService

  • Cluster routing layer : encapsulates routing and load balancing of multiple providers, and bridges the registration center Invokeras extension interface is Cluster, Directory, Router,LoadBalance

  • monitor monitoring layer : monitoring of the number of RPC calls and call time, Statisticscentered on the center, the extension interface is MonitorFactory, Monitor,MonitorService

  • Protocol remote call layer : encapsulates RPC calls, centered on Invocation, Resultand the extended interface is Protocol, Invoker,Exporter

  • Exchange information exchange layer : encapsulation request response mode, synchronous to asynchronous, centered on Request, Responsethe extension interface is Exchanger, ExchangeChannel, ExchangeClient.ExchangeServer

  • transport network transport layer : Abstract Mina and Netty as a unified interface, Messagecentered on the center, and the extended interface is Channel, Transporter, Client, Server,Codec

  • serialize data serialization layer : some reusable tools, the extension interface is Serialization, ObjectInput, ObjectOutput,ThreadPool

Some articles will incorporate Service into Dubbo's hierarchical structure, but in fact Service is part of the user's business logic, not a component of Dubbo itself in the strict sense.

supporting agreement

The protocol is the core function of the RPC framework, which defines the data transmission format. In addition to the data itself, it should also contain control information, such as: serialization method, timeout time, etc.

Dubbo supports a lot of protocols, here I divide them into 5 categories:

insert image description here

Don't be afraid to see that Dubbo supports so many protocols. Although it supports many protocols, we don't have to go deep into each protocol. In the future, when we learn the protocol, we will focus on the Dubbo protocol, the Triple protocol promoted by Dubbo 3.X, and the gRPC that supports HTTP/2. We only need to understand the characteristics of the rest of the protocols.

Tips : In fact, there are very detailed design documents in the official documents of Dubbo 2.X. I don’t know why this part was deleted in Dubbo 3.0.

epilogue

Well, so far I hope you can establish an overall understanding of Dubbo design. Although the design is complex and there are many supported protocols, our goal today is not to "understand everything". We mainly focus on understanding the design given by RPC and Birrell and Nelson. Secondly, we need to establish an overall understanding of Dubbo's design and see what expansions Dubbo has made on the basis of Birrell and Nelson . If you are interested, you can refer to the architecture given by Birrell and Nelson to design your own RPC service. You need to consider how to save the service in the stub? Which way to use for interaction? How should the interactive data structure be designed?

In the next article, let's use Duubbo together to master the application of Dubbo. If this article is helpful to you, please give it a thumbs up . Finally, welcome everyone to pay attention to Wang Youzhi, a financial fisherman , and see you next time!

Guess you like

Origin blog.csdn.net/wyz_1945/article/details/131425355