Introduce the underlying principle of spring cloud with a case

osc_ si3dueof 2021-01-21 10:37:14
introduce underlying principle spring cloud


Catalog

One 、 Business scenario Introduction

Two 、Spring Cloud Core components :Eureka

3、 ... and 、Spring Cloud Core components :Feign

Four 、Spring Cloud Core components :Ribbon

5、 ... and 、Spring Cloud Core components :Hystrix

6、 ... and 、Spring Cloud Core components :Zuul

7、 ... and 、 summary

summary

without doubt ,Spring Cloud It is the leader in the field of microservice architecture , Countless books and blogs are explaining the technology . But most of the explanations are still right Spring Cloud The level of functional use , Many of its underlying principles , Many people may not know . So this article will draw a lot of pictures by hand , Let's talk about Spring Cloud The underlying principle of microservice Architecture .
actually ,Spring Cloud It's a whole family barrel technology stack , It contains a lot of components . This article starts with its core components , To analyze the underlying working principle . That is to say Eureka、Ribbon、Feign、Hystrix、Zuul These components .

One 、 Business scenario Introduction

Let's start with a business scenario , Suppose we develop an e-commerce website now , To realize the function of payment order , The process is as follows :

  • After creating an order , If the user pays for the order immediately , We need to update the order status to “ Paid ”

  • Deduct the corresponding inventory of goods

  • Inform the storage center , Make delivery

  • Add corresponding points to the user's purchase

For the above process , We need to have order service 、 Inventory service 、 Warehousing services 、 Points service . The general idea of the whole process is as follows :

  • After the user completes the payment for an order , I'll go to the order service , Update order status

  • Order service invokes inventory service , Complete the corresponding functions

  • The order service invokes the warehousing service , Complete the corresponding functions

  • Order service calls point service , Complete the corresponding functions

thus , The whole business process of payment order is over

Here's a picture , It clearly shows the calling process between services :

 picture

good ! With the business scenario , Let's have a look Spring Cloud In microservice Architecture , How these components work together , Their roles and the principles behind them .

Two 、Spring Cloud Core components :Eureka

Let's consider the first question : The order service wants to call the inventory service 、 Warehousing services , Or point service , How to call ?

  • The order service doesn't know which machine the inventory service is on at all ! Even if he wants to make a request , I don't know who to send it to , having a heart but no strength !

  • Now , It's your turn Spring Cloud Eureka Appearance. .Eureka It's the registry in the microservice architecture , Responsible for service registration and discovery .

  Let's take a look at the picture below , Combine with the diagram to analyze the whole process : 

 picture

As shown in the figure above , Inventory service 、 Warehousing services 、 There is one in the points service Eureka Client Components , This component is specifically responsible for registering information about this service to Eureka Server in . To put it bluntly , Just tell Eureka Server, Which machine are you on , Which port is listening . and Eureka Server It's a registry , There's a registry in it , Save the machine and port number of each service

There is also one in the order service Eureka Client Components , This Eureka Client Components will look for Eureka Server Ask me : Which machine is the inventory service on ? Which port is listening to ? Warehousing service ? What about the points service ? Then you can take this information from Eureka Server In the registry, pull it to your local cache .

In this case, if the order service wants to call the inventory service , You can't find your own local Eureka Client Ask which machine the stock service is on ? Which port to listen on ? After receiving the response , Then you can send a request to the past , Call the interface of inventory service to deduct inventory ! Empathy , If the order service calls the warehousing service 、 Points service , It's the same thing .

To sum up :

  • Eureka Client: Responsible for registering information about this service to Eureka Server in

  • Eureka Server: Registry Center , There's a registry in it , Save the machine and port number of each service

3、 ... and 、Spring Cloud Core components :Feign

Now the order service does know the inventory service 、 Points service 、 Where is the warehouse service , At the same time, it also monitors which port numbers . But there are new problems : Does the order service have to write a lot of code by itself , Establish network connection with other services , Then construct a complex request , Then send the request over , Finally, write a lot of code to deal with the returned response results ?

This is the code fragment of the above process translation , Let's see , Experience the feeling of despair and helplessness !!!

Friendship tips , high energy alert :

 picture

