Directly connected switches deliver messages to queues based on the route values carried by messages. A queue is bound to a directly connected switch and a routing key is assigned. Then, when a message carrying a route value of X is sent to the switch through the producer, the switch looks for a queue with a bound value of X based on the route value X.

Maven dependency

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.4. The RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>
</dependencies>
Copy the code

2. Configuration files

server:
  port: 30200

spring:
  rabbitmq:
    host: 148.70153.63.
    port: 5672
    username: libai
    password: password
Copy the code

3. Producers

3.1. Configuration files

Declare queues and switches, and set routing keys bound to queues and switches.

@Configuration
public class DirectRabbitConfig {
    // Queue name: directQueue
    @Bean
    public Queue directQueue(a) {
        return new Queue("directQueue".true.false.false);
    }

    //Direct Switch name: directExchange
    @Bean
    public DirectExchange directExchange(a) {
        return new DirectExchange("directExchange".true.false);
    }

    // bind Binds the queue to the switch and sets the routing key: directRouting
    @Bean
    public Binding bindingDirect(a) {
        return BindingBuilder.bind(directQueue()).to(directExchange()).with("directRouting"); }}Copy the code
  • Declare queue parameter list:
    • name: Queue name.
    • durable: Indicates whether to persistRabbitMQWhen the server restarts, the queue no longer exists.
    • exclusive: Exclusive, that is, whether the queue can only be used by the currently created connection and is deleted when the connection is closed. This parameter has a higher prioritydurable.
    • autoDelete: Indicates whether to delete the queue automatically. If no producer or consumer is using the queue, the queue will be deleted automatically.
  • Declare the switch parameter list:
    • name: Switch name.
    • durable: Indicates whether to persistRabbitMQWhen the server restarts, the queue no longer exists.
    • autoDelete: Indicates whether to delete the queue automatically. If no producer or consumer is using the queue, the queue will be deleted automatically.

3.2. Send messages

@RestController
public class SendMessageController {
    @Autowired
    private RabbitTemplate rabbitTemplate;  // Use RabbitTemplate, which provides receive/send and so on

    @PostMapping("/sendDirectMessage")
    public String sendDirectMessage(a) {
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", String.valueOf(UUID.randomUUID()));
        map.put("messageData"."RabbitMQ");
        map.put("createTime", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        // Send the message with the routing key: directRouting to the switch directExchange
        rabbitTemplate.convertAndSend("directExchange"."directRouting", map);
        return "ok"; }}Copy the code

To the specified switch and specify a routing key. RabbitMQ pushes messages to queues bound to the switch based on routing keys.

The convertAndSend function also supports specifying only the name of the queue to which the message is sent, rather than the switch name. As long as you declare the queue to send, you don’t need to bind it to the switch. Instead of using a switch, routes are routed using RabbitMQ’s default switch and the specified queue. Such as:

rabbitTemplate.convertAndSend("directQueue", map);
Copy the code

Start the service and invoke the send message interface with Postman.

3.3, to viewRabbitMQBackground management interface

You can see that a message has been pushed to the queue, waiting to be consumed.

3.4 viewing switches

You can see that the switch has been created.

You can see the binding to the queue.

3.5. View queues

Queues have also been created.

Check which switches are bound.

4. Consumers

Specify the queue to consume with the @rabbitListener annotation.

@Component
@RabbitListener(queues = "directQueue")
@Slf4j
public class DirectReceiver {
    @RabbitHandler
    public void process(Map testMessage) {
        log.info("DirectReceiver Consumer receives message: {}", JSONUtil.toJsonPrettyStr(testMessage)); }}Copy the code

Restart the service and you can see console print indicating that the message has been successfully consumed.

2020- 09- 13 20:37:19.508 [INFO] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0- 1] [net.zhaoxiaobin.rabbitmq.consumer.direct.DirectReceiver:25[] DirectReceiver Consumer receives message: {"createTime": "The 2020-09-13 20:37:19"."messageId": "f5671a04-21ef-4ba5-8944-28d23ad4b0ad"."messageData": "RabbitMQ"
}
Copy the code

No message to be consumed is displayed on the background management page.

Refer to the link

  • Springboot integration with RabbitMq, read this article carefully enough

The code address

  • Github:github.com/senlinmu100…
  • Gitee:gitee.com/ppbin/rabbi…

Personal website

  • Gitee Pages
  • Github Pages