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/03 Report--
How to understand RabbitMQ 3.9.7 mirror mode cluster and Springboot 2.5.5, I believe that many inexperienced people do not know what to do. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
1. Overview
As the old saying goes: people should be flexible and good at thinking. sometimes if they make a slight turn, the problem may be solved.
Back to the point, we talked about the construction of RabbitMQ 3.9.7 mirror mode cluster. Today we will talk about the integration of RabbitMQ 3.9.7 mirror mode cluster with Springboot 2.5.5.
two。 Scene description
Server An IP:192.168.1.22
Server B IP:192.168.1.8
Server C IP:192.168.1.144
The RabbitMQ mirror mode cluster has been set up on these three servers. For the construction of the mirror mode cluster, please see my previous article.
3. Integration with Springboot 3.1 introduces reliance on org.springframework.boot spring-boot-starter-parent 2.5.5 org.springframework.boot spring-boot-starter-amqp 3.2 production service configuration spring: rabbitmq: addresses: 192.168.1.22 spring 5672192.168.1.8VOR 5672192.168.1.144Ranger 5672 username: guest password : guest virtual-host: / connection-timeout: 16000 # enable message acknowledgement mode publisher-confirm-type: correlated # enable return message mode publisher-returns: true template: mandatory: true3.3 production service code import org.springframework.amqp.AmqpException Import org.springframework.amqp.core.MessagePostProcessor;import org.springframework.amqp.rabbit.connection.CorrelationData;import org.springframework.amqp.rabbit.core.RabbitTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.messaging.Message;import org.springframework.messaging.MessageHeaders;import org.springframework.messaging.support.MessageBuilder;import org.springframework.stereotype.Component;import java.util.Map;@Componentpublic class Producer {@ Autowired private RabbitTemplate rabbitTemplate / * confirm callback * / final RabbitTemplate.ConfirmCallback confirmCallback = new RabbitTemplate.ConfirmCallback () {@ Override public void confirm (CorrelationData correlationData, boolean ack, String cause) {/ / correlationData unique ID / / ack mq received the message / / cause failure reason System.out.println ("correlationData:" + correlationData.getId ()) System.out.println ("ack:" + ack); System.out.println ("cause:" + cause);}} / * * send message * @ param messageBody message body * @ param headers additional attribute * @ throws Exception * / public void sendMessage (String messageBody, Map headers, String id) throws Exception {MessageHeaders messageHeaders = new MessageHeaders (headers); Message message = MessageBuilder.createMessage (messageBody, messageHeaders); rabbitTemplate.setConfirmCallback (confirmCallback); String exchangeName = "exchange-hello" String routingKey = "test.123"; CorrelationData correlationData = new CorrelationData (id) RabbitTemplate.convertAndSend (exchangeName, routingKey, message New MessagePostProcessor () {/ * what to do after sending a message * @ param message * @ return * @ throws AmqpException * / @ Override public org.springframework.amqp.core.Message postProcessMessage (org.springframework.amqp.core.Message message) throws AmqpException {return message }}, correlationData) Spring: rabbitmq: addresses: 192.168.1.22 prefetch 5672192.168.1.8 username 5672192.168.1.144 username: guest password: guest virtual-host: / connection-timeout: 16000 listener: simple: # set to manual ACK acknowledge-mode: manual concurrency: 5 prefetch: 1 max-concurrency: 103.5 Fee service code import com.rabbitmq.client.Channel Import org.springframework.amqp.rabbit.annotation.*;import org.springframework.amqp.support.AmqpHeaders;import org.springframework.messaging.Message;import org.springframework.stereotype.Component @ Componentpublic class Consumer {@ RabbitListener (bindings = @ QueueBinding (value = @ Queue (value = "queue-hello", durable = "true"), exchange = @ Exchange (value = "exchange-hello", durable = "true", type = "topic"), key = "test.*") @ RabbitHandler public void onMessage (Message message Channel channel) throws Exception {System.out.println ("received message:" + message.getPayload ()) Long deliveryTag = (Long) message.getHeaders (). Get (AmqpHeaders.DELIVERY_TAG); channel.basicAck (deliveryTag, false);}} 3.6 Rest Test Code @ RestController@RequestMapping ("/ mq") public class RabbitmqController {@ Autowired private Producer producer; @ GetMapping ("/ sendMessage") public String sendMessage (@ RequestParam String messageBody, @ RequestParam String id) throws Exception {Map headers = new HashMap (); producer.sendMessage (messageBody, headers, id) Return "success";}} after reading the above, have you mastered how to understand the RabbitMQ 3.9.7 mirror mode cluster and Springboot 2.5.5? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.