Web Service基础(WSDL、SOAP)

Web ServiceMS在2000年创造了Web Service这个词,它描述的是允许计算机网络(互联网是典型,但不局限与此)相互通信的一套标准,其核心之一是可扩展标记语言(XML),另一个则是HTTP。

 

5个基础标准(其中有两个是早就有了的通用标准,他们被用来实现Web Service方法,另外3个是专门用于Web Service的):

1.       XML 用来描述模型,格式和数据类型的通用格式,其他大多数标准都是用XML来表达的标准。

2.       HTTP(HTTPS),互联网底层协议。HTTP(S)是运用了互联网技术,通过网络发送Web Service的可能的协议之一。

3.       WSDL(Web Services Description Language):用来定义服务接口。描述服务的两个方面:服务的签名(名字和参数),以及服务的绑定和部署细节(协议和位置)。

4.       SOAP是Web Service交换数据所准寻的协议。

5.       UDDI,管理Web Service的标准(注册和找到服务)

 

通常来说,使用WSDL标准是Web Service的关键特性,其他都是可选的。例如,不一定非得使用SOAP和HTTP莱发送服务请求,也可以使用其他协议而仍然算在使用Web Service。另外,UDDI扮演补充的角色,实际中也不用。

 

      WSDL

              怎样向别人介绍你的Web service有什么功能,以及每个函数调用时的参数呢?这就是WSDL。 WSDL标准有不同的版本,主要讲WSDL1.1和WSDL2.0。相比较于1.1,2.0主要有:

  • WSDL2.0 is much more simple and easy to learn, some of the ambiguities of WSDL1.2 have removed.
  • Removal of message constructs. These are specified using the XML schema type system directly.
  • Improved support for HTTP bindings.
  • Unlike WSDL1.1WSDL2.0 core specification supports for REST. Lawrence Mandel wrote a good article about REST support for WSDL2.0.

 

 

 

 

WSDL文件自底向上描述服务。也就是说,它们从用到的数据类型开始,道服务的位置(地址)结束。

 

示例:

<?xml version="1.0" encoding="utf-8" ?>

2 <definitions name="CustomerService"

3 targetNamespace="http://soa-in-practice.com/wsdl"

4 xmlns:tns="http://soa-in-practice.com/wsdl"

5 xmlns:xsd1="http://soa-in-practice.com/xsd"

6 xmlns:xsd="http://www.w3.org/2001/XMLSchema"

7 xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"

8 xmlns="http://schemas.xmlsoap.org/wsdl/">

9

10 <types>

11 <xsd:schema

12 targetNamespace="http://soa-in-practice.com/xsd"

13 xmlns="http://soa-in-practice.com/xsd">

14

15 <xsd:element name="getCustomerAddress">

16 <xsd:complexType>

17 <xsd:sequence>

18 <xsd:element name="customerID" type="xsd:long"/>

19 </xsd:sequence>

20 </xsd:complexType>

21 </xsd:element>

22

23 <xsd:element name="getCustomerAddressResponse" type="Address"/>

24 <xsd:complexType name="Address">

25 <xsd:sequence>

26 <xsd:element name="street" type="xsd:string"/>

27 <xsd:element name="city" type="xsd:string"/>

28 <xsd:element name="zipCode" type="xsd:string"/>

29 </xsd:sequence>

30 </xsd:complexType>

31

32 </xsd:schema>

33 </types>

34

35 <message name="getCustomerAddressInput">

36 <part name="params" element="xsd1:getCustomerAddress"/>

37 </message>

38 <message name="getCustomerAddressOutput">

39 <part name="params" element="xsd1:getCustomerAddressResponse"/>

40 </message>

41

42 <portType name="CustomerInterface" >

43 <operation name="getCustomerAddress">

44 <input message="tns:getCustomerAddressInput" name=" getCustomerAddressInput"/>

45 <output message="tns:getCustomerAddressOutput" name=" getCustomerAddressOutput "/>

46 </operation>

47 </portType>

48

