Talk about architectural layering

Hello everyone, I am Yi An!

In the stage of the system from 0 to 1, in order to get the system online quickly, we usually do not consider layering. However, as the business becomes more and more complex, a large number of codes are entangled, and there will be problems such as unclear logic, interdependence between modules, poor code scalability, and changes in one place will affect the whole body.

At this time, layering the system will be put on the agenda, so how do we layer the architecture? Today we will talk about

What is layered architecture

Layering of software architecture is a common design method in software engineering. It divides the overall system into N layers, each layer has independent responsibilities, and multiple layers cooperate to provide complete functions.

When we first became programmers, we were "educated" that the design of the system should be "MVC" (Model-View-Controller) architecture. It divides the overall system into three levels: Model (model), View (view) and Controller (controller), that is, it isolates the user view from business processing, and connects them through the controller to achieve a good performance. The decoupling of logic and logic is a standard software layered architecture.

alt

Another common layering method is to divide the overall architecture into presentation layer, logic layer and data access layer:

  • The presentation layer, as the name implies, is the layer that displays data results and accepts user instructions, and is the layer closest to the user;
  • There are specific implementations of complex services in the logic layer;
  • The data access layer is the interaction between the main processing and storage.

This is the simplest layering approach in terms of architecture. In fact, we have inadvertently designed the system layered according to the three-tier architecture. For example, when building a project, we usually create three directories: Web, Service, and Dao, which correspond to the presentation layer, logic layer, and There is a data access layer.

alt

In addition, if we pay a little attention, we can find many examples of layering. For example, the OSI network model we learned in college divides the entire network into seven layers, which are the physical layer, data link layer, network layer, transport layer, session layer, presentation layer, and application layer from bottom to top.

The TCP/IP protocol is often used in work, which simplifies the network into four layers, namely the link layer, network layer, transport layer and application layer. Each layer performs its own duties and helps each other. The network layer is responsible for end-to-end addressing and connection establishment, and the transport layer is responsible for end-to-end data transmission. At the same time, there will be data interaction between the two adjacent layers. This isolates concerns and allows different layers to focus on different things.

alt

The Linux file system is also designed in layers. From the figure below, you can clearly see the layers of the file system. At the top of the file system is the virtual file system (VFS), which is used to shield the differences between different file systems and provide a unified system call interface. The lower layer of the virtual file system is various file systems such as Ext3 and Ext4, and the lower layer is to shield the implementation details of different hardware devices. We abstract a separate layer-the general block device layer, and then there are different types of disks. .

We can see that some layers are responsible for the abstraction of different implementations of the lower layers, thereby shielding the implementation details from the upper layers. For example, VFS provides a unified call interface for the upper layer (system call layer), and at the same time stipulates the implementation model for different file systems in the lower layer. When a new file system is implemented, it only needs to follow this model. design, it can be seamlessly inserted into the Linux file system.

alt

So, why do so many systems have to be designed in layers? The answer is that there are certain advantages to layered designs.

Benefits of layered design

Layered design can simplify system design and allow different people to focus on a certain level of work. Imagine how painful it would be if you wanted to design a network program without layering.

Because you have to be an all-rounder who understands the network and knows what the interface of various network devices looks like so that you can send packets to it. You also need to pay attention to the details of data transmission, and need to deal with complex issues such as network congestion and data retransmission overtime. Of course, you need to pay more attention to how the data is transmitted safely on the network, so that it will not be snooped and tampered by others.

With a layered design, you only need to focus on designing the program of the application layer, and the rest can be handed over to the next few layers to complete.

Furthermore, high reuse can be achieved after layering. For example, when we design system A, we find that a certain layer has certain versatility, then we can extract it independently and use it when designing system B, which can reduce the development cycle and improve the efficiency of research and development.

最后一点,分层架构可以让我们更容易做横向扩展。 如果系统没有分层,当流量增加时我们需要针对整体系统来做扩展。但是,如果我们按照上面提到的三层架构将系统分层后,就可以针对具体的问题来做细致的扩展。

比如说,业务逻辑里面包含有比较复杂的计算,导致CPU成为性能的瓶颈,那这样就可以把逻辑层单独抽取出来独立部署,然后只对逻辑层来做扩展,这相比于针对整体系统扩展所付出的代价就要小得多了。

如何来做系统分层

说了这么多分层的优点,那么当我们要做分层设计的时候,需要考虑哪些关键因素呢?

在我看来,最主要的一点就是你需要理清楚每个层次的边界是什么。你也许会问:“如果按照三层架构来分层的话,每一层的边界不是很容易就界定吗?”

