Contents
|
Scale Out Sample
Time comes when a single worker handling messages is not enough. There is a need
to scale out.
The sample will start with a sender and a receiver.
Open the ScaleOut sample in a Visual studio that was started under admin rights (the Master node requires admin rights to start the
gateway on its endpoint). Your screen should look something like this:
Now let's go look at the code:
Code Walk-ThroughThere are three projects in the solution.
Scaling OutLets assume business is booming, orders keep flowing in, and PlaceOrder commands are stacking up in the Orders.Handler endpoint. We need to scale out. Scaling out can be achieved by having the same Orders.Handler project function as a distributor and another copy of Orders.Handler function as a worker. Starting the Orders.Handler with the Master profile, among other things, turns on the Distributor at this endpoint. Starting the Orders.Handler with the Worker profile, makes it enlist with the Distributor and function as a worker.
Being a distributor means
that Workers can send "I'm ready" message to the control endpoint
of the Distributor, and the Distributor will forward messages to them in a round
robin manner. Steps to scale outThe following are the steps required to scale out our message handlers by deploying more workers on additional machines. 1. Setting up the Master (and Distributor) nodeNo changes should be done at the Master Orders.Handler.
You can start it directly from Visual studio (that was started under admin rights)
and specifying the profiles as command line arguments.
In order for the Master node to function, the machine needs be accessible to Raven from where the Worker is deployed. By default, Raven is accepting calls on port 8080, you can check access to this machine by trying to get to the Raven management console on http://ip-of-MasterNode:8080. If management page does not load, open the port in your incoming firewall. 2. Setting up additional WorkerTo scale out we need another worker to handle PlaceOrder messages. The following steps required to have another Worker up and running:
That's it. To start the worker node, type the following at the command line:
By specifying the NServiceBus.Integration and NServiceBus.Worker profiles, along
with the MasterNodeConfig Node setting in the configuration file, NServiceBus will
direct the Orders.Handler Worker to use the same Raven instance as the Orders.Handler
Master instance. The Raven database is used for storing the subscription information. More on profiles, can be found here 3. Setting up the Sender
Nothing have to be done to the Sender.
<MessageEndpointMappings>
<add Messages="Orders.Messages" Endpoint="Orders.Handler" />
</MessageEndpointMappings>
After setting up the Orders.Handler as a distributor and having the Orders.Handler starting as a worker on another machine, the following diagram demonstrates the flow of messages and the queues that exists on both machines: What is going on here?
As can be seen from the diagram, nothing is changed for the Orders.Sender. It is
still sending messages to Orders.Handler endpoint.
Both this worker and the Order.Handlers that is running on another machine with
the NServiceBus.Worker profile send an "I'm ready to process messages"
to the Distributor, to its Orders.Handler.Control endpoint. When completing handling the ProcessOrder command, workers will send again, the "I'm ready to process messages" to the Distributor control endpoint: Orders.Handler.Control.
The workers are sharing the subscription storage so they can both be able to publish
the OrderPlaces event. Worker At Work:The following snapshot shows the Worker console window at work (the worker is running from a different machine than the Distributor).
As you can see from the Worker console window (above), the worker is receiving the Process Order (order2) and replying with 'OK', processing the order and publishes an Order Placed event.
The error messages that can be seen in this sample appears when there is no valid
license for the worker and it is working on a different node than the distributor.
Where to go next?It might be a good idea to read more about the distributor here. |