Depth exploration REST (2): understanding the true REST architectural style

Article reprinted address: https: //www.infoq.cn/article/understanding-restful-style/, such as references to the original article please mark Address

Primer

In the mobile Internet, the rapid development of cloud computing today, as a Web developer, if you have not heard of "REST" the buzzword, apparently have become obsolete. Exaggeration to say that even "out the door are embarrassed to say hello with others." However, for this understanding REST parked to goods, most people (including some senior architect) still remain in the "elephant" stage. Often heard saying on a wide variety of REST, for example: Some people say: "We decided not to use this new API Web Service (SOAP + WSDL), but direct use of HTTP + JSON, which is a RESTful way to develop . "without SOAP, even without XML, it automatically becomes a RESTful. Some people believe: REST Web Service with traditional fact there is no essential difference, but the way to construct a URI more requirements, and these requirements can be fully realized Web Service. The subtext is: both Sheng Yu, Sheng-liang. Web Service has been good enough, why should toss what REST. For these different versions of REST, really? What exactly is REST? Is a new technology, a new architecture or a new norm?

For the first of these questions I do not answer, for in-depth understanding of what REST is, we need to look back at the early years of Web development, talk about REST is how come from the source.

 

The origin of Web technology development and REST

 

Web (WWW World Wide Web for short) is an all-encompassing kaleidoscope, different people viewed from different angles, for Web what will come to very different point of view. As a Web developer, we need to come to understand from the Web technology. From a technical point of view the architectural level, Web technology architecture consists of four cornerstones:

  • HATE
  • HTTP
  • HyperText (in addition to HTML, but also can be hyperlinked XML or JSON)
  • MIME

The four cornerstones of mutual support, to promote this magnificent building Web geometrically pace up. On top of that four cornerstone of the development of Web development techniques can be roughly divided into the following phases:

  1. Static content stage: At this initial stage, use the Web mainly some research institutions. Web by a large number of static HTML documents, most of which are some of the papers. Web server can be seen as a shared file server support for hypertext.
  2. CGI program stage: At this stage, Web server adds some programming API. These applications written API, it can provide some dynamic content to the client. Communication between the Web server and the application via CGI (Common Gateway Interface) protocol is completed, the application is called CGI program.
  3. Scripting language stage: At this stage, the server appeared ASP, PHP, JSP, ColdFusion scripting language technical support session, the browser appeared Java Applet, JavaScript and other technologies. Using these techniques, we can provide a richer dynamic content.
  4. Thin-client application stage: At this stage, the server side of the application server appears independent of the Web server. At the same time there has been Web MVC development model, a variety of Web MVC development framework is gaining popularity, and occupy a dominant position. Web-based application framework for the development of these are usually thin-client applications because they are all generated dynamic content on the server side.
  5. RIA application stage: At this stage, a variety of RIA (Rich Internet Application) technology that significantly improves the user experience of Web applications. The most widely used RIA technology is DHTML + Ajax. Ajax technical support dynamic updates page of the local content without refreshing the page. At the same time the birth of a large number of Web front-end DHTML development libraries, such as Prototype, Dojo, ExtJS, jQuery / jQuery UI and so on, a lot of development libraries support the development of single-page application (Single Page Application) is. Other RIA technologies as well as Adobe's Flex, Microsoft's Silverlight, Sun's JavaFX (now Oracle Corporation all), and so on.
  6. Mobile Web application stage: At this stage, there has been a large number of Web application development technologies for mobile devices. In addition to the technical development of Android, iOS, Windows Phone and other operating system platforms native, HTML5-based development technology has become very popular.

From the development process of the above-mentioned Web development technology perspective, from the stage of the main supports static Web documents its designers originally conceived, gradually becoming more and more dynamic. Interactive mode Web applications become more complex: to document the development from a static content-based portal, e-commerce sites, search engines, social networking sites, to entertainment-based massively multiplayer online games, mobile game.

In the Internet industry, the practice is always ahead of theory. Web Development In 1995, after the CGI, ASP and other technology appears, use for many years, mainly for static documents HTTP / 1.0 protocol has been unable to meet the development needs of Web applications, it is necessary to design a new version of the HTTP protocol. Among the HTTP / 1.0 protocol expert group, a young man come to the fore, showing extraordinary insight, and later he became the head of HTTP / 1.1 protocol expert group. The young man is the core developer of the Apache HTTP Server Roy Fielding, he was a co-founder of the Apache Software Foundation.

