The rest architecture is a brand new development style for Web applications. It is the most successful distributed system architecture of Internet hypermedia in the world today. It makes people really understand the original appearance of the HTTP protocol. As the rest architecture becomes the mainstream technology, a brand new way of thinking for Internet network application development has become popular.
What is rest?
Rest is the abbreviation of representational state transfer ", he is a term proposed by Dr. Roy Thomas Fielding in his paper titled neural tural styles and the design of network-based software ubuntures. Rest itself is only an architectural style designed for Distributed hypermedia systems, rather than a standard.
The web-based architecture is actually a collection of various specifications, which constitute a Web architecture. For example, the HTTP protocol, such as the client server mode, all of these are standards. Every time we add new specifications on the basis of the original specifications, a new architecture will be formed. While rest is such an architecture, which combines a series of specifications to form a new web-based architecture style.
Most traditional Web applications use the B/S architecture, which includes the following specifications.
Customer-Server
- This specification improves the portability of user interfaces across multiple platforms, simplifies server components, and improves system scalability. The most important thing is to separate user interfaces and data storage, so that different user terminals can enjoy the same data.
Stateless
- Stateless is another layer of specification added Based on Customer-server constraints. Communication is essentially stateless, that is, every request from the customer to the server must contain all the information required to understand the request. This specification improves the visibility of the system (stateless makes it unnecessary for the client and server to save the detailed information of the other party, and the server only needs to process the current request without having to know all the request history ), reliability (stateless reduces the number of tasks that the server can recover from a local error), scalability (stateless allows the server to easily release resources, because the server does not have to save the status in multiple requests ). At the same time, the disadvantages of this specification are also obvious. Because the state data cannot be stored in the sharing context on the server, the overhead of sending duplicate data in a series of requests is increased, it seriously reduces the efficiency.
Cache
- To improve the inefficiency of the stateless network, we have added cache constraints. Cache constraints allow you to implicitly or explicitly mark data in a response, so that the client can cache the response data, so that you can share the cached data for future requests, partially or entirely eliminates some interactions, increasing network efficiency. However, the information cached by the client increases the possibility of inconsistency between the client and the server, thus reducing the reliability.
The advantage of the B/S architecture is that its deployment is very convenient, but it is not ideal in terms of user experience. To improve this situation, we introduced rest.
Rest adds three new specifications to the original architecture: unified interfaces, hierarchical systems, and on-demandCode.
Unified Interface
- The core feature of the rest architecture style is to emphasize that there is a unified interface between components. This is manifested in the rest world, and all things on the network are abstracted as resources, while rest is to operate resources through a common linker interface. The benefit of this design is to ensure that the services provided by the system are decoupled, greatly simplifying the system, thus improving the system interaction and reusability. In addition, rest is optimized for common web conditions, so that the rest interface is designed to efficiently transfer large-granularity hypermedia data, this causes the rest interface to be not optimal for other architectures.
Layered System
- The addition of hierarchical system rules improves the independence between various layers, sets boundaries for the complexity of the entire system, and encapsulates legacy services to protect new servers from the influence of legacy clients, this improves the scalability of the system.
On-demand code
- Rest allows extension of client functions. For example, you can download and execute code in the form of an applet or script to expand the client functions. However, this also reduces visibility while improving system scalability. Therefore, it is only an optional constraint of rest.
Rest Design Guidelines
The rest architecture is designed for web applications to reduce development complexity and improve system scalability. Rest proposes the following design principles:
-
- Everything on the network is abstracted as a resource );
- Each resource corresponds to a unique resource identifier );
- Operate resources through the general connector interface (generic connector Interface;
- Operations on resources do not change resource identifiers;
- All operations are stateless ).
Resources in rest refer not to data, but to a combination of data and representations, for example, the "10 most recently accessed members" and "10 most active members" may overlap or be identical in data, but because of their different forms, so it is classified into different resources, which is why the full name of rest is representational state transfer. The resource identifier is the URI (Uniform Resource Identifier), whether it is an image, word, video file, or even a virtual service, whether you are in XML format, the TXT file or other file formats are all uniquely identified by Uris.
Rest is based on HTTP, and any operations on resources are implemented through HTTP. In the past, most web development used the get and post methods in the HTTP protocol and rarely used other methods. This is actually caused by one-sided understanding of the HTTP protocol. HTTP is not only a simple protocol for carrying data, but also a protocol with rich connotation of network software. It not only uniquely identifies Internet resources, but also shows us how to operate these resources. HTTP limits the operation on a resource to four methods: Get, post, put, and delete. This is the implementation of resource crud operations. Because resources correspond to Uris one by one, the URI does not change when performing these operations, which is very different from web development in the past. This greatly simplifies web development and allows the URI to be designed to reflect the resource structure more intuitively. This URI design is called a restful Uri. The developer introduced a new way of thinking: designing the system structure through URLs. Of course, this design method is not applicable to some specific situations, that is, not all Uris can be restful.
Rest can improve system scalability because it requires that all operations are stateless. Without context constraints, it is easier to implement distributed and cluster operations, and the system can use the buffer pool more effectively ). In addition, the server does not need to record a series of client accesses, which reduces the performance of the server.
Use rest Architecture
for developers, they are concerned about how to use the rest architecture. Here we will talk about this. Rest is not only a new architecture, but also a new way of thinking in the Web development process: designing the system structure through URL. In rest, all URLs correspond to resources. As long as the URL design is good, the presented system structure is also good. This is similar to test driven development. It designs system interfaces through test cases. Each test case represents a series of user requirements. Developers do not need to write functions at the beginning, but only need to present the functions to be implemented in the form of test cases. This is similar to the way in rest to design the system structure through URL. We only need to design reasonable URLs as needed. These URLs do not have to be linked to the specified page or complete some actions, as long as they can intuitively display the user interfaces of the system. Based on these URLs, we can easily design the system structure. From the concept of the rest architecture, everything that can be abstracted into resources can be designated as a URL. What developers need to do is to abstract user requirements into resources, and how to abstract precision. Because the more accurate the resource abstraction, the better the rest application. This is very different from the action-based idea in traditional MVC development models. A well-designed URL not only gives developers a clearer understanding of the system structure, but also makes it easier for users to remember and identify resources, because the URL is simple and meaningful. According to the previous design pattern, many URLs are followed by a bunch of parameters, which is inconvenient for users.
Since rest is so easy to use, can all web applications adopt this architecture? The answer is no. We know that, until now, the Model-View-controller mode is still the most common mode for web development. Most companies and developers adopt this architecture to develop Web applications, and the way of thinking also stays here. The MVC mode consists of data, views, and controllers. The event triggers the Controller to change the model and view. With the addition of open-source frameworks such as webwork and struts, the MVC development model is quite mature and its idea is driven by action. From the developer's point of view, it is risky to accept a new architecture. There are too many uncertainties. In addition, the new way of thinking of rest is to abstract all user needs as resources, which is difficult to achieve in actual development, because not all user needs can be abstracted as resources, this means that not the entire system structure can be expressed through rest. Therefore, in development, we need to make a choice in rest and MVC Based on the above two points. We believe that the better way is to mix rest and MVC, because it is suitable for the vast majority of Web application development. Developers only need to adopt the rest development mode for user needs that are easier to abstract as resources, MVC can be used for other needs. Here, we need to mention the ROR (Ruby on Rails) framework, which is an increasingly popular web development framework based on the ruby language. It greatly improves the web development speed. More importantly, the ROR (from version 1.2) framework is the first web development framework to introduce rest as the core idea. It provides the best support for rest, it is also the most successful web development framework for application rest today. In fact, the rest Implementation of ROR is a mix of rest and MVC. developers can use the ROR framework to build Web applications faster and better.
for developers, rest not only contributes to web development, at the same time, we also learned how to systematically apply software engineering principles to the design and evaluation of a real software.