您的位置:首页 > 其它

深入理解Web Service

2015-07-04 22:25 316 查看

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来传输数据。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: