`
mixer_a
  • 浏览: 344237 次
社区版块
存档分类
最新评论

WebServices入门

 
阅读更多

http://dev.csdn.net/develop/article/52/52599.shtm

<!-- end of article title -->
WebServices入门
<!--start of article content -->

[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-BasedMessaging)这一层中主要体现怎么去调用Webservices。现在运用比较广泛的主要有两种,一种是xml-rpc(XML-RemoteProcedureCall),另一种是SOAP(SimpleObjectAccessProtocal)。相比之下SOAP比XML-RPC有一定的优势:SOAP在处理复杂数据(如数组等)要比XML-RPC更容易一些;XML-RPC没有标准化错误代码;下面我们着重看看SOAP:
什么是SOAP?
对SOAP的一种简单理解:
SOAP是一种XMLApplication,SOAP简单的理解,就是这样的一个开放协议SOAP=RPC+HTTP+XML:采用HTTP作为底层通讯协议;RPC作为一致性的调用途径,XML作为数据传送的格式,允许服务提供者和服务客户经过防火墙在INTERNET进行通讯交互。如下图:

对SOAP更深一步的理解:
SOAP简单对象访问协议是在分散或分布式的环境中交换信息的简单的协议,是一个基于XML的协议,它包括四个部分:SOAP封装(envelop),封装定义了一个描述消息中的内容是什么,是谁发送的,谁应当接受并处理它以及如何处理它们的框架,如下图;SOAP编码规则(encodingrules),用于表示应用程序需要使用的数据类型的实例,一般遵循XMLSchema(定义了一系列的简单数据类型)规范;SOAPRPC表示(RPCrepresentation),表示远程过程调用和应答的协定;SOAP绑定(binding),使用底层协议交换信息。虽然这四个部分都作为SOAP的一部分,作为一个整体定义的,但他们在功能上是相交的、彼此独立的。特别的,信封和编码规则是被定义在不同的XML命名空间(namespace)中,这样使得定义更加简单。

图:SOAP封装(envelop)
下面让我们来看一个SOAP的例子:
[pre]POST/calendar-requestHTTP/1.1
Host:www.todaytech.com.cn
Content-Type:text/plain;charset="utf-8"
Content-Length:507
SOAPAction:””

<soapenv:Envelopexmlns: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">
<searchPaynoteReturnhref="#id0"/>
</ns1:searchPaynoteResponse>
<multiRefid="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">
<paynoteNOxsi:type="xsd:string">0300000003004</paynoteNO>
<startDatexsi:type="xsd:string"xsi:nil="true"/>
<disaIDxsi:type="xsd:string"xsi:nil="true"/>
<annotationxsi:type="xsd:string"xsi:nil="true"/>
<chargeItemNO3xsi:type="xsd:string"xsi:nil="true"/>
<amountxsi:type="xsd:double">0.0</amount>
<chargeItemNO2xsi:type="xsd:string"xsi:nil="true"/>
<loginKeyxsi:type="xsd:string">YCpTEwKEw</loginKey>
<chargeItemNO1xsi:type="xsd:string"xsi:nil="true"/>
<userSignxsi:type="xsd:string"xsi:nil="true"/>
<operatorxsi:type="xsd:string"xsi:nil="true"/>
<userCertxsi:type="xsd:string"xsi:nil="true"/>
<quantity3xsi:type="xsd:double">0.0</quantity3>
<quantity2xsi:type="xsd:double">0.0</quantity2>
<unitNamexsi:type="xsd:string"xsi:nil="true"/>
<srvSignxsi:type="xsd:string"xsi:nil="true"/>
<quantity1xsi:type="xsd:double">0.0</quantity1>
<srvCertxsi:type="xsd:string"xsi:nil="true"/>
<externalPaynoteNOxsi:type="xsd:string"xsi:nil="true"/>
<statusxsi:type="xsd:int">1</status>
<randStrxsi:type="xsd:string"xsi:nil="true"/>
<returnCodexsi:type="xsd:int">0</returnCode>
<endDatexsi:type="xsd:string"xsi:nil="true"/>
<price3xsi:type="xsd:double">0.0</price3>
<price2xsi:type="xsd:double">0.0</price2>
<price1xsi:type="xsd:double">0.0</price1>
<unitNOxsi:type="xsd:string"xsi:nil="true"/>
<payerNamexsi:type="xsd:string"xsi:nil="true"/>
</multiRef>
</soapenv:Body>
</soapenv:Envelope>[/pre]
第三层(ServiceDescription):在这一层中主要是我们服务的描述,向客户端说明我们的服务,告诉客户端、我们的提供了什么样的接口可供调用(WHAT),怎么样去调用(HOW),到那去调用(WHERE)。在这一层中主要的协议是WSDL(WebServicesDescriptionLanguage)。WSDL是一种XMLApplication,它的作用就是给客户端描述我们接口的WHAT,HOW,WHERE,也就是说:WSDL服务定义为分布式系统提供了可机器识别的SDK文档,并且可用于描述自动执行应用程序通信中所涉及的细节。
WSDL文档将Web服务定义为服务访问点或端口的集合。在WSDL中,由于服务访问点和消息的抽象定义已从具体的服务部署或数据格式绑定中分离出来,因此可以对抽象定义进行再次使用:消息,指对交换数据的抽象描述;而端口类型,指操作的抽象集合。用于特定端口类型的具体协议和数据格式规范构成了可以再次使用的绑定。将Web访问地址与可再次使用的绑定相关联,可以定义一个端口,而端口的集合则定义为服务。因此,WSDL文档在Web服务的定义中使用下列元素:
•Types-数据类型定义的容器,它使用某种类型系统(一般地使用XMLSchema中的类型系统)。
•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]<?xmlversion="1.0"encoding="UTF-8"?>
<wsdl:definitionstargetNamespace="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>
<schematargetNamespace="http://vo.charge.gfmis.todaytech.com"
xmlns="http://www.w3.org/2001/XMLSchema">
<importnamespace="http://schemas.xmlsoap.org/soap/encoding/"/>
<complexTypename="ExternalPaynoteVO">
<sequence>
<elementname="amount"type="xsd:double"/>
<elementname="annotation"nillable="true"type="xsd:string"/>
<elementname="chargeItemNO1"nillable="true"type="xsd:string"/>
<elementname="chargeItemNO2"nillable="true"type="xsd:string"/>
<elementname="chargeItemNO3"nillable="true"type="xsd:string"/>
<elementname="disaID"nillable="true"type="xsd:string"/>
<elementname="endDate"nillable="true"type="xsd:string"/>
<elementname="externalPaynoteNO"nillable="true"type="xsd:string"/>
<elementname="loginKey"nillable="true"type="xsd:string"/>
<elementname="operator"nillable="true"type="xsd:string"/>
<elementname="payerName"nillable="true"type="xsd:string"/>
<elementname="paynoteNO"nillable="true"type="xsd:string"/>
<elementname="price1"type="xsd:double"/>
<elementname="price2"type="xsd:double"/>
<elementname="price3"type="xsd:double"/>
<elementname="quantity1"type="xsd:double"/>
<elementname="quantity2"type="xsd:double"/>
<elementname="quantity3"type="xsd:double"/>
<elementname="randStr"nillable="true"type="xsd:string"/>
<elementname="returnCode"type="xsd:int"/>
<elementname="srvCert"nillable="true"type="xsd:string"/>
<elementname="srvSign"nillable="true"type="xsd:string"/>
<elementname="startDate"nillable="true"type="xsd:string"/>
<elementname="status"type="xsd:int"/>
<elementname="unitNO"nillable="true"type="xsd:string"/>
<elementname="unitName"nillable="true"type="xsd:string"/>
<elementname="userCert"nillable="true"type="xsd:string"/>
<elementname="userSign"nillable="true"type="xsd:string"/>
</sequence>
</complexType>
<elementname="ExternalPaynoteVO"nillable="true"type="tns2:ExternalPaynoteVO"/>
</schema>
</wsdl:types>

<wsdl:messagename="logoutRequest">
<wsdl:partname="epnvo"type="tns2:ExternalPaynoteVO"/>
</wsdl:message>
<wsdl:messagename="searchPaynoteResponse">
<wsdl:partname="searchPaynoteReturn"type="tns2:ExternalPaynoteVO"/>
</wsdl:message>
<wsdl:messagename="searchPaynoteRequest">
<wsdl:partname="epnvo"type="tns2:ExternalPaynoteVO"/>
</wsdl:message>
<wsdl:messagename="genPaynoteResponse">
<wsdl:partname="genPaynoteReturn"type="tns2:ExternalPaynoteVO"/>
</wsdl:message>
<wsdl:messagename="loginResponse">
<wsdl:partname="loginReturn"type="tns2:ExternalPaynoteVO"/>
</wsdl:message>
<wsdl:messagename="logoutResponse">
<wsdl:partname="logoutReturn"type="tns2:ExternalPaynoteVO"/>
</wsdl:message>
<wsdl:messagename="genPaynoteRequest">
<wsdl:partname="epvo"type="tns2:ExternalPaynoteVO"/>
</wsdl:message>
<wsdl:messagename="loginRequest">
<wsdl:partname="epnvo"type="tns2:ExternalPaynoteVO"/>
</wsdl:message>
<wsdl:portTypename="ExternalInterface">
<wsdl:operationname="login"parameterOrder="epnvo">
<wsdl:inputmessage="intf:loginRequest"name="loginRequest"/>
<wsdl:outputmessage="intf:loginResponse"name="loginResponse"/>
</wsdl:operation>
<wsdl:operationname="logout"parameterOrder="epnvo">
<wsdl:inputmessage="intf:logoutRequest"name="logoutRequest"/>
<wsdl:outputmessage="intf:logoutResponse"name="logoutResponse"/>
</wsdl:operation>
<wsdl:operationname="genPaynote"parameterOrder="epvo">
<wsdl:inputmessage="intf:genPaynoteRequest"
name="genPaynoteRequest"/>
<wsdl:outputmessage="intf:genPaynoteResponse"
name="genPaynoteResponse"/>
</wsdl:operation>
<wsdl:operationname="searchPaynote"parameterOrder="epnvo">
<wsdl:inputmessage="intf:searchPaynoteRequest"
name="searchPaynoteRequest"/>
<wsdl:outputmessage="intf:searchPaynoteResponse"
name="searchPaynoteResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:bindingname="ExternalInterfaceSoapBinding"
type="intf:ExternalInterface">
<wsdlsoap:bindingstyle="rpc"
transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operationname="login">
<wsdlsoap:operationsoapAction=""/>
<wsdl:inputname="loginRequest">
<wsdlsoap:body
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://external.charge.gfmis.todaytech.com"use="encoded"/>
</wsdl:input>
<wsdl:outputname="loginResponse">
<wsdlsoap:body
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://external.charge.gfmis.todaytech.com"use="encoded"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operationname="logout">
<wsdlsoap:operationsoapAction=""/>
<wsdl:inputname="logoutRequest">
<wsdlsoap:bodyencodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://external.charge.gfmis.todaytech.com"use="encoded"/>
</wsdl:input>
<wsdl:outputname="logoutResponse">
<wsdlsoap:bodyencodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://external.charge.gfmis.todaytech.com"use="encoded"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operationname="genPaynote">
<wsdlsoap:operationsoapAction=""/>
<wsdl:inputname="genPaynoteRequest">
<wsdlsoap:bodyencodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://external.charge.gfmis.todaytech.com"use="encoded"/>
</wsdl:input>
<wsdl:outputname="genPaynoteResponse">
<wsdlsoap:bodyencodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://external.charge.gfmis.todaytech.com"use="encoded"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operationname="searchPaynote">
<wsdlsoap:operationsoapAction=""/>
<wsdl:inputname="searchPaynoteRequest">
<wsdlsoap:bodyencodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://external.charge.gfmis.todaytech.com"use="encoded"/>
</wsdl:input>
<wsdl:outputname="searchPaynoteResponse">
<wsdlsoap:bodyencodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://external.charge.gfmis.todaytech.com"use="encoded"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:servicename="ExternalInterfaceService">
<wsdl:portbinding="intf:ExternalInterfaceSoapBinding"name="ExternalInterface">

<wsdlsoap:addresslocation="http://localhost:7001/gfmis/services/ExternalInterface"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>[/pre]
第四层(ServicePublication):
第五层(ServiceDiscovery):这两层都是关于UDDI的协议,我们暂时用不上,现在也暂时没有,以后补上(^_^)。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics