Discussion on HTTP service seven-tier architecture technology

Source: Internet
Author: User
Tags php language smarty template

Original link http://kb.cnblogs.com/page/158568/

1. Why layered?

The architecture of the computer domain is generally hierarchical in its own way.

In terms of the overall structure:

From the lowest level of hardware to the top, in turn:

Applications, such as system programs, operating systems, drivers, and so on.

From the network layering model OSI, from top to bottom:

Application layer, presentation layer, session layer, Transport layer, network layer, data link layer, physical layer

Of course, the layering of TCP/IP protocols in practice is not as complex as the OSI standard.

The process of writing from a C-language file to generating an executable file looks:

Pre-processing (expanded C language code), compiled into assembly language (assembly language source files for specific CPU architectures), assembler generate target file (CPU executable binary instruction machine code), linker connection target file build executable file ( The operating system can load binary files executed)

This is not a hierarchical structure of software, but it can be understood as a way to simplify complex problems through layering. Then the PHP language can be thought of as a layer built on the C language-its interpreter Zend engine is implemented in C language. After all, it is much easier to write dynamic Web pages in a scripting language like PHP than in C.

Of course, there are some of the MVC layering techniques we're most familiar with, and we'll cover them in detail later.

So the benefits of stratification must have been more familiar with, this is a "divide and conquer big and small" thinking. When it comes to layering, we have to mention modules, in fact, hierarchies and modules are from two dimensions to "divide and conquer" way. A module is a horizontal dimension that divides a whole into separate parts, each of which exercises its own responsibility, and of course there may be dependencies between them, which are realized through the services provided externally. If the whole system than the Chinese territory, then the module will be divided into Chinese provinces, autonomous regions and so on. Stratification is from the longitudinal latitude of a whole from high to low divided into a number of independent layers, a complete service from the bottom to the top layer, layer to pass the final output. Layers and modules can be used at the same time, for example, China is divided into provincial-level modules, and then each province's administrative agencies use a hierarchical approach to the exercise of responsibility, from low to high households, villages, townships, districts, municipalities, provinces and so on, each layer is up one floor report. Hierarchies and modules increase system complexity and affect efficiency (users cannot report directly to the province, but need to report on a level up), but this is advantageous to the expansion and maintenance of the system, each layer only need to focus on its own service interface provided and the next layer of the service interface provided, imagine if the province needs to accept from the city, district, township, Village and other sub-level reporting, those provincial cadres will be very headache.

  2. HTTP service traditional three-layer architecture MVC

The most classic layered architecture in HTTP service non-MVC, almost any PHP development framework supports the MVC layering model, which has a long history and is a software model designed for programming language Smalltalk in the 80 's, and has been widely quoted.

Here is a quote from the Baidu Encyclopedia Image:

So I do not introduce the advantages of MVC, search engines can find a lot of relevant information.

The title of this article is the HTTP Service seven layer architecture technology discussion, more than the MVC four layers, such a complex layering is necessary?

About this problem benevolent see, I think the MVC layered granularity is not fine, of course you can continue to adhere to the traditional three layer, then you do not need to watch.

So why is the MVC hierarchy not fine enough, in my experience with the MVC pattern of the open source framework, I found that the functions of V and C are generally clear and stable, but the M layer often seems bloated and clumsy.

C layer is mainly responsible for the overall process control, the general specification of the structure, the process can be drawn with one or several flow charts, then the process is generally fixed.

V layer is mainly responsible for page rendering, may use the Smarty template engine, may also be the template engine, the display of the page may be html,xml or JSON, these kinds are more can be measured, so the M layer can be said to be fixed.

But the M layer is related to the business logic of the system, with the system constantly iterative update, the contents of M layer will continue to evolve, and this layer also has a lot of complex processing, such as file reading, database query, cache strategy, logic operations, data processing, data packaging and so on.

So in the MVC three layer model, the M layer can be subdivided again, when the M layer has a finer and more reasonable layering way, our business logic evolution process will be more handy.

  3. Seven-tier architecture

By the above introduction, then we are in the MVC of the M-layer again layered planning, I give here is a layer of M five-layer way, the reader if the five layer is too much or too little can refer to this plan.

The original M-layer is divided into:

Layer A: Application application layer

Tier B: Business operations layer

Layer C: Component component layer

Layer D: Datadriver data-driven layer

S-layer: Systemdriver system drive Layer

Then the entire seven-tier architecture is:

1. Controller

2. View

3. Application

4. Business

5. Component

6. Datadriver

7. Systemdriver

The structure diagram also refers to classic MVC, where the M layer is replaced with a new five layer.

Now introduce these new layers in turn:

1. Application

