RabbitMQ tutorial: Linux installation, basic commands and Spring Boot integration

RabbitMQ tutorial: Linux installation, basic commands and Spring Boot integration

1. Introduction to RabbitMQ

RabbitMQ is an open source message broker and queue server for delivering messages in distributed systems via the lightweight messaging protocol (AMQP). It supports multiple programming languages, including Java, Python, Ruby, etc.

2. Install RabbitMQ under Linux

2.1 Download RabbitMQ

Visit the RabbitMQ official website (https://www.rabbitmq.com/download.html) and select the version suitable for your operating system to download.

2.2 Unzip and install

Extract the downloaded RabbitMQ compressed package to a suitable directory, for example/opt. Then enter the decompressed directory and execute the following command to install:

sudo apt-get update
sudo apt-get install -y rabbitmq-server

2.3 Start the RabbitMQ service

After the installation is complete, start the RabbitMQ service:

sudo systemctl start rabbitmq-server

2.4 Set up auto-start at boot

In order to have the RabbitMQ service run automatically when the system starts, you can execute the following command:

sudo systemctl enable rabbitmq-server

3. RabbitMQ basic commands

RabbitMQ provides some basic commands to manage queues, switches and binding relationships. Here are some commonly used commands:

3.1 Create queue

Use the rabbitmqctl command to create a new queue:

rabbitmqctl create_queue queue_name

3.2 Delete queue

Use the rabbitmqctl command to delete a queue:

rabbitmqctl delete_queue queue_name

3.3 List all queues

Use the rabbitmqctl command to list all queues:

rabbitmqctl list_queues name messages auto_delete

3.4 Create switch

Use the rabbitmqctl command to create a new switch:

rabbitmqctl add_exchange exchange_name type

Among them, exchange_name is the name of the switch, type is the type of switch, which can be direct, , etc. fanout, topic

3.5 Delete the switch

Use the rabbitmqctl command to delete a switch:

rabbitmqctl delete_exchange exchange_name

3.6 List all switches

Use the rabbitmqctl command to list all switches:

rabbitmqctl list_exchanges name type durable auto_delete internal

3.7 Create binding relationship

Use the rabbitmqctl command to create a new binding:

rabbitmqctl bind_queue queue_name exchange_name routing_key

Among them, queue_name is the name of the queue to be bound, exchange_name is the switch name, and routing_key is the routing key.

3.8 Delete binding relationship

Userabbitmqctl command to delete a binding relationship:

rabbitmqctl unbind_queue queue_name exchange_name routing_key

3.9 List all binding relationships

Use the rabbitmqctl command to list all binding relationships:

rabbitmqctl list_bindings source destination routing_key

4. RabbitMQ integrated with Spring Boot

To integrate RabbitMQ with Spring Boot, you need to follow the following steps:

4.1 Add dependencies

Add RabbitMQ Spring Boot Starter dependency in the project'spom.xml file:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

4.2 Configure RabbitMQ connection information

Configure RabbitMQ connection information in the application.properties or application.yml file:

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

4.3 Create message producer

Create a message producer class to send messages to the RabbitMQ queue:

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MessageProducer {
    
    
    private final AmqpTemplate amqpTemplate;
    private final String exchange = "my_exchange"; // 交换机名称,可自定义
    private final String routingKey = "my_routing_key"; // 路由键,可自定义(可选)

    @Autowired
    public MessageProducer(AmqpTemplate amqpTemplate) {
    
    
        this.amqpTemplate = amqpTemplate;
    }

    public void sendMessage(String message) {
    
    
        amqpTemplate.convertAndSend(exchange, routingKey, message);
    }
}

4.4 Create message consumer

Create a message consumer class to receive messages from the RabbitMQ queue:

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class MessageConsumer {
    
    
    @RabbitListener(queues = "my_queue") // 队列名称,可自定义(可选)
    public void receiveMessage(String message) {
    
    
        System.out.println("Received message: " + message);
    }
}

4.5 Test the integration effect

can now be used in the project to send messagesMessageProducer and to receive messages inMessageConsumer. For example, call the sendMessage method in the main class to send a message:

public static void main(String[] args) {
    
    
    MessageProducer messageProducer = new MessageProducer(); // 假设已经注入了AmqpTemplate实例
    messageProducer.sendMessage("Hello, RabbitMQ!");
}

In this way, we have completed the installation, basic commands and integration with Spring Boot of RabbitMQ under Linux.

Guess you like

Origin blog.csdn.net/qq_38374397/article/details/134645056