RabbitMQ workflow

The producers are on the left, the consumers are on the right, and in the red box in the middle are the RabbitMQ servers, which include switches and message queues

The consumer sends a message to the RabbitMQ server, the switch receives the message and sends it to the message queue according to different switch rules. The consumer subscribes to the message and consumes it

RabbitMQ Switch type

Function: Receives messages and routes them to one or more queues according to routing rules. If the route is not available, it is either returned to the producer or discarded. The common switch types for RabbitMQ are Direct, Topic, and FANout. The other switch types are rarely used due to poor performance. These three switch types will be introduced in the following sections.

Direct Exchange —– A directly connected switch that sends messages to corresponding queues based on the routing keys carried by messages.

Topic Exchange —– Topic switch that posts messages to queues according to certain rules.

Fanout Exchange —– A fan switch that does not have a routing key concept and which, upon receiving a message, directly forwards it to all queues bound to it.

Write RabbitMQ examples
  • Project preparation

To make the demonstration more intuitive, this example will create two projects, both using SpringBoot as the framework, one producer (Rabbitmq-provider) and one consumer (Rabbitmq-consumer).

  • Add rabbitMQ-related dependencies for each project
<! > <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <! --rabbitmq--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId>  </dependency>Copy the code
  • The project is configured with application.yml separately
Spring: Application: name: rabbitmq-provider rabbitMQ: host: 127.0.0.1 port: 5672 username: guest password: guestCopy the code
Spring: application: name: rabbitmq-consumer: rabbitMQ: host: 127.0.0.1 port: 5672 username: guest password: guestCopy the code
  • The producer project creates a direct switch
package com.chentawen.rabbitmqprovider.config; import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.DirectExchange; import org.springframework.amqp.core.Queue; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @Author: CTW * @Date: Create in 2021/8/2 21:23 */ @configuration Public class DirectExchangeConfig {/** * declare direct connection to the switch ** @return */ @bean DirectExchange MyDirectExchange() { return new DirectExchange("MyDirectExchange", true, false); } @queue MyDirectQueue() {return new Queue("MyDirectQueue", true); } /** * bind the switch and queue ** @return */ @bean Binding bindingDirect() {return BindingBuilder.bind(MyDirectQueue()).to(MyDirectExchange()).with("DirectRoutingKey"); }}Copy the code
  • The producer project creates the message sending interface
package com.chentawen.rabbitmqprovider.controller; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.HashMap; import java.util.Map; import java.util.UUID; /** * @Author: CTW * @Date: create in 2021/8/2 21:23 */ @RestController public class SendMessageController { @Resource private RabbitTemplate rabbitTemplate; / * * * * * send a message to the direct connect switches @ return * / @ GetMapping ("/sendMessageDirectExchange ") public String sendMessageDirectExchange () { String messageId = String.valueOf(UUID.randomUUID()); String messageData = "Hello World!" ; String createTime = LocalDateTime. Now (). The format (DateTimeFormatter. OfPattern (" on dd MM yyyy years HH: MM: ss ")); Map<String, Object> map = new HashMap<>(16); map.put("messageId", messageId); map.put("messageData", messageData); map.put("createTime", createTime); / * * * name * routingKey routing key exchange switches * the content of messages sent map * / rabbitTemplate. ConvertAndSend (" MyDirectExchange ", "DirectRoutingKey", map); Return "Message sent successfully!" ; }}Copy the code
  • Use the Postman access interface to send messages

  • View RabbitMQ monitoring

Overview page: Ready messages and total messages are 1

On the switch page, you can view the newly created direct switch:The queue page shows the queue you just created and the number of messages

  • The consumer project creates a message receive listener class
package com.chentawen.rabbitmqconsumer.listener; //package com.chentawen.springbootall.config.rabbitlistener; import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; import java.util.Map; /** * @Author: CTW * @Date: Create in 2021/8/222:23 * MyDirectQueue queue name */ @component @rabbitListener (Queues = "MyDirectQueue") public class DirectReceiver {@rabbithandler public void process(Map MessageData) {system.out.println (" Rabbitmq - Consumer receives messages: " + MessageData.toString()); }}Copy the code
  • Start the project, consume the message

Subsequent messages sent are consumed in real time

You can also see messages being consumed in RabbitMQ monitoring

  • Multiple consumers consume

Create another listener in the consumer project

  • Restart the project, send a message, and view the console

You can see that multiple consumers to consume in a polling way, not repeated consumption

That’s all for this episode, and we’ll keep updating