博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Service Broker between two instance
阅读量:6472 次
发布时间:2019-06-23

本文共 10026 字,大约阅读时间需要 33 分钟。

--====================================================================================================================== --Step 1 --====================================================================================================================== --======================================================================== --Create endpoint with windows authentication --======================================================================== USE master; GO IF EXISTS (SELECT * FROM master.sys.endpoints WHERE name = N'InstTargetEndpoint') DROP ENDPOINT InstTargetEndpoint; GO CREATE ENDPOINT InstTargetEndpoint STATE = STARTED AS TCP ( LISTENER_PORT = 4022 ) FOR SERVICE_BROKER (AUTHENTICATION = WINDOWS ); GO --======================================================================== --Create traget database InstTargetDB --======================================================================== USE master; GO IF EXISTS (SELECT * FROM sys.databases WHERE name = N'InstTargetDB') DROP DATABASE InstTargetDB; GO CREATE DATABASE InstTargetDB; GO USE InstTargetDB; GO CREATE MASTER KEY        ENCRYPTION BY PASSWORD = N'Auto@sql'; GO CREATE USER TargetUser WITHOUT LOGIN; GO --====================================================================================== --Create certification and backup it --====================================================================================== USE InstTargetDB GO CREATE CERTIFICATE InstTargetCertificate AUTHORIZATION TargetUser WITH SUBJECT = 'Target Certificate',           EXPIRY_DATE = N'12/31/2012'; BACKUP CERTIFICATE InstTargetCertificate TO FILE = N'\\Ms-wengao-02\cert\InstTargetCertificate.cer'; GO --====================================================================================== --Create message type,contract,queue,services --====================================================================================== USE InstTargetDB GO CREATE MESSAGE TYPE [//BothDB/2InstSample/RequestMessage]        VALIDATION = WELL_FORMED_XML; CREATE MESSAGE TYPE [//BothDB/2InstSample/ReplyMessage]        VALIDATION = WELL_FORMED_XML; GO CREATE CONTRACT [//BothDB/2InstSample/SimpleContract]       ([//BothDB/2InstSample/RequestMessage]          SENT BY INITIATOR, [//BothDB/2InstSample/ReplyMessage]          SENT BY TARGET       ); GO CREATE QUEUE InstTargetQueue; GO CREATE SERVICE [//TgtDB/2InstSample/TargetService] AUTHORIZATION TargetUser ON QUEUE InstTargetQueue        ([//BothDB/2InstSample/SimpleContract]); GO --====================================================================================================================== --Step 3 --====================================================================================================================== --====================================================================================== --Create message type,contract,queue,services --====================================================================================== USE InstTargetDB GO CREATE USER InitiatorUser WITHOUT LOGIN; CREATE CERTIFICATE InstInitiatorCertificate AUTHORIZATION InitiatorUser FROM FILE = N'\\Ms-wengao-02\cert\InstInitiatorCertificate.cer'; GO USE InstTargetDB; GO ALTER ROUTE InstInitiatorRoute WITH SERVICE_NAME = N'//InstDB/2InstSample/InitiatorService', ADDRESS = N'TCP://172.22.101.96:4022'; USE msdb GO ALTER ROUTE InstTargetRoute WITH SERVICE_NAME = N'//TgtDB/2InstSample/TargetService', ADDRESS =N'TCP://172.22.101.214:4022'; GO GRANT SEND ON SERVICE::[//TgtDB/2InstSample/TargetService] TO InitiatorUser; GO CREATE REMOTE SERVICE BINDING InitiatorBinding TO SERVICE N'//InstDB/2InstSample/InitiatorService' WITH USER = InitiatorUser; GO --====================================================================================================================== --Step 5 --====================================================================================================================== USE InstTargetDB GO SELECT * FROM dbo.InstTargetQueue GO DECLARE @RecvReqDlgHandle UNIQUEIDENTIFIER; DECLARE @RecvReqMsg NVARCHAR(100); DECLARE @RecvReqMsgName sysname; BEGIN TRANSACTION; WAITFOR ( RECEIVE TOP(1) @RecvReqDlgHandle = conversation_handle, @RecvReqMsg = message_body, @RecvReqMsgName = message_type_name FROM InstTargetQueue ), TIMEOUT 1000; SELECT @RecvReqMsg AS ReceivedRequestMsg; IF @RecvReqMsgName = N'//BothDB/2InstSample/RequestMessage' BEGIN DECLARE @ReplyMsg NVARCHAR(100); SELECT @ReplyMsg =         N'
Message for Initiator service.
'; SEND ON CONVERSATION @RecvReqDlgHandle MESSAGE TYPE [//BothDB/2InstSample/ReplyMessage] (@ReplyMsg); END CONVERSATION @RecvReqDlgHandle; END SELECT @ReplyMsg AS SentReplyMsg; SELECT @ReplyMsg; COMMIT TRANSACTION; GO
--====================================================================================================================== --Step 2 --====================================================================================================================== --======================================================================== --Create endpoint with windows authentication --======================================================================== USE master; GO IF EXISTS (SELECT * FROM sys.endpoints WHERE name = N'InstInitiatorEndpoint') DROP ENDPOINT InstInitiatorEndpoint; GO CREATE ENDPOINT InstInitiatorEndpoint STATE = STARTED AS TCP ( LISTENER_PORT = 4022 ) FOR SERVICE_BROKER (AUTHENTICATION = WINDOWS ); GO --======================================================================== --Create Initiator database InstInitiatorDB --======================================================================== USE master; GO IF EXISTS (SELECT * FROM sys.databases WHERE name = N'InstTargetDB') DROP DATABASE InstInitiatorDB; GO CREATE DATABASE InstInitiatorDB; GO USE InstInitiatorDB; GO CREATE MASTER KEY        ENCRYPTION BY PASSWORD = N'Auto@sql'; GO CREATE USER InitiatorUser WITHOUT LOGIN; GO --====================================================================================== --Create certification and backup it --====================================================================================== USE InstInitiatorDB GO CREATE CERTIFICATE InstInitiatorCertificate AUTHORIZATION InitiatorUser WITH SUBJECT = 'Target Certificate',           EXPIRY_DATE = N'12/31/2012'; GO BACKUP CERTIFICATE InstInitiatorCertificate TO FILE = N'\\Ms-wengao-02\cert\InstInitiatorCertificate.cer'; GO --====================================================================================== --Create message type,contract,queue,services --====================================================================================== USE InstInitiatorDB GO CREATE MESSAGE TYPE [//BothDB/2InstSample/RequestMessage]        VALIDATION = WELL_FORMED_XML; CREATE MESSAGE TYPE [//BothDB/2InstSample/ReplyMessage]        VALIDATION = WELL_FORMED_XML; GO CREATE CONTRACT [//BothDB/2InstSample/SimpleContract]       ([//BothDB/2InstSample/RequestMessage]          SENT BY INITIATOR, [//BothDB/2InstSample/ReplyMessage]          SENT BY TARGET       ); GO CREATE QUEUE InstInitiatorQueue; GO CREATE SERVICE [//InstDB/2InstSample/InitiatorService] AUTHORIZATION InitiatorUser ON QUEUE InstInitiatorQueue; GO --====================================================================================== --Create user TargetUser --Use the certificate which create by traget to Create cetificate --====================================================================================== USE InstInitiatorDB CREATE USER TargetUser WITHOUT LOGIN; GO CREATE CERTIFICATE InstTargetCertificate AUTHORIZATION TargetUser FROM FILE = N'\\Ms-wengao-02\cert\InstTargetCertificate.cer' GO --====================================================================================== --Create route and remote service binding --====================================================================================== USE InstInitiatorDB GO CREATE ROUTE InstTargetRoute WITH SERVICE_NAME = N'//TgtDB/2InstSample/TargetService', ADDRESS = 'TCP://172.22.101.214:4022'; USE msdb; GO CREATE ROUTE InstInitiatorRoute WITH SERVICE_NAME = N'//InstDB/2InstSample/InitiatorService',      ADDRESS = N'TCP://172.22.101.96:4022'; GO CREATE REMOTE SERVICE BINDING TargetBinding TO SERVICE N'//TgtDB/2InstSample/TargetService' WITH USER = TargetUser; GO --====================================================================================================================== --Step 4 --====================================================================================================================== --====================================================================================== --Start conversation --====================================================================================== USE InstInitiatorDB; GO DECLARE @InitDlgHandle UNIQUEIDENTIFIER; DECLARE @RequestMsg NVARCHAR(100); BEGIN TRANSACTION; BEGIN DIALOG @InitDlgHandle FROM SERVICE [//InstDB/2InstSample/InitiatorService] TO SERVICE N'//TgtDB/2InstSample/TargetService' ON CONTRACT [//BothDB/2InstSample/SimpleContract] WITH          ENCRYPTION = ON; SELECT @RequestMsg = N'
Message for Target service.
'; SEND ON CONVERSATION @InitDlgHandle MESSAGE TYPE [//BothDB/2InstSample/RequestMessage] (@RequestMsg); SELECT @RequestMsg AS SentRequestMsg; COMMIT TRANSACTION; GO --====================================================================================================================== --Step 6 --====================================================================================================================== USE InstInitiatorDB; GO DECLARE @RecvReplyMsg NVARCHAR(100); DECLARE @RecvReplyDlgHandle UNIQUEIDENTIFIER; BEGIN TRANSACTION; WAITFOR ( RECEIVE TOP(1) @RecvReplyDlgHandle = conversation_handle, @RecvReplyMsg = message_body FROM InstInitiatorQueue ), TIMEOUT 1000; END CONVERSATION @RecvReplyDlgHandle; -- Display recieved request. SELECT @RecvReplyMsg AS ReceivedReplyMsg; COMMIT TRANSACTION; GO

Copy from msdn, does not work...

编辑器加载中...

转载地址:http://rvpko.baihongyu.com/

你可能感兴趣的文章
SQLiteHelper
查看>>
多线程---同步函数的锁是this(转载)
查看>>
鱼C记事本V1.0(下)- 零基础入门学习Delphi28
查看>>
百练 2742 统计字符数 解题报告
查看>>
Ubuntu搜狗输入法候选词乱码
查看>>
js中回调函数写法
查看>>
React native android 最常见的10个问题
查看>>
数据结构和算法
查看>>
int a; int* a; int** a; int (*a)[]; int (*a)(int)
查看>>
.Net 项目代码风格要求
查看>>
java编码规范
查看>>
[pat]1045 Favorite Color Stripe
查看>>
Immutable学习及 React 中的实践
查看>>
【转】性能测试步骤
查看>>
OSI与TCP/IP各层的结构与功能,都有哪些协议
查看>>
Android实例-程序切换到后台及从后台切换到前台
查看>>
spring boot启动定时任务
查看>>
值类型和引用类型
查看>>
查看外键属性
查看>>
[转]html5 Canvas画图教程(6)—canvas里画曲线之arcTo方法
查看>>