Roy Fielding and his colleagues in the design of HTTP / 1.1 protocol, Web reason for great success, in terms of technical factors architecture made some in-depth summary. Fielding will be incorporated into a summary of these in a theoretical framework and use the guidelines in this theoretical framework to guide the design direction of the HTTP / 1.1 protocol. The first draft of HTTP / 1.1 protocol was released in January 1996, it has been revised more than three years, and in June 1999 became the formal specification of the IETF (RFC 2616 and includes for the client to do authentication RFC 2617). HTTP / 1.1 protocol designed for extremely successful, so that after the release of a full 10 years, many people have not considered necessary amendments. This theoretical framework used to guide the HTTP / 1.1 protocol design, initially in the form of a memorandum of exchanges between members of the Group, in addition to expert circles IETF / W3C, and not widespread in the outside world. Fielding After the completion of HTTP / 1.1 protocol design work, he returned to the University of California, Irvine to pursue his doctorate. The next year (2000) in his doctoral dissertation Architectural Styles and the Design of Network-based Software Architectures in, Fielding more systematic and rigorous exposition of this theoretical framework, and the framework of this theory is derived using a new kinds of architectural style, and took a very lighthearted name "REST" for such architectural style - an abbreviation Representational state transfer (Representational state transfer) of the.

In my opinion, Fielding value of this doctoral thesis in the history of the Web, much as Web father Tim Berners-Lee thing read about the classic paper hypertext. Unfortunately, however, this doctoral thesis in nearly five years after the birth of years, has not been enough attention. Web Service related specifications such as SOAP / WSDL designers obviously do not understand what is REST, HTTP / 1.1 is exactly what kind of agreement, why should design this way.

In this case after 2005 has been greatly improved, with the rise of new Web development technology Ajax, Ruby on Rails and other Web development technologies in the community set off a return to the origin of the Web architecture movement, REST architecture style has been more and more attention. In January 2007, REST support the development of Ruby on Rails version 1.2 officially released, and will support REST Rails development as a priority in the future development of the content. Ruby on Rails founder of a group called DHH do "World of Resources" wonderful speech, DHH strong influence in the development of Web technology community that is under the spotlight once REST Web development technology arena.

Today, a variety of popular Web development framework, little does not support the development of REST. Most Web developers are by reading some documents REST development framework, as well as through some example code to learn REST development. However, learning by example code REST a very big limitations. Because REST is not a specific technology, nor is it a particular specification, REST is actually a very rich connotation of architectural styles. REST learn by example code, in addition to learning a fun addition to Web development technology, and can not be comprehensive in-depth understanding of what REST really is. Or even mistaken these simple example code itself is REST, REST is just a simple Web development technology only. Like elephant, elephant nose touched some people, some people touched the elephant ears, some people like to touch the legs, touched some people like tail. They are convinced him feel the elephant, is the most real elephant, while others feel are wrong.

For Web developers do not understand REST, people used to show some examples of code to make them understand REST, I do not agree this practice. If a Web developer wants in-depth understanding of what REST is that it is difficult to avoid this Fielding's doctoral dissertation. In this article the author for what REST is introduced, also based on Fielding's doctoral thesis. Nevertheless, I strongly recommend readers of this article to go and read about Fielding's doctoral dissertation, just want to learn Confucius' thoughts should be directed to read "The Analects of Confucius" and other works, rather than first read others relayed the same. In this article the author is only trying to do a book mispronounced by the twisted and misrepresented it. So, let's get down to business.

In Fielding's this called Architectural Styles and the Design of doctoral thesis Network-based Software Architectures of (Chinese version called "architectural style and design of network-based software architecture") proposes a set of Web-based software (ie the so-called "distributed application") design method, it is worth all the distributed application developers to read, in-depth experience.

In the first three chapters of the thesis, Fielding on the basis of critical inheritance results of previous studies on the methodology established set of research and evaluation software architecture. The core of this methodology is "architectural style" concept. Architectural style is a method of research and evaluation software architecture design, it is more abstract than the concept of architecture. An architectural style is a set of architectural constraints of cooperating to define. Architecture constraint is imposing constraints on the architecture of the operating environment software.

In the fourth chapter in, Fielding studied the Web for such a distributed system software architecture design which made the demand. In the fifth chapter, Fielding will demand raised by the fourth chapter Web embodied some of the architectural constraints, by gradually adding a variety of architectural constraints, we derived a new REST architectural style.

REST architectural style derivation process shown below:

Figure 1: REST architectural style inherited constraints ( artwork can be downloaded here )

In Figure 1, each of the abbreviations represents the oval inside an architectural style, the words of each arrow represents a side architectural constraints.

