WebServices入門
2024-07-21 02:21:24
供稿:網友
[pre]webservices入門
——理論篇
瑞聯科技(中國)有限公司 李春林[/pre]
本文檔是用ctrl+c -> ctrl+v產生出來的入門級文檔,主要是想說明什么是webservices,webservices的架構,以及幾個相關的xml文檔。由于小弟知識及技術非常有限,文中不妥之處請各位兄臺指正,小弟在此謝過啦。
什么是webservices?
從表面上看:webservices 就是一個應用程序,它向外界暴露出一個能夠通過web進行調用的api。這就是說,你能夠用編程的方法通過web來調用這個應用程序。我們把調用這個webservices 的應用程序叫做客戶。
更專業的描述如下:webservices是描述一些操作(利用標準化的 xml 消息傳遞機制可以通過網絡訪問這些操作)的接口。webservices是用標準的、規范的 xml 概念描述的,稱為 webservices的服務描述。這一描述囊括了與服務交互需要的全部細節,包括消息格式(詳細描述操作)、傳輸協議和位置。該接口隱藏了實現服務的細節,允許獨立于實現服務基于的硬件或軟件平臺和編寫服務所用的編程語言使用服務。這允許并支持基于 webservices的應用程序成為松散耦合、面向組件和跨技術實現。webservices履行一項特定的任務或一組任務。webservices可以單獨或同其它 webservices一起用于實現復雜的聚集或商業交易,以及企業集成(eai)。
webservices模型
webservices體系結構基于三種角色(服務提供者、服務注冊中心和服務請求者)之間的交互。交互涉及發布、查找和綁定操作。這些角色和操作一起作用于 webservices構件:
webservices軟件模塊及其描述。在典型(并非 非典^_^)情況下,服務提供者托管可通過網絡訪問的軟件模塊(webservices的一個實現)。服務提供者定義 web 服務的服務描述并把它發布到服務請求者或服務注冊中心。服務請求者使用查找操作來從本地或服務注冊中心檢索服務描述,然后使用服務描述與服務提供者進行綁定并調用 web 服務實現或同它交互。服務提供者和服務請求者角色是邏輯結構,因而服務可以表現兩種特性。下圖 圖示了這些操作、提供這些操作的組件及它們之間的交互。
webservices協議棧
要以一種可互操作的方式執行發布、發現和綁定這三個操作,必須有一個包含每一層標準的 webservices協議棧。下圖展示了一個概念性 webservices協議棧。上面的幾層建立在下面幾層提供的功能之上。垂直的條表示在協議棧中每一層必須滿足的需求。左面的文本表示協議棧的那一層所應用的標準技術。
下面對上面是概念性的協議棧中,的每一層做一個粗糙的說明:
第一層(network):這一層的這些協議都是現在運用比較廣泛的協議啦,現在http運用的最多啦,好處是顯而易見的,其他的我就不多說了吧。(主要我怕我說出來會錯誤百出,被大家嘲笑。 *_*)。
第二層(xml-based messaging)這一層中主要體現怎么去調用webservices。現在運用比較廣泛的主要有兩種,一種是xml-rpc(xml-remote procedure call), 另一種是soap(simple object access protocal)。相比之下soap比xml-rpc有一定的優勢:soap在處理復雜數據(如數組等)要比xml-rpc更容易一些;xml-rpc沒有標準化錯誤代碼;下面我們著重看看soap:
什么是soap?
對soap的一種簡單理解:
soap是一種xml application,soap簡單的理解,就是這樣的一個開放協議soap=rpc+http+xml:采用http作為底層通訊協議;rpc作為一致性的調用途徑,xml作為數據傳送的格式,允許服務提供者和服務客戶經過防火墻在internet進行通訊交互。如下圖:
對soap更深一步的理解:
soap簡單對象訪問協議是在分散或分布式的環境中交換信息的簡單的協議,是一個基于xml的協議,它包括四個部分:soap封裝(envelop),封裝定義了一個描述消息中的內容是什么,是誰發送的,誰應當接受并處理它以及如何處理它們的框架,如下圖;soap編碼規則(encoding rules),用于表示應用程序需要使用的數據類型的實例,一般遵循xmlschema(定義了一系列的簡單數據類型)規范; soap rpc表示(rpc representation),表示遠程過程調用和應答的協定;soap綁定(binding),使用底層協議交換信息。雖然這四個部分都作為soap的一部分,作為一個整體定義的,但他們在功能上是相交的、彼此獨立的。特別的,信封和編碼規則是被定義在不同的xml命名空間(namespace)中,這樣使得定義更加簡單。
圖:soap封裝(envelop)
下面讓我們來看一個soap的例子:
[pre]post /calendar-request http/1.1
host: www.todaytech.com.cn
content-type: text/plain; charset="utf-8"
content-length: 507
soapaction:””
<soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/xmlschema" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance">
<soapenv:body>
<ns1:searchpaynoteresponse
soapenv:encodingstyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:ns1="http://external.charge.gfmis.todaytech.com">
<searchpaynotereturn href="#id0"/>
</ns1:searchpaynoteresponse>
<multiref id="id0" soapenc:root="0" soapenv:encodingstyle="http://schemas.xmlsoap.org/soap/encoding/"
xsi:type="ns2:externalpaynotevo"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:ns2="http://vo.charge.gfmis.todaytech.com">
<paynoteno xsi:type="xsd:string">0300000003004</paynoteno>
<startdate xsi:type="xsd:string" xsi:nil="true"/>
<disaid xsi:type="xsd:string" xsi:nil="true"/>
<annotation xsi:type="xsd:string" xsi:nil="true"/>
<chargeitemno3 xsi:type="xsd:string" xsi:nil="true"/>
<amount xsi:type="xsd:double">0.0</amount>
<chargeitemno2 xsi:type="xsd:string" xsi:nil="true"/>
<loginkey xsi:type="xsd:string">ycptewkew</loginkey>
<chargeitemno1 xsi:type="xsd:string" xsi:nil="true"/>
<usersign xsi:type="xsd:string" xsi:nil="true"/>
<operator xsi:type="xsd:string" xsi:nil="true"/>
<usercert xsi:type="xsd:string" xsi:nil="true"/>
<quantity3 xsi:type="xsd:double">0.0</quantity3>
<quantity2 xsi:type="xsd:double">0.0</quantity2>
<unitname xsi:type="xsd:string" xsi:nil="true"/>
<srvsign xsi:type="xsd:string" xsi:nil="true"/>
<quantity1 xsi:type="xsd:double">0.0</quantity1>
<srvcert xsi:type="xsd:string" xsi:nil="true"/>
<externalpaynoteno xsi:type="xsd:string" xsi:nil="true"/>
<status xsi:type="xsd:int">1</status>
<randstr xsi:type="xsd:string" xsi:nil="true"/>
<returncode xsi:type="xsd:int">0</returncode>
<enddate xsi:type="xsd:string" xsi:nil="true"/>
<price3 xsi:type="xsd:double">0.0</price3>
<price2 xsi:type="xsd:double">0.0</price2>
<price1 xsi:type="xsd:double">0.0</price1>
<unitno xsi:type="xsd:string" xsi:nil="true"/>
<payername xsi:type="xsd:string" xsi:nil="true"/>
</multiref>
</soapenv:body>
</soapenv:envelope>[/pre]
第三層(service description):在這一層中主要是我們服務的描述,向客戶端說明我們的服務,告訴客戶端、我們的提供了什么樣的接口可供調用(what),怎么樣去調用(how),到那去調用(where)。在這一層中主要的協議是wsdl(web services description language)。wsdl 是一種xml application,它的作用就是給客戶端描述我們接口的what,how,where,也就是說:wsdl 服務定義為分布式系統提供了可機器識別的sdk文檔,并且可用于描述自動執行應用程序通信中所涉及的細節。
wsdl 文檔將web服務定義為服務訪問點或端口的集合。在 wsdl 中,由于服務訪問點和消息的抽象定義已從具體的服務部署或數據格式綁定中分離出來,因此可以對抽象定義進行再次使用:消息,指對交換數據的抽象描述;而端口類型,指操作的抽象集合。用于特定端口類型的具體協議和數據格式規范構成了可以再次使用的綁定。將web訪問地址與可再次使用的綁定相關聯,可以定義一個端口,而端口的集合則定義為服務。因此,wsdl 文檔在web服務的定義中使用下列元素:
• types - 數據類型定義的容器,它使用某種類型系統(一般地使用xml schema中的類型系統)。
• message - 通信消息的數據結構的抽象類型化定義。使用types所定義的類型來定義整個消息的數據結構。
• operation - 對服務中所支持的操作的抽象描述,一般單個operation描述了一個訪問入口的請求/響應消息對。
• porttype - 對于某個訪問入口點類型所支持的操作的抽象集合,這些操作可以由一個或多個服務訪問點來支持。
• binding - 特定端口類型的具體協議和數據格式規范的綁定。
• port - 定義為協議/數據格式綁定與具體web訪問地址組合的單個服務訪問點。
• service - 相關服務訪問點的集合。
我們可以參考下圖,來理解一下wsdl文檔的結構組織:
說明:其中,
types是一個數據類型定義的容器,包含了所有在消息定義中需要的xml元素的類型定義,它一般遵循xmlschema的規范。
message具體定義了在通信中使用的消息的數據結構,message元素包含了一組part元素,每個part元素都是最終消息的一個組成部分,每個part都會引用一個datatype來表示它的結構。part元素不支持嵌套(可以使用datatype來完成這方面的需要),都是并列出現。
porttype具體定義了一種服務訪問入口的類型,何謂訪問入口的類型呢?就是傳入/傳出消息的模式及其格式。一個porttype可以包含若干個operation,而一個operation則是指訪問入口支持的一種類型的調用。在wsdl里面支持四種訪問入口調用的模式:
1. 單請求;
2. 單響應;
3. 請求/響應;
4. 響應/請求。
service描述的是一個具體的被部署的web服務所提供的所有訪問入口的部署細節,一個service往往會包含多個服務訪問入口,而每個訪問入口都會使用一個port元素來描述。
port描述的是一個服務訪問入口的部署細節,包括通過哪個web地址(url)來訪問,應當使用怎樣的消息調用模式來訪問等。其中消息調用模式則是使用binding結構來表示。
binding結構定義了某個porttype與某一種具體的網絡傳輸協議或消息傳輸協議相綁定,從這一層次開始,描述的內容就與具體服務的部署相關了。比如可以將porttype與soap/http綁定,也可以將porttype與mime/smtp相綁定等。
下面我們給出一個wsdl的文檔實例:
[pre]<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions targetnamespace="http://external.charge.gfmis.todaytech.com"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:apachesoap="http://xml.apache.org/xml-soap"
xmlns:impl="http://external.charge.gfmis.todaytech.com-impl"
xmlns:intf="http://external.charge.gfmis.todaytech.com"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:tns2="http://vo.charge.gfmis.todaytech.com"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:xsd="http://www.w3.org/2001/xmlschema">
<wsdl:types>
<schema targetnamespace="http://vo.charge.gfmis.todaytech.com"
xmlns="http://www.w3.org/2001/xmlschema">
<import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>
<complextype name="externalpaynotevo">
<sequence>
<element name="amount" type="xsd:double"/>
<element name="annotation" nillable="true" type="xsd:string"/>
<element name="chargeitemno1" nillable="true" type="xsd:string"/>
<element name="chargeitemno2" nillable="true" type="xsd:string"/>
<element name="chargeitemno3" nillable="true" type="xsd:string"/>
<element name="disaid" nillable="true" type="xsd:string"/>
<element name="enddate" nillable="true" type="xsd:string"/>
<element name="externalpaynoteno" nillable="true" type="xsd:string"/>
<element name="loginkey" nillable="true" type="xsd:string"/>
<element name="operator" nillable="true" type="xsd:string"/>
<element name="payername" nillable="true" type="xsd:string"/>
<element name="paynoteno" nillable="true" type="xsd:string"/>
<element name="price1" type="xsd:double"/>
<element name="price2" type="xsd:double"/>
<element name="price3" type="xsd:double"/>
<element name="quantity1" type="xsd:double"/>
<element name="quantity2" type="xsd:double"/>
<element name="quantity3" type="xsd:double"/>
<element name="randstr" nillable="true" type="xsd:string"/>
<element name="returncode" type="xsd:int"/>
<element name="srvcert" nillable="true" type="xsd:string"/>
<element name="srvsign" nillable="true" type="xsd:string"/>
<element name="startdate" nillable="true" type="xsd:string"/>
<element name="status" type="xsd:int"/>
<element name="unitno" nillable="true" type="xsd:string"/>
<element name="unitname" nillable="true" type="xsd:string"/>
<element name="usercert" nillable="true" type="xsd:string"/>
<element name="usersign" nillable="true" type="xsd:string"/>
</sequence>
</complextype>
<element name="externalpaynotevo" nillable="true" type="tns2:externalpaynotevo"/>
</schema>
</wsdl:types>
<wsdl:message name="logoutrequest">
<wsdl:part name="epnvo" type="tns2:externalpaynotevo"/>
</wsdl:message>
<wsdl:message name="searchpaynoteresponse">
<wsdl:part name="searchpaynotereturn" type="tns2:externalpaynotevo"/>
</wsdl:message>
<wsdl:message name="searchpaynoterequest">
<wsdl:part name="epnvo" type="tns2:externalpaynotevo"/>
</wsdl:message>
<wsdl:message name="genpaynoteresponse">
<wsdl:part name="genpaynotereturn" type="tns2:externalpaynotevo"/>
</wsdl:message>
<wsdl:message name="loginresponse">
<wsdl:part name="loginreturn" type="tns2:externalpaynotevo"/>
</wsdl:message>
<wsdl:message name="logoutresponse">
<wsdl:part name="logoutreturn" type="tns2:externalpaynotevo"/>
</wsdl:message>
<wsdl:message name="genpaynoterequest">
<wsdl:part name="epvo" type="tns2:externalpaynotevo"/>
</wsdl:message>
<wsdl:message name="loginrequest">
<wsdl:part name="epnvo" type="tns2:externalpaynotevo"/>
</wsdl:message>
<wsdl:porttype name="externalinterface">
<wsdl:operation name="login" parameterorder="epnvo">
<wsdl:input message="intf:loginrequest" name="loginrequest"/>
<wsdl:output message="intf:loginresponse" name="loginresponse"/>
</wsdl:operation>
<wsdl:operation name="logout" parameterorder="epnvo">
<wsdl:input message="intf:logoutrequest" name="logoutrequest"/>
<wsdl:output message="intf:logoutresponse" name="logoutresponse"/>
</wsdl:operation>
<wsdl:operation name="genpaynote" parameterorder="epvo">
<wsdl:input message="intf:genpaynoterequest"
name="genpaynoterequest"/>
<wsdl:output message="intf:genpaynoteresponse"
name="genpaynoteresponse"/>
</wsdl:operation>
<wsdl:operation name="searchpaynote" parameterorder="epnvo">
<wsdl:input message="intf:searchpaynoterequest"
name="searchpaynoterequest"/>
<wsdl:output message="intf:searchpaynoteresponse"
name="searchpaynoteresponse"/>
</wsdl:operation>
</wsdl:porttype>
<wsdl:binding name="externalinterfacesoapbinding"
type="intf:externalinterface">
<wsdlsoap:binding style="rpc"
transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="login">
<wsdlsoap:operation soapaction=""/>
<wsdl:input name="loginrequest">
<wsdlsoap:body
encodingstyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://external.charge.gfmis.todaytech.com" use="encoded"/>
</wsdl:input>
<wsdl:output name="loginresponse">
<wsdlsoap:body
encodingstyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://external.charge.gfmis.todaytech.com" use="encoded"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="logout">
<wsdlsoap:operation soapaction=""/>
<wsdl:input name="logoutrequest">
<wsdlsoap:body encodingstyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://external.charge.gfmis.todaytech.com" use="encoded"/>
</wsdl:input>
<wsdl:output name="logoutresponse">
<wsdlsoap:body encodingstyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://external.charge.gfmis.todaytech.com" use="encoded"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="genpaynote">
<wsdlsoap:operation soapaction=""/>
<wsdl:input name="genpaynoterequest">
<wsdlsoap:body encodingstyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://external.charge.gfmis.todaytech.com" use="encoded"/>
</wsdl:input>
<wsdl:output name="genpaynoteresponse">
<wsdlsoap:body encodingstyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://external.charge.gfmis.todaytech.com" use="encoded"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="searchpaynote">
<wsdlsoap:operation soapaction=""/>
<wsdl:input name="searchpaynoterequest">
<wsdlsoap:body encodingstyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://external.charge.gfmis.todaytech.com" use="encoded"/>
</wsdl:input>
<wsdl:output name="searchpaynoteresponse">
<wsdlsoap:body encodingstyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://external.charge.gfmis.todaytech.com" use="encoded"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="externalinterfaceservice">
<wsdl:port binding="intf:externalinterfacesoapbinding" name="externalinterface">
<wsdlsoap:address location="http://localhost:7001/gfmis/services/externalinterface"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>[/pre]
第四層(service publication):
第五層(service discovery):這兩層都是關于uddi的協議,我們暫時用不上,現在也暫時沒有,以后補上(^_^)。