REST是更好的Web服务方法还是SOAP?或者它们是针对不同问题的不同工具?或者这是一个微妙的问题——也就是说,一个人在某些领域比另一个人稍微好一点,等等?
我尤其希望了解这些概念以及它们与php世界以及现代高端网络应用程序的关系。
REST是更好的Web服务方法还是SOAP?或者它们是针对不同问题的不同工具?或者这是一个微妙的问题——也就是说,一个人在某些领域比另一个人稍微好一点,等等?
我尤其希望了解这些概念以及它们与php世界以及现代高端网络应用程序的关系。
当前回答
不要忽视XML-RPC。如果您只是追求一个轻量级的解决方案,那么对于一个可以在几页文本中定义并在最少的代码中实现的协议来说,有很多事情要做。XML-RPC已经存在了多年,但已经过时了一段时间——但是极简主义的吸引力似乎使它在最近得到了某种复兴。
其他回答
快速了解一下2012年的问题:
REST在以下方面发挥了很好的作用:
Limited bandwidth and resources. Remember the return structure is really in any format (developer defined). Plus, any browser can be used because the REST approach uses the standard GET, PUT, POST, and DELETE verbs. Again, remember that REST can also use the XMLHttpRequest object that most modern browsers support today, which adds an extra bonus of AJAX. Totally stateless operations. If an operation needs to be continued, then REST is not the best approach and SOAP may fit it better. However, if you need stateless CRUD (Create, Read, Update, and Delete) operations, then REST is it. Caching situations. If the information can be cached because of the totally stateless operation of the REST approach, this is perfect.That covers a lot of solutions in the above three.
那么我为什么要考虑SOAP呢?同样,SOAP是相当成熟和定义良好的,并且带有完整的规范。REST方法就是这样一种方法,它对开发非常开放,所以如果你具备以下条件,那么SOAP就是一个很好的解决方案:
Asynchronous processing and invocation. If your application needs a guaranteed level of reliability and security then SOAP 1.2 offers additional standards to ensure this type of operation. Things like WSRM – WS-Reliable Messaging. Formal contracts. If both sides (provider and consumer) have to agree on the exchange format then SOAP 1.2 gives the rigid specifications for this type of interaction. Stateful operations. If the application needs contextual information and conversational state management then SOAP 1.2 has the additional specification in the WS* structure to support those things (Security, Transactions, Coordination, etc). Comparatively, the REST approach would make the developers build this custom plumbing.
http://www.infoq.com/articles/rest-soap-when-to-use-each
从工具的角度来看,SOAP很有用,因为工具很容易使用WSDL。因此,您可以用您喜欢的语言为您生成Web服务客户机。
REST在AJAX的网页上表现得很好。如果保持请求的简单性,就可以直接从JavaScript调用服务,这非常方便。尽量避免在响应XML中使用任何名称空间,我看到浏览器在使用这些名称空间时会窒息。因此,xsi:type可能不适合您,没有过于复杂的XML schema。
REST tends to have better performance as well. CPU requirements of the code generating REST responses tend to be lower than what SOAP frameworks exhibit. And, if you have your XML generation ducks lined up on the server side, you can effectively stream XML out to the client. So, imagine you're reading rows of database cursor. As you read a row, you format it as an XML element, and you write that directly out to the service consumer. This way, you don't have to collect all of the database rows in memory before starting to write your XML output - you read and write at the same time. Look into novel templating engines or XSLT to get the streaming to work for REST.
另一方面,SOAP往往是由工具生成的服务生成的一个大blob,然后才编写。请注意,这并不是绝对的事实,有很多方法可以从SOAP中获得流特性,比如使用附件。
我的决策过程如下:如果我想让我的服务易于被消费者使用,并且我编写的消息将是中等到小型的(10MB或更小),并且我不介意在服务器上消耗一些额外的CPU周期,那么我就使用SOAP。如果我需要在web浏览器上使用AJAX,或者我需要进行流式处理,或者我的响应非常大,我就使用REST。
最后,围绕SOAP建立了许多很棒的标准,如WS-Security和获得有状态的Web服务,如果使用正确的工具,可以将它们插入。这类东西真的很重要,可以帮助您满足一些棘手的要求。
当我在惠普工作时,我根据开发中的原始规范构建了第一批SOAP服务器之一,包括代码生成和WSDL生成。我不建议在任何事情上使用SOAP。
首字母缩写“SOAP”是一个谎言。它不是简单的,它不是面向对象的,它没有定义访问规则。可以说,它是一项议定书。这是Don Box有史以来最糟糕的规格,这是一个相当了不起的壮举,因为他是“COM”的创造者。
SOAP中没有什么有用的东西是REST不能用于传输的,JSON、XML甚至纯文本不能用于数据表示的。为了传输安全,可以使用https协议。对于认证,使用基本认证。对于会话,有cookie。REST版本将更简单、更清晰、运行更快、使用更少的带宽。
XML-RPC清楚地定义了请求、响应和错误协议,并且对于大多数语言都有很好的库。但是,对于许多任务来说,XML比您所需要的要重。
REST是Roy Fielding发明的一种架构,并在他的论文《架构风格和基于网络的软件架构设计》中进行了描述。Roy也是HTTP协议的主要作者,该协议定义了在万维网上的文档传输。HTTP是RESTful协议。当开发人员谈论“使用REST Web服务”时,可能更准确的说法是“使用HTTP”。
SOAP是一种基于xml的协议,它在HTTP请求/响应中进行隧道传输,因此即使您使用SOAP,您也在使用REST。关于SOAP是否为基本HTTP添加了任何重要的功能存在一些争论。
在创建Web服务之前,我建议学习HTTP。很可能您的需求可以使用规范中已经定义的功能实现,因此不需要其他协议。
我创建了一个基准,以找出它们中哪一个更快! 我看到了这样的结果:
对于1000个请求:
休息用时3秒 SOAP耗时7秒
对于10,000个请求:
休息用时33秒 SOAP用时69秒
对于1,000,000个请求:
休息用时62秒 SOAP耗时114秒