33
DAT340 DAT340 实现异步消息队列 实现异步消息队列 实现异步消息队列 实现异步消息队列 --- ---SQL Server 2005 Service SQL Server 2005 Service Broker Broker

SQL Server 2005 Service SQL Server 2005 Service Brokerdownload.microsoft.com/download/3/7/3/3738f4f6-56eb-4c81-8a3b... · Service Broker Service Broker 消息处理事务 ... 事务性消息传送不需要分布式事务

  • Upload
    buidung

  • View
    289

  • Download
    1

Embed Size (px)

Citation preview

DAT340DAT340

实现异步消息队列实现异步消息队列实现异步消息队列实现异步消息队列------SQL Server 2005 Service SQL Server 2005 Service BrokerBroker

异步式编程环境和队列功能异步式编程环境和队列功能 ––异步式编程环境和队列功能异步式编程环境和队列功能 ––Service BrokerService Broker

回答三个问题:回答三个问题:

什么时候需要异步队列操作什么时候需要异步队列操作??什么时候需要异步队列操作什么时候需要异步队列操作??

Service Broker Service Broker 是什么是什么??

为什么要在数据库中处理消息为什么要在数据库中处理消息??为什么要在数据库中处理消息为什么要在数据库中处理消息??

具体实现与演示具体实现与演示

当数据库项目开始的时候当数据库项目开始的时候

Architecture of Data AccessArchitecture of Data Access

Database Application SQL Server

Architecture of Data Access Architecture of Data Access TechnologiesTechnologies

VBSQL.OXCVBSQL.OXC ADOADO.NET .NET

Framework Framework

Storage Storage EngineEngine

OLEDBOLEDB

DBDB--LibraryLibrary

SQLNCLISQLNCLI

Data ProviderData Providerfor SQLfor SQLServerServer

Relational Relational EngineEngine

TSQL E d i tTSQL E d i tOLEDBOLEDB SQLNCLISQLNCLI

Client Network LibraryClient Network Library

TSQL EndpointTSQL Endpoint

Server Network LibraryServer Network Library

重新考虑软件构架中的因素重新考虑软件构架中的因素

功能性 技术因素

性能

功能性

恢复力

技术因素

区别- 没有运动的部分

吞吐量 失效安全

没有 动 部分- 可以创建新材料- 可以改变物理现象

避免失败

容量 容错能力

避免失败- 将关键部分分散开来- 语义上的一致性职责分散容量

可用性

容错能力 - 职责分散

Have an architecture that makes sense before you write 3.5 millionlines of code.

在超市里排队在超市里排队

在机场里排队在机场里排队☺☺

异步消息队列的使用场景异步消息队列的使用场景

股票交易系统中的结算活动股票交易系统中的结算活动股票交易系统中的结算活动股票交易系统中的结算活动

订单输入系统中的发货信息订单输入系统中的发货信息

旅行预订系统在客户填写完路线后再进行实际的旅行预订系统在客户填写完路线后再进行实际的旅行预订系统在客户填写完路线后再进行实际的旅行预订系统在客户填写完路线后再进行实际的预订预订,,并在预订完成后发达确认电子邮件并在预订完成后发达确认电子邮件

Q2 Services Broker是什么?Q2:Services Broker是什么?

为什么排队应用程序难以编写?为什么排队应用程序难以编写?

消息的完整性消息的完整性消息的完整性消息的完整性

分布分布

事务性消息传送事务性消息传送事务性消息传送事务性消息传送

The Architecture of SQL Server The Architecture of SQL Server 20052005

Analysis Services

Integration Services

Reporting Services

Notification ServicesServices Services Services Services

SQL Server 2005 Database Engine Replication

Full-Text Search

Service Broker

SQL Server SQL Server Agent

Service Broker Service Broker 是什么是什么??

Platform for building asynchronous queuedPlatform for building asynchronous queuedPlatform for building asynchronous queued Platform for building asynchronous queued database applicationsdatabase applications

