Web Service的研究

本文探讨了Web Service的核心概念,包括面向服务架构(SOA)的基本原理,详细阐述了Web Service的工作方式。接着,重点介绍了Web Service描述语言(WSDL)及其在定义服务接口中的作用,以及简单对象访问协议(SOAP)如何作为通信协议来传递信息。通过理解这些关键技术,读者可以更好地掌握Web Service的实现与应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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实践指南》)
<?xml version="1.0" encoding="utf-8" ?>
<definitions name="CustomerService"
     targetNamespace="http://soa-in-practice.com/wsdl"
     xmlns:tns="http://soa-in-practice.com/wsdl"
     xmlns:xsd1="http://soa-in-practice.com/xsd"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema"
     xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
     xmlns="http://schemas.xmlsoap.org/wsdl/">

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

     <message name="getCustomerAddressInput">
          <part name="params" element="xsd1:getCustomerAddress"/>
     </message>
     <message name="getCustomerAddressOutput">
          <part name="params" element="xsd1:getCustomerAddressResponse"/>
     </message>

     <portType name="CustomerInterface" >
          <operation name="getCustomerAddress">
               <input message="tns:getCustomerAddressInput" />
               <output message="tns:getCustomerAddressOutput" />
          </operation>
     </portType>

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

     <service name="CustomerService" >
          <port name="CustomerPort"
               binding="tns:CustomerSOAPBinding">
               <soap:address
               location="http://soa-in-practice.com/customer11"/>
          </port>
     </service>

</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来传输数据。


SOAP Reqeust Example
<?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>
SOAP Response Example
<?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消息的根元素为<Envelope>
从上边可以看出SOAP是基于XML的,除了组织结构,其他非常类似于HTTP的Request和Response。

HTTP Request Example
    GET /path/file.html HTTP/1.0
    From: someuser@jmarshall.com
    User-Agent: HTTPTool/1.0
    [blank line here]
HTTP Response Example
    HTTP/1.0 200 OK
    Date: Fri, 31 Dec 1999 23:59:59 GMT
    Content-Type: text/html
    Content-Length: 1354

    <html>
    <body>
    <h1>Happy New Millennium!</h1>
    (more file contents)
      .
      .
      .
    </body>
    </html>


参考资料:
《SOA实践指南》

转载请注明出处: http://blog.csdn.net/pan_tian/article/details/10008893

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值