Dubbo Stand out from many microservice frameworks , occupy RPC Half of the service framework , Very universal , Have a good command of Dubbo After the application of skills, a deep understanding of its internal implementation principle , So that we can better control the work , Help the workplace , It can make you stand out in the interview .

that Dubbo The interior design concept , What's the implementation principle ?

This article will combine with the 3 Pictures , From the following three aspects to introduce its internal core implementation 、 And how to guide practice .

1、 Service registration and discovery mechanism

Dubbo The service registration and discovery mechanism is shown in the figure below :

stay Dubbo in 4 Class role :

  • Registry

    Registry Center .
  • Consumer

    Service caller 、 The consumer end .
  • Provider

    Service providers .
  • Monitor

    The monitoring center .

The specific interaction process includes the following key steps :

  1. The service provider registers with the registry at startup .
  2. The message consumer subscribes to the specified service from the registry when it starts , The registry will use some mechanism ( Push or pull ) The pattern tells the consumer the list of service providers .
  3. When the number of service providers changes ( Service provider expansion 、 Shrinkage capacity 、 Downtime and other factors ), The registry needs to be in some way ( Push or pull ) Tell the consumer , So that the consumer can carry out normal load balancing .
  4. Service providers 、 Service consumers report to the monitoring center TPS And so on , So that the monitoring center can carry out visual display and so on .

Dubbo There are a variety of official registries , Next, the most commonly used Zookeeper Further introduce the principle of registry .

Let's look at it first Zookeeper The data store directory structure in the registry , From the directory structure to explore its implementation mechanism .

Dubbo Zookeeper Registry Center , Its directory structure is /dubbo/{ServiceName}, Under each service name, there will be 4 A catalog :

  • providers

    List of service providers .
  • consumers

    Consumer list
  • routers

    List of routing rules , Multiple routing rules can be set for a service .
  • configurators

    Dynamic configuration entries . stay Dubbo Without restarting the consumer 、 Dynamic modification of service provider under the premise of service provider 、 Configuration of service consumers , For example, modify the number of threads , Timeout time and other parameters .

be based on Zookeeper The implementation details of the registry are as follows :

  1. The service provider will register with the registry when it starts , Mainly in the corresponding service providers Add a record to the directory ( Temporary node ), Simultaneous monitoring configurators node .
  2. Service consumers will subscribe to the registry when they start , Mainly in the corresponding service consumers Add a record to the directory ( Temporary node ), Simultaneous monitoring configurators、routers Catalog .
  3. Because when a new service provider comes online providers The directory will add a record , Consumers can immediately receive a notification of a change in the list of service providers , To push the latest list of service providers to service callers ( The consumer end ); If a service provider goes down , Because the created node is a temporary node ,Zookeeper The node will be removed , It also triggers events , The consumer knows the latest list of service providers , So as to realize the dynamic registration and discovery of routing .
  4. When Dubbo After the new version goes online , If you need to publish in grayscale , Can pass dubbo-admin Wait for the management platform to add routing rules , Will eventually be written to the router node ( Persistent node ), The service caller will listen for changes in the node , So as to perceive the latest routing rules , Use it for service provider filtering , In order to achieve gray publishing and other functions .
  5. configurators The operation mechanism of node and router The nodes are the same , Don't repeat it .

Expand the thinking

1、 If the registry goes down , What impact will it have on the whole service system ?

If the entire registry goes down , Whole The service call Can work normally , Does not affect existing service consumer calls , But consumers can't find new registered service providers .

2、 If the registry overflows or occurs frequently Full Gc, What impact will it have on the whole cluster ?

If it happens frequently Full GC, And if the Full GC More than Zookeeper The expiration time of the session , Will cause Very serious impact , Will trigger all temporary nodes to be deleted , Consumers will not be able to perceive the existence of service providers , Affect service invocation , Throw out a large area No provider Such mistakes . It's called Chengye temporary node 、 It's also a temporary node .

for fear of Full Gc The serious consequences , be used for Dubbo registry Zookeeper, It's going to be exclusive , And do a good job of memory in time 、CPU And so on .

2、 The service call

Dubbo The design of service invocation is very elegant , The implementation principle is shown in the figure below :

The service call , Focus on the client to initiate a RPC All the implementation details when the service is called , Include Service discovery Fail over Routing and forwarding Load balancing Other aspects , yes Dubbo The theoretical basis of gray publishing .

2.1 Service discovery

When the client makes a request to the server , The first thing you need to know is which service providers are currently available , There are usually two service discovery mechanisms :

  • Static configuration

    Think back to , stay Dubbo Before the emergence of microservice framework , The usual way for one module to call another module is to use a configuration file , Configure the list provided by the service in the configuration file , The client is occupied from the list in the configuration file .

    Its disadvantages It's also very obvious : If there are many services to call , Configuration files get bloated , Management of capacity expansion and capacity reduction 、 Machine downtime and other changes are not friendly , Management is very difficult .

  • Dynamic discovery

    Usually, the registration and dynamic discovery of services are realized based on the registry , As detailed above , I'm not tired of it here .

