restful什么意思
This article was originally published on codurance.com.
本文最初在codurance.com上发布 。
Last month I attended Fast Track to RESTful Microservices training at Skillsmatter. During the course, we explored what REST APIs can offer to web applications in general and microservices communication in particular. Personally, the most important outcome of the course is a better understanding of what REST really means and what are its pros and cons.
上个月,我在Skillsmatter参加了RESTful微服务的快速通道培训 。 在课程中,我们探讨了REST API可以为一般的Web应用程序以及特别是微服务通信提供什么。 就个人而言,该课程的最重要成果是更好地了解REST的真正含义以及它的优缺点。
During most of my career, I’ve been focused on mobile technologies, therefore, being on the consuming side of web APIs. Most APIs that I’ve ever consumed were supposed to be RESTful, now that I better understand what RESTful means I can say that 99% of them weren’t even close to RESTful (Clients are broken because of something being added in the server side sounds familiar?).
在我的职业生涯的大部分时间里,我一直专注于移动技术,因此一直在使用Web API。 我曾经使用过的大多数API都应该是RESTful的,因为现在我更好地了解了RESTful的意思,我可以说其中99%的距离甚至都不是RESTful(由于服务器端添加了某些东西,导致客户端崩溃听起来很熟悉?)。
定义 (Definition)
The term REST stands for “REpresentational State Transfer”. A possible formal definition of it could be as follows.
REST一词代表“代表状态转移”。 可能的正式定义如下。
Architectural API style based on transferring a representation of state (documents) between client and server in order to progress the state of an application.
基于API的体系结构API样式,它在客户端和服务器之间传输状态表示(文档),以提高应用程序的状态。
约束条件 (Constraints)
To consider applications as RESTful, applications need to conform to the following REST constraints. Complying with the constraints enables a distributed hypermedia system to have the following desirable non-functional properties: performance, scalability, simplicity, extensibility, visibility, portability and reliability.
要将应用程序视为RESTful,应用程序需要符合以下REST约束。 遵守约束条件可使分布式超媒体系统具有以下理想的非功能属性:性能,可伸缩性,简单性,可扩展性,可见性,可移植性和可靠性。
- Client-server 客户端服务器
A client-server model favours separation of concerns so that clients are not concerned with data storage. Thus, clients code portability is improved. On the other hand, the server is not concerned about user interface or user state, so that server can be simpler and more scalable. Servers and clients can be developed independently, as long as they conform to the defined contract.
客户端-服务器模型支持关注点分离,以便客户端不关心数据存储。 因此,提高了客户端代码的可移植性 。 另一方面,服务器不关心用户界面或用户状态,因此服务器可以更简单 ,更可扩展 。 服务器和客户端只要符合定义的合同,就可以独立开发。
- Stateless 无状态
Client context is never stored on the server between requests. Each request has to contain all the necessary information. A stateless server improves scalability by allowing the server to quickly free resources and simplifies implementation. Reliability eases recovering from partial failures. Visibility, monitoring system does not have to look beyond a single request to determine the nature of the request.
两次请求之间,客户端上下文永远不会存储在服务器上。 每个请求必须包含所有必要的信息。 无状态服务器通过允许服务器快速释放资源并简化实现来提高可伸缩性 。 可靠性有助于从部分故障中恢复。 可见性 ,监视系统不必查看单个请求即可确定请求的性质。
One of the drawbacks of having a stateless server is decreased network performance as all the needed data has to be sent in each request.
具有无状态服务器的缺点之一是降低了网络性能,因为必须在每个请求中发送所有需要的数据。
- Cacheable 可缓存的
REST applications are web systems; therefore, clients and intermediaries can cache responses. Responses themselves must be defined as cacheable, or not, to prevent clients from reusing stale data that could reduce reliability. If stale data in the cache differs significantly from the data that would have been generated the request been handled by the server. Caching could eliminate some client-server interaction, thus improving scalability, efficiency and user-perceived performance by reducing average latency.
REST应用程序是Web系统。 因此,客户端和中介可以缓存响应。 响应本身必须定义为可缓存或不可缓存,以防止客户端重用可能降低可靠性的陈旧数据。 如果缓存中的陈旧数据与本应生成的数据有显着差异,则请求已由服务器处理。 缓存可以消除一些客户端-服务器交互,从而通过减少平均延迟来提高可伸缩性 , 效率和用户感知的性能 。
- Uniform interface 统一的界面
Using a uniform interface simplifies and decouples the architecture and favours the independent evolution of different parts. As explained later on in this post, URIs, resources and hypermedia help to produce a standard interface that improves visibility of interactions, simplifies the overall system architecture and encourage independent evolution. The trade-off is that it degrades efficiency since information is transferred in a standard format rather one which is particular to an application’s needs.
使用统一的接口可以简化和解耦体系结构,并有利于不同部分的独立发展。 如本文后面的解释,URI,资源和超媒体有助于产生一个标准接口,该接口可提高交互的可见性, 简化整个系统架构并鼓励独立的发展。 折衷是因为信息是以标准格式而不是特定于应用程序需求的格式传输的,因此降低了效率 。
- Layered system 分层系统
Using a layered system reduces complexity by constraining component behaviour such that each element cannot access beyond its immediate layer. Favours substrate independence by restricting knowledge of other parts of the system. Layers can encapsulate legacy components and protect new services from legacy clients. Intermediaries can be used to improved scalability by enabling load balancing across networks. The main trade-off is that layered system add overhead and latency to the data processing, therefore, reducing user-perceived performance.
使用分层系统可通过限制组件的行为来降低复杂性,以使每个元素都无法访问其直接上层。 通过限制系统其他部分的知识来支持衬底独立性。 层可以封装旧组件,并保护新服务不受旧客户端的攻击。 通过启用跨网络的负载平衡,可以使用中介来提高可伸缩性 。 主要的折衷是分层系统增加了数据处理的开销和延迟,因此降低了用户感知的性能 。
- Code-On-Demand (Optional) 按需编码(可选)
REST allows clients to extend their functionality by downloading and executing code scripts. This simplifies clients and improves extensibility. On the other hand, it reduces visibility, that’s why is only an optional constraint.
REST允许客户端通过下载和执行代码脚本来扩展其功能。 这简化了客户并提高了可扩展性 。 另一方面,它降低了可见性 ,这就是为什么这只是一个可选约束。
元素 (Elements)
REST have several elements in its toolbox to build stateless, scalable and simple web APIs.
REST在其工具箱中具有几个元素,可用于构建无状态,可扩展和简单的Web API。
- HTTP HTTP
- Resources 资源资源
- URIs URIs
- Hypermedia 超媒体
HTTP –文档传输应用协议 (HTTP – Document transfer application protocol)
REST is usually used along with HTTP as its transfer protocol as it offers several advantages. Among them are HTTP verbs, status codes and headers.
REST通常与HTTP一起用作其传输协议,因为它具有许多优点。 其中包括HTTP动词,状态代码和标头。
动词 (Verbs)
Instead of defining new verbs for every possible behaviour in our web service, HTTP introduces a standard set of verbs to handle similar situations in the same way, removing unnecessary variation and creating a more intuitive API. Each verb has a different combination of two properties that make them suitable for different scenarios.
HTTP并没有为我们的Web服务中的每种可能的行为定义新的动词,而是引入了一组标准动词来以相同方式处理类似情况,从而消除了不必要的变化并创建了更直观的API。 每个动词都有两个属性的不同组合,使它们适合于不同的场景。
- Idempotent 等幂的
The operation can be repeated in the event of failures.
发生故障时可以重复操作。
- Safe 安全
The operation has no side-effects for which the client is responsible.
该操作没有由客户负责的副作用。
得到 (GET)
Used to read state from the server. Being a safe operation, it can be executed multiple times without risk of data modification or corruption – calling it once has the same effect as calling it ten times. As an idempotent operation, making multiple identical requests has the same result as a single request.
用于从服务器读取状态。 作为安全操作,它可以执行多次,而没有数据修改或损坏的风险–一次调用与十次调用具有相同的效果。 作为幂等操作,发出多个相同的请求具有与单个请求相同的结果。
开机自检 (POST)
Usually used to create some state on the server. It’s neither safe nor idempotent. Therefore multiple requests will create several resources on the server. As a non-idempotent operation, POST should not be used for operations that deal with money, as in the case of a failed request is done multiple times, we would potentially be transferring money or paying multiple times.
通常用于在服务器上创建某些状态。 这既不安全也不是幂等的 。 因此,多个请求将在服务器上创建多个资源。 作为非幂等操作,POST不应用于处理金钱的操作,因为在多次失败请求的情况下,我们可能会转移金钱或支付多次。
放 (PUT)
It’s most used to update state on the server, although it can also be used to create state. It’s Idempotent but not safe as it changes the state of the server. Being an idempotent made PUT a good candidate, for instance, for operations related to money.
尽管它也可以用于创建状态,但它最常用于更新服务器上的状态。 它是幂等的,但不安全,因为它会更改服务器的状态。 作为幂等,PUT成为例如与金钱相关的业务的理想人选。
删除 (DELETE)
It’s used to delete state on the server. It’s idempotent but not safe as it removes state from the server. It’s idempotent as deleting state that has previously been deleted should not have further implications.
它用于删除服务器上的状态。 它是幂等的,但不安全,因为它会从服务器中删除状态。 这是幂等的,因为先前已删除的删除状态不应有其他含义。
响应状态码 (Response Status Codes)
The HTTP status codes provide metadata in the response to the state of the requested resources. They are part of what makes The Web a platform for building distributed systems. They are divided into the following categories:
HTTP状态代码在对请求资源状态的响应中提供元数据。 它们是使Web成为构建分布式系统的平台的一部分。 它们分为以下几类:
1xx
– Metadata1xx
–元数据2xx
– Everything is fine2xx
–一切都很好3xx
– Redirection3xx
–重定向4xx
– Client did something wrong4xx
–客户做错了什么5xx
– Server did something wrong5xx
–服务器做错了
标头 (Headers)
The HTTP headers are components to pass additional information in requests and responses. Headers consist of a case-insensitive name followed by a colon and its value. Headers could be grouped as:
HTTP标头是在请求和响应中传递其他信息的组件。 标题由不区分大小写的名称组成,后跟冒号及其值。 标头可以分为:
- General headers: Apply to both requests and responses but there is no relation to the data transmitted in the body. 常规标头:适用于请求和响应,但与正文中传输的数据无关。
- Request headers: Contain more information about the resource being fetched or about the client making the request. 请求标头:包含有关正在获取的资源或发出请求的客户端的更多信息。
- Response headers: Contain additional information about the response. 响应标题:包含有关响应的其他信息。
- Entity headers: Add information about the body of the entity, like content-length or MIME-type. 实体标头:添加有关实体正文的信息,例如content-length或MIME-type。
资源资源 (Resources)
A resource is anything exposed by the system that has an identity. Resources offer a way to adapt the application domain to web clients. An image, a spreadsheet, a service or a collection of other resources are some resource examples. The resources are fetched or send using a certain representation (XML, JSON, etc.).
资源是系统具有身份的任何公开内容。 资源提供了一种使应用程序域适应Web客户端的方法。 图像,电子表格,服务或其他资源的集合是一些资源示例。 使用某种表示形式(XML,JSON等)来获取或发送资源。
We deal with resources representations, not with the resources themselves, following the idea of “Pass-by-value”. Following the previous REST definition, resources represent the documents being transferred across the network to get work done. Resource state is a server-side concern, as it represents the domain state, clients only fetch or send resource representations to progress the application state. On the other hand, application state is a client-side concern as it represents the progress towards a particular application goal.
我们遵循“传递价值”的思想,而不是资源本身,而是资源的表示形式。 按照先前的REST定义,资源代表通过网络传输以完成工作的文档。 资源状态是服务器端的问题,因为它表示域状态,因此客户端仅获取或发送资源表示以进展应用程序状态。 另一方面,应用程序状态是客户端关注的问题,因为它表示实现特定应用程序目标的进度。
Resources should be named as nouns as they represent concepts in the domain of a particular system and are identified using URIs.
资源应命名为名词,因为它们表示特定系统领域中的概念并使用URI进行标识。
URI(统一资源标识符) (URIs (Uniform Resource Identifiers))
URIs differentiate one resource from another. To access and manipulate a resource, it needs to have at least one address. They are composed of a protocol
+ host
+ path
.
URI将一种资源与另一种资源区分开。 要访问和操作资源,它需要至少有一个地址。 它们由protocol
+ host
+ path
。
Clients should not be coupled to particular resources URIs as they can be changed at server’s discretion. This is where hypermedia has the greatest advantages, as it offers a way to decouple clients from specific URIs and add semantics to the application protocol.
客户端不应与特定资源URI耦合,因为可以根据服务器的判断来更改它们。 这是超媒体最大的优势,因为它提供了一种将客户端与特定URI分离并向应用程序协议添加语义的方法。
超媒体 (Hypermedia)
Hypermedia informs the client about what it can do next, through the use of hypermedia controls (links and forms) in the responses, and the particular URI to do it. The concrete hypermedia format for a particular application is defined in the application Media Type.
超媒体通过在响应中使用超媒体控件(链接和表单)以及执行该操作的特定URI来通知客户端下一步可以做什么。 在应用程序媒体类型中定义了特定应用程序的具体超媒体格式。
Hypermedia links are composed by a href
attribute that specifies the URI to access the linked resource and a rel
attribute that defines the meaning of the relationship, therefore, adding semantics to the state transitions in the application. The use of links allows the server to advertise new capabilities by including new links in the responses without breaking existing clients. As long as the server maintain previously defined links in the responses, clients would be able to follow them just as they were before the new state was added. Clients would need to be updated only if they need to access the new state. Another hypermedia advantage is that it introduces discoverability by providing a way of making a discoverable and self-documenting protocol.
超媒体链接由指定访问链接资源的URI的href
属性和定义关系含义的rel
属性组成,因此为应用程序的状态转换添加了语义。 链接的使用允许服务器通过在响应中包括新的链接来通告新功能,而不会破坏现有的客户端。 只要服务器在响应中维护先前定义的链接,客户端就可以像添加新状态之前一样跟踪它们。 客户端仅在需要访问新状态时才需要更新。 超媒体的另一个优点是,它通过提供一种制作可发现的自记录协议的方式来引入可发现性。
Clients start interacting with the app through a fixed URL, from thereon, all further actions happen by the client following the links, formatted using the media type, provided by the server in each response.
客户端开始通过固定的URL与应用进行交互,此后,客户端将按照链接(由媒体在每个响应中提供的媒体类型格式化)执行所有进一步的操作。
Media types and links define the contract between the application server and the client. The client interacts with the system by navigating the application state using the links. This is what HATEOAS (Hypermedia as the engine of application state) really means.
媒体类型和链接定义了应用程序服务器和客户端之间的合同。 客户端通过使用链接导航应用程序状态来与系统交互。 这就是HATEOAS (作为应用程序状态引擎的超媒体)的真正含义。
Hypermedia (in addition to the already defined elements) is what RESTful really means.
RESTful真正意味着超媒体(除了已经定义的元素之外)。
理查森成熟度模型 (Richardson Maturity Model)
This model has helped me a lot to understand what REST means and how to explain the properties of a web application. It divides the components of a REST system into three levels and provides a way to understand the ideas, concepts and the advantages of RESTful thinking. I’d say that it’s an educational model rather than an assessment mechanism.
这个模型对我了解REST的含义以及如何解释Web应用程序的属性有很大帮助。 它将REST系统的组件分为三个级别,并提供了一种理解RESTful思维的思想,概念和优点的方法。 我想说这是一种教育模式,而不是评估机制。
A detailed explanation of what Richardson Maturity Model is can be found at Martin Fowler’s blog.
有关Richardson成熟度模型的详细说明,请参见Martin Fowler的博客 。
翻译自: https://www.sitepoint.com/what-does-restful-really-mean/
restful什么意思