您的位置:首页 > 其它

Web Service概述

2013-08-23 13:56 260 查看

概述

当前,Web Service技术可谓炙手可热,即使MS宣布放弃Web Service。在没有更好技术标准出现之前,Web Service技术仍是企业级应用集成的基础,也是支撑SOA的构建技术之一。本文试图从一个比较高的角度来分析和概述一下Web Service的核心技术。
Web Service与其说是软件技术发展的进步,不如说是商业和政治上的策略。看看COBRA的推广和接受程度,COM/DCOM组件、J2EE等软件技术的成功,我们有理由相信,无论在技术上,还是商业营销上,软件产业都需要一次概念上的更新。WWW/HTTP的巨大成功和XML的广泛应用给软件业带来了很大的启发,Web Service借助WWW/HTTP和XML的袈裟横空出世,进入企业开发的概念之列,成为程序员的必修技术。
Web Service本质上是一种分布式软件的耦合交互技术,其主要目标是实现跨平台的可互操作性。所谓耦合交互,就是让分布式软件的两端在某种协议下能够交互,而不用关心彼此使用的编程技术。当然,不管这个协议叫RPC(远程过程调用)还是服务(services),我们都可以理解为一种操作。

核心技术

Web Service的核心技术主要有:XML/XML Schema,SOAP,WSDL,UDDI,HTTP。
Web Service使用XML/XSD(可扩展标记语言/ XML Schema )描述数据结构及类型,XML实例易于建立和易于分析,其主要的优点在于平台无关性和厂商无关性。
Web Service使用SOAP表示信息传输协议, SOAP基于XML/XSD,独立于平台和编程语言。
Web Service使用WSDL描述web服务,WSDL基于XML。
Web Service使用UDDI (Universal Description, Discovery and Integration)描述、发现与集成web服务。

概念模型




Web Service概念模型

SOAP

软件就是接受输入数据,处理数据,然后输出结果的过程,分布式软件也不例外。为了实现分布式软件的交换,需要一种协议保证输入数据和输出数据的格式是彼此可理解的,并且与具体的编程语言无关,而SOAP(simple object access protocol)就是规定如何用XML来格式化输入数据和格式化输出结果的协议。Web Service 中将这种格式化后的数据称作SOAP消息(message)。

SOAP特性

l SOAP是一种基于XML的标准消息传输协议,其消息格式由XML Schema模式定义,通过XML命名空间使SOAP具有很强的扩展性。
l SOAP中根元素是Envelope元素。Envelope元素中可以包含多个可选的Header元素,必须同时包含一个Body元素。Header元素必须是Envelope元素的直接子元素,并且要位于Body元素之前 。
l SOAP的命名空间:xmlns:soap=http://schemas.xmlsoap.org/soap/envelope/,命名空间定义了标准SOAP元素(如:Envelope, Header和Body等)。
l SOAP支持四种消息传输模式(RPC/Literal, Document/ Literal, RPC/Encoded, and Document/Encoded),但是WS-I Basic Profile只允许RPC/Literal和Document/Literal,不支持编码模式。Literal(明文)文档可以根据XML Schema验证XML实例的有效性,而Encoded(编码)文档将失去此优势。
l Document/Literal Unwrapped(非包装的文档/明文)模式,如果参数为soap:Body的直接子元素,则为该模式。
l Document/Literal Wrapped(包装的文档/明文)模式,参数包装在一个“wrapper”元素中,“wrapper”元素为soap:Body的直接子元素。

Web Service交互过程




传输模式的例子

1.Document/Literal Unwrapped[/b]的SOAP[/b]请求消息例子:[/b]
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Body xmlns:getord="http://www.example.com/oms/getorders">
<getord:startDate>2005-11-19</getord:startDate>
<getord:endDate>2005-11-22</getord:endDate>
</env:Body>
</env:Envelope>

2.Document/Literal Wrapped[/b]的SOAP[/b]请求消息例子:[/b]
<env1:Envelope xmlns:env1="http://schemas.xmlsoap.org/soap/envelope/">
<env1:Body>
<getord:getOrdersDates xmlns:getord="http://www.example.com/oms/getorders">
<getord:startDate>2005-11-19</getord:startDate>
<getord:endDate>2005-11-22</getord:endDate>
</getord:getOrdersDates>
</env1:Body>
</env1:Envelope>

底层传输协议

那么交互的两端如何传递消息呢?或者说通过什么协议发送/接受SOAP消息呢?由上可知,在互联网中HTTP协议是个不错的选择,当然,Web Service规定不仅仅HTTP可以,SMTP等其它协议也可以。可见Web Service的野心相当大。

WSDL