The application layer is at the top, and it is for a single page in practice or a single interface. The controller finds the corresponding application by using the parameters in the HTTP request address, then executes the public method specified in, such as Main (), and then the application starts. The responsibilities of the application layer include accepting HTTP parameters (which are typically indirectly accepted, such as fetching from the request object), invoking a business-level specific service, and saving the business execution results, which are ultimately displayed by view, and are, of course, coordinated by the controller. The application layer is the highest layer after the M layer is decomposed into five layers, and the controller communicates directly with this layer.

2. Business

Business layer under the application layer, usually an application instance corresponds to a business instance, and a business is likely to serve multiple applications, and the business is an execution flow that accomplishes the application's needs by performing a series of operations. These operations come from the lower component layer component, which may require one or more components to complete a complete requirement. Because a business instance typically only has one function, only one fixed method will be called by the upper application, such as flow (). The business layer's responsibility is to help the application layer execute the business flow and, when necessary, to return the data to the application layer, which invokes the underlying component method.

3. Component

Starting with the component layer and the above two layers have an essential difference, the component layer begins to have the concept of class library. An instance of the previous two layer usually exposes only a special convention to the public method to make the upper call, starting from this layer an instance will provide multiple methods to the upper layer. The component layer usually corresponds to a role in the system, for example, in a blog system, the post is a role, the user is a role, then there will be blog components blogcomponent, user components usercomponent, each role has a corresponding action, such as blog posts and users can add delete changes.

It is important to note that there should be no data-read operations in the component layer, and that data reading is done by the underlying data-driven layer. If the component layer gets data from the lower level, one of its responsibilities is to process the data. For example Blogcomponent has a way to get a blog post Getblog ($id), then the Getblog () method, from the data driven layer obtained the corresponding ID of the blog post data, you need to do some processing of the blog data, such as the blog post HTML special tags filter and so on. The component layer does not care about how the data is read, but it cares about the results of the data, such as the data does not exist or the data has expired.

4. Datadriver

The responsibility of the data-driven layer is to provide the source data for the component layer, which is concerned with the data access media, access methods and so on. The data may be stored on a db,mc,filesystem or remote HTTP server. The data-driven layer does not care about the contents of the data, only the results of the operation of the data read, such as the assumption that the data exists in DB, but the data-driven layer is wrong when executing the database query, then this layer needs to be processed. Assuming that the data is stored on a remote HTTP server, the data-driven layer needs to be concerned whether the HTTP return code is the correct 200 series or the wrong 400,500 series, even if the HTTP request returns the wrong data entity, but the return code is 200, then the data-driven layer does not care, This situation requires the upper component layer to handle.

5. Systemdriver

The system driver layer is a data access instance provided by the system environment, such as the systemdriver of the database service may be a DB handler,http service Systemdriver may be an HTTP handler, the file storage System driver layer may be a Handler, the system-driven layer is relatively simple, and the layer can be combined with the data-driven layer, with less responsibility. Simply perform data-access instructions for the data-driven layer.

Typically, in these five tiers, the number of instances in the upper layer is greater than the number of instances below, and the whole resembles an inverted trapezoid:

In a total of 6 application,5 business,4 a component,3 datadriver,2 systemdriver.

Each application is serviced by a business.

Each business has one or more application (B5 service A5 A6), one or more componet for its service.

Each component is one or more business services, and there is one or more datadriver to serve it.

Each datadriver for one or more component Services, there is one or more systemdriver for its service.

Each systemdriver is one or more datadriver services.

  4. Seven-tier architecture application

Now use such a framework to design a simple blog system, the server is implemented in PHP language. Of course, architecture is thought, not language-sensitive.

The entire system includes the following functions

1. Post a blog post 2. Modify the blog post 3. Delete blog post 4. Comment Post 5. Modify user Information

Each feature is required to record the operation log.

The Designed data store includes

1. Blog Data sheet 2. User Data table 3. Review data table 4. Log (storage file system)

In the table structure design, we added some redundant field information, for example, in the blog post table has the number of comments field comment_nums, post each comment once its value plus 1, each delete a comment its value minus 1

In the user data sheet we have added the number of posts posted by the user field Blog_nums, the user posts a blog post with a value of 1, minus 1 for every blog post deleted.

The following design layering:

  Application layer: There are altogether 5 applications

1. Postblogapplication blog post

2. Updateblogapplication Edit blog post

3. Deleteblogapplication Delete blog post

4. Commentblogapplication Comment Blog

5. updateuserapplication Modify User Information

  Business Layer: These 5 applications have 5 business services

1. Postblogbusiness blog post Business

2. Updateblogbusiness Blog to modify the business

