Dubbo's design idea is hidden in these three pictures

Middleware interest circle 2021-01-22 09:10:09
dubbo design idea hidden pictures


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 :
 Insert picture description here
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 .
 Insert picture description here
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 :
 Insert picture description here
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 :
 Insert picture description here
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 :
 Insert picture description here

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 .

版权声明
本文为[Middleware interest circle]所创,转载请带上原文链接,感谢
https://javamana.com/2021/01/20210122090730015G.html

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云