Dubbo's design idea is hidden in these three pictures

itread01 2021-01-22 12:31:56
dubbo design idea hidden pictures


Dubbo Stand out from many microservice frameworks , Occupy RPC Half of the service framework , Very universal , Master well 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 is the implementation principle ? This article will combine with the 3 Picture , 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 ](https://img-blog.csdnimg.cn/20210116210800489.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3ByZXN0aWdlZGluZw==,size_16,color_FFFFFF,t_70#pic_center) stay Dubbo in 4 Class role :- Registry Registration Center .- Consumer Service callers 、 Consumer side .- Provider Service provider .- Monitor Surveillance 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 is started , 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 、 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 provider 、 Service consumers report to the monitoring center TPS Wait for the call information , 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 . First of all, let's take a look at Zookeeper The data storage directory structure in the registry , From the directory structure to explore its implementation mechanism .![ Insert picture description here ](https://img-blog.csdnimg.cn/20210116210830727.png#pic_center)Dubbo Zookeeper Registration 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, change the number of threads , Timeout, etc . 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 nodes **), 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 nodes **), 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 ( Consumer side ); If a service provider goes down , Since the established 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 do grayscale rendering , It can be done by dubbo-admin And other management platforms , It will eventually be written to the router Node ( Persistent nodes ), The service caller will listen to the changes of the node , So as to perceive the latest routing rules , Use it to filter service providers , So as to realize the gray release and other functions .5. configurators The operation mechanism of node and router Nodes are the same , Don't repeat it .** Think about expansion kits **:1、 If the registry goes down , What impact will it have on the whole service system ? If the entire registry goes down , The whole ** Service call ** Can work normally , Does not affect existing service consumer calls , But consumers can't find new registered service providers .2、 If registry memory overflows or occurs frequently Full Gc, What impact will it have on the whole cluster ? If it happens frequently Full GC, And if 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 calls , Throw a large area No provider Wait for the mistake .** It's called Chengye temporary node 、 It's also a temporary node **.** To avoid Full Gc The serious consequences , Used for Dubbo Of the registry Zookeeper, It's going to be exclusive , And make memory in time 、CPU And so on .**## 2、 Service call Dubbo The design of service call is very elegant , The implementation principle is shown in the figure below :![ Insert picture description here ](https://img-blog.csdnimg.cn/20210116210853206.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3ByZXN0aWdlZGluZw==,size_16,color_FFFFFF,t_70#pic_center) Service call , Focus on the client to initiate a RPC All the implementation details of the service call , Include ** Service discovery **、** Fail over **、** Route forwarding **、** Load balancing ** Other aspects , yes Dubbo The theoretical basis of gray level release .#### 2.1 When the service discovery 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 a 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 It is usually based on the registry to realize the registration and dynamic discovery of services , As detailed above , I'm not tired of it here .#### 2.2 Load balancing clients use 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 a call , This is the so-called ** Load balancing **, namely LoadBalance. stay Dubbo The default in provides random 、 Weighted random 、 At least active connection 、 Uniformity Hash Equal load balancing algorithm .#### 2.3 Routing mechanism is actually Dubbo It not only provides a load balancing mechanism , Intelligent routing mechanism is also provided , This is the realization of **Dubbo Grayscale interpretation ** The theoretical basis of . So called routing mechanism , It's in the list of service providers , Set certain 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 ](https://img-blog.csdnimg.cn/20210116210919335.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3ByZXN0aWdlZGluZw==,size_16,color_FFFFFF,t_70#pic_center) The above set a routing rule , That is, the inquiry agency ID For 102 Query user requests 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 , And then load balancing .** The core idea of routing mechanism **: In progress ** Before load balancing ** First pair ** List of service providers ** Application ** Routing rules **, Get a list of providers involved in load balancing .#### 2.4 The call of the remote service of the fail over usually involves the network and other factors , The client initiates RPC When requesting a call, it doesn't have to be 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 , Usually ** Scenarios for implementing idempotent Services **.- failfast Fail fast , An error is returned immediately after the failure .- failsafe Print error log after call failure , Return to success , Usually ** It is used to record audit log and other scenarios **.- failback After the call failed , Return to success , But it will try again indefinitely in the background , Don't try again after restart .- forking Concurrent calls , After receiving the first response result, return it to the client . Usually ** Scenes with high real-time requirements **, But it's a waste of server resources , Usually through forks Argument sets the concurrent call degree .## 3、 Thread distribution mechanism Dubbo The communication thread model of is shown in the figure below :![ Insert picture description here ](https://img-blog.csdnimg.cn/20210116210947560.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3ByZXN0aWdlZGluZw==,size_16,color_FFFFFF,t_70#pic_center)#### 3.1 Network communication protocol network transmission usually needs custom communication protocol , It is usually used **Header + Body The protocol design concept of **, And 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 , We can compress the transmitted data , It's usually right body Serializing and compressing .Dubbo Support at present support java、compactedjava、nativejava、fastjson、fst、hessian2、kryo And so on .#### 3.2 Thread distribution mechanism in Dubbo The default will be created 200 Threads are used to process business methods , The so-called thread distribution mechanism is IO How threads determine which requests are forwarded to which threads to execute . at present Dubbo in ** All the heartbeat packets 、 Reading and writing on the Internet IO Run 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 Execute on thread .- connection The request event is executed in the thread pool ,** Connect 、 Disconnection events are queued for execution ( Thread pool with one thread )**.- direct All requests go directly to IO Run in thread .> Warm tips : About the thread model , Network communication mode , I can refer to the following article . There are many strategies for thread dispatch , The main consideration is ** Thread switch ** Is the cost tolerable , That is to say, 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 efficiency improvement , Instead, because thread switching is required , Bring efficiency 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 is the biggest encouragement for me to continuously output high quality articles . Welcome to the public account 『 Intermediary software interest circle 』, Explore the source code together , Communication is highly concurrent 、 Architecture experience , Reply PDF A lot of learning materials can be obtained .![](https://oscimg.oschina.net/oscnet/up-62325a67a593e2649766657cba2d964777
版权声明
本文为[itread01]所创,转载请带上原文链接,感谢
https://javamana.com/2021/01/20210122122913968p.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课程百度云