concept Service Broker in category sql

This is an excerpt from Manning's book SQL Server MVP Deep Dives Vol. 2.
Service Broker has been a part of SQL Server since its 2005 version, but it’s still not widely used. This may be due to the lack of a graphical user interface in SSMS, an overly complex setup, and the need to learn XML programming in SQL Server (all messages are in XML format). This is why I’ll give you a template that you can use for secure communication between two instances of SQL Server.
I’ve successfully used this template in a project that required gathering auditing data from multiple servers to a single central location and another project that used Service Broker for custom replication between two databases in combination with Change Data Capture in SQL Server 2008. Another use for Service Broker is having an order-receiving stored procedure switch to an asynchronous execution in peak time. This involved no change to application code at all. The possibilities are endless.
You’ll have two instances: DataSender (the initiator) and DataReceiver (the target). Data will be sent from the initiator to the target. There are two parts of the Service Broker set up on each instance. The first step is to enable the Service Broker functionality at the instance level (endpoint, certificate-based security, and so forth) in the master database. The second step is to create Service Broker objects (for example, message types, contracts, queues, and routes) and functionality (such as activation stored procedures and programming) at the database level.
Let’s look at what you have to do in the master database on the DataReceiver instance. All the code is run under the SA account. You use two databases: DBSender on the DataSender instance and DBReceiver on the DataReceiver instance. Note that if you have the instances on the same physical server, you need to use different ports for each instance. To make the scripts shorter, I’ve omitted the object existence check and cleanup if they exist. The following listing shows the code that enables Service Broker and sets up transport security on the DataReceiver instance.
As you can see, not much code is needed to enable Service Broker; comments take up most of it. The most important parts are saving the master key password somewhere safe and copying the certificate to the DataSender instance. You’ll use this certificate on the DataSender instance to authorize the login that will be sending data through Service Broker. This is why the setup in the master database on the DataSender instance includes creating logins and users and applying permissions (see the next listing).