Вы находитесь на странице: 1из 32

SQL Server

Service Broker

LOGO
www.themegallery.com
Contents

What is SQL Service Broker?

How does Service Broker work?

SQL Service
Demo
Broker
Service Broker Summary

Q&A
What is SQL Service Broker?
 New feature in SQL Server 2005

Database engine – SQL Server 2005


What is SQL Service Broker?
 Asynchronous Messaging Framework

What is SQL Service Broker?
 MSMQ VS. SQL Service Broker
Which one is better for your system?
Feature/Issue MSMQ SQL Service Broker
Cost Free with all business Requires SQL Server
versions of Windows 2005 license

Highest Performance Higher performance Lower performance when


possible standard messaging
functions are not
necessary
Transaction Performance Relatively expensive Efficient transactions
transactions using the using SQL Server's
Distributed Transaction internal transactions
Coordinator
Code Location Queuing code cannot run Message processing logic
in SQL Server can run in SQL Server

... ... ...


What is SQL Service Broker?
 MSMQ VS. SQL Service Broker

Feature/Issue MSMQ SQL Service Broker


... ... ...

Type Enforcement No type enforcement Type enforcement


provided via message
types and contracts
Disconnected Queuing Support for fully Requires connection to
disconnected mode SQL Server (at least
SQL Server Express
Edition) to send
messages
Backup Non-trivial backup Backup via standard
SQL Server backup
procedures
What is SQL Service Broker?
 Helps developers build applications:
 Reliable
 Secure
 Asynchronous
 Scalable
Service Broker Overview

How does Service Broker work?
 Conversation

 Message AOrdering andin real


messaging system Coordination
world
 Transactional Asynchronous Programming
 Support for loosely Coupled Applications
 SQL Service Broker Components
Service Broker Components

Service Components Conversation Components


Service
Program
Begin Dialog
Message Type Message Type Send Message
End Conversation

Target Queue
Contract Contract
<Message/>
<Message/>

Service Queue Service


Program
Receive Message
Service Broker Components
Service Components

Message Type Message Type

Contract Contract

Service Queue
Service Broker Service
Message Type
Message Type Message Type

CREATE MESSAGE TYPE


HelloWorldMessage Contract Contract

VALIDATION =
WELL_FORMED_XML;
Service Queue

 Specifies the name of the message and the validation that SQL
Server performs on messages when they are received in the
queue
 Validation may be NONE, EMPTY, WELL_FORMED_XML,
VALID_XML WITH SCHEMA COLLECTION
 Message types are created in each database participating in the
conversation
Service Broker Service
Contract Message Type Message Type

CREATE CONTRACT
HelloWorldContract
Contract Contract
( HelloWorldMessage SENT BY
INITIATOR,
HelloWorldResponse SENT BY
Service Queue
TARGET );

 Defines the direction and messages types each endpoint can


receive and send in a dialog
 Valid endpoints are INITIATOR, TARGET or ANY

 A contract must have at least one message type that allows the
INITIATOR to send a message
 Identical contracts are created in each database participating in
the conversation
Service Broker Service
Queue Message Type Message Type

CREATE QUEUE dbo.TargetQueue


WITH STATUS = ON, Contract Contract
ACTIVATION(
PROCEDURE_NAME =
dbo.usp_HelloWorld
MAX_QUEUE_READERS = 5, Service Queue
EXECUTE AS SELF )
ON PRIMARY;

 Stores the messages before they’re sent and after they’re


received
 Each message is a row in the queue
 Activation specifies the stored procedure (service program)
used to process the messages
Service Broker Service
Service Message Type Message Type

CREATE SERVICE TargetService


ON QUEUE dbo.TargetQueue Contract Contract

( HelloWorldContract,
HelloUniverseContract);
Service Queue

 Name for a specific business task or set of tasks


 Determines which queue will receive messages and enforces
contracts associated with the conversation
Service Broker Components
Service Broker Conversation
Single SQL Server Instance Different SQL Server Instances
Service
Service Program
Begin Dialog
Program Send Message
Begin Dialog End Conversation
Send Message
End Conversation
Transmission
Queue
<Message/>
Target Queue
<Message/>
<Message/>
<Message/> Target Queue

<Message/>
<Message/>
Service
Program
Receive Message
Service
Program
Receive Message
Service Broker Conversation
Dialog Message Type Message Type

BEGIN DIALOG CONVERSATION


@conversationHandle Contract Contract
FROM SERVICE InitiatorService
TO SERVICE 'TargetService'
ON CONTRACT HelloWorldContract;
Service Queue
WITH
RELATED_CONVERSATION =
Related_conversation_handle,
LIFETIME = Dialog_lifetime

 Conversation between two endpoints


 An initiating service must begin a conversation with the target
service before sending a message
Service Broker Conversation
Message Message Type Message Type

SEND ON CONVERSATION
@conversationHandle Contract Contract
MESSAGE TYPE HelloWorldMessage
(N'<message>Hello,World!</message>');
Service Queue

 Data exchanged between services


 Each message is associated with one conversation and has a