2.2 Load balancing

The client uses the service discovery mechanism , It can dynamically discover the list of currently living service providers , The next thing to consider is if you select a service provider from the list of service providers to initiate the call , That's what's called Load balancing , namely LoadBalance.

stay Dubbo Random... Is provided by default in 、 Weighted random 、 Least active connection 、 Uniformity Hash Load balancing algorithm .

2.3 Routing mechanism

Actually Dubbo It not only provides a load balancing mechanism , Intelligent routing mechanism is also provided , This is to achieve Dubbo Grayscale Publishing Theoretical basis .

So called routing mechanism , It's in the list of service providers , Set some rules , Make filtering selection , In load balancing, only select from the list of service providers filtered by the routing filtering rules , In order to explain the working principle of routing mechanism more vividly , The following diagram is given :

The above set a routing rule , That is, the inquiry agency ID by 102 Query user request information , Please send to the new version , namely 192168.3.102 On , That is mainly to implement routing rules before load balancing , Filtering from the original list of service providers according to routing rules , Pick out a list of qualified providers , Then load balancing .

The core idea of routing mechanism : It's going on Before load balancing First pair List of service providers Application Routing rules , Get a list of providers involved in load balancing .

2.4 Fail over

Remote service invocation usually involves network and other factors , The client initiates RPC It's not necessary to call 100% success , When the call fails, what strategy should be adopted ?

Dubbo The following strategies are provided :

  • failover

    Select another service provider to try again after failure , The number of retries is configurable , Generally suitable Scenarios for implementing idempotent Services .

  • failfast

    Fast failure , An error is returned immediately after the failure .

  • failsafe

    Print error log after call failure , Return to success , Usually Used to record audit logs and other scenarios .

  • failback

    After the call fails , Return to success , But it will try again indefinitely in the background , Don't try again after restart .

  • forking

    Concurrent invocations , After receiving the first response result, return it to the client . Generally suitable Scenes with high real-time requirements , But waste server resources , Usually it can be done by forks Parameter to set the concurrent call degree .

3、 Thread dispatch mechanism

Dubbo The communication thread model of is shown in the figure below :

3.1 Network communication protocol

Network transmission usually needs custom communication protocol , Usually used Header + Body The protocol design concept of , also Header Fixed length , And it contains a length field , Used to record the size of the entire protocol package .

Network transmission in order to improve the transmission efficiency , The transmission data can be compressed , Usually yes. body Serializing and compressing .

Dubbo Support at present support java、compactedjava、nativejava、fastjson、fst、hessian2、kryo And so on .

3.2 Thread dispatch mechanism

stay Dubbo By default 200 Threads are used to process business methods , The so-called thread distribution mechanism is IO How does a thread decide which request is forwarded to which thread for execution .

at present Dubbo in All the heartbeat packets 、 Reading and writing on the Internet IO Execute in thread , Cannot be modified by configuration .

Dubbo The following thread distribution mechanisms are provided (Dispatcher):

  • all

    All requests are forwarded to the business thread pool for execution ( except IO Reading and writing 、 Heartbeat bag )

  • message

    Only request events are executed in the thread pool , Others are IO Execution on thread .

  • connection

    Request events executed in the thread pool , Connect 、 Disconnect event queued ( Thread pool with one thread ).

  • direct

    All requests go directly to IO Execute in thread .

reminder : About the thread model , Network communication mode , I can refer to the following article .

There are many strategies for thread distribution , It's mainly about Thread switching Is the cost tolerable , That is, the cost of thread switching is less than that of multithreading .

For example, in Dubbo in , For heartbeat packets, just return directly PONG package (OK), The logic is very simple , If you convert it to a business thread pool , It doesn't lead to performance improvement , Instead, because of the need for thread switching , Performance loss , Therefore, in IO Sending response packets directly in a thread is a very desirable practice .

There is a rule to follow in network programming Best practices IO There can be no blocking operations in a thread , Blocking operations need to be forwarded to the business thread pool .

Okay , This article introduces here , Your likes and forwards It's the biggest encouragement for me to continuously output high quality articles .

Welcome to the official account 『 Middleware interest circle 』, Explore the source code together , Communication is highly concurrent 、 Architecture experience , reply PDF We can get a lot of learning materials .

