What is Exchange in RabbitMQ? What types does it have?

What is Exchange in RabbitMQ? What types does it have?

In RabbitMQ, Exchange (switch) is a message transfer station, used to receive messages sent by producers and route them to one or more queues. Exchange sends messages to queues according to specific routing rules so that consumers can receive messages from the queue.

RabbitMQ provides several types of Exchange, each with different routing rules and behavior. Below we will introduce these types one by one and illustrate their use through Java code examples.

  1. Direct Exchange:
    Direct Exchange is the simplest type and sends messages to a queue that exactly matches the routing key of the message. The following is an example of Java code using Direct Exchange:
    This code uses the RabbitMQ client library to create a Direct Exchange (direct exchange) and send messages.
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class DirectExchangeExample {
    
    
    // 定义交换机的名称
    private static final String EXCHANGE_NAME = "direct_exchange";
    // 定义路由键
    private static final String ROUTING_KEY = "direct_routing_key";
    // 定义要发送的消息
    private static final String MESSAGE = "Hello, RabbitMQ!";

    public static void main(String[] args) {
    
    
        try {
    
    
            // 创建连接工厂
            ConnectionFactory factory = new ConnectionFactory();
            // 设置RabbitMQ服务器的主机名
            factory.setHost("localhost");
            // 创建连接
            Connection connection = factory.newConnection();
            // 创建通道
            Channel channel = connection.createChannel();

            // 声明交换机,指定交换机名称和类型为direct
            channel.exchangeDeclare(EXCHANGE_NAME, "direct");
            // 发布消息到交换机,指定交换机名称、路由键、消息属性和消息内容
            channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY, null, MESSAGE.getBytes());

            System.out.println("Message sent!");

            // 关闭通道和连接
            channel.close();
            connection.close();
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
    }
}

In the above code, the name of the switch (EXCHANGE_NAME), the routing key (ROUTING_KEY) and the message to be sent (MESSAGE) are first defined.

Then, a ConnectionFactory object is created and the host name of the RabbitMQ server is set to "localhost".

Next, create a Connection object through the connection factory, and create a Channel object through the connection.

In the channel, use exchangeDeclare()the method to declare a direct switch, specifying the name and type of the switch as "direct".

Then, use basicPublish()the method to send the message to the switch, specifying the name of the switch, routing key, message properties, and message content.

Finally, the channel and connection are closed.

With this code, we can send a message to a specified directly connected switch and specify a routing key to determine the route of the message. In this way, consumers can subscribe to messages of interest based on routing keys.

  1. Fanout Exchange (broadcast switch):
    Fanout Exchange will broadcast the message to all queues bound to it, regardless of the routing key of the message. Here is a Java code example using Fanout Exchange:

This code uses the RabbitMQ client library to create a Fanout Exchange and send messages.

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class FanoutExchangeExample {
    
    
    // 定义交换机的名称
    private static final String EXCHANGE_NAME = "fanout_exchange";
    // 定义要发送的消息
    private static final String MESSAGE = "Hello, RabbitMQ!";

    public static void main(String[] args) {
    
    
        try {
    
    
            // 创建连接工厂
            ConnectionFactory factory = new ConnectionFactory();
            // 设置RabbitMQ服务器的主机名
            factory.setHost("localhost");
            // 创建连接
            Connection connection = factory.newConnection();
            // 创建通道
            Channel channel = connection.createChannel();

            // 声明交换机,指定交换机名称和类型为fanout
            channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
            // 发布消息到交换机,指定交换机名称、空的路由键、消息属性和消息内容
            channel.basicPublish(EXCHANGE_NAME, "", null, MESSAGE.getBytes());

            System.out.println("Message sent!");

            // 关闭通道和连接
            channel.close();
            connection.close();
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
    }
}

In the above code, the name of the switch (EXCHANGE_NAME) and the message to be sent (MESSAGE) are first defined.

Then, a ConnectionFactory object is created and the host name of the RabbitMQ server is set to "localhost".

Next, create a Connection object through the connection factory, and create a Channel object through the connection.

In the channel, exchangeDeclare()a fan switch is declared using the method, specifying the name and type of the switch as "fanout".

Then, use basicPublish()the method to send the message to the switch, specifying the name of the switch, an empty routing key, message attributes, and message content.

Finally, the channel and connection are closed.

With this code, we can send a message to a specified sector switch, which will broadcast the message to all queues bound to it. This way, all consumers receive the same message.

  1. Topic Exchange:
    Topic Exchange matches messages based on their routing keys and patterns and sends the messages to the matching queue. Routing keys can use wildcards for fuzzy matching. The following is an example of Java code using Topic Exchange:

This code uses the RabbitMQ client library to create a Topic Exchange and send messages.

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class TopicExchangeExample {
    
    
    // 定义交换机的名称
    private static final String EXCHANGE_NAME = "topic_exchange";
    // 定义路由键
    private static final String ROUTING_KEY = "topic.routing.key";
    // 定义要发送的消息
    private static final String MESSAGE = "Hello, RabbitMQ!";

    public static void main(String[] args) {
    
    
        try {
    
    
            // 创建连接工厂
            ConnectionFactory factory = new ConnectionFactory();
            // 设置RabbitMQ服务器的主机名
            factory.setHost("localhost");
            // 创建连接
            Connection connection = factory.newConnection();
            // 创建通道
            Channel channel = connection.createChannel();

            // 声明交换机,指定交换机名称和类型为topic
            channel.exchangeDeclare(EXCHANGE_NAME, "topic");
            // 发布消息到交换机,指定交换机名称、路由键、消息属性和消息内容
            channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY, null, MESSAGE.getBytes());

            System.out.println("Message sent!");

            // 关闭通道和连接
            channel.close();
            connection.close();
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
    }
}

In the above code, the name of the switch (EXCHANGE_NAME), the routing key (ROUTING_KEY) and the message to be sent (MESSAGE) are first defined.

Then, a ConnectionFactory object is created and the host name of the RabbitMQ server is set to "localhost".

Next, create a Connection object through the connection factory, and create a Channel object through the connection.

In the channel, exchangeDeclare()a topic switch is declared using the method, specifying the name and type of the switch as "topic".

Then, use basicPublish()the method to send the message to the switch, specifying the name of the switch, routing key, message properties, and message content.

Finally, the channel and connection are closed.

With this code, we can send a message to a designated topic exchange and determine the route of the message based on the routing key. This way, consumers can use wildcards to subscribe to messages of interest.

Guess you like

Origin blog.csdn.net/qq_51447496/article/details/132892019