1. What is NService Bus
2. How to prepare
3. Messages
4. Routing
5. Configurations
6. Fault tolerance
What is NService Bus
1. Frame work enabling communication between application using messaging.It lies on top of messaging backed called transports. So NService Bus work like an abstraction for transports.
2. Transports are configurable. Initially NService Bus supports MSMQ now it support RabitMq, Azure Service bus, ASW SQS.
https://docs.particular.net/transports/
3. It is opensource, but not free licence is required.
4. Download NService Bus we can install it in local machine in background NService Bus agent will install it will take care about configurations.
5. We can use Nuget package to install NService Bus to Applications.
6. We have three main Nuget packages.
a. NServiceBus
b. Different Transports Nuget packages
c. NService Bus hosting Nuget package
Problem Resolved By NServiceBus
1. If business get increase then to much load on the servers it may cause orders are getting lost.
So solution is put orders in queues and process one by one.
2. So Api servers will sends commands those commands are in the form of messages that is queued under transport.
How to prepare
1. Create Web Api This will communicate to end user to place an order.
Also add NService bus package from Nuget. and Message class library as reference to this project.
Install-Package NServiceBus -Version 6.4.0
2. Create class library that can share between website, micro services and Nservice bus server. It will contain messages like c# class only contain properties it like a contact to find how the app should communicate.
This class library will contain commands. Create commands using ICommand Interface from NServiceBus package.
3. Create console app this will act as server.
Add NService bus package from Nuget and we can self host this app or we can use NService Bus host Nuget package to host this server.
Add Process order handler this will handle the commands.
Technical Details
Solution details
1.Create command in message library this command will use in client and server
2.Create Nservice Bus Client endpoint using Learn Transport
3.Create Nservice Bus Client endpoint using AWS SQS Transport
1.We have to set AWS_ACCESS_KEY_ID , AWS_SECRET_ACCESS_KEY in environment variable.
2.By default, AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are discovered from environment variables of the machine that is running the endpoint:
3.These AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY we will get from ASW Login .
4. We need region also we will get this from AWS login.
4.Create command from client and send this command to queue.
5. Create Server that will read commands from queue and process them
6. Create Handler that will handle commands
Messages
A Message is the unit of communication for NServiceBus. There are two sub-types of messages that capture more of the intent and help NServiceBus enforce messaging best practices.
Routing
1. Routing subsystem is responsible for finding destinations for the messages. In most cases the sending code should not care about the destination of the message being sent:
2. var endpointInstance = await Endpoint.Start(endpointConfiguration)
.ConfigureAwait(false);
var message = new MyMessage();
await endpointInstance.Send(message)
.ConfigureAwait(false);
Based on the type of the message the routing provides the destination address.
3. NServiceBus routing consists of two layers, logical and physical. Logical routing defines which logical endpoint should receive a given outgoing message. Physical routing defines to which actual physical instance of the selected endpoint should the message be delivered. While logical routing is a developer's concern, physical routing is controlled by operations.
Configurations
// The endpoint name will be used to determine queue names and serves
// as the address, or identity, of the endpoint
var endpointConfiguration = new EndpointConfiguration(
endpointName: "Order.Client");
endpointConfiguration.SendFailedMessagesTo("error");
// Use XML to serialize and deserialize messages (which are just
// plain classes) to and from message queues
endpointConfiguration.UseSerialization<XmlSerializer>();
// Ask NServiceBus to automatically create message queues
endpointConfiguration.EnableInstallers();
// Store messages on disk for this example, rather than in
// a real queue.
endpointConfiguration.UseTransport<LearningTransport>();
//var transport = endpointConfiguration.UseTransport<SqsTransport>();
//transport.Region("us-east-1");
//endpointConfiguration.DisableFeature<MessageDrivenSubscriptions>();
//transport.CredentialSource(SqsCredentialSource.EnvironmentVariables);
// Store information on disk for this example, rather than in
// a database. In this sample, only subscription information is stored
endpointConfiguration.UsePersistence<LearningPersistence>();
// Initialize the endpoint with the finished configuration
var endpointInstance = await Endpoint.Start(endpointConfiguration)
.ConfigureAwait(false);
return endpointInstance;
Fault tolerance
With Delayed Retries, the message causing the exception is instantly retried via a retries queue instead of an error queue.
2. How to prepare
3. Messages
4. Routing
5. Configurations
6. Fault tolerance
What is NService Bus
1. Frame work enabling communication between application using messaging.It lies on top of messaging backed called transports. So NService Bus work like an abstraction for transports.
2. Transports are configurable. Initially NService Bus supports MSMQ now it support RabitMq, Azure Service bus, ASW SQS.
https://docs.particular.net/transports/
3. It is opensource, but not free licence is required.
4. Download NService Bus we can install it in local machine in background NService Bus agent will install it will take care about configurations.
5. We can use Nuget package to install NService Bus to Applications.
6. We have three main Nuget packages.
a. NServiceBus
b. Different Transports Nuget packages
c. NService Bus hosting Nuget package
Problem Resolved By NServiceBus
1. If business get increase then to much load on the servers it may cause orders are getting lost.
So solution is put orders in queues and process one by one.
2. So Api servers will sends commands those commands are in the form of messages that is queued under transport.
How to prepare
1. Create Web Api This will communicate to end user to place an order.
Also add NService bus package from Nuget. and Message class library as reference to this project.
Install-Package NServiceBus -Version 6.4.0
2. Create class library that can share between website, micro services and Nservice bus server. It will contain messages like c# class only contain properties it like a contact to find how the app should communicate.
This class library will contain commands. Create commands using ICommand Interface from NServiceBus package.
3. Create console app this will act as server.
Add NService bus package from Nuget and we can self host this app or we can use NService Bus host Nuget package to host this server.
Add Process order handler this will handle the commands.
Technical Details
Solution details
1.Create command in message library this command will use in client and server
2.Create Nservice Bus Client endpoint using Learn Transport
3.Create Nservice Bus Client endpoint using AWS SQS Transport
1.We have to set AWS_ACCESS_KEY_ID , AWS_SECRET_ACCESS_KEY in environment variable.
2.By default, AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are discovered from environment variables of the machine that is running the endpoint:
3.These AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY we will get from ASW Login .
4. We need region also we will get this from AWS login.
4.Create command from client and send this command to queue.
Messages
A Message is the unit of communication for NServiceBus. There are two sub-types of messages that capture more of the intent and help NServiceBus enforce messaging best practices.
Routing
1. Routing subsystem is responsible for finding destinations for the messages. In most cases the sending code should not care about the destination of the message being sent:
2. var endpointInstance = await Endpoint.Start(endpointConfiguration)
.ConfigureAwait(false);
var message = new MyMessage();
await endpointInstance.Send(message)
.ConfigureAwait(false);
Based on the type of the message the routing provides the destination address.
3. NServiceBus routing consists of two layers, logical and physical. Logical routing defines which logical endpoint should receive a given outgoing message. Physical routing defines to which actual physical instance of the selected endpoint should the message be delivered. While logical routing is a developer's concern, physical routing is controlled by operations.
Configurations
// The endpoint name will be used to determine queue names and serves
// as the address, or identity, of the endpoint
var endpointConfiguration = new EndpointConfiguration(
endpointName: "Order.Client");
endpointConfiguration.SendFailedMessagesTo("error");
// Use XML to serialize and deserialize messages (which are just
// plain classes) to and from message queues
endpointConfiguration.UseSerialization<XmlSerializer>();
// Ask NServiceBus to automatically create message queues
endpointConfiguration.EnableInstallers();
// Store messages on disk for this example, rather than in
// a real queue.
endpointConfiguration.UseTransport<LearningTransport>();
//var transport = endpointConfiguration.UseTransport<SqsTransport>();
//transport.Region("us-east-1");
//endpointConfiguration.DisableFeature<MessageDrivenSubscriptions>();
//transport.CredentialSource(SqsCredentialSource.EnvironmentVariables);
// Store information on disk for this example, rather than in
// a database. In this sample, only subscription information is stored
endpointConfiguration.UsePersistence<LearningPersistence>();
// Initialize the endpoint with the finished configuration
var endpointInstance = await Endpoint.Start(endpointConfiguration)
.ConfigureAwait(false);
return endpointInstance;
Fault tolerance
With Delayed Retries, the message causing the exception is instantly retried via a retries queue instead of an error queue.
No comments:
Post a Comment