REST architectural style of the most important architectural constraints there are six:

  • Client - server (Client-Server)

Communication can only unilaterally initiated by the client end, the performance of the request - response form.

  • Stateless (Stateless)

Session State (Session State) should be responsible for all communications maintained by the client.

  • Cache (Cache)

Content may be cached in response to somewhere in the communication chain, in order to improve network efficiency.

  • Unified interface (Uniform Interface)

Communications link between the components communicate with each other through a unified interface to improve the visibility of interaction.

  • Layered system (Layered System)

By limiting the behavior of components (that is, each component can only "see" the layer adjacent to interact with), the layer architecture is decomposed into several levels.

  • Code on demand (Code-On-Demand, optional)

Support by downloading and executing some code (such as Java Applet, Flash or JavaScript), the function of the client to be extended.

Derived in the REST architectural style paper as shown below:

Figure 2: REST architectural style ( artwork can be downloaded here )


 

While HTTP / 1.1 protocol architecture as an example of a REST architectural style, its structure as shown below:

Figure 3: a view-based process REST architecture (the original can be downloaded here )

User interaction agent in the middle of three parallel (a, b and c). User agent client connection cache can not satisfy the request, it according to the client configuration properties, and a connector for each resource identifiers, each request is routed to the source resources. Request (a) is sent to a home agent, the agent then accesses a lookup finds a cache gateway via the DNS, the gateway forwards the request to a meet the source server of the request, the internal resource server by a package through the object request agent (object request broker) architecture defined. (B) requests sent directly to a source server, it is possible to satisfy the request through its own cache. Request (c) is sent to a proxy, it can be directly access WAIS (one kind of information with the Web services architecture separated), and the response format translation WAIS a universal connector interface can be identified. Each component only know their own interaction with the client or server connectors; the whole process topology is a product of our view.

通过比较图2和图3,读者不难发现这两张图中的架构是高度一致的。对于HTTP/1.1协议为何要设计成这个样子,读者想必已经有所领悟。

在论文的第六章中,Fielding对于到2000年为止在Web基础架构协议的设计和开发方面的一些经验教训进行了深入的分析。其中,“HTTP不是RPC”、“HTTP不是一种传输协议”两部分值得读者反复阅读。时至13年之后的今日,对于HTTP协议的误解仍然广泛存在。

以上简要介绍了Fielding博士论文中的内容。为了帮助读者仔细阅读Fielding的博士论文,笔者整理了一套Fielding博士论文的导读,将在本专栏后续文章中载出。

REST详解

REST究竟是什么?因为REST的内涵非常丰富,所以很难用一两句话解释清楚这个问题。

首先,REST是Web自身的架构风格。REST也是Web之所以取得成功的技术架构方面因素的总结。REST是世界上最成功的分布式应用架构风格(成功案例:Web,还不够吗?)。它是为 运行在互联网环境 的 分布式 超媒体系统量身定制的。互联网环境与企业内网环境有非常大的差别,最主要的差别是两个方面:

  • 可伸缩性需求无法控制:并发访问量可能会暴涨,也可能会暴跌。

  • 安全性需求无法控制:无法控制客户端发来的请求的格式,很可能会是恶意的请求。

而所谓的“超媒体系统”,即,使用了超文本的系统。可以把“超媒体”理解为超文本+媒体内容。

REST是HTTP/1.1协议等Web规范的设计指导原则,HTTP/1.1协议正是为实现REST风格的架构而设计的。新的Web规范,其设计必须符合REST的要求,否则整个Web的体系架构会因为引入严重矛盾而崩溃。这句话不是危言耸听,做个类比,假如苏州市政府同意在市区著名园林的附近大型土木,建造大量具有后现代风格的摩天大楼,那么不久之后世界闻名的苏州园林美景将不复存在。

上述这些关于“REST是什么”的描述,可以总结为一句话:REST是所有Web应用都应该遵守的架构设计指导原则。当然,REST并不是法律,违反了REST的指导原则,仍然能够实现应用的功能。但是违反了REST的指导原则,会付出很多代价,特别是对于大流量的网站而言。

要深入理解REST,需要理解REST的五个关键词:

  1. 资源(Resource)
  2. 资源的表述(Representation)
  3. 状态转移(State Transfer)
  4. 统一接口(Uniform Interface)
  5. 超文本驱动(Hypertext Driven)

什么是资源?