没错,当业务逻辑简单时,层次之间的边界的确清晰,开发新的功能时也知道哪些代码要往哪儿写。但是当业务逻辑变得越来越复杂时,边界就会变得越来越模糊,给你举个例子。

任何一个系统中都有用户系统,最基本的接口是返回用户信息的接口,它调用逻辑层的GetUser方法,GetUser方法又和User DB交互获取数据,就像下图左边展示的样子。

这时,产品提出一个需求,在APP中展示用户信息的时候,如果用户不存在,那么要自动给用户创建一个用户。同时,要做一个HTML5的页面,HTML5页面要保留之前的逻辑,也就是不需要创建用户。这时逻辑层的边界就变得不清晰,表现层也承担了一部分的业务逻辑(将获取用户和创建用户接口编排起来)。

alt

那我们要如何做呢?参照阿里巴巴Java开发手册,我们可以将原先的三层架构细化成下面的样子:

alt

我来解释一下这个分层架构中的每一层的作用。

  • 终端显示层:各端模板渲染并执行显示的层。当前主要是 Velocity 渲染,JS 渲染, JSP 渲染,移动端展示等。
  • 开放接口层:将Service层方法封装成开放接口,同时进行网关安全控制和流量控制等。
  • Web层:主要是对访问控制进行转发,各类基本参数校验,或者不复用的业务简单处理等。
  • Service层:业务逻辑层。
  • Manager 层:通用业务处理层。这一层主要有两个作用,其一,你可以将原先Service层的一些通用能力下沉到这一层,比如与缓存和存储交互策略,中间件的接入;其二,你也可以在这一层封装对第三方接口的调用,比如调用支付服务,调用审核服务等。
  • DAO层:数据访问层,与底层 MySQL、Oracle、HBase 等进行数据交互。
  • 外部接口或第三方平台:包括其它部门 RPC 开放接口,基础平台,其它公司的 HTTP 接口。

在这个分层架构中主要增加了Manager层,它与Service层的关系是:Manager层提供原子的服务接口,Service层负责依据业务逻辑来编排原子接口。

以上面的例子来说,Manager层提供创建用户和获取用户信息的接口,而Service层负责将这两个接口组装起来。这样就把原先散布在表现层的业务逻辑都统一到了Service层,每一层的边界就非常清晰了。

除此之外,分层架构需要考虑层次之间一定是相邻层互相依赖,数据的流转也只能在相邻的两层之间流转。

我们还是以三层架构为例,数据从表示层进入之后一定要流转到逻辑层,做业务逻辑处理,然后流转到数据访问层来和数据库交互。那么你可能会问:“如果业务逻辑很简单的话可不可以从表示层直接到数据访问层,甚至直接读数据库呢?”

其实从功能上是可以的,但是从长远的架构设计考虑,这样会造成层级调用的混乱,比方说如果表示层或者业务层可以直接操作数据库,那么一旦数据库地址发生变更,你就需要在多个层次做更改,这样就失去了分层的意义,并且对于后面的维护或者重构都会是灾难性的。

分层架构的不足

任何事物都不可能是尽善尽美的,分层架构虽有优势也会有缺陷,它最主要的一个缺陷就是增加了代码的复杂度。

This is obvious. It is obvious that you can directly query the database to obtain the results after receiving the request, but you have to insert multiple layers in the middle, and it is possible that each layer simply transfers data. Sometimes adding a small requirement also requires changing the code at all levels, which seems to increase the cost of development, and also increases the complexity from the perspective of debugging. Originally, if I directly access the database, I only need to debug one method, but now I Multiple methods at multiple levels to debug.

Another possible flaw is that if we deploy each layer independently, and the layers interact through the network, then the multi-layer architecture will lose performance. This is why the performance of the service-oriented architecture is slightly worse than that of the monolithic architecture, which is the so-called "one more hop" problem.

So should we choose a layered architecture? The answer is of course yes.

You have to know that any solution architecture has advantages and disadvantages. The world is not complete, let alone our architecture? Although the layered architecture will increase the complexity of the system, it may also cause performance loss, but compared to the benefits it can bring us, these are acceptable, or can be solved by other solutions. We must not be biased when making decisions.

Summarize

Today I talked about the advantages and disadvantages of the layered architecture, and how we layer the architecture in actual work. Layered architecture is an external embodiment of software design ideas and a way of realization. Some of the software design principles we are familiar with are reflected in the layered architecture. Mastering these design ideas will naturally understand the beauty of layered architecture design, and it can also help us make better design solutions.

This article is published by mdnice multi-platform

Guess you like

Origin blog.csdn.net/qq_35030548/article/details/130550360