Message Type
 Each message can be up to 2 GB in size
Service Broker Conversation
Acknowledgement

 Acknowledgements are automatic and handled completely by


Service Broker.
 Service Broker uses message acknowledgement to provide
reliable messaging without distributed transactions.
 A recipient sends an acknowledgement only after the message has

been added to the queue.


 The sender holds the message in the transmission queue until the

acknowledgement for that message arrives.


Service Broker Conversation
Conversation Group

DECLARE @conversation_group_id UNIQUEIDENTIFIER;


WAITFOR (
GET CONVERSATION GROUP @conversation_group_id
FROM dbo.TargetQueue);

 A group of conversations related to the same task through a


conversation group identifier.
 Service Broker uses the conversation group identifier to
manage message locking and guarantee that messages across
related conversations are processed.
 Exactly-once-in-order.
Service Broker Conversation
Conversation Send Example
DECLARE @conversationHandle UNIQUEIDENTIFIER ;

BEGIN DIALOG CONVERSATION @conversationHandle


FROM SERVICE InitiatorService
TO SERVICE 'TargetService'
ON CONTRACT HelloWorldContract;

SEND ON CONVERSATION @conversationHandle


MESSAGE TYPE HelloWorldMessage
(N'<message>Hello, World!</message>');

 The conversations ID coupled with the sequence number


ensures “exactly-once-in-order” processing of messages
throughout the lifetime of the dialog
 The lifetime of the dialog may span multiple transactions.
Service Broker Conversation
Conversation Receive Example
DECLARE @variables…
BEGIN TRANSACTION ;
RECEIVE TOP(1) @conversation_handle =
conversation_handle,
@message_type_name = message_type_name,
@message_body = CAST(message_body AS XML)
FROM dbo.TargetQueue

IF @message_type_name = N'HelloWorldMessage‘
l BEGIN
l SEND ON CONVERSATION @conversationHandle
l MESSAGE TYPE HelloWorldResponse
l (N'<message>Right Back At You!</message>')
l END CONVERSATION @conversationHandle
COMMIT TRANSACTION;
Service Broker Components
Networking and security components
Networking & Security Components

Remote service binding

CREATE REMOTE SERVICE BINDING binding_name


TO SERVICE 'service_name'
WITH USER = user_name

 Remote Service Binding (RSB) is used to establish security


credentials which will be used by Initiating service to
authenticate itself with the Remote Service.
 RSB uses a Certificate associated with the specified database
user account to connect to Remote Instance.
Networking & Security Components

Route
CREATE ROUTE route_name
WITH
SERVICE_NAME = service_name,
BROKER_INSTANCE = broker_instance_identifier,
ADDRESS = 'TCP://172.22.26.216:4022'

 Route is used to locate a service that it is sending message to.


 When no route is explicitly associated with a service then
Service Broker will deliver the message within the current
instance.
Networking & Security Components

Endpoint

CREATE ENDPOINT EndPoint_name


STATE = STARTED
AS TCP (LISTENER_PORT = 4022)
FOR SERVICE_BROKER
( AUTHENTICATION = CERTIFICATE EndPointCert,
ENCRYPTION = SUPPORTED );

 End Points will accept incoming and outgoing TCP/IP


connections on a Specific port.
 We can have only one End Point for instance which can be
shared between all services in the instance.
 Endpoints can provide transport security, which prevents
unauthorized connections to the endpoint.
Networking & Security Components

Security: Service Broker allows two types of security

 Dialog Security:
 Provides remote authorization for conversations
 End-to-end encryption (encryption message)

 Transport Security:
 Prevents unauthorized network connections.

 Controls which instances can communicate and provides

encryption between the two instances, but it doesn't secure the


contents of individual messages
Demo

 Sending messages between instances on difference server


 Server A sends message to Server B and Server C.
 Server B & C receive the message and send a reply message to
Server A.

 Features
 SQL Service Broker activation.
 Locking related messages.
 Parallel store procedure
Demo
Service Broker Summary
 Impact on database design/architecture
 Asynchronous: parallel stored procedures
 Queuing: handle peak workloads

 Distributed: scalability

 Decoupled: availability

 Features
 Automatic activation

 Single queue with multiple readers

 Related messages locked and processed by the same reader

 Cross database, instance and machine boundaries without changing

application code
 Events in Profiler

 Any program that can issue TSQL, like a C# middle tier Windows

Service, can be a Service Broker endpoint


SQL Service Broker
References
 SQL Server 2005 Books Online
 Demo
http://www.sqlservercentral.com/articles/Service+Broker/2797/
 Article:
 SQL Server 2005 Service Broker

http://dotnet.sys-con.com/node/299073?page=0,0
 SQL Server Service Broker Activation http://e-

articles.info/e/a/title/SQL-Server-Service-Broker-Activation
 SQL Service Broker VS. MSMQ

http://www.devx.com/dbzone/Article/34110
 etc…
Thank You!

LOGO
www.themegallery.com

Вам также может понравиться