Dubbo The design concept of the original hidden in the three pictures of more related articles

  1. Hidden in the law of primes Pi -- BZOJ1041 Problem solving report

    Retired dog saw an interesting video in the process of brushing the itinerary book , It's an interesting question . After a random search on the Internet, I actually found one with its background OI Title , BZOJ1041. The following content will first review the knowledge discussed in the video , Yes ...

  2. A picture compares Docker and Git: Image management design concept

    Docker In the design of image management, we have learned a lot from Git Idea . The following figure compares the core concepts and operations of the two , It is helpful for us to master the management at a high speed Docker The right way to mirror . watermark/2/text/aHR ...

  3. STL Algorithm design philosophy - Dual function , Binary predicates and in set Application in

    demo Binary function objects #include <iostream> #include <cstdio> #include <vector> #include <a ...

  4. Use dubbo Exceptions when referencing and publishing services :HTTP state 500 - Request processing failed ; Nested exceptions are com.alibaba.dubbo.rpc.RpcException: Can't be in service cn.e3mall.service.ItemService The calling method getTbItemById. Use dubbo edition 2.5.3 In consumer ...

    The abnormal conditions are as follows : From the perspective of abnormality , Mainly because TbItem No serialization : To analyze problems : When the presentation layer calls the server, it returns a TbItem The object is Java object , At this point, the remote call of this object must be serialized , In order to carry out network transmission, we must first put ...

  5. CPU The secret of executing the program , It's hidden here 15 Zhang Tuli

    Preface So much code , You know, a = 1 + 2 How this code was CPU Is it executed ? So much software , You know the software 32 Bit and 64 The difference between bits ? Come again 32 Bit operating system can run on 64 A bit of electricity ...

  6. dubbo Distributed rpc Frame usage

    dubbo Alibaba's open source distributed service framework , Committed to providing high performance and transparency rpc Remote service invocation scenarios , as well as soa Service governance solution , If there are no distributed requirements , You don't need to dubbo Of , Distributed environment dubbo The use of the framework of the official gives a description ...

  7. Dubbo Principle analysis - monitor

    Dubbo In the release code , With a simple monitoring center to achieve . For general small businesses, the monitoring center should be able to meet the needs , For those large companies with large business volume, they usually have their own monitoring center , More abundant functions, such as common alarm SMS notification and so on . This chapter is about analysis ...

  8. Dubbo.xml Configuration source -Dubbo.xsd analysis

      We use Dubbo when , Generally used xml Configure basic information , Such as project name (application). Registry Center (register). agreement (protocal). service (service), As shown below : 1 2 3 ...

  9. Microservice architecture - choice Spring Cloud, give up Dubbo

    Spring Cloud Can it be used in small and medium-sized companies in China ? from 2016 From the beginning of the year until now , We've been on this road for more than a year . In the use of Spring Cloud Before , We don't have much experience in microservice practice . from ...

  10. Dubbo Source code analysis - Service invocation procedure

    notes : This series of articles has been donated to Dubbo Community , You can also be in Dubbo Read this series in the official document . 1. brief introduction In the previous article , We analyzed Dubbo SPI. Service export and import . And the code for cluster fault tolerance . after ...

Random recommendation

  1. wxpython Draw line chart

    environment:win10 + eclipse + pydev + python2.7.11 + wxpython3.0.2 code sample: #!/usr/bin/env pytho ...

  2. [LintCode] Min Stack Smallest stack

    Implement a stack with min() function, which will return the smallest number in the stack. It should ...

  3. [LeetCode] Remove Duplicates from Sorted Array

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  4. 20145338 《Java Programming 》 The first 1 Weekly learning summary

    Summary of the learning content of the textbook Chapter one java Introduction to platform 1.1Java It's not just language Java The first is Sun company " Green Project " In writing Star The programming language of the application , At the time called Oak.1995 year 5 month 23 Renamed ...

  5. Xiao Bai's diary 49:kali Penetration test Web penetration -XSS( 3、 ... and )- Storage type XSS、DOM type XSS、 Artifact BEFF

    Storage type XSS And DOM type XSS [XSS principle ] Storage type XSS 1. It can be stored on the server for a long time 2. Every user access is performed js Script , The attacker only needs to listen on the specified port # The method of attack utilization is similar to that of reflection xss utilize ## Most of them are in Liuzhou ...

  6. C# Get the phone home function

    Today, I learned from the information web Page grabbing function of , application HttpWebRequest and HttpWebResponse function , from http://www.showji.com Grab the location information from the website It should be said that this method is from other ...

  7. Linux kernel ‘fib6_add_rt2node’ Function security vulnerability

    Vulnerability name : Linux kernel ‘fib6_add_rt2node’ Function security vulnerability CNNVD Number : CNNVD-201307-265 Release time : 2013-07-16 Update time : 2013-07 ...

  8. Apache multiple domains setup

    Running several name-based web sites on a single IP address. Your server has a single IP address, an ...

  9. Java Web( fourteen ) To write MyBookStore Summary of the project

    I haven't blogged these days , The reason is to write a small project of book city , As web The final precipitation of learning , The next step is to learn the framework . --WH One . Project introduction A small project of a training institution from the Internet , The name is called Bookstore shopping website Well , One of them is Fen Qian ...

  10. R Language automated report format ——knitr

    R Language automated report format --knitr Related literature : R Language automated report format --knitr information | R Notebooks Upcoming release ------------------------------------ ...