In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how springboot+rabbitmq can achieve specified consumer consumption. Xiaobian thinks it is quite practical, so share it with you for reference. I hope you can gain something after reading this article.
How to ensure that messages in the mq queue are consumed only by consumers on the test server, avoiding accidental consumption by the local environment?
There is an application scenario in the program that uses rabbitmq_When the finance department confirms that the payment amount from the enterprise has been received, the system will generate a user payment slip for the enterprise order. Due to the large amount of order record data, it is implemented asynchronously through mq instead. That is, after finance confirms the receipt operation, it puts the enterprise order data into mq, and the other end listens to mq message queue, converts the received enterprise order processing into user payment document, and makes it persistent.
The local development environment shares a rabbitmq with the test environment. When the project is deployed to the test environment, QA testing always "inexplicably" finds problems with the saved user payment slip data.
Of course, the first thing to do is to troubleshoot the program and check whether there are bugs in Consumer's data processing logic. After unit testing, it was found that there were no bugs in the test environment.
It turns out that message queues are consumed "abnormally"!
Q: What is the situation?
A: Several partners participate in the project, everyone always has to debug their own program. And if the native program happens to hear a message in the message queue, the message is consumed by the native program. This is where the problem arises!--- Team development, people do not check out the latest version of the program on git in time. If the local version of the program is not the latest and correct version, bugs are bound to occur.
So, what do we do?
Every time you change logic, tell people to get up to date?
Unrealistic, conventional things often don't work.
How to ensure that messages in the mq queue are consumed only by consumers on the test server, avoiding accidental consumption by the local environment? Or, how to achieve targeted consumption of messages?
As long as you are willing to ponder, there are always more ways than difficulties! It's easy to understand!
We know that rabbitmq manual ack mode. That's not enough, because how do we let consumers decide whether to consume? Therefore, we need an identifier---producer to set an identifier, consumer if it matches this identifier, consume, otherwise reject it and put it back into the message queue.
By looking at the code for spring-rabbit/spring-amqp, you can see that you can make an issue of Message Properties in spring-amqp. Producer and consumer each message transmission will carry a MessageProperties, usually we are not specified, take the default value of MessageProperties.
My strategy: Message Properties has a property called AppId. Our program deploys only one test machine, i.e. message Producer and message Consumer on one machine. Then I can use the machine's IP to identify the message. Messages are consumed only if the IP of Producer matches that of Consumer. If the programmer's native IP is different from the test server's IP, he will refuse to receive the message and put the message back into the message queue, waiting for the test server's Consumer to consume it.
Not much to say, code it,
Producer Code:
package com.sboot.mq;import org.junit.Test;import org.springframework.amqp.core.Message;import org.springframework.amqp.core.MessageProperties;import org.springframework.amqp.rabbit.core.RabbitTemplate;import org.springframework.amqp.support.converter.MessageConverter;import org.springframework.amqp.support.converter.SimpleMessageConverter;import org.springframework.beans.factory.annotation.Autowired;import java.net.InetAddress;import java.util.UUID;public class MQProducerTest extends BaseTest { @Autowired RabbitTemplate rabbitTemplate; @Test public void test() throws Exception { for (int i = 1; i
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.