SQLServer -ServiceBroker

1.ServiceBroker简介

直接把文档原话copy过来吧:

SQL Service Broker is one of the best recent features in SQL Server 2005 because it makes it

possible to write queuing and message-based applications. With Service Broker, database

developers can write asynchronous applications to communicate between databases or servers and

can easily build distributed, secure, and reliable applications.

简单来说,Service Broker就是个消息队列,并且可以用来写分布式的异步的应用程序。

 

2.ServiceBroker的结构:

Service Broker的建立还是有点复杂的,说具体情况前:

先说下Service Broker基本构成:message type,

然后是根据message type创建的contracts,

接着是queue,

最后是基于queue和contracts建立的service,

以上的message type,contract,queue,还有service是service broker的基本构成。

另外servicebroker是可以在同一个sqlserver实例上两个数据库间进行,也可以在两个sqlserver实例间进行(当然这个的配置会比

前者麻烦的多,但也只有这种的模式才更有意义),所以实现servicebroker至少需要两个数据库,一个发起数据库,一个目标数据库。

 

3.ServiceBroker的创立

在创立之前,先要执行以下脚本修改数据库的TRUSTWORTHY属性, 

如果不是在两个sqlserver实例间,而只是在同一sqlserver实例中的两个数据库间使用servicebroker就需要打开这个属性。

如果是两个sqlserver实例间,因为会使用transport的安全(这个跟看的书讲的有点差别,书上说的可以不开启这个属性的情况

只有在使用证书安全并启了会话加密的情况才不需要设置这个属性)就不需要开启这个属性了。 

另外还有检查当前数据库是不是开启了servicebroker功能,这个功能默认可能是关闭的,如果是关闭的,可以在要使用的数据库

的属性窗口的选项页中,开启这个功能。

修改trustworth设置和检查servicebroker是否开启的脚本:

/*If you are using certificate security with dialog encryption ON (explained later in

the chapter), then setting the TRUSTWORTHY bit to ON is not necessary.

在两个sqlserver实例间进行servicebroker,也不需要将其设置为on,即使endpoint的验证使用的是windows模式*/

ALTER DATABASE dbname SET TRUSTWORTHY ON
GO
use initialDB
GO


/*如果is_broker_enabled没有开启,则需要在数据库属性中打开,否则

无法使用servicebroker*/

select service_broker_guid,is_broker_enabled,is_trustworthy_on

from sys.databases where database_id=DB_ID()

go

 

创建message type的脚本,这里建立了三个message type(比较懒,就没创立xml类型的消息,只建立了个任意类型的消息,另外

消息类型名起的也不标准,准确具体的参见sql的联机丛书吧,下面的各命令也都一样,具体的参见sql联机帮助吧):

create message type mtesttype

validation=none

go

create message type mtesttype1

validation=none

go

create message type mtesttype2

validation=none

go

 

创建合同contract,这里是根据上面创建的三个消息类型,定义mtesttype只能被初始数据库发送,type1只能由目标数据库发出,

type2可以被任意(也就是初始和目标)数据库发送。

create contract mtestcontract
(
mtesttype sent by initiator,
mtesttype1 sent by target,
mtesttype2 sent by any
)


下面是创建一个在数据库收到消息的时候自动执行的存储过程,这个存储过程将用在创建队列的时候使用,因为这个存储过程是建立在发起数据库上的,所以在这个存储过程里只接收了消息,并对消息的类型进行了判断,然后根据消息类型执行结束会话或者将从目标数据库发来的消息插入一张表中。
create proc processmsg
as
declare 
@msgtype varchar(255),
@msgbody varchar(255),
@cid uniqueidentifier
begin try
begin tran
waitfor(
/*You can also specify a WHERE clause in the RECEIVE statement, but it is not a full-featured WHERE
clause. You can only specify a conversation_handle or conversation_group_id.*/
receive top(1)
@msgtype=message_type_name,
@msgbody=case when validation='N'
then cast(message_body as varchar(255))
end,
@cid=conversation_handle
from dbo.testqueue),timeout 3000
if(@@ROWCOUNT = 0 or @@ERROR <> 0)
begin
rollback tran
goto done
end
else
begin
if @msgtype = 'mtesttype1'
begin
insert into t2(msg,cid,mtype) values(@msgbody,@cid,@msgtype)
end
if @msgtype = 'http://schemas.microsoft.com/SQL/ServiceBroker/EndDialog'
begin
end conversation @cid
end
end
commit tran
end try
begin catch
while(@@TRANCOUNT > 0) rollback tran
select ERROR_NUMBER() as en,ERROR_LINE() as el,ERROR_MESSAGE() as emsg,
ERROR_SEVERITY() as es 
end catch
done:
go