很显然有了SOAP消息和底层传输协议的支持,我们就可以发送消息了。慢着,在发送消息前请先回答几个问题,SOAP消息的内容是什么呀?SOAP消息该发往那里呢?
是的,我们还不知道消息的内容,也不知道将消息发往那里,以及如何处理传输时的必要参数?而这些统统由WSDL(Web Service Definition Language)定义。

特性

l Web Service Definition Language基于XML,为其本质为Interface Definition Language(IDL)。
l WSDL定义端点操作(Endpoint Operations)和消息内容及格式(Messages and Format)。
l WSDL定义消息交互模式:One-Way, Request/Response, Solicit-response, Notification ?
l WSDL定义消息传输模式:SOAP:RPC/Document, Encoded/Literal;HTTP:get/post?
l WSDL将消息绑定到底层传输协议:soap:binding, http:binding?
l WSDL定义服务的发布地址(Services及Port)。

WSDL文档结构

WSDL文档可以分为两部分:抽象定义部分和具体描述部分。抽象定义部分以独立于平台和编程语言的方式定义SOAP消息及服务操作,如此就定义了一系列服务。而具体描述部分将这些服务绑定到指定的地址,以便客户端能够找到该服务。
抽象定义部分:
Types
Message
PortType

具体描述部分:
Binding
Service
Port
Address

结构示例:[/b][/b]
<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://WebXml.com.cn/" targetNamespace="http://WebXml.com.cn/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types></wsdl:types>
<wsdl:message name="getMobileCodeInfoSoapIn"></wsdl:message>
<wsdl:message name="getMobileCodeInfoSoapOut"></wsdl:message>
<wsdl:portType name="MobileCodeWSSoap"></wsdl:portType>
<wsdl:binding name="MobileCodeWSSoap" type="tns:MobileCodeWSSoap"></wsdl:binding>
<wsdl:service name="MobileCodeWS">
<wsdl:port name="MobileCodeWSSoap" binding="tns:MobileCodeWSSoap">
<soap:address location="http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

抽象定义部分

Types[/b]
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://WebXml.com.cn/">
<s:element name="getMobileCodeInfo">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="mobileCode" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="userID" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="getMobileCodeInfoResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="getMobileCodeInfoResult" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="string" nillable="true" type="s:string" />
</s:schema>
</wsdl:types>

Message[/b]
<wsdl:message name="getMobileCodeInfoSoapIn">
<wsdl:part name="parameters" element="tns:getMobileCodeInfo" />
</wsdl:message>
<wsdl:message name="getMobileCodeInfoSoapOut">
<wsdl:part name="parameters" element="tns:getMobileCodeInfoResponse" />
</wsdl:message>
<wsdl:message name="getMobileCodeInfoHttpGetIn">
<wsdl:part name="mobileCode" type="s:string" />
<wsdl:part name="userID" type="s:string" />
</wsdl:message>
<wsdl:message name="getMobileCodeInfoHttpGetOut">
<wsdl:part name="Body" element="tns:string" />
</wsdl:message>
<wsdl:message name="getMobileCodeInfoHttpPostIn">
<wsdl:part name="mobileCode" type="s:string" />
<wsdl:part name="userID" type="s:string" />
</wsdl:message>
<wsdl:message name="getMobileCodeInfoHttpPostOut">
<wsdl:part name="Body" element="tns:string" />
</wsdl:message>

PortType[/b]
wsdl:portType [/b](端点类型)是消息与端点操作的映射集合:[/b]
<wsdl:portType name="MobileCodeWSSoap">
<wsdl:operation name="getMobileCodeInfo">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
获得国内手机号码归属地省份、地区和手机卡类型信息
输入参数:mobileCode = 字符串(手机号码,最少前7位数字),
userID = 字符串(商业用户ID) 免费用户为空字符串;返回数据:字符串(手机号码:省份 城市 手机卡类型)。
</wsdl:documentation>
<wsdl:input message="tns:getMobileCodeInfoSoapIn" />
<wsdl:output message="tns:getMobileCodeInfoSoapOut" />
<wsdl:fault name="fault" message="tns:faultMsg"/>
</wsdl:operation>
<wsdl:operation name=“XXX">
… ...
</wsdl:operation>
</wsdl:portType>
<wsdl:portType name="MobileCodeWSHttpGet">
<wsdl:operation name="getMobileCodeInfo">
<wsdl:input message="tns:getMobileCodeInfoHttpGetIn" />
<wsdl:output message="tns:getMobileCodeInfoHttpGetOut" />
</wsdl:operation>
</wsdl:portType>
<wsdl:portType name="MobileCodeWSHttpPost">
<wsdl:operation name="getMobileCodeInfo">
<wsdl:input message="tns:getMobileCodeInfoHttpPostIn" />
<wsdl:output message="tns:getMobileCodeInfoHttpPostOut" />
</wsdl:operation>
</wsdl:portType>

