1. ENABLE OUTBOUND CONNECTIONS ON THE FirsT SERVER.
1.1. Create the SQL service master key.
On the master database,create the sql service master key. This can be used to initial transparent security.
USEMASTER;
GO
--MAKE SURE MASTER HAD MASTER KEY
CREATEMASTERKEY
ENCRYPTIONBYPASSWORD='???????';
GO
1.2. Make a certificate for this server instance.
Create a certificate in master,the public key of this cert will be used for authentication purposes it is a self-signed cert.
CREATECERTIFICATETRPCERT_SERVER1
WITHSUBJECT='TRPCERT_SERVER1',
START_DATE='10/30/2006',
EXPIRY_DATE='10/30/2016'--SHOULD BE LATER THAN START DATE.
GO
1.3. Backup the certificate and copy it to the second server.
Backup the public key of the certificate to a file. This file will then be exchanged with the other instance,The file needs to be copied to the second server.
BACKUPCERTIFICATETRPCERT_SERVER1
TOFILE='C:\DD11\TRPCERT_SERVER1.CER';
GO
1.4. Create a mirroring endpoint for server broker using the certificate.
You need to create an endpoint in order to enable communication outside of this instance.if you configure sql Server 2005,you can use the same step.
CREATEENDPOINTSERVER1_WH_SSB
STATE=STARTED
ASTCP
( LISTENER_PORT=4022)
FORSERVICE_broKER
(
AUTHENTICATION=CERTIFICATETRPCERT_SERVER1,
ENCRYPTION=required
);
GO
2. ENABLE OUTBOUND CONNECTIONS ON THE SECOND SERVER
2.1. On the master database,create the database master key.
USEMASTER;
GO
--MAKE SURE MASTER HAD MASTER KEY
CREATEMASTERKEY
ENCRYPTIONBYPASSWORD='???????';
GO
2.2. New one certificate on the Mirror server instance.
Create a certificate in master database,the public key of this certificate will be used for authentication purposes when the remote instance tries to connect to this server.
CREATECERTIFICATETRPCERT_SERVER2
WITHSUBJECT='TRANSPORT CERTIFICATE FOR SERVER2',
START_DATE='10/1/2006',
EXPIRY_DATE='10/1/2016'
GO
2.3. Backup the certificate and copy it to other server.
Backup the public key of the certificate to a file,this file will then be exchanged with the other instance. The file needs to be copied to first server.
BACKUPCERTIFICATETRPCERT_SERVER2
TOFILE='C:\DD11\TRPCERT_SERVER2.CER';
GO
2.4. Create the mirroring endpoint for the server broker instance.
You need to create an endpoint in order to enable communication outside of this instance.
CREATEENDPOINTSERVER2_WH_SSB
STATE=STARTED
ASTCP
( LISTENER_PORT=4022)
FORSERVICE_broKER
(
AUTHENTICATION=CERTIFICATETRPCERT_SERVER2,
ENCRYPTION=required
);
GO
3. ENABLE INBOUND CONNECTIONS ON THE FirsT SERVER
3.1. Create a login on first server.
Create a login and a user which you will assign a public key from the certificate in the remote master database.
USEMASTER;
GO
IF NOTEXISTS(SELECT*FROMSYS.SERVER_PRINCIPALSWHERENAME=N'WH10DBO')
BEGIN
CREATELOGINWH10DBO
WITHPASSWORD=N'???????',DEFAULT_DATABASE=MASTER,DEFAULT_LANGUAGE=[US_ENGLISH],CHECK_EXPIRATION=OFF,CHECK_POLICY=OFF
END
GO
3.2. Create a user for the login.
CREATEUSERWH10DBOFROMLOGINWH10DBO
GO
3.3. Grant connect permission on the endpoint to the login.
You must grant connect permission,otherwise you will get error when you are using the profiler90.
GRANTCONNECTONENDPOINT::SERVER1_WH_SSBTOWH10DBO
GO
3.4. Associate the mirror server certificate with the user.
CREATECERTIFICATETRPCERT_SERVER2
AUTHORIZATIONWH10DBO
FROMFILE='C:\DD11\TRPCERT_SERVER2.CER';
GO
4. ENABLE INBOUND CONNECTIONS ON THE SECOND SERVER
4.1. Create a login for the first server.
Create a login and a user which you will assign a public key from the certificate in the remote master database to it.
USEMASTER;
GO
CREATELOGINSSBDBOWITHPASSWORD='??????';
GO
4.2. Create a user from the login.
CREATEUSER SSBDBOFROMLOGIN SSBDBO;
GO
4.3. Grant connection permission to the user.
GRANTCONNECTONENDPOINT::SERVER2_WH_SSB
TOSSBDBO--CAN ONLY GRANT TO USER OR GROUP.CAN’T BE A LOGIN.THIS IS WHY WE CREATE USER ABOVE.
GO
4.4. Associate a certificate with the user we created.
Create a certificate from the public certificate of first server. The certificate you backed is only the public key.
CREATECERTIFICATETRPCERT_SERVER1
AUTHORIZATIONSSBDBO
FROMFILE='C:\DD11\TRPCERT_SERVER1.CER';
GO
5. SET THE DATABASE OUTBOUND FOR THE FirsT SERVER
5.1. Create a database which will be configured as service broker database.
CREATEDATABASESSB
GO
5.2. Create database master key for encrypting our certificate.
USESSB;
GO
--SET MASTER KEY
CREATEMASTERKEYENCRYPTIONBYPASSWORD='??????*';
GO
5.3. Create certificate for transport security.
Here we create a self-signed certificate.
CREATECERTIFICATEDLGCERT_SERVER1
WITHSUBJECT='DLGCERT_CENTER',START_DATE='10/30/2006',
EXPIRY_DATE='10/30/2016'ACTIVEFORBEGIN_DIALOG=ON;
GO
5.4. Backup the certificate.
Backup the public key of the certificate to a file,and then copy it to second server.
BACKUPCERTIFICATEDLGCERT_SERVER1
TOFILE='C:\DD11\DLGCERT_SERVER1.CER';
GO
6. SET THE DATABASE OUTBOUND FOR SECOND SERVER.
6.1. Create the database.
CREATEDATABASESSB
GO
6.2. Create the database master key.
This key then will be used to encryption our certificate.
USESSB;
GO
--SET MASTER KEY
CREATEMASTERKEY
ENCRYPTIONBYPASSWORD='???????*';
GO
6.3. Create certificate for transport security.
Here you create a self-signed certificate,which will be encrypted by the above key.
CREATECERTIFICATEDLGCERT_SERVER2
WITHSUBJECT='DLGCERT_SERVER2',START_DATE='10/1/2006',
EXPIRY_DATE='10/1/2016'ACTIVEFORBEGIN_DIALOG=ON;
GO
6.4. Backup the Certificate and copy to the first server.
Backup the public key of the certificate to a file this will then be exchanged with the other instance make sure that the path you define below can be accessed by sql server. The file needs to be copied to server 2
BACKUPCERTIFICATEDLGCERT_SERVER2
TOFILE='C:\DD11\DLGCERT_SERVER2.CER';
GO
7. SET THE DATABASE INBOUND FOR THE FirsT SERVER.
7.1. Create a user to assign a public key.
Create a user which you will assign a public key from the certificate in the remote database.
CREATEUSERWH10DBOFROMLOGINWH10DBO;
GO
7.2. Associate the second server db’s public cert to the use.
You create a certificate from server 2's public certificate and assign it to the user created above
CREATECERTIFICATEDLGCERT_SERVER2
AUTHORIZATIONWH10DBO
FROMFILE='C:\DD11\DLGCERT_SERVER2.CER';
GO
8. SET THE DATABASE INBOUND FOR SERVER2
8.1. Create a user that will own the public key from second server db.
Create a user which you eventually will assign a public key from the cert in the remote db
CREATEUSERSSBDBO FROMLOGINSSBDBO;
GO
8.2. Associate the related public key with the user.
CREATECERTIFICATEDLGCERT_SERVER1
AUTHORIZATIONSSBDBO
FROMFILE='C:\DD11\DLGCERT_SERVER1.CER';
9. SET THE DATABASE SERVICE broKER MetaDATA FOR SERVER1
9.1. Create two message types.
USESSB;
GO
-- WE NEED TWO MESSAGE TYPES
CREATEMESSAGETYPE[http://soa.microsoft.com/SOA/SOA/USA/InfrastructureService/V10/Types/Warehouse/AllRequestMessageType]
VALIDATION=WELL_FORMED_XML;
CREATEMESSAGETYPE[http://soa.microsoft.com/SOA/SOA/USA/InfrastructureService/V10/Types/Warehouse/AllReponseMessageType]
VALIDATION=WELL_FORMED_XML;
GO
9.2. Create the message contract.
CREATECONTRACT[http://soa.microsoft.com/SOA/USA/InfrastructureService/V10/Warehouse/AllContract]
(
[http://soa.microsoft.com/SOA/SOA/USA/InfrastructureService/V10/Types/Warehouse/AllRequestMessageType] SENTBYINITIATOR,
[http://soa.microsoft.com/SOA/SOA/USA/InfrastructureService/V10/Types/Warehouse/AllReponseMessageType] SENTBYTARGET
);
GO
9.3. Create the queue.
Note: At this stage we do not care about activation,so the queue can’t auto process.
CREATEQUEUE[http://soa.microsoft.com/SOA/USA/InfrastructureService/V10/Warehouse/AllQueue]
WITHSTATUS=ON;
GO
9.4. Create a service.
CREATESERVICE[http://soa.microsoft.com/SOA/USA/InfrastructureService/V10/Warehouse00/AllSSBService]
ONQUEUE[http://soa.microsoft.com/SOA/USA/InfrastructureService/V10/Warehouse/AllQueue]
(
[http://soa.microsoft.com/SOA/USA/InfrastructureService/V10/Warehouse/AllContract]
);
GO
9.5. Create a route to the remote service.
CREATEROUTE[http://soa.microsoft.com/SOA/USA/InfrastructureService/V10/Warehouse10/AllRoute]
WITH SERVICE_NAME='http://soa.microsoft.com/SOA/USA/InfrastructureService/V10/Warehouse10/AllSSBService',
ADDRESS='TCP://SERVER2:4022';
GO
9.6. Create a remote service binding.
We'll do encrypted dialogs we need a remote service binding
CREATEREMOTESERVICEBINDING[http://soa.microsoft.com/SOA/USA/InfrastructureService/V10/Warehouse10/AllServiceRemoteServiceBinding]
TOSERVICE'http://soa.microsoft.com/SOA/USA/InfrastructureService/V10/Warehouse10/AllSSBService'
WITH USER=WH10DBO,
ANONYMOUS=OFF
GO
9.7. Grant the user send rights on the service.
GRANTSENDONSERVICE::[http://soa.microsoft.com/SOA/USA/InfrastructureService/V10/Warehouse00/AllSSBService]TOWH10DBO;
GO
10. SET THE DATABASE SERVICE broKER MetaDATA FOR SERVER2
10.1. Create two message types.
USESSB;
GO
CREATEMESSAGETYPE[http://soa.microsoft.com/SOA/SOA/USA/InfrastructureService/V10/Types/Warehouse/AllRequestMessageType]
VALIDATION=WELL_FORMED_XML;
CREATEMESSAGETYPE[http://soa.microsoft.com/SOA/SOA/USA/InfrastructureService/V10/Types/Warehouse/AllReponseMessageType]
VALIDATION=WELL_FORMED_XML;
GO
10.2. Create the message contract.
CREATECONTRACT[http://soa.microsoft.com/SOA/USA/InfrastructureService/V10/Warehouse/AllContract]
([http://soa.microsoft.com/SOA/SOA/USA/InfrastructureService/V10/Types/Warehouse/AllRequestMessageType] SENTBYINITIATOR,
[http://soa.microsoft.com/SOA/SOA/USA/InfrastructureService/V10/Types/Warehouse/AllReponseMessageType] SENTBYTARGET
);
GO
10.3. Create the queue.
Create the queue,at this stage we do not care about activation.
CREATEQUEUE[http://soa.microsoft.com/SOA/USA/InfrastructureService/V10/Warehouse/AllQueue]
WITHSTATUS=ON;
GO
10.4. Create the Service.
CREATESERVICE[http://soa.microsoft.com/SOA/USA/InfrastructureService/V10/Warehouse10/AllSSBService]
ONQUEUE[http://soa.microsoft.com/SOA/USA/InfrastructureService/V10/Warehouse/AllQueue]
([http://soa.microsoft.com/SOA/USA/InfrastructureService/V10/Warehouse/AllContract]);
GO
10.5. Create a route to the remote service.
Create a route to the remote service.
CREATEROUTE[http://soa.microsoft.com/SOA/USA/InfrastructureService/V10/Warehouse00/AllRoute]
WITH
SERVICE_NAME='http://soa.microsoft.com/SOA/USA/InfrastructureService/V10/Warehouse00/AllSSBService',
ADDRESS='TCP://SERVER1:4022';
GO
10.6. Create a remote service binding.
As we'll be doing encrypted dialogs we need a remote service binding
CREATEREMOTESERVICEBINDING[http://soa.microsoft.com/SOA/USA/InfrastructureService/V10/Warehouse00/AllServiceRemoteServiceBinding]
TOSERVICE'http://soa.microsoft.com/SOA/USA/InfrastructureService/V10/Warehouse00/AllSSBService'
WITH USER=SSBDBO,
ANONYMOUS=OFF
GO
10.7. Grant the user send rights on the service.
GRANTSENDONSERVICE::[http://soa.microsoft.com/SOA/USA/InfrastructureService/V10/Warehouse10/AllSSBService]TOSSBDBO;
GO
11. CONfigURE THE LOG TABLE ON BOTH SERVERS
CREATETABLE[DBO].[SUCCESS_RECORD](
[CONVERSATION_HANDLE] [UNIQUEIDENTIFIER]NOTNULL,
[MESSAGE_TYPE_NAME] [SYSNAME]COLLATE sql_latin1_GENERAL_CP1_CI_ASNOTNULL,
[MESSAGE_BODY] [XML]NULL,
[INDATE] [DATETIME]NULLDEFAULT(GETDATE()),
[MESSAGENAMESPACE] [VARCHAR](100)COLLATEsql_latin1_GENERAL_CP1_CI_ASNULL,
[MESSAGEACTION] [VARCHAR](20)COLLATEsql_latin1_GENERAL_CP1_CI_ASNULL,
[PROCEsstYPE] [CHAR](1)COLLATEsql_latin1_GENERAL_CP1_CI_ASNULLDEFAULT('N'),
PRIMARYKEYCLUSTERED
(
[CONVERSATION_HANDLE]ASC
)WITH(PAD_INDEX =OFF,IGnorE_DUP_KEY=OFF)ON[PRIMARY]
)ON[PRIMARY]
GO
CREATETABLE[DBO].[EXCEPTION_RECORD](
[MESSAGE_BODY] [XML]NULL,
[INDATE] [DATETIME]NOTNULLCONSTRAINT[DF_EXCEPTION_RECORD_INDATE] DEFAULT(GETDATE())
)ON[PRIMARY]
GO
CREATETABLE[DBO].[SENT_RECORD](
[MESSAGE_BODY] [XML]NULL,
[INDATE] [DATETIME]NULLCONSTRAINT[DF_SENT_RECORD_INDATE] DEFAULT(GETDATE())
)ON[PRIMARY]
GO
12. SET THE DATABASE SERVICE broKER CONVESATION
12.1. Create Send message on server1.
USESSB
GO
CREATEPROC[DBO].[UP_SSB_SENDMSG]
@SERVICENAMESYSNAME,
@MSGXML
AS
BEGIN
--START THE DIALOG AND SEND A MESSAGE
--UNCOMMENT FROM HERE UNTIL THE FOLLOWING GO STATEMENT AND RUN
DECLARE@HUNIQUEIDENTIFIER--CONVERSATION HANDLE
--DECLARE @MSG XML; --WILL HOLD THE MESSAGE
BEGINTRY
BEGINDIALOGCONVERSATION@H
FROMSERVICE[http://soa.microsoft.com/SOA/USA/InfrastructureService/V10/Warehouse00/AllSSBService]
-- TO SERVICE 'S_RECSERVICE'
--TO SERVICE 'S_RECSERVICE_S8sql01'
TOSERVICE@SERVICENAME
ONCONTRACT[http://soa.microsoft.com/SOA/USA/InfrastructureService/V10/Warehouse/AllContract]
--WITH ENCRYPTION=OFF;
;
--SET @MSG = '<HELLO00/>';
SENDONCONVERSATION@H
MESSAGETYPE[http://soa.microsoft.com/SOA/SOA/USA/InfrastructureService/V10/Types/Warehouse/AllRequestMessageType]
(@MSG);
ENDTRY
BEGINCATCH
BEGIN
INSERTINTOSSB.DBO.disPATH_ERRORRECORD(MESSAGE_BODY)VALUES(@MSG)
--SELECT [MESSAGE]='MESSAGE CHANNEL NOT DEFINED IN E3BZT.SSB_CENTER'
END
ENDCATCH
END
12.2. Receive message on server2.
CREATEPROC[DBO].[UP_SSB_SENDMSG]
@SERVICENAMESYSNAME,
@MSGXML
AS
BEGIN
--START THE DIALOG AND SEND A MESSAGE
--UNCOMMENT FROM HERE UNTIL THE FOLLOWING GO STATEMENT AND RUN
DECLARE@HUNIQUEIDENTIFIER--CONVERSATION HANDLE
--DECLARE @MSG XML; --WILL HOLD THE MESSAGE
BEGINTRY
BEGINDIALOGCONVERSATION@H
FROMSERVICE[http://soa.microsoft.com/SOA/USA/InfrastructureService/V10/Warehouse10/AllSSBService]
-- TO SERVICE 'S_RECSERVICE'
--TO SERVICE 'S_RECSERVICE_S8sql01'
TOSERVICE@SERVICENAME
ONCONTRACT[http://soa.microsoft.com/SOA/USA/InfrastructureService/V10/Warehouse/AllContract]
--WITH ENCRYPTION=OFF;
;
--SET @MSG = '<HELLO00/>';
SENDONCONVERSATION@H
MESSAGETYPE[http://soa.microsoft.com/SOA/SOA/USA/InfrastructureService/V10/Types/Warehouse/AllRequestMessageType]
(@MSG);
--CHECK IF THE TRANSMISSION_STATUS COLUMN IN SYS.TRANSMISSION_QUEUE
--HAS ANY MESSAGES,IF SO SOMETHING IS NOT ENTIRELY CORRECT
ENDTRY
BEGINCATCH
BEGIN
INSERTINTOSSB.DBO.disPATH_ERRORRECORD(MESSAGE_BODY)VALUES(@MSG)
--SELECT [MESSAGE]='MESSAGE CHANNEL NOT DEFINED IN E3BZT.SSB_CENTER'
END
ENDCATCH
END
13. RECEIVE STORE PROCEDURE
CREATEPROCEDURE[DBO].[UP_SSB_PROCESSINCOMINGMSG]
AS
BEGIN
SETNOCOUNTON
DECLARE@MESSAGETYPESYSNAME,
@CONVERSATIONHANDLEUNIQUEIDENTIFIER,
@MESSAGEBODYXML,
@MESSAGENAMESPACEVARCHAR(100),
@MESSAGEACTION VARCHAR(20)
WHILE(1=1)
BEGIN
BEGINTRY
WAITFOR(RECEIVETOP(1)
@MESSAGETYPE=MESSAGE_TYPE_NAME,
@MESSAGEBODY=MESSAGE_BODY,
@CONVERSATIONHANDLE=CONVERSATION_HANDLE
--,@MESSAGENAMESPACE=@MESSAGEBODY.VALUE('(/ROOT/NODE/MESSAGEHEAD/NAMESPACE)[1]','VARCHAR(100)')
--,@MESSAGEACTION=@MESSAGEBODY.VALUE('(/ROOT/NODE/MESSAGEHEAD/ACTION)[1]','VARCHAR(20)')
FROM[http://soa.microsoft.com/SOA/USA/InfrastructureService/V10/Warehouse/AllQueue]
),TIMEOUT1000
IF(@@ROWCOUNT=0)
BEGIN
BREAK
END
--IF WE DON'T GET ANYTHING FROM THE QUEUE AFTER 1 SECOND,THE QUEUE IS EMPTY,SO BAIL OUT
ELSE
--CHECK THE END DIALOG
IF(@MESSAGETYPE='http://schemas.microsoft.com/sql/Servicebroker/EndDialog')
BEGIN
---WHEN RECEIVE END DIALOG,WE NEED TO END ALSO
ENDCONVERSATION@CONVERSATIONHANDLE
END
ELSE
--CHECK FOR ERROR MESSAGE
IF(@MESSAGETYPE='http://schemas.microsoft.com/sql/Servicebroker/Error')
BEGIN
--HANDLE THE ERROR HERE,RECORD TO ONE TABLE.
INSERTERROR_RECORD
(CONVERSATION_HANDLE,
MESSAGE_TYPE_NAME,
MESSAGE_BODY,
ERROR_DATE)
VALUES(@CONVERSATIONHANDLE,
@MESSAGETYPE,
@MESSAGEBODY,
GETDATE() )
ENDCONVERSATION@CONVERSATIONHANDLE
END
ELSE
--CHECK FOR THE APPLICATION MESSAGE
IF(@MESSAGETYPE='http://soa.microsoft.com/SOA/SOA/USA/InfrastructureService/V10/Types/WAREHOUSE/ALLREQUESTMESSAGETYPE')
BEGIN
--SELECT @MESSAGENAMESPACE=@MESSAGEBODY.VALUE('(/ROOT/NODE/MESSAGEHEAD/NAMESPACE)[1]','VARCHAR(100)')
--,'VARCHAR(20)') ;
--PROCESS HERE
INSERTSUCESS_RECORD
(CONVERSATION_HANDLE,
MESSAGE_TYPE_NAME,
MESSAGE_BODY,
INDATE,
MESSAGENAMESPACE,
MESSAGEACTION)
VALUES(@CONVERSATIONHANDLE,
@MESSAGETYPE,
@MESSAGEBODY,
GETDATE(),
@MESSAGENAMESPACE,
@MESSAGEACTION)
ENDCONVERSATION@CONVERSATIONHANDLE
END
ELSE
--CHECK FOR THE APPLICATION MESSAGE
IF(@MESSAGETYPE='http://soa.microsoft.com/SOA/SOA/USA/InfrastructureService/V10/Types/Warehouse/AllReponseMessageType')
BEGIN
--PROCESS HERE
ENDCONVERSATION@CONVERSATIONHANDLE
END
ELSE
BEGIN
--PROCESS THE OTHER informatION
--HANDLE THE ERROR HERE,RECORD TO ONE TABLE.
INSERTERROR_RECORD
(CONVERSATION_HANDLE,
ERROR_DATE)
VALUES(@CONVERSATIONHANDLE,
GETDATE())
ENDCONVERSATION@CONVERSATIONHANDLE
END
ENDTRY
BEGINCATCH
INSERTERROR_RECORD
(CONVERSATION_HANDLE,
MESSAGE_TYPE_NAME,
MESSAGE_BODY,
ERROR_DATE)
VALUES(@CONVERSATIONHANDLE,
@MESSAGETYPE,
@MESSAGEBODY,
GETDATE())
CONTINUE
ENDCATCH
END--END LOOP
END
14. SEND SAMPLE
EXEC[UP_SSB_SENDMSG]'http://soa.microsoft.com/SOA/USA/InfrastructureService/V10/Warehouse10/AllSSBService','<test/>'