This article focuses on basic integration. Run the code first, then talk about advanced features.
Some terminology in RabbitMQ
If you open the RabbitMQ Web console you will find that one of the Exhanges is hard to understand. Here’s a quick explanation.
Exchange
A switch is like a router. We send messages to the switch, which then posts them to the corresponding queue based on a routingKey. (It’s important to understand this concept, and the following code demonstrates it.)
Queue
Queues are easy to understand without explanation.
Binding
How does the exchange know which queue to post this message to? This is where bindings come in. A Queue is bound to an Exchange using a routingKey so that the Exchange knows which Queue to post the message to based on the routingKey. (This is fully reflected in the following code)
Add RabbitMQ Maven dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
Copy the code
Add another dependency (this dependency can be omitted to simplify the code)
<dependency> <groupId>cn. Hutool </groupId> <artifactId>hutool-all</artifactId> <version>4.0.2</version> </dependency>Copy the code
RabbitMQConfig. Java configuration
@Configuration public class RabbitMQConfig { public final static String QUEUE_NAME = "spring-boot-queue"; public final static String EXCHANGE_NAME = "spring-boot-exchange"; public final static String ROUTING_KEY = "spring-boot-key"; @bean public Queue Queue() {return new Queue(QUEUE_NAME); } // create a TopicExchange @bean public TopicExchange exchange() {return new TopicExchange(EXCHANGE_NAME); } // Use a routingKey to bind a Queue to an Exchange @bean public Binding Binding (Queue Queue, TopicExchange exchange) { return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY); } @Bean public ConnectionFactory connectionFactory() { CachingConnectionFactory connectionFactory = new CachingConnectionFactory (" 127.0.0.1 ", 5672); connectionFactory.setUsername("guest"); connectionFactory.setPassword("guest"); return connectionFactory; } @Bean public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) { return new RabbitTemplate(connectionFactory); }}Copy the code
producers
Just call the convertAndSend method of rabbitTemplate. As you can see from the code below, instead of sending a message directly to the queue, we send it to the exchange, which then posts our message to the corresponding queue based on the routing key.
@RestController public class ProducerController { @Autowired private RabbitTemplate rabbitTemplate; @GetMapping("/sendMessage") public Object sendMessage() { new Thread(() -> { for (int i = 0; i < 100; i++) { String value = new DateTime().toString("yyyy-MM-dd HH:mm:ss"); Console.log("send message {}", value); rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME, RabbitMQConfig.ROUTING_KEY, value); } }).start(); return "ok"; }}Copy the code
consumers
The consumer is as simple as adding the @rabbitListener annotation to the corresponding method and specifying the name of the queue to listen on.
@Component
public class Consumer {
@RabbitListener(queues = RabbitMQConfig.QUEUE_NAME)
public void consumeMessage(String message) {
Console.log("consume message {}", message);
}
}
Copy the code
Run the project
Run the project, and then open the browser, enter http://localhost:9999/sendMessage. On the console, you can see that producers are constantly sending messages and consumers are constantly consuming messages.
Open the RabbitMQ Web console and you can also see the switch and queue that we configured in the code, along with the binding information.
Click to enter the details of the switch
conclusion
Due to my limited knowledge and ability, if there is anything unclear in the article, I hope you can point it out in the comments section to help me write my blog better.