首先明白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實踐指南》