RabbitMQ communication model routing model

RabbitMQ communication model routing model

Hello everyone, I am amazing.

Today, I will lead you to continue learning RabbitMQ and understand the routing model, one of the five communication models of RabbitMQ. There will be a series of tutorials on RabbitMQ in the future. If it helps you, remember to pay attention~

Previous Portal

​​RabbitMQ (1) hello world​​

RabbitMQ (II) Communication Model - Work Model

RabbitMQ (III) Communication Model: Publish-Subscribe Model

Routing Model

RabbitMQ provides five different communication models. In the previous article, we briefly introduced the publish-subscribe model of RabbitMQ. This article will learn about the routing model (direct) in RabbitMQ.

Routing model (direct): The routing mode is equivalent to an upgraded version of the distributed subscription mode, with an additional routing key to constrain the binding of queues and switches.

In the routing model, the producer sends a message to the switch, and the switch forwards the message to the corresponding queue according to the routing key of the message. Each queue can be bound to multiple routing keys, and each routing key can be bound to multiple queues. Consumers receive messages from queues and process them. When a routing key is bound to multiple queues, the switch will send the message to all bound queues. When a queue is bound to multiple routing keys, the queue will be able to receive messages corresponding to all routing keys.

Applicable scenarios

The routing model is suitable for scenarios that require point-to-point communication, such as:

  1. System monitoring alarm notification;
  2. Task distribution;
  3. User private message system;
  4. Order confirmation notification, etc.

Demo

  1. Producer
 // Producer
public class Producer {
private static final String EXCHANGE_NAME = "exchange_direct_1" ;
// Define the key of the route. The key value can be defined arbitrarily
private static final String EXCHANGE_ROUTING_KEY1 = "direct_km1" ;
private static final String EXCHANGE_ROUTING_KEY2 = "direct_km2" ;

public static void main ( String [ ] args ) throws IOException , TimeoutException {
Connection connection = ConnectionUtils .getConnection ( ) ;
Channel channel = connection .createChannel ( ) ;
channel .exchangeDeclare ( EXCHANGE_NAME , "direct" ) ;
for ( int i = 0 ; i < 100 ; i ++ ) {
if ( i % 2 == 0 ) {
channel .basicPublish ( EXCHANGE_NAME , EXCHANGE_ROUTING_KEY1 , null , ( "The " + i + "th message sent by the routing model " ) .getBytes ( ) ) ;
} else {
channel .basicPublish ( EXCHANGE_NAME , EXCHANGE_ROUTING_KEY2 , null , ( "The " + i + "th message sent by the routing model " ) .getBytes ( ) ) ;
}
}
channel .close ( ) ;
connection .close ( ) ;
}
}
  1. consumer
 // Consumer 1
public class Consumer {
private static final String QUEUE_NAME = "queue_direct_1" ;
private static final String EXCHANGE_NAME = "exchange_direct_1" ;
private static final String EXCHANGE_ROUTING_KEY1 = "direct_km1" ;

public static void main ( String [ ] args ) throws IOException , TimeoutException {
Connection connection = ConnectionUtils .getConnection ( ) ;
Channel channel = connection .createChannel ( ) ;
channel .queueDeclare ( QUEUE_NAME , false , false , false , null ) ;
channel .exchangeDeclare ( EXCHANGE_NAME , "direct" ) ;
channel .queueBind ( QUEUE_NAME , EXCHANGE_NAME , EXCHANGE_ROUTING_KEY1 ) ;
DefaultConsumer defaultConsumer = new DefaultConsumer ( channel ) {
@Override
public void handleDelivery ( String consumerTag , Envelope envelope , AMQP .BasicProperties properties , byte [ ] body ) throws IOException {
System .out .println ( "The message received by Consumer 1 is: " + new String ( body ) ) ;
}
} ;
channel .basicConsume ( QUEUE_NAME , true , defaultConsumer ) ;
}
}
 // Consumer 2
public class Consumer2 {
private static final String QUEUE_NAME = "queue_direct_2" ;
private static final String EXCHANGE_NAME = "exchange_direct_1" ;
private static final String EXCHANGE_ROUTING_KEY2 = "direct_km2" ;

public static void main ( String [ ] args ) throws IOException , TimeoutException {
Connection connection = ConnectionUtils .getConnection ( ) ;
Channel channel = connection .createChannel ( ) ;
channel .queueDeclare ( QUEUE_NAME , false , false , false , null ) ;
channel .exchangeDeclare ( EXCHANGE_NAME , "direct" ) ;
channel .queueBind ( QUEUE_NAME , EXCHANGE_NAME , EXCHANGE_ROUTING_KEY2 ) ;
DefaultConsumer defaultConsumer = new DefaultConsumer ( channel ) {
@Override
public void handleDelivery ( String consumerTag , Envelope envelope , AMQP .BasicProperties properties , byte [ ] body ) throws IOException {
System .out .println ( "The message received by Consumer 2 is: " + new String ( body ) ) ;
}
} ;
channel .basicConsume ( QUEUE_NAME , true , defaultConsumer ) ;
}
}
  1. The test starts two consumers first, and then starts the producer. The result is that consumer 1 gets the message with an even sequence number and consumer 2 gets the message with an odd sequence number.

summary

This article introduces the use of the routing model in the RabbitMQ communication model, which implements point-to-point communication through switches and routing keys. It is suitable for scenarios that require point-to-point communication. In actual use, the following points should be noted:

  1. The routing key must be the same as the routing key when the consumer binds the queue, otherwise the message cannot be received;
  2. More flexible message routing can be achieved through multiple exchanges and routing keys.

I will continue to update the series of articles on RabbitMQ in the future. Interested friends please continue to pay attention~~~

<<:  How to ensure wireless network infrastructure supports Wi-Fi 6/6E?

>>:  MWC2023 China Telecom-Huawei Cloud Network Core Capabilities Innovation Achievements Global Conference Held

Recommend

The fatal factor affecting TCP connection throughput: HOL

1. What is HOL HOL means Head of line blocking. I...

A Brief Analysis of Data Flow Technology in Data Centers

What is the most valuable thing in a data center?...

How is lisahost? Lisa host US dual ISP three network 9929 line VPS simple test

A few days ago, the tribe shared the product info...

5 Advantages of Edge Computing in Enterprise Network Strategy

Edge computing has quickly become popular for com...

Experts gather between REST, gRPC and GraphQL!

REST, GraphQL, and gRPC are the three most common...

World Cup employees are distracted and use enterprise-level routing to control

The World Cup has entered the semi-finals, with F...