After reading that big piece of code , Do you feel cold in your back 、 A cold sweat ? In fact, when you make an inter service call , If you write the code every time , The amount of code is at least several times more than the above section , So it's not something that people on earth can do at all .

In that case , Then what shall I do? ? Don't worry. ,Feign It has already provided us with elegant solutions . Let's see if you use Feign Words , What will the code of your order service calling inventory service become ?

 picture

How do you feel after reading the code above ? Does it feel like the whole world is clean , And found the courage to live ! There is no underlying connection 、 Construct request 、 Code to parse the response , It's just to define a FeignClient Interface , Then you can call that interface . The somebody else Feign Client At the bottom, according to your notes , Establish a connection with the service you specify 、 Construct request 、 Initiate a request for 、 Get a response 、 Parse response , wait . This series of dirty work is very tiring , The somebody else Feign It's all for you .

So here comes the question ,Feign How can it be so magical ? It's simple ,Feign One of the key mechanisms is the use of dynamic agents . Let's take a look at the picture below , Combine the graph to analyze :

  • First , If you define an interface @FeignClient annotation ,Feign A dynamic proxy will be created for this interface

  • Then if you call that interface , The essence is to call  Feign Dynamic agents created , This is the core of the core

  • Feign The dynamic proxy will be based on your @RequestMapping Etc , To dynamically construct the address of the service you want to request

  • Finally, for this address , Initiate request 、 Parse response

 

 picture

Four 、Spring Cloud Core components :Ribbon

That's it Feign, It's not over yet. . Now there are new questions , If someone's inventory service is deployed in 5 On the machine , As shown below :

  • 192.168.169:9000

  • 192.168.170:9000

  • 192.168.171:9000

  • 192.168.172:9000

  • 192.168.173:9000

This is the trouble ! The somebody else Feign How to know which machine to ask for ?

  • At this time Spring Cloud Ribbon That comes in handy .Ribbon It's the problem that's solved . Its function is load balancing , It will help you choose a machine for each request , Distribute requests evenly to machines

  • Ribbon By default, load balancing uses the most classic Round Robin Polling algorithm . What is this? ? Simply speaking , If the order service initiates the inventory service 10 Requests , Let's ask you first 1 Taiwan machine 、 And then there's the 2 Taiwan machine 、 The first 3 Taiwan machine 、 The first 4 Taiwan machine 、 The first 5 Taiwan machine , Then come again — Cycle , The first 1 Taiwan machine 、 The first 2 Taiwan machine ... And so on .

  Besides ,Ribbon Is and Feign as well as Eureka Close collaboration , Finish the work , As follows :

  • First Ribbon From  Eureka Client Get the corresponding service registry , We know which machines all the services are deployed on , What port numbers are listening for .

  • then Ribbon You can use the default Round Robin Algorithm , Choose a machine

  • Feign Will target this machine , Construct and initiate a request .

For the whole process , Another picture , To help you understand :

 picture

 

5、 ... and 、Spring Cloud Core components :Hystrix

In the microservice Architecture , A system will have many services . Take the business scenario of this article as an example : The order service needs to call three services in a business process . Now suppose that the order service itself is at most 100 Threads can handle requests , so what , Unfortunately, the points service went down , Every time the order service calls the point service , It's stuck for a few seconds , Then throw — Timeout exception .

Let's analyze , What's the problem with this ?

  1. If the system is in a high concurrency scenario , When a lot of requests come in , Order service 100 All threads will be stuck in the request integration service . As a result, the order service does not have a thread to process requests

  2. And then it will cause others to ask for order services , I found that the order service was also suspended , Not responding to any requests

Above this , Namely The terrible service avalanche problem in microservice Architecture , As shown in the figure below :

 picture

 

Pictured above , So many services call each other , Without any protection , One of the services has hung up , It will cause a chain reaction , Cause other services to hang up . For example, the point service is suspended , The thread of the order service will be stuck in the request integration service , No thread can work , In an instant, the order service is suspended , All requests for order services will get stuck , Unable to respond .

But let's think about , Even if the points service is suspended , Order service can also be done without hanging up ! Why? ?

  • Let's look at it in terms of business : When you pay for an order , Just deduct the inventory , Then inform the warehouse to deliver the goods OK 了

  • If the points service goes down , Big deal... After he recovers , Slowly recover data by hand ! Why do you have to hang up because of a point service , It directly causes the order service to hang up ? Unacceptable !