然后是创建队列 queue(一些具体的参数参见sql联机丛书吧,里面有详细说明)
create queue testqueue
with status = on,
retention = off,
activation(
status = off, --这里没有打开收到消息时执行的存储过程
procedure_name=dbo.processmsg,
max_queue_readers=5,
execute as self 
)


因为上面没有激活收到消息执行的存储过程,这里修改队列以激活它
alter queue testqueue
with status = on,
retention = off,
activation(
status = on,
procedure_name=dbo.processmsg,
max_queue_readers=5,
execute as self
)


最后create service
create service testservice
on queue testqueue(mtestcontract)


以上就是创建最基本的servicebroker需要执行的脚本,以上的脚本是在起始数据库上创建servicebroker的脚本,在目标服务器上的创建脚本略有不同:
一是处理接收消息的存储过程不同,因为这里目标服务器要对起始服务器进行消息的回发,并发出结束会话的消息(在消息会话处理结束时,一定要结束会话,不然会对性能造成严重影响)

use targetdb
go
create proc processmsg
as
declare 
@msgtype varchar(255),
@msgbody varchar(255),
@cid uniqueidentifier
begin try
begin tran
waitfor(
/*You can also specify a WHERE clause in the RECEIVE statement, but it is not a full-featured WHERE
clause. You can only specify a conversation_handle or conversation_group_id.*/
receive top(1)
@msgtype=message_type_name,
@msgbody=case when validation='N'
then cast(message_body as varchar(255))
end,
@cid=conversation_handle
from dbo.testqueue),timeout 3000
if(@@ROWCOUNT = 0 or @@ERROR <> 0)
begin
rollback tran
goto done
end
else
begin
if @msgtype = 'mtesttype'
begin
insert into t2(Msg,Cid,Mtype) values(@msgbody,@cid,@msgtype);
send on conversation @cid
message type mtesttype1('p1 has received ur msg,test ok');
end
end conversation @cid
end
commit tran
end try
begin catch
while(@@TRANCOUNT > 0) rollb

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
SQL Server 2005 的一个主要成就是可以实现可靠、可扩展且功能完善的数据库应用程序。与 .NET Framework 2.0 公共语言运行库 (CLR) 的集成使开发人员可以将重要的业务逻辑与存储过程合并,而 T-SQL 和 XML 中的新增功能扩展了数据操作的可用范围以及开发人员可用的存储功能。另一个重要功能是 SQL Server Service Broker,它为数据库应用程序增加了可靠、可扩展、分布式异步功能。 为什么要编写异步排队操作? 在开发 SQL Server 2005 时,我们与成功开发过大型可扩展数据库应用程序的人员进行了交谈。结果发现他们几乎所有的应用程序中都有一个或多个操作是以异步排队方式执行的。股票交易系统中的结算活动是排队的,这样可以在后台进行结算,在前端处理其他交易。订单输入系统中的发货信息放在一个排队中,稍后将由另一台服务器(通常位于其他位置)上运行的发货系统读取。旅行预订系统在客户填写完路线后再进行实际的预订,并在预订完成后发送确认电子邮件。在所有这些示例中,许多工作都是通过异步方式完成的,从而提高了系统的响应速度,因此用户无须等待工作完成就可以收到响应。 在大多数大型系统中,经过仔细分析后都可以发现,许多处理都可以通过异步方式完成。虽然应用程序中的排队任务无须立即完成,但系统必须确保这些任务能够可靠地完成。Service Broker 使异步排队任务的执行可靠并且易于实现。 使应用程序中的部分任务异步执行的另一个优势是这些任务的处理工作可以在资源可用时完成。如果订单输入系统中的发货任务可以从队列中执行,发货系统就无需具有处理峰值订单负载的能力。在许多订单输入系统中,峰值订单率可能是平均订单率的两倍或三倍。由于订单输入系统必须具有处理峰值负载的能力,因此大量处理能力在很大一部分时间内都处于闲置状态。如果在出现峰值时对异步任务进行排队并在空闲时执行,将显著提高系统的利用率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神毓逍遥-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值