3. Deleteblogbusiness Blog Delete Business

4. Commentblogbusiness Blog Comment Business

5. updateuserbusiness users to modify the business

  Component Layer : System A total of 4 roles corresponding to 4 components

1. Blogcomponent Blog Components

Methods of delivery include

1> Postblog () Post blog post

2> deleteblog () Delete blog post

3> updateblog () Edit blog post

4>getblog () Get the content of your blog

2. Commentcomponent Comment Component

Methods of delivery include

1>postcomment () post a comment

2> deletecomment () Delete Comment

3. Usercomponent User Components

Methods of delivery include

1> UpdateUser () modify user information

4. Logcomponent Log Component

Methods of delivery include

1>LOGMSG () Logging information

Data driven layer: corresponds to 4 components

1. Blogdatadriver db type provides the blog's Select Insert Delete update

2. Commentdatadriver DB Type provides comment Select Insert Delete update

3. Userdatadriver DB type provides user's Select Insert Delete update

4. Logdatadriver FS type provides a read write for file

system drive Layer: DB type and FS type

1. Mysqlsystemdriver db of Handler

2. Filesystemdriver FS Handler

Now let's publish a blog post to describe the process. Assume the interface address is: Http://www.xxxxx.com/postBlog:

1. The controller finds its corresponding application as postblogapplication by rewriting the rules, and postblogapplication is instantiated, and the special method in which main () is automatically invoked.

2. Postblogapplication needs postblogbusiness business to complete the post operation, Postblogbusiness is instantiated, and the special Method flow () is called.

3. Depending on your needs, when posting a blog post, you need to insert a blog post into your blog post and then modify the number of posts in the User information field. Then the postblogbusiness business flow consists of two operations, which are implemented by the Postblog () method in Blogcomponent and the UpdateUser () method in Usercomponent, respectively, The former inserts post information into the blog post, which adds 1 to the number of posts in the user's information field. Since the system requires logging for any operation, there is a third operation that logs the log and is implemented through the Blogcomponent logmsg () method. Then the postblogbusiness business flow consists of three operations, each of which consists of three components.

4. Below you need to consider the above three components of the lower call

Where Blogcomponet's Postblog () calls Blogdatadriver's insert-related method to insert the post data, Blogdatadriver is the DB type and is therefore implemented by Mysqlsystemdriver.

Usercomponent's UpdateUser () calls Userdatadriver's update-related method to update the number of posts, Userdatadriver is also a DB type, and therefore is also implemented by Mysqlsystemdriver.

Logcomponent's logmsg () calls Logdatadriver's write-related method, Logdatadriver is the FS type, and is therefore implemented by Filesystemdriver.

5. After the successful operation of the three components, Postblogbusiness told Postblogapplication that the blog post was successful, Postblogapplication then invokes the view-related method through the controller to display the result of the execution.

There are several other operating procedures that the reader can extrapolate, which is no longer introduced here.

Now let's take a look at the benefits of this layering by using the evolutionary use cases of several system functions.

Use Case 1: To facilitate the management of the log, you now want to be able to store the logs in DB instead of FS.

Workaround: This is a transformation of the data store, and we know that we should start with the data-driven layer. The log function is implemented by the log component logcomponent, where Logcomponet logmsg () method calls Logdatadriver to save the log. We converted the logdatadriver from FS to DB type, and the interface method remained the same, so the transformation was done quickly.

Use case 2: New requirement-User A can transfer one post to another user B.

Workaround:

Step 1. First, this new requirement corresponds to a new application, so we have added a sendblogapplication.

Step 2. Need to have a business complete operation, the new business is sendblogbusiness.

Step 3. Consider transferring a blog post about the action 1. The number of id;2.a users who switched the User ID field in the blog post to B by the ID of a is reduced by 1;3.b the number of users ' posts plus 1.

These three operations need two components to complete, the two components of our system already have, Blogcomponent updateblog () complete Operation 1,usercomponent UpdateUser () complete operation 2, 3.

As can be seen from use case 2, when a new requirement is added, we add an instance to the application and business layers, and the component layer remains the same because the component layer is now able to meet the new business requirements, and we need to modify the component layer when our existing components do not meet the new business requirements.

Through these two simple use cases we find that our modifications to the system can be made very clear on which layers, or from the upper components down, are easy to operate.

  5. Summary

This vertical layering and horizontal modules together, can make the whole system structure clear and smooth, in my design of a framework, the original support for such layered architecture and modules, users only need to follow the same pattern simple operation, the interface between the layers and protocols have been agreed by the framework itself, the framework is not perfect, Own internal use can, for the time being not released.

Discussion on HTTP service seven-tier architecture technology

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.