首先明白SOA和Web Service的关系:html
* SOA面向服务架构,用于大型分布式系统的一个概念;java
* Web Service是实现SOA的方式之一,不是全部的SOA都是基于Web service的;web
* 但Webservice确实为最主流的SOA实现方式,有的人甚至把SOA等同于Webservice。不能否认,正是Webservice的成功才造就了SOA这个概念的成功;网络
Webservice有三个基础标准:架构
1.WSDL: Web服务定义语言(Web Service Definition Language),用来定义服务接口。实际上,它能描述服务的两个不一样方面:服务的签名(名字和参数),以及服务的绑定和部署细节(协议和位置)。oracle
2.SOAP:简单对象访问协议(Simple Object Access Protocol),是定义Webservice的协议。HTTP是一个网络数据交互的底层协议,而SOAP是Web Service数据交换的专用协议。app
3.UDDI:通用描述、发现与集成服务(Universal Description, Discovery and Integration),UDDI 是一种目录服务,企业可使用它对 Web services 进行注册和搜索。框架
SOAP是协议,就像HTTP协议同样,通常框架都已经集成;分布式
UDDI扮演者补充的角色,非必须,并且一般在实践中也不用。spa
WSDL是开发人员打交道最多的东西,也算是Webservice的核心了。
WSDL如今主要有两个版本,1.1和2.0,两个版本标示大致结构类似,略有不一样。(WSDL1.1版本根节点为definitions,2.0版本根节点为description)
WSDL Example
WSDL一般是框架来生成的,并非手工写的,好比Java可使用wsgen 生成webservice,.Net框架也有本身方法,均可以经过自身的框架把接口发布称WSDL文件。
一个WSDL的简单示例。这个WSDL文件定义了一个被称为CustomerService的服务,该服务提供了一个被称为getCustomerAdress()的操做。这个操做的输入参数为一个类型为long的客户ID,输出为一个包含3个string属性-街道、城市和邮编的结构。(示例来自于《SOA实践指南》)
<?xml version="1.0" encoding="utf-8" ?> <definitions name="CustomerService" targetNamespace="http://soa-in-practice.com/wsdl" xmlns:tns="http://soa-in-practice.com/wsdl" xmlns:xsd1="http://soa-in-practice.com/xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns="http://schemas.xmlsoap.org/wsdl/"> <types> <xsd:schema targetNamespace="http://soa-in-practice.com/xsd" xmlns="http://soa-in-practice.com/xsd"> <xsd:element name="getCustomerAddress"> <xsd:complexType> <xsd:sequence> <xsd:element name="customerID" type="xsd:long"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="getCustomerAddressResponse" type="Address"/> <xsd:complexType name="Address"> <xsd:sequence> <xsd:element name="street" type="xsd:string"/> <xsd:element name="city" type="xsd:string"/> <xsd:element name="zipCode" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:schema> </types> <message name="getCustomerAddressInput"> <part name="params" element="xsd1:getCustomerAddress"/> </message> <message name="getCustomerAddressOutput"> <part name="params" element="xsd1:getCustomerAddressResponse"/> </message> <portType name="CustomerInterface" > <operation name="getCustomerAddress"> <input message="tns:getCustomerAddressInput" /> <output message="tns:getCustomerAddressOutput" /> </operation> </portType> <binding name="CustomerSOAPBinding" type="tns:CustomerInterface" > <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> <operation name="getCustomerAddress"> <soap:operation soapAction="http://soa-in-practice.com/getCustomerAddress" /> <input> <soap:body use="literal" /> </input> <output> <soap:body use="literal" /> </output> </operation> </binding> <service name="CustomerService" > <port name="CustomerPort" binding="tns:CustomerSOAPBinding"> <soap:address location="http://soa-in-practice.com/customer11"/> </port> </service> </definitions>
WSDL文件的解读
阅读一个WSDL,须要从下往上看:
最后的<service>节点定义了这个服务的名称为CustomerService,而且该服务能够在http://soa-in-practice.com/customer11找到。
<service name="CustomerService" > <port name="CustomerPort" binding="tns:CustomerSOAPBinding"> <soap:address location="http://soa-in-practice.com/customer11"/> </port> </service>
<binding>节点定义了用来提供Webservice的协议和格式。CustomerSOABiding是Binding的名称,并指出Binding要从哪一个接口开始(这里是从CustomerInterface这个接口开始)
<binding name="CustomerSOAPBinding" type="tns:CustomerInterface" > <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> <operation name="getCustomerAddress"> <soap:operation soapAction="http://soa-in-practice.com/getCustomerAddress" /> <input> <soap:body use="literal" /> </input> <output> <soap:body use="literal" /> </output> </operation> </binding>
<portType>描述了CustomerInterface这个接口,其中接口包含一个叫getCustomerAddress的Operation。在Operation下边,getCustomerAddressInput和getCustomerAddressOutput是这个Operation的输入消息和输出消息。
<portType name="CustomerInterface" > <operation name="getCustomerAddress"> <input message="tns:getCustomerAddressInput" /> <output message="tns:getCustomerAddressOutput" /> </operation> </portType>
<message>节点定义了各个消息,使用的是<portType>节点引用的标识符。
<message name="getCustomerAddressInput"> <part name="params" element="xsd1:getCustomerAddress"/> </message> <message name="getCustomerAddressOutput"> <part name="params" element="xsd1:getCustomerAddressResponse"/> </message>
<type>节点定义了将会使用到的数据类型:输入参数customerID的类型为long,输出参数address的类型是有3个字符串属性的结构/记录。全部类型在本身的命名空间xsd1中。
<types> <xsd:schema targetNamespace="http://soa-in-practice.com/xsd" xmlns="http://soa-in-practice.com/xsd"> <xsd:element name="getCustomerAddress"> <xsd:complexType> <xsd:sequence> <xsd:element name="customerID" type="xsd:long"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="getCustomerAddressResponse" type="Address"/> <xsd:complexType name="Address"> <xsd:sequence> <xsd:element name="street" type="xsd:string"/> <xsd:element name="city" type="xsd:string"/> <xsd:element name="zipCode" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:schema> </types>
SOAP (Simple Object Access Protocol)是一个消息框架,这个消息框架是基于XML协议的,从下图可以看到,SOAP的框架很是像HTTP协议,都包含的消息的Header和消息的Body,只不过SOAP是Web Service数据交换的专用协议。SOAP是HTTP的上层协议,最终仍是经过HTTP来传输数据。
SOAP Reqeust Example
<?xml version='1.0' ?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Header> ... </soap:Header> <soap:Body> <getCustomerAddress xmlns="http://soa-in-practice.com/xsd"> <customerID>12345678</customerID> </getCustomerAddress > </soap:Body> </soap:Envelope>
SOAP Response Example
<?xml version='1.0' ?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Header> ... </soap:Header> <soap:Body> <getCustomerAddressResponse xmlns="http://soa-in-practice.com/xsd"> <address> <street>Gaussstr. 29</street> <city>Braunschweig</city> <zipCode>D-38106</zipCode> </address> </getCustomerAddressResponse> </soap:Body> </soap:Envelope>
SOAP消息的根元素为<Envelope>
从上边能够看出SOAP是基于XML的,除了组织结构,其余很是相似于HTTP的Request和Response。
HTTP Request Example
GET /path/file.html HTTP/1.0 From: someuser@jmarshall.com User-Agent: HTTPTool/1.0 [blank line here]
HTTP Response Example
HTTP/1.0 200 OK Date: Fri, 31 Dec 1999 23:59:59 GMT Content-Type: text/html Content-Length: 1354 <html> <body> <h1>Happy New Millennium!</h1> (more file contents) . . . </body> </html>
参考资料:
《SOA实践指南》