消息交换模式

Request-Response[/b]模式[/b][/b]
Request-Response模式类似于通常的同步过程调用,Service Client发送Request Message后中断,直到Service Server返回Response Message。
operation必须有input/output message,且Input message在output message之前。
例子[/b][/b]
<portTyle>
<!-- Request-Response Operations (Client initiated) -->
<operation name = "iniit">
<input message = "initRequest"/>
<output message = "initResponse"/>
</operation>
<operation name = "search">
<input message="searchRequest"/>
<output message="searchResponse"/>
</operation>
</portType>

Solicit-Response[/b](征求响应)模式[/b][/b]
Solicit-Response(征求响应)模式也是同步过程调用, Service Server 首先发送Response Message,然后接受Service Client发送的Request Message。
operation必须有input/output message,且Input message在output message之后。
例子[/b][/b]
<portTyle>
<!-- Solicit-Response Operation (Server initiated) -->
<operation name = "accessControl">
<output message = "accessControlResponse"/>
<input message = "accessControlRequest"/>
</operation>
<operation name = "resourceControl">
<output message="resourceControlResponse"/>
<input message="resourceControlRequest"/>
</operation>
</portType>
[/b]
One-way[/b]模式[/b][/b]
One-way模式,首先Service Client发送Request Message给Service Server,但是不期望接受Response Message。
operation只接受input message,不返回output message,因此只有input message。
例子[/b][/b]
<portTyle>
<!-- One-way Operations (Client initiated) -->
<operation name = "triggerResourceControl">
<input message = "triggerResourceControlRequest"/>
</operation>
</portType>
[/b]
Notification[/b]模式[/b][/b]
Notification模式,Service Server发送Response Message给Service Client,但是不期望接受Request Message。
operation只发送output message,不接受input message,因此只有output message。
例子[/b][/b]
<portTyle>
<!-- Notification Operations (Server initiated) -->
<operation name = "segmenty">
<output message = "segmentRequest"/>
</operation>
</portType>
[/b]

具体描述部分

Binding[/b]
wsdl:binding定义了端点操作消息的传输协议模式,绑定了消息的底层传输协议。
下面的例子定义消息的传输协议模式为SOAP的document/literal,消息绑定的底层传输协议为HTTP:
<wsdl:binding name="MobileCodeWSSoap" type="tns:MobileCodeWSSoap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="getMobileCodeInfo">
<soap:operation soapAction="http://WebXml.com.cn/getMobileCodeInfo" style="document" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
<wsdl:fault name="fault">
<soap:fault use="literal" name="fault"/>
</wsdl:fault>
</wsdl:operation>
</wsdl:binding>

下面的例子中消息的传输协议模式为HTTP的http:urlEncoded/mime:mimeXml,
消息绑定的底层传输协议为HTTP的Get:
<wsdl:binding name="MobileCodeWSHttpGet" type="tns:MobileCodeWSHttpGet">
<http:binding verb="GET" />
<wsdl:operation name="getMobileCodeInfo">
<http:operation location="/getMobileCodeInfo" />
<wsdl:input>
<http:urlEncoded />
</wsdl:input>
<wsdl:output>
<mime:mimeXml part="Body" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>

下面的例子中消息的传输协议模式为HTTP的application/x-www-form-urlencoded/mime:mimeXml,消息绑定的底层传输协议为HTTP的Post:
<wsdl:binding name="MobileCodeWSHttpPost" type="tns:MobileCodeWSHttpPost">
<http:binding verb="POST" />
<wsdl:operation name="getMobileCodeInfo">
<http:operation location="/getMobileCodeInfo" />
<wsdl:input>
<mime:content type="application/x-www-form-urlencoded" />
</wsdl:input>
<wsdl:output>
<mime:mimeXml part="Body" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>

Service
wsdl:service定义了可访问的Services,而这些Services将wsdl:binding映射到指定的Port地址。下面的例子中发布了3个Service:
<wsdl:service name="MobileCodeWS">
<wsdl:port name="MobileCodeWSSoap" binding="tns:MobileCodeWSSoap">
<soap:address location="http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx" />
</wsdl:port>
<wsdl:port name="MobileCodeWSHttpGet" binding="tns:MobileCodeWSHttpGet">
<http:address location="http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx" />
</wsdl:port>
<wsdl:port name="MobileCodeWSHttpPost" binding="tns:MobileCodeWSHttpPost">
<http:address location="http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx" />
</wsdl:port>
</wsdl:service>

接收/发送消息

SOAP协议

