在平常的项目设计中,我们经常会用到各种各样的队列来实现分布式系统的异步调用逻辑和数据消息的传递,从而来达到增强应用程序的性能和可伸缩性,通过事务性队列的离线消息处理机制更可以大大提高系统的可靠性。sqlserver自2005以后版本便增加了一个新的内置
在平常的项目设计中,我们经常会用到各种各样的队列来实现分布式系统的异步调用逻辑和数据消息的传递,从而来达到增强应用程序的性能和可伸缩性,通过事务性队列的离线消息处理机制更可以大大提高系统的可靠性。sqlserver自2005以后版本便增加了一个新的内置队列处理应用模块servicebroker,此功能模块大大简化了队列的使用操作,更方便的是能和原先的sqlserver系统在事务处理上完美的结合在一起。可是唯一的缺陷是增加了设计的耦合性。
接下来我们就如何使用servicebroker来做一个场景描述。我们需要设计一个web系统,系统由一个主web系统和多个子web系统组成。有一种情况下,在主模块中会产生一类行为数据,这类行为数据需要传播或者记录到各个子模块的数据库中处理。要如何实现这样的功能呢,当然我们可以选用多种现有技术来实现,如(webservice,链接服务器技术,wcf等技术)。在这里为了说明servicebroker的简单易用性,我们为此做一简单示例。
前提:各个应用数据库要允许servicebroker和设置数据库主密钥
代码部署划分:1公共部分(初始方和目标方共同使用),2.初始方,3.目标方
示例具体实现步骤主要分为(具体参数详细配置请参考msdn文档):
1.实现servicebroker消息、队列和服务
公共部分
定义消息类型架构集合
create xml schema collection
[http://samples/sql/servicebroker/msgoperationschema]
as n'
';
定义消息类型
create message type [http://samples/sql/servicebroker/msgoperation]
validation = valid_xml with schema collection
[http://samples/sql/servicebroker/msgoperationschema];
定义消息契约
create contract [http://samples/sql/servicebroker/msgoperationcontract]
(
[http://samples/sql/servicebroker/msgoperation]
sent by initiator
);
初始方
定义队列
create queue msgoperationinitqueue
with
status = on,
retention = off
go
定义初始服务
create service [http://samples/sql/servicebroker/msgoperationinitservice]
on queue msgoperationinitqueue
([http://samples/sql/servicebroker/msgoperationcontract]);
go
定义初始存储过程
create procedure dbo.usp_msgoperation_set
@msgid int,
@msgcontent nvarchar(2000)
as
declare @message_body as xml([http://samples/sql/servicebroker/msgoperationschema]);
declare @dialog as uniqueidentifier;
--填充消息体
set @message_body ='
'+cast(@msgid as varchar)+'
'+@msgcontent+'
';
begin dialog @dialog
from service [http://samples/sql/servicebroker/msgoperationinitservice]
to service 'http://samples/sql/servicebroker/msgoperationprocessservice'
on contract [http://samples/sql/servicebroker/msgoperationcontract];
--with encryption = off , lifetime = 3600;
--发送消息
send on conversation @dialog
message type [http://samples/sql/servicebroker/msgoperation] (@message_body);
end conversation @dialog;
go
目标方
定义队列处理存储过程
create procedure dbo.usp_msgoperation_cmd as
return 0
go
定义队列
create queue msgoperationprocessqueue
with
status = on,
retention = off,
activation
(
status = on,
procedure_name = dbo.usp_msgoperation_cmd,
max_queue_readers = 1,
execute as self
);
实现队列处理存储过程
alter procedure dbo.usp_msgoperation_cmd
as
declare @message_body as xml;
declare @message_type as sysname;
declare @dialog as uniqueidentifier;
while(1=1)
begin
begin transaction
--接收下一条可用的消息
waitfor(
receive top(1) --一次只处理一条消息
@message_type = message_type_name,
@message_body = message_body,
@dialog = [conversation_handle]
from dbo.msgoperationprocessqueue
), timeout 2000
--如果没收到任何消息则跳出循环
if (@@rowcount = 0)
begin
rollback transaction
break;
end
--根据接收的消息类型执行不同的消息处理逻辑
if (@message_type = 'http://schemas.microsoft.com/sql/servicebroker/enddialog')
begin
end conversation @dialog;
end
else if(@message_type = 'http://schemas.microsoft.com/sql/servicebroker/error')
begin
end conversation @dialog;
end
else if(@message_type = 'http://samples/sql/servicebroker/msgoperation')
begin
declare @msgid int
declare @msgcontent nvarchar(2000)
begin try
set @msgid = @message_body.value('data(//msgid)[1]', 'int');
set @msgcontent = @message_body.value('data(//msgcontent)[1]', 'nvarchar(2000)');
--此处可以处理自定义业务逻辑
end conversation @dialog;
end try
begin catch
rollback transaction
continue
end catch
end
commit transaction
end
go
实现目标处理服务
create service [http://samples/sql/servicebroker/msgoperationprocessservice]
on queue dbo.msgoperationprocessqueue
([http://samples/sql/servicebroker/msgoperationcontract])
go
2.实现servicebroker安全配置
在一台数据库服务器上的不同数据库之间的安全配置比较简单,默认情况下数据库之间是没有外部访问权限的,要实现你就需要在本地服务器上开启模拟上下文的数据库模块,即在数据库中设置 alter database database_name set trustworthy on 来实现互相访问的目的。
这里我们需要实现一种更灵活,更安全的配置方式,那就是基于证书的安全配置。
初始方
创建拥有服务的用户
create user msgoperationinitserviceuser without login;
alter authorization on
service::[http://samples/sql/servicebroker/msgoperationinitservice]
to
msgoperationinitserviceuser;
创建与该用户关联的私钥证书
create certificate msgoperactioninitservicecertpriv authorization msgoperationinitserviceuser
with subject = 'formsgoperactioninitservice',
start_date = '01/01/2009',
expiry_date = '01/01/2100';
将公钥证书备份到文件以供目标方服务使用
backup certificate msgoperactioninitservicecertpriv
to file = 'x:\**\msgoperactioninitservicecertpub.cer';
创建调用目标服务的用户
create user msgoperationprocessserviceuser without login;
导入目标服务的证书并把刚才创建的用户设为所有者
create certificate msgoperactionprocessservicecertpub authorization msgoperationprocessserviceuser
from file = 'x:\**\msgoperactionprocessservicecertpub.cer';
建立目标服务远程服务绑定
create remote service binding tomsgoperactionprocessservice
to service 'http://samples/sql/servicebroker/msgoperationprocessservice'
with user = msgoperationprocessserviceuser;
目标方
创建拥有服务的用户
create user msgoperationprocessserviceuser without login;
alter authorization on service::[http://samples/sql/servicebroker/msgoperationprocessservice] to msgoperationprocessserviceuser;
创建与该用户关联的私钥证书
create certificate msgoperactionprocessservicecertpriv authorization msgoperationprocessserviceuser
with subject = 'formsgoperactionprocessservice',
start_date = '01/01/2009',
expiry_date = '01/01/2100';
将公钥证书备份到文件以供初始方服务使用
backup certificate msgoperactionprocessservicecertpriv
to file = 'x:\**\msgoperactionprocessservicecertpub.cer';
创建调用初始服务的用户
create user msgoperationinitserviceuser without login;
导入初始服务的证书并把刚才创建的用户设为所有者
create certificate msgoperactioninitservicecertpub authorization msgoperationinitserviceuser
from file = 'x:\**\msgoperactioninitservicecertpub.cer';
授予用户发送服务的权限
grant send on service::[http://samples/sql/servicebroker/msgoperationinitservice] to msgoperationinitserviceuser;
3.实现servicebroker通讯设置(不同服务器之间通讯)
要把servicebroker部署到不同服务器的数据库实例,需要在master数据库和应用数据库中同时做相应的设置。
master数据库同样要允许servicebroker和设置数据主密钥。
初始方master数据库
创建初始服务器通讯证书
create certificate [server1_certpriv]
with subject = 'forserver1auth',
start_date = '01/01/2009',
expiry_date = '01/01/2100'
active for begin_dialog = on;
将公钥证书备份到文件以供目标服务器使用
backup certificate [server1_certpriv]
to file = 'x:\**\server1_certpub.cer';
go
创建初始服务器通讯终结点,这里我们假设使用33333端口监听
create endpoint [server1_endpoint]
state = started
as tcp ( listener_port =33333 )
for service_broker
(
authentication = certificate [server1_certpriv]
);
创建目标服务器的用户和登录
create login [server2_userproxy] with password = '123456';
create user [server2_userproxy];
导入由目标服务器导出的证书
create certificate [server2_certpub] authorization [server2_userproxy]
from file = 'x:\**\server2_certpub.cer';
为表示目标服务器用户的登录授予connect权限
grant connect on endpoint::[server1_endpoint] to [server2_userproxy];
初始方应用数据库
服务路由设置
create route msgoperationprocessserviceroute with
service_name = 'http://samples/sql/servicebroker/msgoperationprocessservice',
--broker_instance = 'cfdf4485-faef-47f9-b1f6-40dfd65685b7',
address = 'tcp://[ip]:33333';
go
目标方master数据库
创建目标服务器通讯证书
create certificate [server2_certpriv]
with subject = 'forserver2auth',
start_date = '01/01/2009',
expiry_date = '01/01/2100'
active for begin_dialog = on;
将公钥证书备份到文件以供初始服务器使用
backup certificate [server2_certpriv]
to file = 'x:\**\server2_certpub.cer';
go
创建目标服务器通讯终结点,这里我们假设使用33333端口监听
create endpoint [server2_endpoint]
state = started
as tcp ( listener_port = 33333 )
for service_broker
(
authentication = certificate [server2_certpriv]
);
创建初始服务器的用户和登录
create login [server1_userproxy] with password = '123456';
create user [server1_userproxy];
导入由初始服务器导出的证书
create certificate [server1_certpub] authorization [server1_userproxy]
from file = 'x:\**\server1_certpub.cer';
为表示初始服务器的登录授予connect权限
grant connect on endpoint::[server2_endpoint] to [server1_userproxy];
目标方应用数据库
服务路由设置
create route msgoperationinitserviceroute with
service_name = 'http://samples/sql/servicebroker/msgoperationinitservice',
--broker_instance = '52cad803-6951-4fd3-a16a-6995c50024b1',
address = 'tcp://[ip]:33333';
go
最后总结下sqlsvr2005 servicebroker部署在生产环境中的一些心得
1.安全策略,防火墙策略一定要配置正确
2.从备份还原的数据库可能要重新开启servicebroker开关
3.需要数据库带外访问的一定要设置数据库带外访问权限
4.每个服务器的主密钥都是不同的,部署的新服务器上的每个要使用servicebroker的数据库一定要重新创建数据库主密钥