How to use SpringBoot+RabbitMQ to achieve reliable message transmission
May 29, 2023 pm 10:34 PMEnvironment configuration
SpringBoot
Integration RabbitMQ
realizes the sending of messages.
1. Add maven
dependencies
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
2. Add application.yml configuration file
1 2 3 4 5 6 |
|
3. Configure switches, queues and bindings
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
4. Production sends messages
1 2 3 4 5 6 7 8 9 |
|
5. Consumers receive messages
1 2 3 4 5 6 |
|
6. Call the production end to send messages hello
, console output:
[Send message] hello
[Receive message] hello Current time 2022-05-12 10:21:14
Indicates that the message has been successfully received.
Message loss analysis
From production to consumption of a message, message loss may occur in the following stages:
Lost on the production side: The producer cannot transmit to
RabbitMQ
Lost on the storage side:
RabbitMQ
The storage itself is downLost at the consumer end: The storage cannot be sent to the consumer end due to network problems, or consumption hangs and normal consumption cannot be sent
RabbitMQ
Provides good support for reliable transmission from the production side, storage side, and consumer side.
Production phase
The production phase uses the request confirmation mechanism to ensure reliable transmission of messages. After sending a message to the RabbitMQ server, RabbitMQ receives the message and returns a request confirmation to the sender, indicating that the RabbitMQ server has successfully received the message.
Configuration application.yml
1 2 3 4 5 6 |
|
Configuration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
|
Messages from producer to switch, there is confirmCallback
Confirm mode. After the message is successfully sent, the message will call the method confirm(CorrelationData correlationData, boolean ack, String cause)
, and determine whether the message is successfully sent based on ack
.
Messages from switch to queue have returnCallback
return mode.
Send message product message
The console output is as follows:
[Send message]product message
[Receive message]product message Current time 2022-05 -12 11:27:56
[correlationData]:null
[ack]true
[cause]null
[Send successfully]
The production end simulation message is lost
There are two solutions here:
Close the broker immediately after sending the message. The latter shuts down the network, but after the broker is closed, the console will always report an error and send the message. Also reported a 500 error.
Send non-existent switch:
1 2 |
|
Result:
[correlationData]:null
【ack】false
【cause】channel error; protocol method: #method(reply-code=404, reply-text=NOT_FOUND - no exchange 'myExchangexxxxx' in vhost '/', class-id =60, method-id=40)
[Failed to send]
When sending fails, you can retry the message
The switch is correct and the queue that does not exist is sent:
The switch receives the message and returns a success notification. The console output:
[correlationData]:CorrelationData [id=7d468b47-b422-4523-b2a2-06b14aef073c]
[ack ]true
[cause]null
[Send successfully]
The switch did not find the queue and returned failure information:
[Message sending failed]
[message]product message
[replyCode]312
RabbitMQ
Enable queue persistence, create queues and switchesThe default configuration is persistence of. First, set the queue and switch correctly, and modify the queue for consumption monitoring so that messages are stored in the queue.
Modify the persistence of the queue to non-persistence:
1 2 3 4 5 |
|
After sending the message, the message is stored in the queue, and then restarts RabbitMQ
, the message no longer exists.
Set queue persistence:
1 2 3 4 5 |
|
After restarting, the messages in the queue still exist.
Consumer end
The consumer end starts by default ack
Automatic confirmation mode. When the queue message is received by the consumer, it will be automatically deleted from the queue regardless of whether there is a message from the consumer end. news. Therefore, in order to ensure that the consumer can successfully consume the message, change the automatic mode to manual confirmation mode:
Modify the application.yml file
1 2 3 4 5 6 |
|
After consuming and receiving the message, manual confirmation is required:
1 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
If not added:
1 |
|
Send two messages
After the message is received, there is no confirmation and it is put back into the queue:
Restart the project. After that, the messages in the queue will be sent to the consumer, but without ack confirmation, they will continue to be put back into the queue.
After adding channel.basicAck
, restart the project
The queue message will be deleted
basicAck
The last parameter of the method multiple
means deleting the previous queue.
multiple
is set to true
, and all subsequent queues are cleared
The above is the detailed content of How to use SpringBoot+RabbitMQ to achieve reliable message transmission. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use RabbitMQ to implement distributed message processing in PHP

How to build a reliable messaging app with React and RabbitMQ

Comparison and difference analysis between SpringBoot and SpringMVC

Using RabbitMQ in Go: A Complete Guide

Application practice of go-zero and RabbitMQ

SpringBoot+Dubbo+Nacos development practical tutorial

Solution for real-time data synchronization between Golang and RabbitMQ

Golang RabbitMQ: Architectural design and implementation of a highly available message queue system
