Transcript Document
冯志勇 天津大学计算机学院 2008年10月 WEB SERVICES及其协议栈 1 大纲 SOA模型 Web Services概述 XML and XML Schema 通信协议 Web Services 描述 Web Services 流描述 Web Services 发布和发现 2 SOA模型 3 SOA模型 作为一个分布式计算架构, Web服务是最重要的SOA实现。 SOA模型是Web服务体系结构的概念 4 SOA模型中的概念 角色 操作 Services Provider Services Requestor Services Registry Publish Find Bind 关键组件 Services Services Description 5 SOA模型中的主要标准 标准 Web Services Description Language (WSDL) Universal Description, Discovery and Integration (UDDI) Simple Object Access Protocol (SOAP) Web Service Flow Language (WSFL) …… 6 SOA开发过程 业务组件分析 服务识别 服务定义 组件识别 组件定义 服务实现决策 SOA 实现 7 SOA开发的层模型 业务过程 服务 集 成 架 构 Qos 服 务 消 费 者 安 全 管 理 组件 服 务 提 供 者 已有应用资源 8 过程 VS 层模型 9 RELATIONSHIP BETWEEN SOA AND WEB SERVICES Based on open standard and flexible implementation, Web services is natively applicable to SOA implementation. The Relationships between Web services and SOA are: Web services provide an open standard and machine-readable model (WSDL) for creating explicit, implementation-independent descriptions of service interfaces. Web services provide communication mechanisms that are locationtransparent and interoperable. Web services are evolving through BPEL4WS, document-style SOAP, and WSDL, and emerging technologies such as WS-ResourceFramework to support the technical implementation of well-designed services that encapsulate and model reusable function in a flexible manner. 10 WEB SERVICES概述 11 WEB SERVICES Web: 灵活的人机交互 Web Services: Web服务是最近制定的技术规范,充分利用现 有的开放标准如XML , URL 和HTTP提供一个新的系统对系统 通信标准 工作定义:通过标准化协议访问网络软件服务 简单对象访问协议(SOAP):非常灵活的远程过程调用 (RPC) 大量的利益在商业界,学术界,标准制定机构, . . . 应用电子商务,电信,科技,资源数据库,政府,教育, . . . 12 WEB SERVICES类型 面向业务的Web Services 面向消费的Web Services B2C的网站,全国多方B2C系统 面向设备的Web Services ERP,CRM, 应用系统集成 支持不同种类的服务。如天气报告,电子邮件服务 面向系统的 Web Services 认证 监测 QoS 13 WEB SERVICES过程 简化和/或自动化Web服务 发现 组合 指定组成的目标; 指定组成的约束; 构建一个组成; 分析组成的成分。 调用 是什么性质应予以说明? 如何有效地对他们的查询? 保持颁布分离; 提供交易担保。 监控 如何跟踪条款; 回收失败条款。 14 WEB SERVICES 标准栈 Web Services 组合: WSFL,BPEL4WS WS-CDL WS-CAF Web Services 事务: WS-Coordination WS-Transaction 发布和发现: WS-AtomicTransaction UDDI, WSWS-BusinessActivity WSIL SecureConversation Web Services , WS-Discovery WSManagement: SecurityPolicy WSDM, WS-Manageability WS-Trust SPML, WS-Provisioning Web Services 安全: XML-Encryption XML-Signature WS-Security 服务描述层: WSDL, WSCL, WSCI,WS-MetadataExchange, WS-Policy XML 消息层: SOAP , WS-Addressing, WS-Notification, WS-Eventing, WS-Enumeration, WS-MessageDelivery, WS-Reliability,WS Reliable Messaging, WS-Resources WS-Transfer 传输层: HTTP, SMTP, FTP, etc. 15 接口 WSDL 消息 SOAP 类型 XML Schema 数据 XML Web Service 标准 Sun J2EE BPEL Microsoft .Net 行为 IBM WebSphere WEB SERVICE 核心标准 实现平台 16 XML AND XML SCHEMA 17 XML的结构 标签:部分数据的标签 元素和子元素:数据节以<tagname>开始和</tagname>结 束, 内容必须正确匹配 正确匹配 不正确匹配 <account> … <balance> …. </balance> </account> <account> … <balance> …. </account> </balance> 正确:对同一父元素, 每一个开始标签必须有一个独特 的匹配的结束标记. 18 XML的结构 每个文件必须有一个顶层元素 <bank> <customer> <name> Hayes </name> <street> Main </street> <city> Harrison </city> <account> <account-number> A-102 </account-number> <branch-name>Perryridge </branch-name> <balance> 400 </balance> </account> <account> … </account> </customer> . . Top level element </bank> 19 XML的结构 属性Attribute 元素可以有属性 <account acct-type = “checking” > <account-number> A-102 </account-number> <branch-name> Perryridge </branch-name> <balance> 400 </balance> </account> 对内部开始标记的一个组成部分,属性定义为 name=value 一个因素可能有若干属性,但每个属性名称只能出现一 次 <account acct-type = “checking” monthly-fee=“5”> 20 XML的结构 元素和属性之间的区别 属性不能嵌套 <customer name=Hayes street=Main city=Harrison> <account> <account-number> A-102 </account-number> <branch-name> Perryridge </branch-name> <balance> 400 </balance> </account> <account> … </account> </customer> 21 XML的结构 Well-Formed XML 文档 (称之为根元素) 每个元素包含一个开始和相应的结束标记 标签不能重叠, 如 <author><name>LeeHong</author></name> 在属性的一个因素具有唯一的名字 元和标记名称必须是合法的 文件中只有一个外层元素 22 XML的结构 树模型的XML文件 只有一个根 没有环 根以外的每个节点只有一个父节点 每个节点有一个标签、元素或属性 该元素的序是很重要的 23 24 命名空间 XML数据已成为各组织之间的交换; 在不同的组织相同的标记名称可能有不同的含义, 造成交换文件的混乱; 指定一个独特的字符串作为一个元素名称避免混淆; 解决办法:使用 unique-name:element-name 使用XML命名空间可以避免在整个文件中使用长的 名字 25 命名空间 <Schema name="mySchema" xmlns="urn:schemas-microsoft-com:xml-data" xmlns:dt="urn:schemas-microsoft-com:datatypes" xmlns:myNS=http://www.xml_step_by_step.edu\ns.xml > 三个命名空间: schema namespace datatype namespace myNS namespace 26 XML SCHEMA 数据库schemas限制哪些信息可以存储,存储值的数据类型 Schemas 对XML数据交换非常重要 否则,一个网站不能自动解释从另一个网站收到的数据 XML schema两个机制 Document Type Definition (DTD) XML Schema 27 XML SCHEMA XML Schema Supports 数据类型 E.g. integer, string, etc Also, constraints on min/max values 用户定义类型 不像DTDs,XML Schema 采用XML语法, 更多标准表达,且更细致 与命名空间集成(reuse and refine) 更多特征 列表, 唯一和外键约束,继承 .. 28 <xsd:schema xmlns:xsd=http://www.w3.org/2001/XMLSchema> <xsd:element name=“bank” type=“BankType”/> <xsd:element name=“account”> <xsd:complexType> <xsd:sequence> <xsd:element name=“account-number” type=“xsd:string”/> <xsd:element name=“branch-name” type=“xsd:string”/> <xsd:element name=“balance” type=“xsd:decimal”/> </xsd:squence> </xsd:complexType> </xsd:element> ….. definitions of customer and depositor …. <xsd:complexType name=“BankType”> <xsd:squence> <xsd:element ref=“account” minOccurs=“0” maxOccurs=“unbounded”/> <xsd:element ref=“customer” minOccurs=“0” maxOccurs=“unbounded”/> <xsd:element ref=“depositor” minOccurs=“0” maxOccurs=“unbounded”/> </xsd:sequence> </xsd:complexType> </xsd:schema> 29 XML SCHEMA 元素类型 <element name=". . ."/> with possible attributes: ‘类型’属性 定义该元素的类型: type=". . ." (more on types later) 基数限制 : • minOccurs=“x”, x可以使任何自然数(包括 0) • maxOccurs=“x”, x可以使任何自然数(包括 0) 或无限 30 XML SCHEMA <element name="email"/> <element name="head" minOccurs="1" maxOccurs="1"/> <element name="to" minOccurs="1"/> 31 XML SCHEMA 属性类型 <attribute name=". . ."/> with possible attributes type=". . ." use="x", corresponds to #OPTIONAL and #IMPLIED in DTDs use="x" value=". . .", where x may be default or fixed 32 XML SCHEMA <attribute name="id" type="ID" use="required"/> <attribute name="speaks" type="LanguageType" use="default“ value="en"/> 33 XML SCHEMA 数据类型 数字数据类型, 包括:integer, Short, Byte, Long, Float, Decimal 串类型,包括: string, ID, IDREF, CDATA, Language 日期和时间类型 用户定义的数据类型 简单数据类型和复杂数据类型 34 XML SCHEMA 复杂数据类型和扩展数据类型 通过对子element的组合与其他类型扩展来完成的, 它有三种组合方式: sequence,all,choice. 对于sequence方式,各子element在对应的XML文档中的出现的次序与定义 中的次序必须一致; 对于all方式,各子element在对应的XML文档中的可以以任意次序出现; 对于choice方式,各子element在对应的XML文档只出现其中的一个。 <complexType name="lecturerType"> <sequence> <element name="firstname" type="string" minOccurs="0" maxOccurs="unbounded"/> <element name="lastname" type="string"/> </sequence> <attribute name="title" type="string" use="optional"/> </complexType> 35 XML SCHEMA <element name=“lecture” type=“lectureType”> <lecture title=“associate professor”> <firstname>Zhiyong</firstname> <lastname>Feng</lastname> </lecture> 36 XML SCHEMA 数据类型扩展:现有的数据类型可以扩展新的元素 或属性 <complexType name="extendedLecturerType"> <extension base="lecturerType"> <sequence> <element name="email" type="string" minOccurs="0" maxOccurs="1"/> </sequence> <attribute name="rank" type="string" use="required"/> </extension> </complexType> 37 XML SCHEMA 限制数据类型和简单的数据类型 通过限制现有的数据类型 定义 <complexType name="restrictedLecturerType"> <restriction base="lecturerType"> <sequence> <element name="firstname" type="string" minOccurs="1" maxOccurs="2"/> </sequence> <attribute name="title" type="string" use="required"/> </restriction> </complexType> 38 XML SCHEMA Simple data types can also be defined by restricting existing data types. <simpleType name="dayOfMonth"> <restriction base="integer"> <minInclusive value="1"/> <maxInclusive value="31"/> </restriction> </simpleType> 39 通信协议(SOAP) 40 为什么要有简单对象访问协议(SOAP) SOAP协议是一个XML消息传递协议,独立于任何特定的传输 协议。 替代复杂的分布式对象技术的轻量级协议 开始为BizTalk (Microsoft/UserLand/DevelopMentor )设计 是W3C 标准 基于XML 41 SOAP 消息结构 SOAP Envelope SOAP Header Header Block ... Header Block SOAP Body Body Block ... Body Block Envelope contains Header Body Header is optional Out-of-band information such as… Authentication information Message routes Logging Transaction flow Body contains XML body of RPC call 42 SOAP 例子 <?xml version="1.0"?> <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-ncoding"> <soap:Body> <m:GetPrice xmlns:m="http://www.w3schools.com/prices"> <m:Item>Apples</m:Item> </m:GetPrice> </soap:Body> </soap:Envelope> 43 SOAP例子 <?xml version="1.0"?> <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> <soap:Body> <m:GetPriceResponse xmlns:m="http://www.w3schools.com/prices"> <m:Price>1.90</m:Price> </m:GetPriceResponse> </soap:Body> </soap:Envelope> 44 WEB SERVICES 描述语言 45 WEB SERVICES描述语言 (WSDL) WSDL是一种基于XML的接口定义语言, 它分离功能和 执行,使SOA建议的契约设计成为可能 WSDL 定义 Services 做什么– 接口 interface 访问说明 – how Services 位置– where 46 WSDL要素 Supports 接口 Port Type Operation Input & Output Formats & Protocols 访问 说明 Binding How to encode Message Implements Port Provides Services Endpoints 47 WSDL主体结构 <definitions namespace = “http://… ”> <types> XML schema types </type> <message> definition of a message消息定义 </message> <portType> a set of operations一组操作 </portType> <binding> communication protocols通信协议 </binding> <Services> a list of binding and ports绑定和端口列表 </Services> </definitions> 48 类型 <types> <schema targetNamespace=“http://example.com/stockquote.xsd” <types> define data types used in defining messages xmlns=“http://www.w3.org/2000/10/XMLSchema”> <element name=“TradePriceRequest”> XML<complexType> Schema, DTD, and etc. <all> XML Schema must be supported by any vendor of WSDL <element name=“tickerSymbol” type=“string“ conformant products minOccur = “1” maxOccur=“10”/> <element name = “payment”> <complexType> <choice> <element name = “account” type=“string” /> <element name = “creditcard” type=“string” /> </choice> </complexType> </element> </all> </complexType> </element> </schema> </types> 49 WSDL 消息 A <message> element defines the data elements of an operation Each message can be the input or output of an operation, and may consist of one or more parts A part resembles a parameter of a function <message name=“GetLastTradePriceInput”> <part name=“body” element="TradePriceRequest"/> </message> <message name=“GetLastTradePriceOutput”> <part name=“body” element=“TradePrice” /> </message> 50 WSDL PORTTYPE端口类型 The <portType> element is the most important WSDL element: it defines a web Services <portType name=“StockQuotePortType”> the operations that can be performed, and <operation name=“GetLastTradePrice”> the messages that are involved <input message=“tns:GetLastTradePriceInput” /> The <port> defines the connection point to a web Services, an <output message=“tns:GetLastTradePriceOutput” /> instance of <portType> </operation> It can be compared to a function library (or a module, or a </portType> class) in a traditional programming language Each operation can be compared to a function in a traditional programming language 51 OPERATION TYPES操作类型 请求响应型是最常见的操作类型,定义的WSDL四种类型 : One-way:这次行动可以收到一个讯息,但不会返回响应 Request-response:这次行动可以接收请求,并返回响应 Solicit-response: 操作可以发送请求,并等待回应 Notification: 操作可以发送消息,但不会等待回应 WSDL 1.2 adds: request – multiple response 52 OPERATION TYPES操作类型 One-way: <message name="newTermValues"> <part name="term" type="xs:string"/> <part name="value" type="xs:string"/> </message> <portType name="glossaryTerms"> <operation name="setTerm"> <input name="newTerm" message="newTermValues"/> </operation> </portType > Request-response: <message name="getTermRequest"> <part name="term" type="xs:string"/> </message> <message name="getTermResponse"> <part name="value" type="xs:string"/> </message> <portType name="glossaryTerms"> <operation name="getTerm"> <input message="getTermRequest"/> <output message="getTermResponse"/> </operation> </portType> 53 BINDING绑定 定义消息如何传递,以及服务的位置 <binding> element有两个属性 : type: the port type name: name of the binding <soap:binding>有两个属性 : style: either “document” or “rpc” transport: protocol to use, e.g., “http” 54 BINDING绑定 <binding name="StockQuoteSoapBinding“ type="tns:StockQuotePortType"> <soap:binding style=“document” transport=“http://schemas.xmlsoap.org/soap/http” /> <operation name="GetLastTradePrice"> <soap:operation soapAction=“http://example.com/GetLastTradePrice” /> <input> <soap:body use=“literal” /> </input> <output> <soap:body use=“literal” /> </output> </operation> </binding> 55 SERVICES <Services name=“StockQuoteServices”> <documentation> My first Services </documentation> <port name=“StockQuotePort” binding=“tns:StockQuoteBinding”> <soap:address location=“http://example.com/stockquote” /> </port> </Services> 56 WSDL总结 Types Message Operation PortType Binding Port Service 57 WEB SERVICES FLOW DESCRIPTION 58 业务流程执行语言(BPEL) 把业务流程定义为相互协作的Web服务组; 定义抽象过程和可执行过程; 创建Web服务组合; 来自: 扎根在传统的流模型; 概念来源于结构化编程语言; 建立于核心的WSDL和XML规范之上; 融合WSFL和XLANG的概念。 OASIS WS BPEL 技术委员会正在推进 BPEL4WS规范 59 BPEL WEB SERVICES组合 60 BPEL PROCESS 结构 <process ...> <partners> ... </partners> <messageExchanges> ... </ messageExchanges > <correlationSets> ... </correlationSets> <faultHandlers> ... </faultHandlers> <compensationHandlers> ... </compensationHandlers> (activities)* 与过程交互的Web Services 过程使用的数据 用于支持异步交互 处理错误条件执行路径 当执行“undoing”命令时的执 行代码 过程实际做的工作 </process> 61 PARTNER的定义和联系 Partner通过web服务信道接受,由服务链接类型定义 A is accessed over a WS “channel <partner name=“...” ServicesLinkType=“...” partnerRole=“...” myRole=“...”/> SLT 定义两个角色,每一个角色需要 portTypes支持 <ServicesLinkType name=“...”> <role name=“..."> <portType name=“...” />* </role> <role name=“...”> <portType name=“...”/>* </role> </ServicesLinkType> 62 CONTAINERS(容器)的定义 <containers> <container name=“…" messageType=“…"/> <container name=“…" messageType=“…"/> </containers> 63 BPEL 基本活动 调用一个partner上的操作 <invoke partner=“...” portType=“...” operation=“...” inputContainer=“...” outputContainer=“...”/> 从partner接受调用 <receive partner=“...” portType=“...” operation=“...” container=“...” [createInstance=“...”] /> 发送应答消息给调用的partner <reply partner=“...” portType=“...” operation=“...” container=“...”/> 在容器间复制数据 <assign> <copy> <from container=“...”/> <to container=“...”/> </copy>+ </assign> 64 MORE基本活动 检测处理错误和切换到故障处理模式 <throw faultName=“...” faultContainer=“...”/> 活动可以用于立即放弃这个终止活动的业务流程实例中的所有执行。 <terminate/> 活动使流程能够等待一段特定的时间间隔,或者一直等到某个截止期限为止。 <wait for=“...”? until=“...”? /> 活动什么都不做。如果您需要捕获一个故障并抑制它,您可以使用这个活动。 <empty/> 65 BPEL 结构活动 <sequence> 活动包含一个或多个顺序执行的活动。 <sequence standard-attributes>activity+</sequence> <pick> 活动包含一组事件处理程序。每个处理程序包含一个活动,处 理程序在 pick 已经启动且处理程序正在等待事件发生时运行。 <pick createInstance="yes|no"? standard-attributes>…<pick> <flow> 构造提供了以并行方式运行活动的能力,还提供了定义防护性 链接的能力。 <flow standard-attributes>activity+</flow> <switch> 活动起的作用与许多传统编程语言中出现的 switch 构造很相 似。 <swith standard-attributes> <case>…</case> … <otherwise>…</otherwise> </swith> 66 MORE STRUCTURED ACTIVITIES <while> 活动重复执行它的子活动,直到对指定的布尔条件求得的值 不再为真。 <while standard-attributes><condition>..</condition>activity</while> <repeatUntil>活动也是执行它的子活动,与<while> 活动不同的是, 该活动直到对指定的布尔条件求得的值为真,才退出; <repeatUntil standard-attributes>activity <condition>…</condition></repeatUntil> <if>活动,首先判断<condition>中的条件是否满足,满足就执行,执行完退 出。否则,依次检查<elseif>中的<condition>中的条件是否满足,满足就执行, 执行完退出。如果都不满足则执行<else>中的活动; <if standard-attributes>standard-elements <condition>…</condition>activity <elseif>*<condition>…</condition>activity</elseif> <else>activity</else></if> 67 一个活动用例 <sequence> <sequence> <receive .../> Seq execute activities sequentially <flow> <flow> <sequence> Flow <invoke ... /> execute activities in parallel Seq Seq <while ... > <while> <assign> ... </assign> </while> iterate execution of activities until condition is violated <pick> </sequence> While <sequence> several event activities <receive ... /> (receive message, timer event) scheduled<invoke for execution ... /> in parallel; first one is selected and corresponding code executed </sequence> </flow> <link ...> <reply ... /> defines a control dependency between </sequence> a source activity and a target 68 WEB SERVICES 发布和发现 69 UDDI UDDI的服务器作为提供目录服务的服务提供商。 SOAP的可用于查询UDDI, 发现WSDL定义的服务的位 置,或在设计或开发时间搜寻可以通过用户界面执 行。 数据结构规范描述什么样的数据存储在UDDI。 序员的API规范包含UDDI如何注册可以访问。 副本规格说明包含如何复制注册信息。 70 UDDI 三个基本功能 发布:如何注册Web服务 搜索:如何找到一个特定Web服务 绑定:如何连接到一个Web服务 71 UDDI REGISTRIES UDDI的注册包含有关业务和服务信息 – Public registries – Private registries 72 PUBLIC REGISTRIES • IBM Public registry Registration: https://uddi.ibm.com/ubr/registry.html inquiryURL= https://uddi.ibm.com/ubr/inquiryapi publishURL= https://uddi.ibm.com/ubr/publishapi • Test registry Registration: https://uddi.ibm.com/testregistry/registry.html inquiryURL= https://uddi.ibm.com/testregistry/inquiryapi publishURL= https://uddi.ibm.com/testregistry/publishapi 73 PUBLIC REGISTRIES公共 注册 • HP Registration: http://uddi.hp.com inquiryURL = http://uddi.hp.com/ubr/inquire publishURL = https://uddi.hp.com/ubr/publish • Microsoft Registration: http://uddi.rte.microsoft.com inquiryURL=http://uddi.rte.microsoft.com/inquire publishURL=https://uddi.rte.microsoft.com/publish • SAP Registration: http://udditest.sap.com inquiryURL=http://uddi.sap.com/UDDI/api/inquiry/ publishURL=https://uddi.sap.com/UDDI/api/publish/ 74 私有UDDI 注册 • 为了测试目的经营你自己的注册表是; • 建立您自己的登录服务为自己的组织; • 信托问题 75 总结 • 作为一个分布式计算架构, Web服务是最重要的SOA的实 现; • Web服务:灵活的机械与机械交互; • XML是一种强大的语言来描述数据。由XML模式定义它的 构造规则。 • SOAP的是一种轻量的复杂的分布式对象技术替代,我们使 用的封装Web Services消息 • 通过WSDL开放Web服务功能和接口, Web服务之间的交 互用BPEL描述 • 通用描述,发现和集成( UDDI规范)是一种技术规范来描 述,发现和集成网络服务 76 REFERENCE SOA & Webservices.http://www-900.ibm.com/developerWorks/cn/ webservices WebServices. http://www.w3.org/2002/ws/ Ueli Wahli ,Thomas Kjaer…“WebSphere Version 6 Web Services Handbook Development&Deployment”,http://www.redbooks.ibm.com /redbooks/pdfs/sg246461.pdf,2005 Stan Kleijnen,Srikanth Raju.An Open Web Services Archiecture.NewYork: ACM Press,2003 XML.http://www.w3.org/TR/xml/ XML Schema. http://www.w3.org/TR/xmlschema11-1/ XML Schema. http://www.w3.org/TR/xmlschema11-2/ WSDL. http://www.w3.org/TR/wsdl Tony Andrews, Francisco Curbera…Business Process Execution Language for Web Services Version 1.1.http://download.boulder.ibm.com/ ibmdl/pub/software/dw/specs/ws-bpel/ws-bpel.pdf The Stencil Group.” The Evolution of UDDI ,UDDI.org White Paper” http://www.uddi.org/pubs/the_evolution_of_uddi_20020719.pdf Java Web Services Application and development, Publication House of 77