Queues as first class database objectsQueues as first class database objectsQueues as first class database objectsQueues as first class database objects

Queue manipulation built into TSQLQueue manipulation built into TSQL

Transactional message processingTransactional message processing

Reliable distributed queuingReliable distributed queuing

考虑使用考虑使用 Service BrokerService Broker的因素的因素

Factors to consider

Large messagesSecurity Large messages

Reliable messaging Heterogeneous data

Security

Use Service Broker when you need to:

g g g

Scale out the systemImprove reliability and availabilityImprove application response timeBuild an auditing systemMaintain up-to-date cached data

Service Broker Service Broker 消息处理事务的步消息处理事务的步骤骤

1. 1. 开始事务。开始事务。开始事务开始事务

2. 2. 从会话组中接收一条或多条消息。从会话组中接收一条或多条消息。

3. 3. 从状态表中检索会话的状态。从状态表中检索会话的状态。

4. 4. 处理消息并根据消息内容对应用程序数据进行一项或多处理消息并根据消息内容对应用程序数据进行一项或多项更新。项更新。

55 发送 些发送 些S i B kS i B k 消息 即 将响应发送到传消息 即 将响应发送到传5. 5. 发送一些发送一些Service Broker Service Broker 消息:即,将响应发送到传消息:即,将响应发送到传入的消息或将消息发送到处理传入消息所需的其他服务。入的消息或将消息发送到处理传入消息所需的其他服务。

6.6. 如果此会话组包含其他消息,则读取和处理此会话组中如果此会话组包含其他消息,则读取和处理此会话组中6. 6. 如果此会话组包含其他消息,则读取和处理此会话组中如果此会话组包含其他消息,则读取和处理此会话组中的其他消息。的其他消息。

7. 7. 使用新的会话状态更新会话状态表。使用新的会话状态更新会话状态表。

8. 8. 提交事务提交事务

Q3 为什么要在数据库中处理消息?Q3:为什么要在数据库中处理消息?

为什么要在数据库中进行消息传送?为什么要在数据库中进行消息传送?消息和数据的单客户端连接。消息和数据的单客户端连接。

事务性消息传送不需要分布式事务或两阶段提交。事务性消息传送不需要分布式事务或两阶段提交。

备份和恢复数据库还可以备份和恢复消息队列。备份和恢复数据库还可以备份和恢复消息队列。

群集或数据库镜像群集或数据库镜像

确定队列中的操作非常简单确定队列中的操作非常简单

在提交事务时只需写入单个日志在提交事务时只需写入单个日志在提交事务时只需写 单个在提交事务时只需写 单个

可靠的消息传送通常将消息从传送队列传送到接收队列可靠的消息传送通常将消息从传送队列传送到接收队列

队列读取器管理队列读取器管理队列读取器管理队列读取器管理

ActivationActivation

具体实现与演示具体实现与演示

Service Broker Service Broker 系统架构系统架构

SQL Server ObjectSQL Server Object DescriptionDescription

M TM T D fi lid f h b iD fi lid f h b iMessage TypeMessage Type Defines valid messages for exchange between servicesDefines valid messages for exchange between services

ContractContractSpecifies type of messages and their direction in a Specifies type of messages and their direction in a conversation, initiator or targetconversation, initiator or target

S b f di d f iS b f di d f iQueueQueue

Stores messages before sending and after receipt as Stores messages before sending and after receipt as result setresult set

Service ProgramService ProgramThe part of a service broker application that reads The part of a service broker application that reads messages from a queue and processes them.messages from a queue and processes them.

ServiceService Addressable endpoint for service communicationAddressable endpoint for service communication

QueueMessage type

S i Contract Service programContract Service

如何实现如何实现 Service BrokerService Broker

How to Enable Service Broker in a How to Enable Service Broker in a DatabaseDatabase

Implementing ServicesImplementing Services

1.1. How to Create ContractsHow to Create Contracts

2.2. How to Create QueuesHow to Create Queues

3.3. How to Create ServicesHow to Create Services