资源是一种看待服务器的方式,即,将服务器看作是由很多离散的资源组成。每个资源是服务器上一个可命名的抽象概念。因为资源是一个抽象的概念,所以它不仅仅能代表服务器文件系统中的一个文件、数据库中的一张表等等具体的东西,可以将资源设计的要多抽象有多抽象,只要想象力允许而且客户端应用开发者能够理解。与面向对象设计类似,资源是以名词为核心来组织的,首先关注的是名词。一个资源可以由一个或多个URI来标识。URI既是资源的名称,也是资源在Web上的地址。对某个资源感兴趣的客户端应用,可以通过资源的URI与其进行交互。

什么是资源的表述?

资源的表述是一段对于资源在某个特定时刻的状态的描述。可以在客户端-服务器端之间转移(交换)。资源的表述可以有多种格式,例如HTML/XML/JSON/纯文本/图片/视频/音频等等。资源的表述格式可以通过协商机制来确定。请求-响应方向的表述通常使用不同的格式。

什么是状态转移?

状态转移(state transfer)与状态机中的状态迁移(state transition)的含义是不同的。状态转移说的是:在客户端和服务器端之间转移(transfer)代表资源状态的表述。通过转移和操作资源的表述,来间接实现操作资源的目的。

什么是统一接口?

REST要求,必须通过统一的接口来对资源执行各种操作。对于每个资源只能执行一组有限的操作。以HTTP/1.1协议为例,HTTP/1.1协议定义了一个操作资源的统一接口,主要包括以下内容:

  • 7个HTTP方法:GET/POST/PUT/DELETE/PATCH/HEAD/OPTIONS

  • HTTP头信息(可自定义)

  • HTTP响应状态代码(可自定义)

  • 一套标准的内容协商机制

  • 一套标准的缓存机制

  • 一套标准的客户端身份认证机制

REST还要求,对于资源执行的操作,其操作语义必须由HTTP消息体之前的部分完全表达,不能将操作语义封装在HTTP消息体内部。这样做是为了提高交互的可见性,以便于通信链的中间组件实现缓存、安全审计等等功能。

什么是超文本驱动?

“超文本驱动”又名“将超媒体作为应用状态的引擎”(Hypermedia As The Engine Of Application State,来自Fielding博士论文中的一句话,缩写为HATEOAS)。将Web应用看作是一个由很多状态(应用状态)组成的有限状态机。资源之间通过超链接相互关联,超链接既代表资源之间的关系,也代表可执行的状态迁移。在超媒体之中不仅仅包含数据,还包含了状态迁移的语义。以超媒体作为引擎,驱动Web应用的状态迁移。通过超媒体暴露出服务器所提供的资源,服务器提供了哪些资源是在运行时通过解析超媒体发现的,而不是事先定义的。从面向服务的角度看,超媒体定义了服务器所提供服务的协议。客户端应该依赖的是超媒体的状态迁移语义,而不应该对于是否存在某个URI或URI的某种特殊构造方式作出假设。一切都有可能变化,只有超媒体的状态迁移语义能够长期保持稳定。

一旦读者理解了上述REST的五个关键词,就很容易理解REST风格的架构所具有的6个的主要特征:

  • 面向资源(Resource Oriented)

  • 可寻址(Addressability)

  • 连通性(Connectedness)

  • 无状态(Statelessness)

  • 统一接口(Uniform Interface)

  • 超文本驱动(Hypertext Driven)

这6个特征是REST架构设计优秀程度的判断标准。其中,面向资源是REST最明显的特征,即,REST架构设计是以资源抽象为核心展开的。可寻址说的是:每一个资源在Web之上都有自己的地址。连通性说的是:应该尽量避免设计孤立的资源,除了设计资源本身,还需要设计资源之间的关联关系,并且通过超链接将资源关联起来。无状态、统一接口是REST的两种架构约束,超文本驱动是REST的一个关键词,在前面都已经解释过,就不再赘述了。

从架构风格的抽象高度来看,常见的分布式应用架构风格有三种:

  • 分布式对象(Distributed Objects,简称DO)

架构实例有CORBA/RMI/EJB/DCOM/.NET Remoting等等

  • 远程过程调用(Remote Procedure Call,简称RPC)

架构实例有SOAP/XML-RPC/Hessian/Flash AMF/DWR等等

  • 表述性状态转移(Representational State Transfer,简称REST)

架构实例有HTTP/WebDAV

DO和RPC这两种架构风格在企业应用中非常普遍,而REST则是Web应用的架构风格,它们之间有非常大的差别。

