为什么我们需要 RESTful Web 服务? [英] Why do we need RESTful Web Services?

查看:30
本文介绍了为什么我们需要 RESTful Web 服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将学习 RESTful Web 服务(最好说我必须这样做,因为它是 CS 硕士学位课程的一部分).

I'm going to learn RESTful web services (it's better to say that I'll have to do this because it's a part of CS master degree program).

我在 Wikipedia 上阅读了一些信息,我也在 Sun Developer Network 上阅读了一篇关于 REST 的文章,我发现这不是一项简单的技术,有用于构建 RESTful 应用程序的特殊框架,并且经常将其与 SOAP 进行比较Web 服务和程序员应该了解何时使用 SOAP 以及何时使用 REST 是不错的方法.

I've read some info in Wikipedia and I've also read an article about REST at Sun Developer Network and I see that it's not easy technology, there are special frameworks for building RESTful apps, and it's often being compared to SOAP web services and programmer should understand when to use SOAP and when REST could be nice approach.

我记得几年前 SOAP 非常流行(时尚?),并且每个好的简历中都必须出现SOAP"项目.但在实践中,它很少使用,并且用于实现非常简单的目的.

I remember that several years ago SOAP was very popular (fashionable?) and item 'SOAP' had to be present in every good CV. But in practice it was used very rarely and for achieving very simple purposes.

在我看来,REST 是另一个时尚的最后一句话"(或者我可能完全错了,因为我从未在实践中见过 REST).

