REST(二)框架约束

概述

Representational State Transfer (REST) is an architectural style that defines a set of constraints to be used for creating web services.

By using a stateless protocol and standard operations, REST systems aim for fast performance, reliability, and the ability to grow, by re-using components that can be managed and updated without affecting the system as a whole, even while it is running.

REST并不是一个标准,不是像JWT一样的标准,只是一种设计风格。虽然REST风格的URL中的名词是复数,但是并没有什么标准必须是复数,很多大厂的URL中名字仍然是单数。

使用无状态的协议,抽象了不少的概念,搞了很多约束,会让系统获得更好的performance,scalability, simplicity, modifiability, visibility, portability, and reliability。其实这些也是一般系统的比较常见的非功能要求。

即使没有REST这个概念,大的互联网公司一般也会把服务器做成“无状态”的,至少对外表现是无状态的,同一个请求,发送到服务器A和服务器B都能够执行。无状态的特性,最直接的好处是可伸缩性,因为无状态可以容易的增加处理服务器的数目;然后由于伸缩性,加个可靠的负载均衡,能够做到高可用和高性能。

可扩展性,其实也是无状态特性带来的特性,目前来看,微服务的设计风格比REST带来的可扩展性优势更大,虽然两者不是一个维度的。

框架约束

Client–server architecture

The principle behind the client–server constraints is the separation of concerns. Separating the user interface concerns from the data storage concerns improves the portability of the user interface across multiple platforms. It also improves scalability by simplifying the server components. Perhaps most significant to the Web, however, is that the separation allows the components to evolve independently, thus supporting the Internet-scale requirement of multiple organizational domains.

看上去很平常的特性,还有不是CS的架构么?当然,比如最近的区块链。 客户端服务器的架构,让客户端可以做的很简单,不需要考虑存储,只关注与可以使用的接口,客户端与服务器只通过接口交互,可以使客户端与服务端独自发展。客户端可以使web可以使app,服务器可以是用java也可以用c++实现,互相松耦合,能很好的分工——提高生产的效率重要手段。

Statelessness

The client–server communication is constrained by no client context being stored on the server between requests. Each request from any client contains all the information necessary to service the request, and session state is held in the client. The session state can be transferred by the server to another service such as a database to maintain a persistent state for a period and allow authentication. The client begins sending requests when it is ready to make the transition to a new state. While one or more requests are outstanding, the client is considered to be in transition. The representation of each application state contains links that can be used the next time the client chooses to initiate a new state-transition.

上面几句话信息量很大。

The client–server communication is constrained by no client context being stored on the server between requests. Each request from any client contains all the information necessary to service the request, and session state is held in the client.

最主要的就是这句“Each request from any client contains all the information necessary to service the request”,客户端包括了执行这个request的所有的信息,举个常见的例子,在使用token来进行访问控制的时候,token中可以包括这个用户所拥有的角色,而服务器执行的时候,直接在接口处定义什么角色的人拥有此接口的权限,不用访问数据库就完成了这次认证。而通常cookie加session的做法根据sessionId去数据库找用户,再找用户的角色,多了两次数据库的访问操作,效率自然很低。这里可以获取“performance”这个特性。

The session state can be transferred by the server to another service such as a database to maintain a persistent state for a period and allow authentication.

当然,绝对的无状态很难做的,比如特别复杂的权限或者购物车这种功能,作者在这里也说了,session的状态可以统一转移到一个service中,把服务中不可避免的状态拿出去,服务还是无状态的。

The client begins sending requests when it is ready to make the transition to a new state. While one or more requests are outstanding, the client is considered to be in transition.

“表现层状态转移”,从这句描述中,“表现层”应该是指客户端所呈现的,当客户端发了几个请求还没有收到回复,状态是在“转移中”。

The representation of each application state contains links that can be used the next time the client chooses to initiate a new state-transition.

这也是无状态的一个实现的具体的方式。

Cacheability

As on the World Wide Web, clients and intermediaries can cache responses. Responses must therefore, implicitly or explicitly, define themselves as cacheable or not to prevent clients from getting stale or inappropriate data in response to further requests. Well-managed caching partially or completely eliminates some client–server interactions, further improving scalability and performance.

没什么特别的,可以缓存,但不是所有的请求都能缓存。

Layered system

A client cannot ordinarily tell whether it is connected directly to the end server, or to an intermediary along the way. Intermediary servers can improve system scalability by enabling load balancing and by providing shared caches. They can also enforce security policies.

分层,没什么特别的。

Code on demand (optional)

Servers can temporarily extend or customize the functionality of a client by transferring executable code. For example, compiled components such as Java applets, and client-side scripts such as JavaScript.

Uniform interface

The uniform interface constraint is fundamental to the design of any REST service. It simplifies and decouples the architecture, which enables each part to evolve independently. The four constraints for this uniform interface are:

设计的基础,也就是REST的接口设计风格。

参考

wiki Representational state transfer
作者论文部分

Table of Contents