Now the problem is analyzed , How to solve ?

It's the turn of Hystrix Come on .Hystrix It's isolation 、 Fusing and degrading a frame . What do you mean ? To put it bluntly ,Hystrix There will be many small thread pools , For example, the order service request inventory service is a thread pool , The request warehousing service is a thread pool , The request integration service is a thread pool . The threads in each thread pool are only used to request that service .

For example : Now unfortunately , The points service has been suspended , What will happen ?

Of course, the thread used to call the integration service in the order service is stuck and can't work ! But because the order service invokes the inventory service 、 Both thread pools of the warehousing service are working , So these two services will not be affected in any way .

At this time, if someone asks for order service , The order service can still call the inventory service to deduct the inventory , Call warehousing service to notify delivery . It's just when calling the points service , Every time I make a mistake . But if the points service is suspended , Every call has to be stuck for a few seconds ? Does it make sense ? Of course not ! So we can't just blow the points service , For example 5 Within minutes, the request for points service will return directly , Don't go to the Internet and ask to get stuck for a few seconds , This process , It's called fusing !

The other said , brother , When the point service is suspended, you will blow , What can I do for you ! Don't go back without doing anything ? That's all right. , Let's have a demotion : Every time I call the points service , You record a message in the database , Say how many points have been added to XX user , Because the points service was suspended , Lead to no increase in success ! In this way, when the point service is restored , You can add points by hand according to these records . This process , It's called relegation .

To help you understand more intuitively , Next, use a picture , Sort out Hystrix Isolation 、 The whole process of fusing and degradation :

 picture

  6、 ... and 、Spring Cloud Core components :Zuul

That's it Hystrix, Let's talk about the last component :Zuul, That's microservice gateway . This component is responsible for network routing . Don't know network routing ? That's ok , Let me tell you something , without Zuul What will happen to our daily work ?

Suppose you deploy hundreds of services in the background , Now there's a front-end brother , The request was sent directly from the browser . For example : They want to ask for inventory service , Do you still let people remember the name of this service is inventory-service? Deployed in 5 On the machine ? Even if someone is willing to remember this one , You can have hundreds of service names and addresses in the background ? It's hard not to ask for one , You have to remember one ? You have to play like this , That's a friendship boat , Turn what you say !

In this case , It's not realistic at all . Therefore, a gateway must be designed in general microservice architecture , image android、ios、pc front end 、 Wechat applet 、H5 wait , Don't worry about hundreds of services on the back end , You know there's a gateway , All requests go to the gateway , The gateway will follow some characteristics in the request , Forward requests to back-end services .

And with a gateway , There are many other benefits , For example, we can do unified degradation 、 Current limiting 、 Certificate authority 、 Security , wait .

7、 ... and 、 summary :

Finally, let's sum up , The above Spring Cloud Core components , In the microservices architecture , The roles played by each :

  • Eureka: When each service starts ,Eureka Client Will register the service to Eureka Server, also Eureka Client It can also be reversed from Eureka Server Pull the registry , To know where other services are

  • Ribbon: When a request is made between services , be based on Ribbon Load balancing , Choose one of the machines in a service

  • Feign: be based on Feign Dynamic agent mechanism , According to the annotation and the selected machine , Splicing request URL Address , Initiate request

  • Hystrix: The request is made by Hystrix The thread pool comes and goes , Different services go through different thread pools , Implemented isolation of different service calls , Avoid service avalanches

  • Zuul: If the front end 、 The mobile side should call the back-end system , Unified from Zuul Gateway access , from Zuul The gateway forwards the request to the corresponding service

The above is through an e-commerce business scenario , Elaborated Spring Cloud The underlying principles of several core components of microservice Architecture .

Text summary is not intuitive enough ? That's all right. ! We will Spring Cloud Of 5 The core components are connected in series through a diagram , Let's feel the underlying architecture principle directly :

 picture

版权声明
本文为[osc_ si3dueof]所创,转载请带上原文链接,感谢
https://javamana.com/2021/01/20210121103259100p.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课程百度云