What do you need to pay attention to about microservice architecture
2021-07-20 04:08:50 【mob604756fb8908】
# Scan the QR code above to sign up for Chengdu Yuanchuang Hui #
Today we talk about the system architecture pattern , It's hard not to think of microservice Architecture . Enterprises or organizations in the practice of system architecture , From the original monomer Architecture , Later trend SOA, Gradually distributed , Finally, the microservice architecture came into being .
The emergence of microservice Architecture , In response to rapidly changing business needs 、 The lengthy development cycle provides a new solution , It responds to rapidly changing business needs in a modular way , Decouple the subsystems between the systems 、 Business 、 database , Even the development team , Use automated deployment 、 Automatic business monitoring and early warning 、 Call chain monitoring 、 Containerization , And agile development to speed up the software development cycle , Faster implementation 、 Higher quality delivery , Become one of the most popular architecture styles at present .
Microservice architecture is so hot , Developers want to know something about it , You can read books 、 Learn the practical experience of each company , Or consult an experienced expert , Or go to a technology conference to share , At the same time, there are many related theories and practice summary on the Internet .
And we used to go through the website “ Master Q & A ” This column , It provides you with a centralized opportunity to ask questions to experts , Let more people face the experts directly , Improve the related capabilities of microservice architecture . There are many highlights in the audience's questions and experts' answers , So this article is sorted out , To readers .
Q： Microservice architecture and traditional SOA What's the difference between architecture ？
Service Oriented Architecture （SOA） It's a component model , It will be different functional units of the application （ It's called service ） Through well-defined interfaces and contracts between these services . Interfaces are defined in a neutral way , It should be independent of the hardware platform that implements the service 、 Operating system and programming language . This enables services built in a variety of systems to interact in a unified and common way .
It's all about service , So micro services and SOA What are the similarities and differences between them ？
The same thing
need Registry, Realize dynamic service registration and discovery mechanism .
You need to consider transaction consistency under distributed ,CAP In principle , Two stage submission does not guarantee performance , Transaction compensation mechanism needs to consider .
Synchronous call or asynchronous messaging , How to ensure message reliability ？SOA from ESB To integrate all messages .
All need to be unified Gateway Come and gather 、 Choreograph interfaces , Achieve a unified authentication mechanism , External provision APP The use of RESTful Interface .
We should also pay attention to how to redistribute the positioning system , How to do log tracking ？ It's like the signaling tracking function in the telecommunication field for more than ten years .
It's continuous integration 、 Continuous deployment ？ about CI、CD（ Continuous integration 、 Continuous deployment ）, This in itself and agile 、DevOps It's intertwined , So we prefer the field of software engineering to the microservice technology itself .
Is it different to use different communication protocols ？ The benchmark communication protocol for microservices is RESTful, While traditional SOA It's usually SOAP, But for now, we're using lightweight RPC frame （Dubbo、Thrift、gRPC） A lot , stay Spring Cloud There are also Feign Framework will be standard RESTful Converted to code API This imitation RPC act , These communication protocols should not distinguish between microservice architecture and SOA The core difference .
Is it a popular container based framework or a virtual machine based one ？Docker Both virtual machine and physical machine are a way of architecture implementation , It's not the core difference .
SOA One of the main differences with microservices is the degree of automation . Most of SOA Implementation only achieves service level abstraction , Microservices reach the abstraction level of implementation and running environment .
In a canonical microservice , Every microservice should be built into a platform JAR（fat JAR）, All the dependencies are built in , And then as a separate Java The process exists .
Q： How to layer the microservice business layer ？ How to grade the business system , Why the standard ？
In general, the layering of micro services needs to be based on the company's own situation .
The same company uses unified application layering , To reduce development and maintenance learning costs . Application layering looks simple , But every programmer has his own way , Even beginners , So it's not so easy to implement .
The layered architecture we first came into contact with should be the one we are most familiar with MVC（Model-View-Controller） framework , It divides applications into models 、 View and control layer , It can be said that it has guided the vast majority of developers . And now the application （ Including framework ） This pattern is used by many architecture designs in . And then it evolved MVP（Model-View-Presenter） and MVVM（Model-View-ViewModel）. It can be said that with the continuous development of Technology , In order to cope with the different scenarios evolved from the model .
Each architecture of microservice can be subdivided into domain models , Let's take a look at the classic domain model architecture .
It includes Domain、Service and Repositories. Core entities （Entity） And the value object （Value Object） belong Domain layer , Defined domain services （Domain Service） stay Service layer , The storage and query logic for entities and value objects should be in Repositories layer .
It is worth noting that , Don't put the Entity The attributes and behaviors of are separated into Domain and Service Two layers , The so-called anemia model , It turns out that this kind of implementation will cause a lot of maintenance problems . Based on this design , The structure of the project can be constructed as ：
Of course , In the framework of microservices , Every microservice doesn't have to strictly follow this rule , Don't try to follow the rigid rules , The most important thing is to understand the business . In different business situations , The design of the architecture can be adjusted appropriately , After all, the right architecture must be flexible .
The principle of stratification is as follows ：
The advantage of using folder mode in application layering is that it can be large or small 、 Simple and easy to use 、 Uniform specification , Can include 5 A project , It can also include 50 A project , To meet a variety of different scenarios of all business applications .
In the development process , You need to follow the constraints of a hierarchical architecture , Cross level calls are prohibited .
The lower class serves the upper class
user-centric , Goal oriented . The upper （ Business logic layer ） What you need , The lower （ Data access layer ） That's what they offer , Not the lower class （ Data access layer ） what are you having? , Just to the top （ Business logic layer ） What to offer .
Entity layer protocol
Entity It's a data table object , Not data access layer objects ;DTO It's the object of network transmission , It's not a presentation object ;BO Is a memory computing logic object , Not business logic layer objects , Not only for the business logic layer . If access is limited to this layer , This leads to a large number of worthless object transformations in a single application . User centered design of entity classes , It can reduce worthless duplicate objects and useless transformations .
U Type access
When going down, the presentation layer is Input, The business logic layer is Process, The data access layer is Output. When uplink, the data access layer is Input, The business logic layer is Process, The presentation layer is Output.
Q： How to divide the granularity of micro services , Do you have any experience ？
This is also a design problem , First of all, the business must be familiar with , Then we can divide the domain according to the domain model , Splitting can be done according to AKF Expansion Cube （Scalability Cube）.
This cube has three axes , Each axis describes a dimension of extensibility , They are products 、 Process and team ：
X Axis —— Represents the indifference of cloning services and data , The work can be evenly distributed among different service instances ;
Y Axis —— Focus on the division of responsibilities in application , For example, data types , Division of transaction execution types ;
Z Axis —— Focus on prioritizing services and data , Such as the division of regions .
A comparison of the expansion of the three dimensions
Through the expansion of these three dimensions , Can quickly improve the expansion ability of the product , Adapt to the rapid growth of products in different scenarios . Expansion in different dimensions , There are different advantages and disadvantages ：
X Axis extension
advantage ： Lowest cost , Simple implementation ;
shortcoming ： Constrained by the number of instruction sets and the size of data sets . When a single product or application is too large , Slow service response , Unable to get X The horizontal expansion of the axis increases the speed ;
scene ： Initial stage of development , Low business complexity , Need to increase system capacity .
Y Axis extension
advantage ： It can solve the constraint of instruction set and data set , Solve code complexity problems , Fault isolation can be realized , Can improve response time , Can make the team focus more conducive to team growth ;
shortcoming ： The cost is relatively high ;
scene ： Business complex , Large amount of data , High code coupling , The team is big .
Z Axis extension
advantage ： Can solve data set constraints , Reduce the risk of failure , Achieve progressive delivery , Can bring maximum scalability .
shortcoming ： The most expensive , And it doesn't necessarily solve the problem of instruction set ;
scene ： Users are growing exponentially .
How to put theory into practice ？
To extend the segmentation application **
X Axis ： From a single system or service , Horizontal cloning of many systems , Distribute requests equally through load balancing ;
Y Axis ： Service oriented segmentation , Based on function or service segmentation , For example, e-commerce websites can log in 、 Search for 、 Order and other services Y Axis split , Each group of services goes on again X The extension of the axis ;
Z Axis ： Search oriented segmentation , Based on users 、 Request or data segmentation , For example, different products can be SKU Divided into different search services , Users can be hashed to different services, etc .
** Split database for extension **
X Axis ： From single library , Horizontal cloning is read on multiple libraries , A library to write , Through the self replication of the database , To allow a certain read-write delay ;
Y Axis ： Depending on the type of information , Split into different databases , That is, sub Treasury , For example, product library , User library, etc ;
Z Axis ： According to a certain algorithm , Slice , For example, search by MapReduce It's divided into pieces according to the principle , hold SKU According to different hash values, the data of the system is stored in pieces , Each piece is divided into two parts X Shaft redundancy .
Caching for expansion **
In an ideal situation , The best way to deal with large traffic is to avoid it by caching . From the perspective of Architecture , We can control the following three levels of caching ：
Object caching ： Object cache is used to store application objects for reuse , Usually inside the system , By using the application cache, you can help the database and application layer unload the load .
Application cache ： Application caching includes proxy caching and reverse proxy caching , One on the client side , One is on the server side , The goal is to improve performance or reduce resource usage .
Content delivery network caching ：CDN The general principle is to push content as close to the user's terminal as possible , Use different... Through different areas ISP Gateway cache for , Achieve faster response time and fewer requests to the source service .
Asynchronous for expansion
Synchronous to asynchronous ： A synchronous invocation , Due to the synchronous dependency between calls , It could lead to an avalanche effect , There are a series of cascading failures , And then lead to the whole system problems , So in system design , Consider asynchronous calls as much as possible , The mail system is a very good example of asynchronous calls .
Application stateless ： When doing AKF When expanding on any axis of the cube , We should first solve the problem of application state , That is, session management , By avoiding 、 Centralized and decentralized solutions .
Q： How does microservice implement transaction management ？
In fact, microservice transactions are distributed transactions , Rigid transaction and flexible transaction .
Rigid transaction means strictly following ACID A matter of principle , For example, database transaction in stand-alone environment . Flexible transactions refer to following BASE Theoretical business , Usually used in distributed environments , Common ways to achieve this are ： Two-phase commit (2PC),TCC Compensatory submission , Message based asynchronous guaranteed , Try your best to inform .
Data consistency can be divided into the following situations ：
When the update operation is complete , Any subsequent process or thread access will return the latest updated value . This is the most user-friendly , It's what the user wrote the last time , Make sure you can read something next time . according to CAP theory , This implementation requires sacrificing availability .
The system does not guarantee that subsequent processes or threads will return the latest updated values . After the data is written successfully , Do not promise to read the latest written value immediately , I don't promise how long I'll be able to read it .
The specific form of weak consistency . The system guarantees that without subsequent updates , The system finally returns the value of the last update operation . Without failure , The time of inconsistent window is mainly delayed by communication 、 System load and number of replicas impact .DNS It's a typical final consistency system .
Various implementations of distributed transactions ：
If business scenarios need strong consistency , So try to avoid putting them in different services , That is to use local transactions as much as possible , Avoid using highly consistent distributed transactions .
If the business scenario can accept the final consistency , So it's best to use a message based final consistency solution ( Asynchronous assured ) To solve .
If business scenarios need strong consistency , And only distributed services can be deployed , So it's best to use TCC The plan is not 2PC Plan to solve .
Q： Based on Spring Cloud In the microservice architecture of , Use Docker and k8s What are the benefits of these containerization technologies ？ For small and medium-sized micro service architecture , Is it necessary to use them ？
Container technology doesn't mimic the hardware level , But in Linux The kernel uses cgroup and namespaces To make light 、 Virtual technology operating system environment close to bare metal speed . Because it's not virtualized storage , So container technology doesn't take care of the underlying storage or file system , It's where you put it , Where does it operate .
in other words , It can control... More finely Linux, Be able to distribute on demand , One of the problems we often face in enterprise development is lack of resources , While using Docker Resources can be used more effectively . I don't think it's very big with the scale of the enterprise .
Q： Database management in microservice mode , How to do data aggregation among multiple services to avoid the difficulty of query implementation caused by too scattered data ？ Feeling k8s Can replace Spring Cloud Some of the components of （ For example, load balancing 、 Configure the service 、 Service discovery ）, How to distinguish the relationship between them ？
Data aggregation actually involves a design pattern of microservices , There are several design patterns of microservices ： Chain design pattern 、 Aggregator design patterns 、 Data sharing design pattern and asynchronous message control pattern .
The aggregator design pattern is to route requests uniformly from the gateway to the aggregator , The aggregator routes down to the specified microservice to get the result , And complete the aggregation . The home page shows 、 Category search and personal center usually use this design .
Data sharing mode is also a kind of microservice design mode . The application calls multiple microservices through the gateway , Data sharing between microservices through the same database , This can effectively reduce the number of requests , And it is very suitable for some cases with small amount of data .
in other words , Services need to be designed and processed . The gateway part can be used to realize the routing of data query .
Speaking of k8s and Spring Cloud, Both platforms are strong in core areas , And improve in other areas .Spring Cloud Can be used quickly 、 Developer friendly , However Kubernetes Yes DevOps friendly , Hard learning curve , But the broader micro service barrier . Here is a summary of these points .
The two architectures deal with a different range of MSA obstacle , And they use fundamentally different methods .Spring Cloud The way is to try to solve the problem in JVM Each of them MSA Challenge , and Kubernetes The way is to try to make the problem disappear , For developers in the platform layer .Spring Cloud stay JVM It's very powerful ,Kubernetes Manage those JVM Very powerful . alike , It's like a natural development , Combine the two tools and benefit from the best of the two projects .
Q： Please consult , About... In microservice architecture Service Mesh, What are its advantages ?
Service Mesh It's not the introduction of a new feature in the end , It's a shift in functional orientation . First we need to know Web Applications always have to manage the complexity of communication between services .
Service Mesh It's a network model , It is located in TCP/IP The abstraction layer above , It assumes that the underlying L3/L4 The Internet is real , And be able to pass bytes point to point . But with TCP The difference is ,Service Mesh Has higher performance .
It exists as a dedicated infrastructure layer , Usually as a group of lightweight high performance network agents , These agents are deployed with the program code , But the application doesn't need any action on the agent , It's used to handle inter service communication , Make the process of request delivery more reliable through complex topology .
You can think about 2000 Medium sized in 2000 Web Typical architecture of an application : Three tier applications . In this model , Application logic 、Web Service logic and storage logic are separate layers , Communication between layers is complex , But this complexity is limited to a certain range , Because there are only two jumps after all . Nothing here “ grid ”（Mesh）, But there is communication logic between the jumps processed in each layer of code .
When this kind of architecture is faced with the situation that the internal logic of the application is becoming more and more complicated , It starts to collapse . image Google、Netflix and Twitter Such companies are faced with huge traffic demand all the time , They implement the predecessor of cloud native solutions ： The application layer is broken down into many services ( Sometimes called “ Microservices ”), There is a topology between the layers .
In these systems , The broad communication layer suddenly becomes relevant , But usually in the form of “ Fat client ” The library set of （library） Form appears , such as Twitter Of Finagle、Netflix Of Hystrix, as well as Google Of Stubby This is the case .
In many ways , image Finagle、Stubby and Hystrix These collections are actually Service Mesh The prototype of . Although they are influenced by the details of their surroundings , And you need to use specific languages and frameworks , But they are dedicated infrastructure for managing service to service communication , also ( Open source Finagle and Hystrix In the case of library sets ) Can be used outside their company .
When it comes to cloud native applications , The cloud native model itself combines the microservice approach of many small services with two additional factors ： Containers ( for example Docker), It provides resource isolation and dependency management , And a choreography layer ( for example Kubernetes), It abstracts the underlying hardware into a homogeneous pool .
These three components support the natural mechanism of elastic scaling of applications under load , And be able to deal with some faults existing in the cloud platform environment .
But in the face of hundreds of services or thousands of instances , And rearrange the choreography layer of instances at any time , The path of a single request through the service topology can be very complex , And because containers make it easier for each service to write in a different language , The library set method is no longer feasible .
This combination of complexity and criticality , Stimulated the need for a dedicated infrastructure layer for service to service communication , This dedicated layer is separated from the application code , And it can capture the highly dynamic characteristics of the underlying environment . It's this special layer that we call Service Mesh. such Service Mesh You can understand the advantages of .
Q： I am from a small and medium-sized enterprise operation and development personnel , Always interested in microservice Architecture , Excuse me, if our enterprise plans to adopt micro service architecture in the future , What challenges are we going to face ？
A change of mind . First of all, we need to thoroughly understand what micro services are , You know, it's better than a single architecture 、SOA What's the difference in Architecture 、 What are the advantages and disadvantages , How to land .
Learning new technologies and new frameworks . such as spring boot、docker etc. .
Some suggestions ：
By looking up relevant information , The difference between traditional architecture and micro service architecture and the evolution process , You can first have a conceptual understanding of .
We can first let some people use the microservice architecture and do it in depth , And then promote it in the enterprise as a whole .
Space limit , Only part of it was intercepted , Click below for the full content “ Read the original ” see .
The guest ： Zhang Feng ,《 Microservice architecture practice 》 Author of a Book , Master of software engineering, Beijing University of Aeronautics and Astronautics , Senior architect , Yes 10 Years of management and architecture experience , It has great prestige and influence in the industry . Worked in China data 、 Asiatics 、 Chinese online and many Internet companies , As an architect and technical director, etc , Now working in CYTS , As the leader of the architecture group , Successfully managed and guided the three rural comprehensive service information platform 、 Northwest Enterprise Cloud Service Platform 、 Upgrading of provincial telecom platform and multiple Internet platforms . With the Ministry of industry and information technology certification senior information system project manager qualification .
- HDU 1050 Moving Tables（贪心）
- HDU 2037 今年暑假不AC【贪心】
- C＃ 四种九九乘法表
- 如何在 C＃中的listView 控件中显示图片？
- C＃ string格式的日期时间转为DateTime类型
- C＃ 计算两个日期的时间间隔
- C++学习之路—— C++异常处理机制
- C++学习之路—— STL标准模板库概述
- C++学习——int、long、long long, double, long double等的占用空间及取值范围
- C++学习——CString，char * ,string的相互转换
- SQL Server——数据库创建及修改
- SQL Server数据库数据类型详解
- SQL Server——数据表的基本操作
- SQL Server——T-SQL基础技术
- SQL Server基本操作练习
- Grey life
- Innovation and Shanzhai
- 12 golden rules for success before 35
- 57 experiences of technology companies
- Lenovo joins lvfs
- Sad news: the death of Gerald Weinberg
- Google announces dart 2 stable and dart web platform
- With the support of TLS 1.3, you can surf the Internet faster and more safely!