SOAP[/b]绑定HTTP[/b]协议,Client[/b]发送请求的消息:[/b]
POST http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx HTTP/1.1
Host: webservice.webxml.com.cn:80
Content-Type: text/xml; charset=utf-8
Content-Length: 375
SOAPAction: "http://WebXml.com.cn/getMobileCodeInfo"
Connection: close

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://WebXml.com.cn/">
<soapenv:Body>
<q0:getMobileCodeInfo>
<q0:mobileCode>13401018516</q0:mobileCode>
</q0:getMobileCodeInfo>
</soapenv:Body>
</soapenv:Envelope>

SOAP[/b]绑定HTTP[/b]协议,Server[/b]发送响应的消息:[/b]
HTTP/1.1 200 OK
Connection: close
Content-Type: text/xml; charset=utf-8
Content-Length: 437

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/">
<getMobileCodeInfoResult>
13401018516:北京 北京 北京移动动感地带卡
</getMobileCodeInfoResult>
</getMobileCodeInfoResponse>
</soap:Body>
</soap:Envelope>
[/b]

HTTP/Get协议

请求[/b]
----------------------------------------------------------------------------------------------------------------------[/b]
GET http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode=13401018516&userID= HTTP/1.1
Host: webservice.webxml.com.cn
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
----------------------------------------------------------------------------------------------------------------------[/b]

响应[/b]
----------------------------------------------------------------------------------------------------------------------[/b]
HTTP/1.1 200 OK
Date: Mon, 09 Nov 2009 03:09:13 GMT
Server: Microsoft-IIS/6.0
Content-Type: text/xml; charset=utf-8
Content-Length: 142

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://WebXml.com.cn/">
13401018516:北京 北京 北京移动动感地带卡
</string>
----------------------------------------------------------------------------------------------------------------------[/b]

HTTP/Post协议

请求[/b]
----------------------------------------------------------------------------------------------------------------------[/b]
POST http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo HTTP/1.1
Content-type: application/x-www-form-urlencoded
Content-Length: 30

mobileCode=13401018516&userID=
----------------------------------------------------------------------------------------------------------------------[/b]

响应[/b]
----------------------------------------------------------------------------------------------------------------------[/b]
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: 142

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://WebXml.com.cn/">
13401018516:北京 北京 北京移动动感地带卡
</string>
----------------------------------------------------------------------------------------------------------------------[/b]
[/b]

UUDI

统一描述发现和集成(UDDI)提供一种发布和查找服务描述的方法。UDDI 数据实体提供对定义业务和服务信息的支持。WSDL 中定义的服务描述信息是UDDI 注册中心信息的补充。UDDI 提供对许多不同类型的服务描述的支持。因此,UDDI 没有对 WSDL 的直接支持,也没有对任何其它服务描述机制的直接支持。

UDDI数据类型

UDDI 注册中心有 4 种主要的数据类型: BusinessEntity、 BusinessService、BindingTemplate和 tModel。下图展现了这些数据类型的关系:




※ BusinessEntity描述关于服务提供商的信息,可以包含一个或多个BusinessService。
※ BusinessService和BindingTemplate定义web服务的技术和业务描述,每个BindingTemplate包含一个或多个tModel引用。
※ tModel定义服务的技术规范。

WSDL文档类型

为了在UDDI注册中心发布和查找服务,WSDL被分成2中类型:服务接口(Service Interface)和服务实现(Service Implementation)。




WSDL文档类型
服务接口由 WSDL服务定义文档来描述,这种文档包含服务接口的 types、 import、 message、 portType和 binding等元素。服务接口文档包含将用于实现一个或多个服务的 WSDL 服务定义。
通过使用一个 import 元素,一个服务接口文档可以引用另一个服务接口文档。 例如,一个仅包含 message 和 portType 元素的服务接口可以被另一个仅包含此 portType 的绑定的服务接口引用。
WSDL服务实现文档将包含 import和 service元素。服务实现文档包含实现一个服务接口的服务描述。import 元素中至少会有一个将包含对 WSDL 服务接口文档的引用。一个服务实现文档可以包含对多个服务接口文档的引用。
WSDL 服务实现文档中的 import 元素包含两个属性。 namespace的属性值是一个与服务接口文档中的 targetNamespace相匹配的 URL。 location属性是一个用于引用包含完整的服务接口定义的 WSDL 文档的 URL。port 元素的 binding属性包含对服务接口文档中的某个特定绑定的引用。
服务接口文档由服务接口提供者开发和发布。服务实现文档由服务提供者创建和发布。服务接口提供者与服务提供者这两个角色在逻辑上是分离的,但他们可以是同一个商业实体。




WSDL到UDDI的映射
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: