Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Spring Boot integrates RabbitMQ

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Xiaobian to share with you how Spring Boot integrates RabbitMQ, I hope you have something to gain after reading this article, let's discuss it together!

Spring Boot integrates RabbitMQ switches (Exchange)

A switch is like a router. We first send messages to the switch, and then the switch sends messages based on the binding key and the routing key used by the producer when sending messages.

Delivers messages to corresponding queues according to Exchange Type(fanout, direct, topic).

queue (Queue)

A queue for messages.

Binding

How does the switch know which queue to deliver the message to? This requires binding. Roughly: Use a binding key to bind a queue to an Exchange, so that the Exchange knows which queue to deliver the message to based on the routing key.

Add RabbitMQ maven dependency

configured

Configuration in the application.yaml file

RabbitMQConfig.java Configuration @Configurationpublic class RabbitMQConfig {

public final static String QUEUE_NAME = "spring-boot-queue";

public final static String EXCHANGE_NAME = "spring-boot-exchange";

public final static String BINDING_KEY = "spring.boot.key.# ";

//create queue @Bean public Queue queue() {

return new Queue(QUEUE_NAME);

}

//Create a topic exchanger @Bean public TopicExchange exchange() {

return new TopicExchange(EXCHANGE_NAME);

}

//bind Queue to Exchange using routingKey @Bean public Binding binding(Queue queue, TopicExchange exchange) {

return BindingBuilder.bind(queue).to(exchange).with(BINDING_KEY);

}

}

Note: The above configuration is TopicExchange

In practice, multiple queues and binding can be configured to meet requirements.

producers

Just call rabbitTemplate's convertAndSend method directly. As can be seen from the code below, we do not send messages directly to the queue, but first to the switch, and the switch then delivers our messages to the corresponding queue according to the routing key.

consumers

Consumers are also very simple, just add the @RabbitListener annotation to the corresponding method and specify the queue name to listen to.

run the project

Run the project, then open a browser and type http://localhost:9999/sendMessage (depending on the server). On the console, you can see that producers are constantly sending messages and consumers are constantly consuming messages.

After reading this article, I believe you have a certain understanding of "Spring Boot how to integrate RabbitMQ". If you want to know more about it, please pay attention to 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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report