码迷,mamicode.com
首页 > Web开发 > 详细

深入理解Web Service

时间:2015-07-04 23:30:16      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

SOA和Web Service

首先明白SOA和Web Service的关系:
* SOA面向服务架构,用于大型分布式系统的一个概念;
* Web Service是实现SOA的方式之一,不是所有的SOA都是基于Web service的;
* 但Webservice确实为最主流的SOA实现方式,有的人甚至把SOA等同于Webservice。不可否认,正是Webservice的成功才造就了SOA这个概念的成功;
 

Webservice

Webservice有三个基础标准:

1.WSDL: Web服务定义语言(Web Service Definition Language),用来定义服务接口。实际上,它能描述服务的两个不同方面:服务的签名(名字和参数),以及服务的绑定和部署细节(协议和位置)。
2.SOAP:简单对象访问协议(Simple Object Access Protocol),是定义Webservice的协议。HTTP是一个网络数据交互的底层协议,而SOAP是Web Service数据交换的专用协议。
3.UDDI:通用描述、发现与集成服务(Universal Description, Discovery and Integration),UDDI 是一种目录服务,企业可以使用它对 Web services 进行注册和搜索。
SOAP是协议,就像HTTP协议一样,一般框架都已经集成;
UDDI扮演者补充的角色,非必须,而且通常在实践中也不用。
WSDL是开发人员打交道最多的东西,也算是Webservice的核心了。

WSDL

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实践指南》)
 1 <?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" />  
45                <output message="tns: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>  
57                     <soap:body use="literal" />  
58                </input>  
59                <output>  
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>  
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

 SOAP (Simple Object Access Protocol)是一个消息框架,这个消息框架是基于XML协议的,从下图能够看到,SOAP的框架非常像HTTP协议,都包含的消息的Header和消息的Body,只不过SOAP是Web Service数据交换的专用协议。SOAP是HTTP的上层协议,最终还是通过HTTP来传输数据。

深入理解Web Service

标签:

原文地址:http://www.cnblogs.com/waitforever/p/4621359.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!