4.4. How to Send a MessageHow to Send a Message

5.5. How to Receive a MessageHow to Receive a Message

How to Enable Service Broker in a DatabaseHow to Enable Service Broker in a Database

Check Service Broker status1

SELECT is broker enabledSELECT is_broker_enabledFROM sys.databasesWHERE database_id = db_id()

Enable Service Broker2

ALTER DATABASE AdventureWorksSET ENABLE_BROKER

Implementing ServicesImplementing Services

Design:Design:

Message typesC t tContractsQueuesServicesService program logic

1.How to Create Contracts1.How to Create Contracts

Create message types1 Create message types1

CREATE MESSAGE TYPE CREATE MESSAGE TYPE [//Ad t[//Ad t W k /E /E Cl i ]W k /E /E Cl i ][//Adventure[//Adventure--Works.com/Expenses/ExpenseClaim] Works.com/Expenses/ExpenseClaim] VALIDATION = WELL_FORMED_XML VALIDATION = WELL_FORMED_XML

CREATE MESSAGE TYPE CREATE MESSAGE TYPE [//Adventure[//Adventure--Works.com/Expenses/ClaimResponse] Works.com/Expenses/ClaimResponse] VALIDATION = VALID XML WITH SCHEMA COLLECTION awschemasVALIDATION = VALID XML WITH SCHEMA COLLECTION awschemas

Create contract2

VALIDATION = VALID_XML WITH SCHEMA COLLECTION awschemasVALIDATION = VALID_XML WITH SCHEMA COLLECTION awschemas

CREATE CONTRACT CREATE CONTRACT [//Adventure[//Adventure--Works.com/Expenses/ProcessExpense] Works.com/Expenses/ProcessExpense]

//// / // /( [//Adventure( [//Adventure--Works.com/Expenses/ExpenseClaim] Works.com/Expenses/ExpenseClaim] SENT BY INITIATOR, SENT BY INITIATOR, [// Adventure[// Adventure--Works.com/Expenses/ClaimResponse] Works.com/Expenses/ClaimResponse] SENT BY TARGET )SENT BY TARGET )

2.How to Create Queues2.How to Create Queues

C t 1 Create queue name1

Choose whether queue is available2

Specify any activation parameters3

CREATE QUEUE ExpenseQueueCREATE QUEUE ExpenseQueue

CREATE QUEUE ExpenseQueueWithActivationCREATE QUEUE ExpenseQueueWithActivationWITH STATUS = OFF, WITH STATUS = OFF, ACTIVATION ( ACTIVATION (

PROCEDURE_NAME = ProcessExpense, PROCEDURE_NAME = ProcessExpense, MAX QUEUE READERS = 5, MAX QUEUE READERS = 5, _Q _ ,_Q _ ,EXECUTE AS SELF)EXECUTE AS SELF)

3.How to Create Services3.How to Create Services

Create service name1

Choose storage queue2 g q

List contracts for which the service is a target

Ch h th t t i f 3

Choose whether to retain messages for conversation lifetime4

CREATE SERVICE [//AdventureCREATE SERVICE [//Adventure--Works.com/SubmitExpense] Works.com/SubmitExpense] ON QUEUE ExpenseQueueON QUEUE ExpenseQueue( [//Ad t( [//Ad t W k /E /P E ] )W k /E /P E ] )( [//Adventure( [//Adventure--Works.com/Expenses/ProcessExpense] )Works.com/Expenses/ProcessExpense] )

4.How to Send a Message4.How to Send a Message

Declare a dialog handle variable1 Declare a dialog handle variable1

Begin a dialog conversation2

Send the message3

DECLARE @dialog_handle uniqueidentifierDECLARE @dialog_handle uniqueidentifierDECLARE @dialog_handle uniqueidentifierDECLARE @dialog_handle uniqueidentifier

BEGIN DIALOG CONVERSATION @dialog_handleBEGIN DIALOG CONVERSATION @dialog_handleFROM SERVICE [//AdventureFROM SERVICE [//Adventure--Works com/SubmitExpense]Works com/SubmitExpense]

DECLARE @dialog_handle uniqueidentifierDECLARE @dialog_handle uniqueidentifier

BEGIN DIALOG CONVERSATION @dialog_handleBEGIN DIALOG CONVERSATION @dialog_handleFROM SERVICE [//AdventureFROM SERVICE [//Adventure--Works com/SubmitExpense]Works com/SubmitExpense]FROM SERVICE [//AdventureFROM SERVICE [//Adventure Works.com/SubmitExpense]Works.com/SubmitExpense]TO SERVICE '//AdventureTO SERVICE '//Adventure--Works.com/ProcessExpense'Works.com/ProcessExpense'ON CONTRACT [//AdventureON CONTRACT [//Adventure--Works.com/Expenses/ProcessExpense]Works.com/Expenses/ProcessExpense]

FROM SERVICE [//AdventureFROM SERVICE [//Adventure Works.com/SubmitExpense]Works.com/SubmitExpense]TO SERVICE '//AdventureTO SERVICE '//Adventure--Works.com/ProcessExpense'Works.com/ProcessExpense'ON CONTRACT [//AdventureON CONTRACT [//Adventure--Works.com/Expenses/ProcessExpense]Works.com/Expenses/ProcessExpense]

SEND ON CONVERSATION @dialog_handleSEND ON CONVERSATION @dialog_handleMESSAGE TYPE [//AdventureMESSAGE TYPE [//Adventure--Works.com/Expenses/ExpenseClaim]Works.com/Expenses/ExpenseClaim](@msgString)(@msgString)

5.How to Receive a Message5.How to Receive a Message

Declare variables for storing message details1 g g1

Call RECEIVE statement2

Ch k th t d di l3 Check the message type, and process accordingly3

If finished conversation, call END CONVERSATION4DECLARE @conversation UNIQUEIDENTIFIERDECLARE @conversation UNIQUEIDENTIFIERDECLARE @msg NVARCHAR(MAX), @msgType NVARCHAR(256)DECLARE @msg NVARCHAR(MAX), @msgType NVARCHAR(256)DECLARE @conversation UNIQUEIDENTIFIERDECLARE @conversation UNIQUEIDENTIFIERDECLARE @msg NVARCHAR(MAX), @msgType NVARCHAR(256)DECLARE @msg NVARCHAR(MAX), @msgType NVARCHAR(256)DECLARE @conversation UNIQUEIDENTIFIERDECLARE @conversation UNIQUEIDENTIFIERDECLARE @msg NVARCHAR(MAX), @msgType NVARCHAR(256)DECLARE @msg NVARCHAR(MAX), @msgType NVARCHAR(256)DECLARE @conversation UNIQUEIDENTIFIERDECLARE @conversation UNIQUEIDENTIFIERDECLARE @msg NVARCHAR(MAX), @msgType NVARCHAR(256)DECLARE @msg NVARCHAR(MAX), @msgType NVARCHAR(256)

;RECEIVE TOP(1) @conversation = conversation_handle,;RECEIVE TOP(1) @conversation = conversation_handle,@msgType = message_type_name, @msg = message_body@msgType = message_type_name, @msg = message_bodyFROM ExpenseQueueFROM ExpenseQueue

;RECEIVE TOP(1) @conversation = conversation_handle,;RECEIVE TOP(1) @conversation = conversation_handle,@msgType = message_type_name, @msg = message_body@msgType = message_type_name, @msg = message_bodyFROM ExpenseQueueFROM ExpenseQueue

@ //@ // / / i/ / i

RECEIVE TOP(1) @conversation = conversation_handle,RECEIVE TOP(1) @conversation = conversation_handle,@msgType = message_type_name, @msg = message_body@msgType = message_type_name, @msg = message_bodyFROM ExpenseQueueFROM ExpenseQueue

@ //@ // / / i/ / iIF (@msgType = '//AdventureIF (@msgType = '//Adventure--Works.com/Expenses/ExpenseClaim')Works.com/Expenses/ExpenseClaim')---- process @msg …process @msg …

IF (@msgType = '//AdventureIF (@msgType = '//Adventure--Works.com/Expenses/ExpenseClaim')Works.com/Expenses/ExpenseClaim')---- process @msg …process @msg …

END CONVERSATION @conversationEND CONVERSATION @conversation

ResourcesResourcesTechnical Chats and Webcastshttp://www.microsoft.com/communities/chats/default.mspxhttp://www microsoft com/usa/webcasts/default asphttp://www.microsoft.com/usa/webcasts/default.asp

Microsoft Learning and Certificationhttp://www.microsoft.com/learning/default.mspx

MSDN & TechNet http://microsoft.com/msdnhttp://microsoft com/technethttp://microsoft.com/technet

Virtual Labshttp://www.microsoft.com/technet/traincert/virtuallab/rms.mspx

Newsgroupshttp://communities2.microsoft.com/communities/newsgroups/en-us/default.aspxcommunities/newsgroups/en us/default.aspx

Technical Community Siteshttp://www.microsoft.com/communities/default.mspx

User Groupshttp://www.microsoft.com/communities/usergroups/default.mspx

ResourcesResources

BlogsBlogs

http://blogs.msdn.com/remusrusanu/http://blogs.msdn.com/remusrusanu/

http://rushi.desai.name/Blog/tabid/54/BlogID/1/Default.aspxhttp://rushi.desai.name/Blog/tabid/54/BlogID/1/Default.aspx

http://blogs.msdn.com/rogerwolterblog/default.aspxhttp://blogs.msdn.com/rogerwolterblog/default.aspxp:// og . .co / oge o e og/ e . pp:// og . .co / oge o e og/ e . p

SamplesSamples

http://www.gotdotnet.com/codegallery/codegallery.aspx?id=9f7ahttp://www.gotdotnet.com/codegallery/codegallery.aspx?id=9f7ae2afe2af--31aa31aa--44dd44dd--9ee89ee8--6b6b6d3d63196b6b6d3d6319e2afe2af--31aa31aa--44dd44dd--9ee89ee8--6b6b6d3d63196b6b6d3d6319

ForumForum

http://forums.microsoft.com/msdn/ShowForum.aspx?ForumID=91http://forums.microsoft.com/msdn/ShowForum.aspx?ForumID=91

© 2006 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not

be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

附附::为什么要在数据库中处理消息为什么要在数据库中处理消息??

Integrated API for Messages and DataIntegrated API for Messages and DataSingle connection for messaging and other database Single connection for messaging and other database g g gg g goperationsoperations

Transactional receives/sends from remote systemsTransactional receives/sends from remote systems

Integrated management deployment andIntegrated management deployment andIntegrated management, deployment, and Integrated management, deployment, and operationsoperations

Backup, restore, configuration, monitoring, securityBackup, restore, configuration, monitoring, securityp, , g , g, yp, , g , g, y

Startup and shutdown start, recover and stop Service Startup and shutdown start, recover and stop Service BrokerBroker

Queued messages and application session state may beQueued messages and application session state may beQueued messages and application session state may be Queued messages and application session state may be queriedqueried

Trace Events for monitoring and debuggingTrace Events for monitoring and debugging

Messages have database integrity and recoverabilityMessages have database integrity and recoverabilityMessages have database integrity and recoverabilityMessages have database integrity and recoverability

附附::为什么要在数据库中处理消息为什么要在数据库中处理消息??

Performance advantages for database Performance advantages for database applicationsapplications

No twoNo two--phase commits for transactional messagingphase commits for transactional messaging

Single log write on commitSingle log write on commit

No process boundary crossing to external messagingNo process boundary crossing to external messagingNo process boundary crossing to external messaging No process boundary crossing to external messaging softwaresoftware

Optimized “inOptimized “in--instance” deliveryinstance” delivery