REST与DO的差别在于:

  • REST支持抽象(即建模)的工具是资源,DO支持抽象的工具是对象。在不同的编程语言中,对象的定义有很大差别,所以DO风格的架构通常都是与某种编程语言绑定的。跨语言交互即使能实现,实现起来也会非常复杂。而REST中的资源,则完全中立于开发平台和编程语言,可以使用任何编程语言来实现。

  • DO中没有统一接口的概念。不同的API,接口设计风格可以完全不同。DO也不支持操作语义对于中间组件的可见性。

  • DO中没有使用超文本,响应的内容中只包含对象本身。REST使用了超文本,可以实现更大粒度的交互,交互的效率比DO更高。

  • REST支持数据流和管道,DO不支持数据流和管道。

  • DO风格通常会带来客户端与服务器端的紧耦合。在三种架构风格之中,DO风格的耦合度是最大的,而REST的风格耦合度是最小的。REST松耦合的源泉来自于统一接口+超文本驱动。

REST与RPC的差别在于:

  • REST支持抽象的工具是资源,RPC支持抽象的工具是过程。REST风格的架构建模是以名词为核心的,RPC风格的架构建模是以动词为核心的。简单类比一下,REST是面向对象编程,RPC则是面向过程编程。

  • RPC中没有统一接口的概念。不同的API,接口设计风格可以完全不同。RPC也不支持操作语义对于中间组件的可见性。

  • RPC中没有使用超文本,响应的内容中只包含消息本身。REST使用了超文本,可以实现更大粒度的交互,交互的效率比RPC更高。

  • REST支持数据流和管道,RPC不支持数据流和管道。

  • 因为使用了平台中立的消息,RPC风格的耦合度比DO风格要小一些,但是RPC风格也常常会带来客户端与服务器端的紧耦合。支持统一接口+超文本驱动的REST风格,可以达到最小的耦合度。

比较了三种架构风格之间的差别之后,从面向实用的角度来看,REST架构风格可以为Web开发者带来三方面的利益:

  • 简单性

采用REST架构风格,对于开发、测试、运维人员来说,都会更简单。可以充分利用大量HTTP服务器端和客户端开发库、Web功能测试/性能测试工具、HTTP缓存、HTTP代理服务器、防火墙。这些开发库和基础设施早已成为了日常用品,不需要什么火箭科技(例如神奇昂贵的应用服务器、中间件)就能解决大多数可伸缩性方面的问题。

  • 可伸缩性

充分利用好通信链各个位置的HTTP缓存组件,可以带来更好的可伸缩性。其实很多时候,在Web前端做性能优化,产生的效果不亚于仅仅在服务器端做性能优化,但是HTTP协议层面的缓存常常被一些资深的架构师完全忽略掉。

  • 松耦合

统一接口+超文本驱动,带来了最大限度的松耦合。允许服务器端和客户端程序在很大范围内,相对独立地进化。对于设计面向企业内网的API来说,松耦合并不是一个很重要的设计关注点。但是对于设计面向互联网的API来说,松耦合变成了一个必选项,不仅在设计时应该关注,而且应该放在最优先位置。

有的读者可能会问:“你说了这么多,REST难道就没有任何缺点了吗?”当然不是,正如Fielding在博士论文中阐述的那样,评价一种软件架构的优劣,不能脱离开软件的具体运行环境。永远不存在适用于任何运行环境的、包治百病的银弹式架构。笔者在前面强调过REST是一种为运行在互联网环境中的Web应用量身定制的架构风格。REST在互联网这个运行环境之中已经占据了统治地位,然而,在企业内网运行环境之中,REST还会面临DO、RPC的巨大挑战。特别是一些对实时性要求很高的应用,REST的表现不如DO和RPC。所以需要针对具体的运行环境来具体问题具体分析。但是,REST可以带来的上述三方面的利益即使在开发企业应用时,仍然是非常有价值的。所以REST在企业应用开发,特别是在SOA架构的开发中,已经得到了越来越大的重视。本专栏将有一篇文章专门介绍REST在企业级应用中与SOA的结合。

到了这里,“REST究竟是什么”这个问题笔者就解答完了。本文开头那些说法是否正确,笔者还是笑而不语,读者此时应该已经有了自己的判断。在接下来的REST系列文章中,我将会为读者澄清一些关于HTTP协议和REST的常见误解。

参考资料:

Roy Fielding博士论文英文版

Roy Fielding博士论文中文版

HTTP/1.1协议RFC2616RFC2617

 

文章转载地址:https://www.infoq.cn/article/understanding-restful-style/,如引用请标注文章原地址

Guess you like

Origin www.cnblogs.com/lpob/p/11595148.html