49 <binding name="CustomerSOAPBinding"

50 type="tns:CustomerInterface" >

51 <soap:binding style="document"

52 transport="http://schemas.xmlsoap.org/soap/http" />

53 <operation name="getCustomerAddress">

54 <soap:operation

55 soapAction="http://soa-in-practice.com/getCustomerAddress" />

56 <input name=" getCustomerAddressInput ">

57 <soap:body use="literal" />

58 </input>

59 <output name=" getCustomerAddressOutput ">

60 <soap:body use="literal" />

61 </output>

62 </operation>

63 </binding>

64

65 <service name="CustomerService" >

66 <port name="CustomerPort"

67 binding="tns:CustomerSOAPBinding">

68 <soap:address

69 location="http://soa-in-practice.com/customer11"/>

70 </port>

71 </service>

72

73 </definitions>

l         最后的service定义了一个名为CustomerService的服务,该服务可以从http://soa-in-practice.com/customer11得到,与其一同提供的有被称为CustomerSOAPBinding的绑定。前缀tns是可以找到这个绑定标示符细节的命名空间,可以在最前面找到。

l         Binding定义了用来提供服务的协议和格式。这里我们可以看到CustomerSOAPBinding的定义。指定用于CustomerInterface的接口类型。这个绑定是基于底层HTTP协议的SOAP绑定,这里的风格为document/ literal

l         portType定义了接口CustomerInterface的接口。它包含一个叫getCustomerAddress的操作,指明了当操作被调用时候,服务总线发送的消息。它发送一个请求,收到一个应答(牵涉到消息交换格式)。这通过一个输入消息getCustomerAddressInput,以及一个输出消息getCustomerAddressOutput来定义。

l         message定义了各个消息,使用的是portType节引用的标示符。不管是getCustomerAddressInput还是getCustomerAddressOutput,都是用了在<types>节中定义的数据类型。

l         types定义了将会使用的数据类型:输入参数customerID的类型是long,输出参数address的类型是有3个字符串属性的结构记录。所有类型在自己的命名空间wsdl中。

 

版本2.0对应的WSDL文件:

 

1 <?xml version="1.0" encoding="utf-8" ?>

2 <description

3 targetNamespace="http://soa-in-practice.com/wsdl"

4 xmlns:tns="http://soa-in-practice.com/wsdl"

5 xmlns:xsd1="http://soa-in-practice.com/xsd"

6 xmlns:xsd="http://www.w3.org/2001/XMLSchema"

7 xmlns:wsoap="http://www.w3.org/2006/01/wsdl/soap"

8 xmlns:wsdlx="http://www.w3.org/2006/01/wsdl-extensions"

9 xmlns="http://www.w3.org/2006/01/wsdl">

10

11 <types>

12 <xsd:schema

13 targetNamespace="http://soa-in-practice.com/xsd"

14 xmlns="http://soa-in-practice.com/xsd">

15

16 <xsd:element name="getCustomerAddress">

17 <xsd:complexType>

18 <xsd:sequence>

19 <xsd:element name="customerID" type="xsd:long"/>

20 </xsd:sequence>

21 </xsd:complexType>

22 </xsd:element>

23

24 <xsd:element name="getCustomerAddressResponse" type="Address"/>

25 <xsd:complexType name="Address">

26 <xsd:sequence>

27 <xsd:element name="street" type="xsd:string"/>

28 <xsd:element name="city" type="xsd:string"/>

29 <xsd:element name="zipCode" type="xsd:string"/>

30 </xsd:sequence>

31 </xsd:complexType>

32

33 </xsd:schema>

34 </types>

35

36 <interface name = "CustomerInterface" >

37

38 <operation name="getCustomerAddress"

39 pattern="http://www.w3.org/2006/01/wsdl/in-out"

40 style="http://www.w3.org/2006/01/wsdl/style/iri"

41 wsdlx:safe = "true">

42 <input messageLabel="In"

43 element="xsd1:getCustomerAddress" />

44 <output messageLabel="Out"

