Rest-style Software Architecture

Source: Internet
Author: User
Tags representational state transfer


If a website is not a rest-style architecture, it will be despised by programmers!

The rapid development of mobile Internet, especially mobile Internet, brings new opportunities and challenges to developers. In addition to apps, mobile phones often access mobile Web. In addition to browsers, many apps also use web services. We perform more complex operations on mobile phones, the old generation's system architecture is no longer adapted, and it needs more standardized and excellent software architecture to cope with today's challenges, that isRest.

Starting from the HTTP protocol

First, you are familiar with a concept URI. every type of resource available on the web-HTML documents, images, video clips, programs, etc.-is identified by a general resource identifier ("Uri ").
For example:http://www.bigertech.com/a.jpg

The HTTP protocol defines different methods for client-server interaction. There are four basic methods: Get, post, put, and delete.

  • To create resources on the server, use the POST method.
  • To retrieve a resource, use the get method.
  • To change the resource status or update it, use the put method.
  • To delete a resource, use the delete method.

Multiple resource expressions
Multiple Expressions of resources are provided for different needs. The multiple expressions mentioned here include XML, JSON, and HTML. That is to say, the server must provide multiple formats of resource expressions for external use by different clients. For example, mobile apps can use XML or JSON to communicate with the server, while browsers can understand HTML.

Note:: In the HTTP specification, get is used for information retrieval and should be secure and idempotent.

In actual development, many people violate this protocol.

  • Many users are greedy and convenient, so it is difficult for post to use forms. Get is used to update resources.
  • You can use GET/post to add, delete, modify, and query resources without using put or delete.
  • On the other hand, in the early days, Web MVC Framework designers did not consciously view and design URLs as abstract resources. Another serious problem is that the traditional Web MVC framework basically only supports the get and post HTTP methods, rather than the put and delete methods.

Therefore, the new HTTP software architecture style has emerged.

What is rest?

Restful (representational state transfer), which represents state escaping. This is a type of information that Dr. Roy fielding proposed in his doctoral thesis in 2000.Software ArchitectureStyle.

More and more services are designed and implemented using this software architecture. For example, Amazon.com provides a rest-style Web Service for book search. Yahoo provides a rest-style Web service.

** It is worth noting that rest is a design style rather than a standard. Instead, it describes a principle of transmission status through representional. Its purpose is to observe the entire network from the perspective of resources, and the resources distributed in various places are determined by the URI, and the client application obtains the resource representation through the URI. The acquisition of these representations leads these applications to change their state. With the constant acquisition of resource characterization, client applications are constantly changing their status.

The rest software architecture uses the crud principle, which tells us that only four actions are required for resources (including network resources): Create, read, and update) and delete can be combined into countless other operations. In fact, all things in the world follow this rule: Generate, change, see, and destroy. This principle is derived from our data operations on database tables: insert, select, update, and delete ), so sometimes crud is written as Rudi (read update Delete Insert ). These four operations are the most basic operations, that is, operations that cannot be subdivided. They can be used to construct complex operation processes, just as the four mathematical operations are the most basic operations of numbers.

Rest requirements
  • Client and server structure
  • The connection protocol is stateless.
  • Ability to use the cache mechanism to improve performance
  • Hierarchical System
About status

Note the difference between the application status and the connection protocol status. HTTP connections are stateless (that is, do not record the information of each connection), while rest transmission contains all the status information of the application, therefore, it can greatly reduce the consumption of repeated HTTP connection request resources.

Web services with status transmission

A Web Service (also called restful web API) that uses HTTP and follows the rest principle. It defines resources in the following three aspects:

  1. Intuitive and brief resource address: URI, for example:http://example.com/resources/.
  2. Transmitted resources: Internet media types accepted and returned by web services, such as JSON, XML, and yaml.
  3. Operations on resources: a series of request methods (such as post, get, put, or delete) supported by the web service on this resource ).
    The put and delete methods are idempotent methods. The get method is a security method (it does not modify the server side, so it is also idempotent ). Idempotence means that multiple requests to the same URL should return the same result. For example, an absolute value operation is an example. In a real number set, ABS (A) = ABS (a) is used )).
Rest implementation

The Restful architecture of all major clients and servers is implemented accordingly, including Android, IOS, and Web

Web end

Ajax functions of jquery

$.ajax({            type: ‘PUT‘,   //options GET、POST、DELETE          url: this.myurl,       });
Nodejs
  • Express 4.x comes with a rest route
  • Node-restify
  • Restler client uses rest
Rest and Ajax

Before the emergence of Ajax, the browser's functions were relatively weak and only some thin client functions could be implemented. Therefore, web application developers can only migrate the functions to the server as much as possible, this gives rise to the widely used webmvc architecture pattern. In this way, many parts have already violated the rest architecture constraints. However, after Ajax emerged, the browser's capabilities were greatly enhanced, at this time, relying on AJAX capabilities may actually fully comply with the rest architecture constraints, which requires many functions to be moved forward to build more powerful clients. This may be the reason why rest gradually becomes popular with the emergence of Ajax. Of course, the vigorous promotion of rails DHH is also indispensable.

Advantages of rest
  • Cache can be used more efficiently to increase response speed
  • The stateless nature of communication allows different servers to process different requests in a series of requests, improving server scalability.
  • A browser can act as a client to simplify software requirements
  • Compared with other mechanisms superimposed on the HTTP protocol, rest has less software dependency.
  • No additional resource discovery mechanism
  • Long-term compatibility in software technology evolution is better

As we all know, network transmission is an important factor affecting application performance for network-based distributed applications. How to Use cache to save the cost of network transmission is a must for developers who build distributed network applications.

Conditional http get is designed to save the overhead of network transmission between the client and the server, this also makes it possible for the client to implement the cache mechanism (including any proxy between the client and the server. HTTP uses the HTTP header domain: If-modified-since/last-modified, if-None-match/etag to implement conditional GET requests.

Rest applications can fully explore the cache support capability of HTTP protocol. After the client sends an http get request to the server for the first time, the content may be cached by the cache server. When the current client requests the same resource, the cache can provide a direct response without requesting a remote server to obtain it. All of this is transparent to the client.

Rest-style Software Architecture

Related Article

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.