This article mainly talks about basic integration. Let’s start the code first, and then talk about advanced features.
Some terms in RabbitMQ
If you open the RabbitMQ web console, you will find that one of the Exhanges is difficult to understand. Let me briefly explain it below.
Exchange
A switch is like a router. We first send messages to the switch, and then the switch delivers messages to the corresponding queue according to the routing key. (It is important to understand this concept, and this is fully reflected in the subsequent code)
Queue
The queue is easy to understand, so there is no need to explain it.
Binding
How does the switch know which queue to deliver this message? This requires binding. It is probably: use a routingKey to bind a queue to a certain exchange, so that the switch knows which queue to deliver the message to according to the routing key. (This is fully reflected in the code below)
Join RabbitMQ maven dependencies
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId></dependency>
Add another dependency (this dependency can be omitted, mainly used to simplify the code)
<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>4.0.2</version></dependency>
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 ROUTING_KEY = "spring-boot-key"; // Create a queue @Bean public Queue queue() { return new Queue(QUEUE_NAME); } // Create a topic type exchange @Bean public TopicExchange exchange() { return new TopicExchange(EXCHANGE_NAME); } // Use the routing key (routingKey) to bind the queue (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); }}Producer
Just call rabbitTemplate's convertAndSend method directly. It can also be seen from the following code that we do not send the message directly to the queue, but first send it to the switch, and the switch then delivers our message to the corresponding queue according to the routing key.
@RestControllerpublic 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"; }}consumer
It is also very simple for consumers. They just need to add the @RabbitListener annotation to the corresponding method and specify the name of the queue to be listened to.
@Componentpublic class Consumer { @RabbitListener(queues = RabbitMQConfig.QUEUE_NAME) public void consumptionMessage(String message) { Console.log("consume message {}", message); }}Run the project
Run the project, then open the browser and enter http://localhost:9999/sendMessage . On the console, you can see the producers constantly sending messages, and consumers constantly consuming messages.
Open the RabbitMQ web console and you can also see the switches and queues we configured in the code just now, as well as binding information.
Click to enter the exchange details
Conclusion
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.