It seems to me that REST is another 'last word of fashion' (or I can be totally wrong because I haven't ever seen REST in practice).

你能给我举一些例子,说明应该使用 REST,为什么我们不能在没有 REST 的情况下做同样的事情(或者为什么我们应该花更多的时间在没有 REST 的情况下做同样的事情)?

Can you give me some examples were REST should be used and why we can't do the same without REST (or why we should spend much more time to do the same without REST)?

UPD:不幸的是,我在第一条评论中看不到任何可以让我大吃一惊的具体论点.让我觉得 REST 是很棒的技术!

UPD: Unfortunatelly I can't see any concrete arguments which can blow my mind in first comments. Make me think that REST is awesome technology!

我希望看到这样的答案:

I'd like to see answers like this:

我正在开发另一个复合体HelloWorld 应用程序,我们需要传输大量/微小的数据和我向我的同事提出的 REST 解决方案:

I was developing another complex HelloWorld application and we need to transfer lots of / tiny data and I proposed REST solution to my workmate:

–哦,该死的!强尼,我们应该当然使用 REST 来实现这个应用程序!
–是的,比利,我们可以使用 REST,但我们最好使用肥皂.相信我,因为我知道一些事情关于开发HelloWorld应用程序.
–但是 SOAP 是过去的老式技术世纪,我们可以更好地使用一.
–比利,你准备好了吗花了 3 天时间进行实验休息?我们可以在 2 中用 SOAP 做到这一点小时..
–是,我确定我们将花更多的时间来达到相同的安全性/性能//可扩展性/SOAP 的任何其他内容.我确定 HelloWorld 应用程序应该只用 REST 开发从现在开始.

– Oh, damn! Jonny, we should certainly use REST for implementing this app!
– Yes, Billy, we can use REST, but we would better use SOAP. Trust me 'cause I know something about developing HelloWorld applications.
– But SOAP is old-fashioned technology from the last century and we can use better one.
– Billy, are you ready to spent 3 days for experimenting with REST? We can do this with SOAP in 2 hours..
– Yes, I'm sure that we'll spent even more time to achieve the same security/performance/ /scalability/whatever else with SOAP. I'm sure that HelloWorld applications should be developed only with REST from now.

推荐答案

如果在分布式应用程序中最小化客户端和服务器组件之间的耦合非常重要,则应该使用 REST.

REST should be used if it is very important for you to minimize the coupling between client and server components in a distributed application.

如果您的服务器将被您无法控制的许多不同的客户端使用,则可能就是这种情况.如果您希望能够定期更新服务器而无需更新客户端软件,也可能是这种情况.

This may be the case if your server is going to be used by many different clients that you do not have control over. It may also be the case if you want to be able to update the server regularly without needing to update the client software.

我可以向您保证,实现这种低耦合度并不容易.遵循 REST 的所有约束以取得成功至关重要.保持纯粹的无状态连接是困难的.选择正确的媒体类型并将数据压缩到格式中是很棘手的.创建自己的媒体类型可能更难.

I can assure you that achieving this low level of coupling is not easy to do. It is critical to follow all of the constraints of REST to succeed. Maintaining a purely stateless connection is difficult. Picking the right media-types and squeezing your data into the formats is tricky. Creating your own media types can be even harder.

与相对简单的 RPC 方法相比,将丰富的服务器行为适应统一的 HTTP 接口可能会令人困惑,有时显得迂腐.

Adapting rich server behaviour into the uniform HTTP interface can be confusing and at times appears pedantic in comparison to the relatively straightforward RPC approach.

尽管存在困难,但好处是您拥有一项服务,由于一致使用 HTTP 协议,因此客户端开发人员应该能够轻松理解该服务.服务应该由于超媒体而容易被发现,并且客户端应该非常适应服务器上的变化.

Despite the difficulties, the benefits are that you have a service that a client developer should be able to easily understand due to the consistent use of the HTTP protocol. The service should be easily discoverable due to hypermedia and the client should be extremely resilient to changes on the server.

超媒体的好处和避免会话状态使负载平衡变得简单并且服务分区可行.对 HTTP 规则的严格遵守使得调试器和缓存代理等工具的可用性变得非常棒.

The benefits of hypermedia and the avoidance of session state makes load balancing simple and service partitioning feasible. The strict conformance to HTTP rules make the availability of tools like debuggers and caching proxies wonderful thing.

更新

在我看来,REST 是另一个时尚的最后一句话"(或者我可以完全错了,因为我从来没有在实践中见过 REST).

It seems to me that REST is another 'last word of fashion' (or I can be totally wrong because I haven't ever seen REST in practice).

我认为 REST 已经变得流行,因为尝试进行 SOA 类型项目的人们发现使用 SOAP 堆栈他们没有意识到所承诺的好处.人们不断将网络作为简单集成方法的一个例子.不幸的是,我认为人们低估了创建网络所需的计划和远见,他们过度简化了需要做的事情,以实现网络上确实发生的那种偶然的重用.

I think REST has become fashionable because people attempting to do SOA type projects have found that using the SOAP stack they are not realizing the benefits that were promised. People keep turning back to the web as an example of simple integration methodologies. Unfortunately, I think people underestimate the amount of planning and foresight that went into creating the web and they oversimplify what needs to be done to allow the kind of serendipitous reuse that does occur on the web.

您说您在实践中从未见过 REST,但如果您曾经使用过网络浏览器,那这不可能是真的.Web 浏览器是 REST 客户端.

You say that you have never seen REST in practice, but that cannot possibly be true if you ever use a web browser. The web browser is a REST client.

  • 为什么不需要做浏览器当有人更改某些 html 时更新在网站上?
  • 为什么我可以添加一套完整的新页面到网站和客户端"仍然可以访问那些新页面没有更新?
  • 为什么我不需要提供服务描述语言"到网络浏览器告诉它什么时候去http://example.org/images/cat返回类型将是 jpeg 图像当你去http://example.org/description/cat返回类型将是 text/html?
  • 为什么我可以使用网络浏览器访问不存在的网站浏览器发布了?怎么可能客户知道这些网站吗?

这些问题听起来可能很愚蠢,但是如果您知道答案,那么您就可以开始了解 REST 的全部内容了.查看 StackOverflow 以了解 REST 的更多好处.当我查看问题时,我可以将该页面添加为书签或将 URL 发送给朋友,他可以看到相同的信息.他无需浏览网站即可找到该问题.

These may sound like inane questions, but if you know the answer, then you can start to see what REST is all about. Look at StackOverflow for more benefits of REST. When I am looking at a question, I can bookmark that page or send the url to a friend and he can see the same information. He doesn't have to navigate through the site to find that question.

StackOverflow 使用各种 OpenId 服务进行身份验证,gravatar.com 用于头像图像,google-analytics 和 Quantserve 用于分析信息.这种多公司集成是SOAP 世界梦寐以求的事情.最好的例子之一是用于驱动 StackOverflow UI 的 jQuery 库是从 Google 的内容交付网络检索的.SO 可以指示客户端(即您的 Web 浏览器)从第三方站点下载代码以提高性能这一事实证明了 Web 客户端和服务器之间的低耦合.

StackOverflow uses a variety of OpenId services for authentication, gravatar.com for avatar images, google-analytics and Quantserve for analytical information. This kind of multi-company integration is the type of thing the SOAP world only dreams of. One of the best examples is the fact that the jQuery libraries that are used to drive the StackOverflow UI are retrieved from Google's Content Delivery Network. The fact that SO could direct the client (i.e. your web browser) to download code from a third-party site to improve performance is testament to the low coupling between web client and server.

这些是工作中的 REST 架构的示例.

These are examples of a REST architecture at work.

现在一些网站/应用程序确实违反了 REST 规则,然后浏览器无法按预期工作.

Now some web sites / applications do break the rules of REST and then the browser does not work as expected.

  • 臭名昭著的后退按钮问题是由使用服务器端引起的会话状态.
  • 负载平衡可能会成为一种痛苦您有服务器端会话状态.
  • Flash 应用程序通常会阻止URL 来自专门标识一个代表.
  • 另一个破坏网络的问题浏览器不符合媒体类型标准.我们听到所有关于 IE6 需要怎样的时间被杀.问题是没有正确遵守标准,或因任何原因被忽略.
  • 登录会话的使用是许多安全漏洞的来源.
  • The infamous back button problem is caused by using server side session state.
  • Load balancing can become a pain when you have server side session state.
  • Flash applications often prevent the URL from specifically identifying a representation.
  • The other problem that breaks web browsers is poor conformance to media-type standards. We hear all of the time about how IE6 needs to be killed. The problem there is that standards were not properly followed, or were ignored for whatever reason.
  • The use of login sessions are the source of many security holes.

REST 无处不在.它是网络的一部分,使其运行良好.如果您想构建可以像 Web 一样扩展的分布式应用程序,像 Web 一样灵活地进行更改并像 Web 那样促进重复使用,那么请遵循他们在构建 Web 浏览器时所做的相同规则.

REST is everywhere. It is the part of the web that makes it work well. If you want to build distributed applications that can scale like the web, be resilient to change like the web and promote re-use as the web has done, then follow the same rules they did when building web browsers.

这篇关于为什么我们需要 RESTful Web 服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