In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "how to use RabbitMQ elegantly". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use RabbitMQ gracefully.
RabbitMQ is undoubtedly one of the most popular message queues and has rich support for a variety of locales. As a .NET developer, it is necessary to learn and understand this tool. There are about three usage scenarios for message queuing:
1. System integration and the design of distributed system. Various subsystems are docked through messages, and this solution has gradually evolved into an architectural style, that is, "architecture through messaging".
2. When the synchronous processing in the system seriously affects the throughput, such as logging. If we need to record all the user behavior logs in the system, if we record the logs synchronously, the response speed of the system will be affected. When we send the log messages to the message queue, the logging subsystem will consume the log messages asynchronously.
3. High availability of the system, such as the second kill scenario of e-commerce. When the application server or database server receives a large number of requests at some point, the system downtime will occur. If the request can be forwarded to the message queue and then consumed by the server, it will make the request smooth and improve the availability of the system.
If you want to learn Java engineering, high-performance and distributed, profound and simple. Micro services, Spring,MyBatis,Netty source code analysis friends can add my Java advanced communication: 854630135, the group has Ali Daniel live explanation technology, as well as Java large-scale Internet technology video to share with you for free.
First, start using RabbitMQ
The RabbitMQ official website provides detailed installation steps. In addition, the official website also provides tutorials for using RabbitMQ in six scenarios. Tutorials 1, 3, and 6 will cover 99% of the usage scenarios, so normally you only need to understand these three tutorials to get started quickly.
Second, simple analysis
Let's do a simple comb with the official tutorial 1: this tutorial shows how Producer sends a message (message) to a message queue (message queue), and the message consumer (Consumer) receives the message and consumes the message.
1. Producer end:
Var factory = new ConnectionFactory () {HostName = "localhost"}
Using (var connection = factory.CreateConnection ())
{
While (Console.ReadLine ()! = null)
{
Using (var channel = connection.CreateModel ())
{
/ / create a message queue called "hello"
Channel.QueueDeclare (queue: "hello"
Durable: false
Exclusive: false
AutoDelete: false
Arguments: null)
Var message = "Hello World!"
Var body = Encoding.UTF8.GetBytes (message)
/ / send a message message to the message queue
Channel.BasicPublish (exchange: ""
RoutingKey: "hello"
BasicProperties: null
Body: body)
Console.WriteLine ("[x] Sent {0}", message)
}
}
}
The code is so simple that it is almost impossible to simplify: create a channel (channel)-> create a queue-> send a message to that queue.
2. Consumer end
Var factory = new ConnectionFactory () {HostName = "localhost"}
Using (var connection = factory.CreateConnection ())
{
Using (var channel = connection.CreateModel ())
{
/ / create a queue named "hello" to prevent the producer side from not creating the queue
Channel.QueueDeclare (queue: "hello"
Durable: false
Exclusive: false
AutoDelete: false
Arguments: null)
/ / callback, which will be executed when consumer receives the message
Var consumer = new EventingBasicConsumer (channel)
Consumer.Received + = (model, ea) = >
{
Var body = ea.Body
Var message = Encoding.UTF8.GetString (body)
Console.WriteLine ("[x] Received {0}", message)
}
/ / consume messages in queue "hello"
Channel.BasicConsume (queue: "hello"
NoAck: true
Consumer: consumer)
Console.WriteLine ("Press [enter] to exit.")
Console.ReadLine ()
}
}
This code can be understood as: create channel-> create queue-> define callback function-> consume messages.
This example describes the Send/Receive pattern, which can be simply understood as a scenario of 1 (producer) VS 1 (consumer).
Example 3 describes the Publish/Subscriber pattern, that is, 1 (producer) VS multiple (consumer)
In the above two examples, producer only needs to send a message and does not care about the return result of consumer. Example 6 describes a RPC call scenario in which producer receives the return result of consumer after sending messages, which seems to run counter to the purpose of using message queues. Because one of the purposes of using message queues is to be asynchronous, but this scenario seems to turn asynchronous into synchronous, but this scenario is also useful, for example, a user operation generates a message, and after receiving the message, the application service executes some logic and changes the database. UI will wait for the return result of the application service before refreshing the page.
If you want to learn Java engineering, high-performance and distributed, profound and simple. Micro services, Spring,MyBatis,Netty source code analysis friends can add my Java advanced communication: 854630135, the group has Ali Daniel live explanation technology, as well as Java large-scale Internet technology video to share with you for free.
Third, discover abstraction
I have a RabbitMQ in Action on my desk, and the documents provided on the official website are very detailed. I feel that I can master RabbitMQ within a month, and then I can write "proficient …" on my resume. It feels a little smug, but I know it's not the best way to use RabbitMQ.
We know that reasonable abstraction can help us hide some technical details and let's focus on the core business. For example, someone asks you, "how can I get to the Big Wild Goose Pagoda?" Your answer may be "Xiao Zhai to the east, go straight for two stops, on the right hand side", if you answer: "turn right 45 degrees, go ahead 100 meters, and then turn 90 degrees." The other person will be lost in these details.
There is actually an abstraction hidden in the use of message queues-Service bus (Service Bus).
Looking back at the first example, the business implied in this example is that ClientA sends an instruction and ClientB reacts to it. If so, why should we care about how to create a channel and how to create a queue? I just want to send a message. In addition, this example is not robust enough:
There is no retry mechanism: how to process the message if the ClientB does not execute successfully the first time?
There is no error handling mechanism: what if ClientB retries N times and still has an exception to handle the message?
There is no fuse mechanism.
How to make a schedule for ClientA, such as scheduled delivery, etc.
There is no message audit mechanism.
Unable to track the status of the message
Dealing with things, etc.
The service bus is the abstraction of this scenario and provides us with these mechanisms, so let's take a quick look at it.
IV. First acquaintance of MassTransit
MassTransit is an open source free ESB product under the .NET platform, the official website: http://masstransit-project.com/ dagger GitHub 700 star,500 Fork, similar products and NServiceBus, the reason for choosing MassTransit is that it is lighter than NServiceBus, in addition, at the beginning of MassTransit development, I chose RabbitMQ as a message transmission component; at the same time, I want to compare him with NServiceBus to see what they focus on.
1. Create a new console application: Masstransit.RabbitMQ.GreetingClient
Using MassTransit, you can install from Nuget:
Install-Package MassTransit.RabbitMQ
2. Create a service bus and send a command
Static void Main (string [] args)
{
Console.WriteLine ("Press' Enter' to send a message.To exit, Ctrl + C")
Var bus = BusCreator.CreateBus ()
Var sendToUri = new Uri ($"{RabbitMqConstants.RabbitMqUri} {RabbitMqConstants.GreetingQueue}")
While (Console.ReadLine ()! = null)
{
Task.Run (() = > SendCommand (bus, sendToUri). Wait ()
}
Console.ReadLine ()
}
Private static async void SendCommand (IBusControl bus,Uri sendToUri)
{
Var endPoint = await bus.GetSendEndpoint (sendToUri)
Var command = new GreetingCommand ()
{
Id = Guid.NewGuid ()
DateTime = DateTime.Now
}
Await endPoint.Send (command)
Console.WriteLine ($"send command:id= {command.Id}, {command.DateTime}")
}
This code hides many details about the message queue and focuses our attention on sending messages. At the same time, the API provided by ServiceBus is closer to the business. Although we are sending a message, it is a command in this scenario, and the API of Send (command) describes our intention.
If you want to learn Java engineering, high-performance and distributed, profound and simple. Micro services, Spring,MyBatis,Netty source code analysis friends can add my Java advanced communication: 854630135, the group has Ali Daniel live explanation technology, as well as Java large-scale Internet technology video to share with you for free.
3. The server receives this command
Create a new command console control program: Masstransit.RabbitMQ.GreetingServer
Var bus = BusCreator.CreateBus ((cfg, host) = >
{
Cfg.ReceiveEndpoint (host, RabbitMqConstants.GreetingQueue, e = >
{
E.Consumer ()
});
});
This code can be understood to mean that the server is listening for messages. We have registered a consumer named "GreetingConsumer" with the server, the definition of GreetingConsumer:
Public class GreetingConsumer: IConsumer
{
Public async Task Consume (ConsumeContext context)
{
Await Console.Out.WriteLineAsync ($"receive greeting commmand: {context.Message.Id}, {context.Message.DateTime}")
}
}
The consumer can consume messages of type GreetingCommand. This example almost hides the technical details about RabbitMQ, puts the code center in the business, and tries to run the two console applications:
Fifth, implement the Publish/Subscribe model
The publish / subscribe model makes the messaging-based software architecture possible, and this ability is represented by the fact that both ClientA and ClientC can subscribe to message X.
1. Let's modify it in the above example. When GreetingConsumer receives the GreetingCommand, send a GreetingEvent:
Var greetingEvent = new GreetingEvent ()
{
Id = context.Message.Id
DateTime = DateTime.Now
}
Await context.Publish (greetingEvent)
2. Create a new console program Masstransit.RabbitMQ.GreetingEvent.SubscriberA to subscribe to GreetingEvent messages:
Var bus = BusCreator.CreateBus ((cfg, host) = >
{
Cfg.ReceiveEndpoint (host, RabbitMqConstants.GreetingEventSubscriberAQueue, e = >
{
E.Consumer ()
});
});
Bus.Start ()
Define GreetingEventConsumer:
If you want to learn Java engineering, high-performance and distributed, profound and simple. Micro services, Spring,MyBatis,Netty source code analysis friends can add my Java advanced communication: 854630135, the group has Ali Daniel live explanation technology, as well as Java large-scale Internet technology video to share with you for free.
Public class GreetingEventConsumer:IConsumer
{
Public async Task Consume (ConsumeContext context)
{
Await Console.Out.WriteLineAsync ($"receive greeting event: id {context.Message.Id}")
}
}
This code is almost identical to Masstransit.RabbitMQ.GreetingServer receiving a command, except that:
In Send/Receive mode, Client first obtains the endpoint (endpoint) of the other party (Server) and sends commands directly to that endpoint. The Server party listens to its own endpoints and consumes commands.
In Publish/Subscribe mode, Client publish an event, SubscriberA listens for events at its own endpoint (endpointA), and SubscriberB listens for events at its own endpoint (endpointB).
3. Define another Masstransit.RabbitMQ.GreetingEvent.SubscriberB according to the above analysis
4. Run 4 console applications to have a look.
Sixth, realize the RPC mode
This pattern is called Request/Response pattern in Masstransit, and related operations are implemented through the IRequestClient interface. A related example is on the official github.
At this point, I believe you have a deeper understanding of "how to use RabbitMQ elegantly". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.