DOCX

Use NServiceBus development of distributed applications

By Samuel King,2015-02-20 23:35
24 views 0
Use NServiceBus development of distributed applications

    richiezhang

    Series theme:The software architecture model based on message

    NServiceBus is a.net platform open source message Service framework, this kind of product is also sometimes referred to as the ESB (Enterprise Service Bus), Enterprise Service Bus (ESB).

NServicebus official address:http://particular.net/

    git:https://github.com/Particular/NServiceBus

    NServiceBus author Udi Dahan, this product as early as 2006 released the first version, it is an enterprise open source products, enterprise development need to purchase a License, with reference to:http://particular.net/licensing

    A, the characteristics of NServiceBus

    1, high performance and scalability

    Can be widely used in many areas of business, scalability and performance have been a reality check.

    2, with automatic retry the reliability of integration

    Through the configuration mechanism provides the best practice of scheme based on message communication, able to identify fault response and automatically retry.

    3, workflow, and the background task scheduling

    Done through Saga of the long-running process definition and function of management, provide a powerful and flexible workflow capabilities.

    4, message focused review process

    The entire distributed system easily gathered in a central location configuration news review.

    5, through the publish/subscribe to reduce the coupling

    Provides the publish/subscribe mechanism.Extensible and configurable and easy to understand and easy to use.

    6, easy to expand and configuration

    Multiple flexible extension point and configuration options, NServieBus can according to user requirements for various features for custom configurations.

    7, support for a wide range of message transmission technology

    Provides MSMQ, RabbitMQ, SQL Server, Windows Azure the Queues, Windows AzureService Bus message transmission mechanism, of course you also can choose custom or by the community development of message transmission scheme.

    Second, the.net platform to introduce other esbs

    1Biztalk

    In the world, Microsoft BizTalk Server has been used to solve the complexity of the data exchange between heterogeneous platform application integration problems.BizTalk also provides the publish/subscribe pattern implementation loosely coupled architecture.Sometimes you need to existing code and a run on historical legacy applications Integration of different technologies and protocols, this is a classic Enterprise Application Integration (Enterprise Application Integration, EAI) scenario.Under this scenario, you can use NServiceBus between business services, within the boundary of these services, you can use BizTalk integration with existing legacy applications.

    As you can see, the after service boundaries of BizTalk is an integration of heterogeneous applications.

    2MassTransit

    MassTransit is a.net platform used to create a distributed application of lightweight open source message bus.

    Website:http://masstransit-project.com/

    git:https://github.com/MassTransit/MassTransit

    MassTransit in 2007, the first version of the development, the author Chris Patterson and Dru Sellers, accidentally met in a meeting, they felt at the time. Not a.net platform service bus frame they want, and then NServiceBus also has just been released, many functions are not perfect, and no good community support.So they developed their own ESB products - MassTransit, at present the latest MassTransit based on the asynchronous support NET4.0 rewrite all the code.MassTransit goal isn't to trying to adapt the big enterprise development in the field of distributed, but rather to achieve a strong lightweight message bus.

    Third,start from Hello World

    Distributed application development is a complicated process, both from the involved technology knowledge system and development, debugging and deployment will bring a lot of challenges, I hope that through this simple example shows the basic ideas of the distributed development.

    1, the preparation work

    Installation MSMQ service, NServiceBus default to use MSMQ service, so beforestarting this case to ensure that the installed MSMQ service.

    2, a new type for the Console Application client: NBus. Practice. GreetingClient, client will command output services "Hello World".

    Install nuget package:

    Install-Package NServiceBus

    3, initialize a Bus, and then command to the server.

    NServiceBus provides the Host program, the application itself Host or use NServiceBus. The Host program to Host the application.Of course, you can also be NServicBus program Host in a Windows service.This client we choose to Host in the client itself.

    We only used a few necessary option to configure the bus.The focus is on this piece of code is configured with an EndPoint: "Nbus. Practice. The HelloWorld. Client", the name on behalf of the Client's network address.

    With this configuration, we can use these configuration to create a bus. using (IBus bus = Bus.Create(busConfiguration).Start())

     {

     SendGreetingCommand(bus);

     }

    The following code by bus. The Send < TCommand > TCommand (CMD) method for the server sends a command:

    private static void SendGreetingCommand(IBus bus)

     {

     Console.WriteLine("Press 'Enter' to send a message.To exit, Ctrl + C");

     var i = 0;

     while (Console.ReadLine() != null)

     {

     var id = Guid.NewGuid();

     bus.Send(new GreetingCommand() { Id = id, Times = i });

     i++;

     Console.WriteLine("Send a new GreetingCommand message with id: {0}", id.ToString("N"));

     }

     }

    GreetingCommand is a message in the message bus, all communication is achieved through the message.

    4, establish a public library: NBus. Practice. GreetingMessage to define the message

    In NServiceBus, command type to inherit in ICommand, event type to inherit in IEvent, simple message is a composed of attribute types, eventually need to serialize and can be spread over the Internet.

    public class GreetingCommand:ICommand

     {

     public Guid Id { get; set; }

     public int Times { get; set; }

     }

    5, the client almost finished, we create a bus, then send a message.Now the problem is that the coupon sent to who?Who sent it to done through a configuration file.

    

    

    Endpoint="Nbus.Practice.HelloWorld.Server" />

    

    The meaning of this configuration is: will be defined in the assembly NBus. Practice. The message is sent to the EndPoint for NBus GreetingMessage. Practice. The HelloWorld. Server program.

    6, obviously we need an EndPoint for Nbus. Practice. The HelloWorld. Server Server, create a new type for the Console Application Server type: Nbus. Practice. GreetingServer, and create a bus andstart with the same method. static void Main(string[] args)

     {

     var busConfiguration = new BusConfiguration();

     busConfiguration.EndpointName("Nbus.Practice.HelloWorld.Server");

     busConfiguration.UseSerialization();

     busConfiguration.UsePersistence();

     using (IBus bus = Bus.Create(busConfiguration).Start())

     {

     Console.WriteLine("Press any key to exit");

     Console.ReadKey();

     }

     }

    To handle GreetingCommand, create a new GreetingHandler. Cs class, only needs to inherit IHandleMessages < GreetingCommand > can show that the class can handle type for GreetingCommand messages.

    public class GreetingHandler:IHandleMessages

     {

     private readonly IBus _bus;

     public GreetingHandler(IBus bus)

     {

     _bus = bus;

     }

     public void Handle(GreetingCommand message)

     {

     Console.WriteLine("Received greetingCommand:{0}, times:{1}, Hello world",message.Id,message.Times);

     _bus.Publish(new GreetingEvent(){Id = message.Id,Times = message.Times});

     }

     }

    Methods public void Handle (GreetingCommand message) describes received GreetingCommand news will output the string "Hello World".Additional code finally released a type for GreetingEvent events, all interested in this event subscribers can subscribe to this event.Publish an event used in NServiceBus bus. The Publish < TEvent > (TEvent event) method.You will notice that we pass to get the bus instance constructor injection.

    Maybe you will be very concerned about how to configure the configuration file of the server?This server received other application sends the message, he didn't know who sent.Issued a separate incident, but specific who subscribe to the event, as the event publishers didn't know it, so the message routing configuration is empty:

    

    7, so far, we have completed the display hello world task, let's run up and have a look:

    In the heart of the project configuration NBus. Practice. GreetingClient and NBus Practice. GreetingServer set tostart at the same time.CTRL + F5

Run results: when we press "Enter" key in the client, the server will receive

a message and prints "Hello World".

    At this point if we shut down the server, and knock a few times more enter in the client sends GreetingCommand message?Let me yao to simulate the real scenario server application downtime due to unknown reasons, to see what would happen?

    At this point if we open the MSMQ management tool will find the service side unhandled messages stored in the queue, until the server back online to process these messages, to ensure the data's eventual consistency in distributed applications.

Report this document

For any questions or suggestions please email
cust-service@docsford.com