45 element="xsd1:getCustomerAddressResponse" />

46 </operation>

47

48 </interface>

49

50 <binding name="CustomerSOAPBinding"

51 interface="tns:CustomerInterface"

52 type="http://www.w3.org/2006/01/wsdl/soap"

53 wsoap:protocol="http://www.w3.org/2003/05/soap/bindings/HTTP">

54

55 <operation ref="tns:getCustomerAddress"

56 wsoap:mep="http://www.w3.org/2003/05/soap/mep/soap-response"/>

57

58 </binding>

59

60 <service name="CustomerService"

61 interface="tns:CustomerInterface">

62

63 <endpoint name="CustomerEndpoint"

64 binding="tns:CustomerSOAPBinding"

65 address="http://soa-in-practice.com/customer20"/>

66

67 </service>

68

69 </description>

 

主要区别是:

1.       XML跟元素的名字是description

2.       service节是用的是endpoint而非port

3.       在binding节内,定义了SOAP1.2

4.       portType被interface取代,是用了根具体,定义了底层消息顺序的消息交换模式

5.       message节点没有了,操作世界引用数据类型,定义在types中,发送那个消息遵循interface节中指定的类型和消息交换模式

6.       用不同的命名空间来标识WSDL2.0和SOAP1.2

 

SOAP

       SOAP最初是“简单对象访问协议”的首字母缩写词,后来开发者发现SOAP既不简单,也不处理对象访问,从1.2版本开始,这个缩写词就被当做一个词使用,不再代表一种缩写了。

根据上面的例子。其SOAP请求的一个例子,如下:

<?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>

一个相应的应答消息如下:

<?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消息格式为XML,包含一个被称为Envelope的跟元素。跟元素包含一个可选的Header和必备的Body元素。Body元素包含有效载荷(请求、回复、或者错误数据),Header包含额外的,帮助基础设施处理消息的信息(如路由提示,安全提示等)。

 

UDDI

       “通用描述,发现和集成UDDI”最初是更宽泛的术语,“通用描述、发现和集成业务注册中心”。最初的想法是,引入一个有效的市场包含的全部3个角色:提供服务的供应者,需要服务的消费者,以及通过广告和定位服务的两方撮合道一起的中间人。这就产生了解释Web Services通常要展示的著名三角形:

 

 

 

 

 

 

 

Web Service其他相关的标准:

       Web Service的第一个问题是,它不是一个由单个组织定义的标准。由超过50个不同的Web Service规范,分别由三家不同的标准化组织制定,W3C,OASIS,WS-I。

       即时对于前面的5个基础标准,一直以来,达到互操作性的目标也很困难。即时选择了同一版本,规范也太粗糙,太宽泛。于是,在2002年,WS互操作性组织(WS-I)成立。

       WS-I的一样重要东西就是“配套资料”,它是一套已经定义了的标准,每一个标准都制定了特定的版本。

 

总结:

l         Web Service是SOA基础设施(ESB)事实上的标准

l         基于XML和HTTP这两个协议,最重要的是用于描述Web Service接口的WSDL,以及用于规定Web Service通信协议格式的SOAP。

l         WSDL文件将不同的层用于服务接口,绑定和部署细节,因此,通常需要适当的过程来处理WSDL文件的生命周期。

l         WSDL并没有绑定在SOAP上,可以使用其他绑定,所以接口描述在原则上能和任何技术一起使用

l         作为一个标准,WSDL只定义了互操作性的核心特性,没有提供按去啊你选哪个、SLA等附加特性,这些也行可以由其他标准提供。

l         不同的标准化组织规定可许多不同的Web Service标准,各标准也有许多不同的版本,因此,互操作成为一个问题。Web Service互操作性组织(WS-I)试图解决这个问题,其方法为针对某些标准的集合提供各种配套资料

l         在实践中,为了互操作,通常应该使用WS-I基本配套资料和SOAP document/literal wrapped绑定

 

posted @ 2013-12-01 13:00  拖鞋公子  阅读(120)  评论(0编辑  收藏  举报