冯志勇天津大学计算机学院2008 年 10 月
1
SOA 模型 Web Services 概述 XML and XML Schema 通信协议 Web Services 描述 Web Services 流描述 Web Services 发布和发现
2
3
作为一个分布式计算架构, Web 服务是最重要的 SOA实现。
SOA 模型是 Web 服务体系结构的概念
4
角色 Services Provider Services Requestor Services Registry
操作 Publish Find Bind
关键组件 Services Services Description
5
标准 Web Services Description Language (WSDL) Universal Description, Discovery and Integration
(UDDI) Simple Object Access Protocol (SOAP) Web Service Flow Language (WSFL) ……
6
7
业务组件分析
服务识别
服务定义
组件识别
组件定义
服务实现决策
SOA 实现
8
已有应用资源
组件
服务
业务过程
服务提供者
服务消费者
Qo
s
安全管
理
集成架
构
9
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 location-transparent 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
11
Web: 灵活的人机交互 Web Services: Web 服务是最近制定的技术规范,充分利
用现有的开放标准如 XML , URL 和 HTTP 提供一个新的系统对系统通信标准
工作定义:通过标准化协议访问网络软件服务 简单对象访问协议 (SOAP) :非常灵活的远程过程调用
(RPC) 大量的利益在商业界,学术界,标准制定机构 , . . . 应用电子商务,电信,科技,资源数据库,政府,教育 , . . .
12
面向业务的 Web Services ERP,CRM , 应用系统集成
面向消费的 Web Services B2C 的网站,全国多方 B2C 系统
面向设备的 Web Services 支持不同种类的服务。如天气报告,电子邮件服务
面向系统的 Web Services 认证 监测 QoS
13
简化和 / 或自动化 Web 服务 发现
是什么性质应予以说明? 如何有效地对他们的查询?
组合 指定组成的目标 ; 指定组成的约束 ; 构建一个组成 ; 分析组成的成分。
调用 保持颁布分离 ; 提供交易担保。
监控 如何跟踪条款 ; 回收失败条款。
14
15
传输层 : HTTP, SMTP, FTP, etc.
XML 消息层 : SOAP , WS-Addressing, WS-Notification, WS-Eventing, WS-Enumeration, WS-MessageDelivery, WS-Reliability,WS Reliable Messaging, WS-Resources WS-Transfer
服务描述层 : WSDL, WSCL, WSCI,WS-MetadataExchange, WS-Policy
Web Services 组合 :
WSFL,BPEL4WS WS-CDL WS-CAF
发布和发现 :UDDI,WSIL
, WS-Discovery
Web Services 事务 :
WS-Coordination WS-Transaction
WS-AtomicTransaction WS-BusinessActivity
Web Services Management:
WSDM, WS-Manageability SPML, WS-Provisioning
Web Services 安全 :
XML-Encryption XML-Signature WS-Security
WS-SecureConversation
WS-SecurityPolicy
WS-Trust
16
数据
类型
接口
行为
消息
BPEL
Web Service 标准 实现平台
WSDL
SOAP
XML Schema
XML Su
n J2
EE
Mic
roso
ft .
Net
IBM
Web
Sp
here
17
标签:部分数据的标签 元素和子元素:数据节以 <tagname> 开始和 </
tagname> 结束 , 内容必须正确匹配 正确匹配
<account> … <balance> …. </balance> </account>
不正确匹配 <account> … <balance> …. </account> </balance>
正确:对同一父元素 , 每一个开始标签必须有一个独特的匹配的结束标记 .
18
每个文件必须有一个顶层元素
<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>. .
</bank>
19
Top level element
属性 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
元素和属性之间的区别属性不能嵌套
<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
Well-Formed XML 文档 文件中只有一个外层元素 ( 称之为根元素 ) 每个元素包含一个开始和相应的结束标记 标签不能重叠 , 如
<author><name>LeeHong</author></name>
在属性的一个因素具有唯一的名字 元和标记名称必须是合法的
22
树模型的 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 namespacedatatype namespacemyNS namespace
26
数据库 schemas限制哪些信息可以存储,存储值的数据类型
Schemas 对 XML 数据交换非常重要 否则,一个网站不能自动解释从另一个网站收到的数据
XML schema两个机制 Document Type Definition (DTD) XML Schema
27
XML Schema Supports 数据类型
E.g. integer, string, etc Also, constraints on min/max values
用户定义类型 不像 DTDs , XML Schema 采用 XML语法 ,
更多标准表达,且更细致 与命名空间集成 (reuse and refine) 更多特征
列表 , 唯一和外键约束 ,继承 ..
28
29
<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>
元素类型 <element name=". . ."/> with possible attributes:
‘类型’属性定义该元素的类型 : type=". . ." (more on types later) 基数限制 :
• minOccurs=“x”, x 可以使任何自然数 ( 包括 0)• maxOccurs=“x”, x 可以使任何自然数 ( 包括
0) 或无限
30
<element name="email"/><element name="head" minOccurs="1" maxOccurs="1"/><element name="to" minOccurs="1"/>
31
属性类型<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
<attribute name="id" type="ID" use="required"/>
<attribute name="speaks" type="LanguageType" use="default“ value="en"/>
33
数据类型 数字数据类型 , 包括: integer, Short, Byte,
Long, Float, Decimal 串类型 , 包括: string, ID, IDREF, CDATA,
Language 日期和时间类型
用户定义的数据类型 简单数据类型和复杂数据类型
34
复杂数据类型和扩展数据类型 通过对子 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
<element name=“lecture” type=“lectureType”>
<lecture title=“associate professor”><firstname>Zhiyong</firstname><lastname>Feng</lastname>
</lecture>
36
数据类型扩展:现有的数据类型可以扩展新的元素或属性 <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
限制数据类型和简单的数据类型 通过限制现有的数据类型 定义<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
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
40
SOAP 协议是一个 XML 消息传递协议,独立于任何特定的传输协议。
替代复杂的分布式对象技术的轻量级协议 开始为 BizTalk (Microsoft/UserLand/DevelopMentor )
设计 是 W3C 标准 基于 XML
41
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 Envelope
SOAP Header
Header Block
Header Block
. . .
SOAP Body
Body Block
Body Block
. . .
43
<?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>
<?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
45
WSDL 是一种基于 XML 的接口定义语言 , 它分离功能和执行,使 SOA 建议的契约设计成为可能
WSDL 定义 Services 做什么– 接口 interface 访问说明 – how Services 位置– where
46
47
OperationPort Type
MessageBinding
Port Services
Supports
Input & Output
Provides
How to encode
Formats & ProtocolsHow to invoke
Implements
接口
访问说明
Endpoints
48
<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>
<types> define data types used in defining messages
XML Schema, DTD, and etc. XML Schema must be supported by any vendor of
WSDL conformant products
49
<types> <schema targetNamespace=“http://example.com/stockquote.xsd”
xmlns=“http://www.w3.org/2000/10/XMLSchema”> <element name=“TradePriceRequest”>
<complexType> <all>
<element name=“tickerSymbol” type=“string“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>
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
50
<message name=“GetLastTradePriceInput”> <part name=“body” element="TradePriceRequest"/>
</message>
<message name=“GetLastTradePriceOutput”> <part name=“body” element=“TradePrice” />
</message>
The <portType> element is the most important WSDL element: it defines a web Services the operations that can be performed, and the messages that are involved
The <port> defines the connection point to a web Services, an instance of <portType> It can be compared to a function library (or a
module, or a class) in a traditional programming language
Each operation can be compared to a function in a traditional programming language
51
<portType name=“StockQuotePortType”> <operation name=“GetLastTradePrice”> <input message=“tns:GetLastTradePriceInput” /> <output message=“tns:GetLastTradePriceOutput” /> </operation> </portType>
请求响应型是最常见的操作类型,定义的 WSDL四种类型 : One-way: 这次行动可以收到一个讯息,但不会返回响应
Request-response: 这次行动可以接收请求,并返回响应
Solicit-response: 操作可以发送请求,并等待回应
Notification: 操作可以发送消息,但不会等待回应
WSDL 1.2 adds: request – multiple response
52
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> 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 name="StockQuoteSoapBinding“type="tns:StockQuotePortType"> <soap:binding style=“document”
transport=“http://schemas.xmlsoap.org/soap/http” />
<operation name="GetLastTradePrice"> <soap:operationsoapAction=“http://example.com/
GetLastTradePrice” /> <input> <soap:body use=“literal” /> </input> <output> <soap:body use=“literal” /> </output> </operation>
</binding> 55
56
<Services name=“StockQuoteServices”> <documentation>
My first Services</documentation> <port name=“StockQuotePort”
binding=“tns:StockQuoteBinding”> <soap:address
location=“http://example.com/stockquote” /> </port>
</Services>
Types Message Operation PortType Binding Port Service
57
58
把业务流程定义为相互协作的 Web 服务组 ; 定义抽象过程和可执行过程 ; 创建 Web 服务组合 ; 来自 :
扎根在传统的流模型 ; 概念来源于结构化编程语言 ; 建立于核心的 WSDL 和 XML 规范之上 ; 融合 WSFL 和 XLANG 的概念。
OASIS WS BPEL 技术委员会正在推进 BPEL4WS 规范
59
60
与过程交互的 Web Services
过程使用的数据
用于支持异步交互 处理错误条件执行路径
当执行“ undoing”命令时的执行代码
过程实际做的工作61
<process ...>
<partners> ... </partners><messageExchanges> ... </ messageExchanges ><correlationSets> ...</correlationSets><faultHandlers> ...</faultHandlers><compensationHandlers> ...</compensationHandlers>
(activities)*
</process>
Partner 通过 web 服务信道接受,由服务链接类型定义 A is accessed over a WS “channel
SLT 定义两个角色 , 每一个角色需要 portTypes 支持
62
<partner name=“...” ServicesLinkType=“...” partnerRole=“...” myRole=“...”/>
<ServicesLinkType name=“...”><role name=“...">
<portType name=“...” />*</role><role name=“...”>
<portType name=“...”/>*</role>
</ServicesLinkType>
<containers> <container name=“…"
messageType=“…"/> <container name=“…"
messageType=“…"/> </containers>
63
调用一个 partner上的操作
从 partner 接受调用
发送应答消息给调用的 partner
在容器间复制数据
64
<assign><copy>
<from container=“...”/> <to container=“...”/>
</copy>+</assign>
<invoke partner=“...” portType=“...” operation=“...”
inputContainer=“...” outputContainer=“...”/><receive partner=“...” portType=“...” operation=“...” container=“...” [createInstance=“...”] />
<reply partner=“...” portType=“...” operation=“...” container=“...”/>
检测处理错误和切换到故障处理模式
活动可以用于立即放弃这个终止活动的业务流程实例中的所有执行。
活动使流程能够等待一段特定的时间间隔,或者一直等到某个截止期限为止。
活动什么都不做。如果您需要捕获一个故障并抑制它,您可以使用这个活动。
65
<wait for=“...”? until=“...”? />
<throw faultName=“...” faultContainer=“...”/>
<terminate/>
<empty/>
<sequence> 活动包含一个或多个顺序执行的活动。
<pick> 活动包含一组事件处理程序。每个处理程序包含一个活动,处理程序在 pick 已经启动且处理程序正在等待事件发生时运行。
<flow> 构造提供了以并行方式运行活动的能力,还提供了定义防护性链接的能力。
<switch> 活动起的作用与许多传统编程语言中出现的 switch 构造很相似。
66
<flow standard-attributes>activity+</flow>
<sequence standard-attributes>activity+</sequence>
<pick createInstance="yes|no"? standard-attributes>…<pick>
<swith standard-attributes><case>…</case>
…<otherwise>…</otherwise>
</swith>
<while> 活动重复执行它的子活动,直到对指定的布尔条件求得的值不再为真。
<repeatUntil> 活动也是执行它的子活动,与<while> 活动不同的是,该活动直到对指定的布尔条件求得的值为真,才退出;
<if> 活动,首先判断<condition> 中的条件是否满足,满足就执行,执行完退出。否则,依次检查 <elseif> 中的 <condition> 中的条件是否满足,满足就执行,执行完退出。如果都不满足则执行 <else> 中的活动;
67
<if standard-attributes>standard-elements<condition>…</condition>activity<elseif>*<condition>…</condition>activity</elseif><else>activity</else></if>
<while standard-attributes><condition>..</condition>activity</while>
<repeatUntil standard-attributes>activity<condition>…</condition></repeatUntil>
68
<sequence>execute activities sequentially
<flow>execute activities in parallel
<while>iterate execution of activities until condition is violated
<pick>several event activities (receive message, timer event) scheduled for execution in parallel; first one is selected and corresponding code executed
<link ...>defines a control dependency betweena source activity and a target
FlowSeq Seq
Seq
While
<sequence><receive .../><flow>
<sequence><invoke ... /><while ... >
<assign> ... </assign>
</while></sequence><sequence>
<receive ... /><invoke ... />
</sequence></flow><reply ... />
</sequence>
69
UDDI 的服务器作为提供目录服务的服务提供商。 SOAP 的可用于查询 UDDI, 发现 WSDL 定义的服务的位置,或在设计或开发时间搜寻可以通过用户界面执行。
数据结构规范描述什么样的数据存储在 UDDI 。 序员的 API 规范包含 UDDI 如何注册可以访问。 副本规格说明包含如何复制注册信息。
70
三个基本功能 发布:如何注册Web 服务 搜索:如何找到一个特定 Web 服务 绑定:如何连接到一个 Web 服务
71
UDDI 的注册包含有关业务和服务信息
– Public registries – Private registries
72
73
• IBM Public registry Registration: https://uddi.ibm.com/ubr/registry.htmlinquiryURL= 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
74
• 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/
75
• 为了测试目的经营你自己的注册表是 ;
• 建立您自己的登录服务为自己的组织 ;
• 信托问题
76
• 作为一个分布式计算架构, Web 服务是最重要的 SOA 的实现 ;
• Web 服务:灵活的机械与机械交互 ; • XML 是一种强大的语言来描述数据。由 XML 模式定义它
的构造规则。 • SOAP 的是一种轻量的复杂的分布式对象技术替代,我们使用的封装Web Services 消息
• 通过 WSDL 开放 Web 服务功能和接口, Web 服务之间的交互用 BPEL 描述
• 通用描述,发现和集成( UDDI 规范)是一种技术规范来描述,发现和集成网络服务
77
SOA & Webservices.http://www-900.ibm.com/developerWorks/cn/ webservicesWebServices. 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,2005Stan Kleijnen,Srikanth Raju.An Open Web Services Archiecture.NewYork: ACM Press,2003XML.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/wsdlTony 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.pdfThe Stencil Group.” The Evolution of UDDI ,UDDI.org White Paper” http://www.uddi.org/pubs/the_evolution_of_uddi_20020719.pdfJava Web Services Application and development, Publication House of