SpringBoot整合RabbitMq

发布于:2025-03-13 ⋅ 阅读:(16) ⋅ 点赞:(0)

1.引入依赖

<!--RabbitMq相关-->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

2.application.yml文件配置

spring:
  rabbitmq:
    host: 192.168.101.129
    port: 5672
    username: admin
    password: admin
    virtual-host: /

3.编写配置类

主要是声明RabbitTemplate ,创建交换机和队列并进行绑定

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfig {

    public static final String QUEUE_NAME = "exampleQueue";
    public static final String EXCHANGE_NAME = "exampleExchange";
    public static final String ROUTING_KEY = "exampleRoutingKey";

    @Bean("exampleQueue")
    public Queue exampleQueue() {
        // 队列名称
        String name = QUEUE_NAME;
        // 是否持久化,如果设置为 true,队列会在服务器重启后仍然存在
        boolean durable = true;
        // 是否排他,如果设置为 true,该队列仅对首次声明它的连接可见,并在连接断开时自动删除
        boolean exclusive = false;
        // 是否自动删除,如果设置为 true,当没有生产者或者消费者使用时,该队列会自动删除
        boolean autoDelete = false;
        // 队列的其他属性,以键值对的形式提供。这些属性可以用于配置队列的其他行为,
        // 例如消息过期时间、死信交换机等Map.of("x-message-ttl", 60000)
        Map<String, Object> arguments = new HashMap<>();
        return new Queue(name, durable, exclusive, autoDelete, arguments);
    }

    @Bean("exampleExchange")
    public TopicExchange exampleExchange() {
        // 交换机名称
        String name = EXCHANGE_NAME;
        // 是否持久化,如果设置为 true,交换机会在服务器重启后仍然存在
        boolean durable = true;
        // 是否自动删除,如果设置为 true,当没有生产者或者消费者使用时,该交换机会自动删除
        boolean autoDelete = false;
        // 默认为 null。用于设置交换机的其他属性,例如备用交换机、延迟消息等。
        Map<String, Object> arguments = new HashMap<>();
        return new TopicExchange(name, durable, autoDelete, arguments);
    }

    /**
     * 绑定队列和交换机
     *
     * @param exampleQueue
     * @param exampleExchange
     * @return
     */
    @Bean
    public Binding binding(@Qualifier("exampleQueue") Queue exampleQueue,
                           @Qualifier("exampleExchange") TopicExchange exampleExchange) {
        return BindingBuilder.bind(exampleQueue) // 要绑定的队列
                .to(exampleExchange) // 交换机
                .with(ROUTING_KEY); // 路由键
    }

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        return new RabbitTemplate(connectionFactory);
    }
}

4.编写监听类

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

/**
 * description
 *
 * @author PC 2025/03/12 22:02
 */
@Component
public class RabbitMQConsumer {

    @RabbitListener(queues = "exampleQueue")
    public void receiveMessage(String message) {
        System.out.println("Received message: " + message);
    }
}

5. 发送消息试一试

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * description
 *
 * @author PC 2025/03/12 21:52
 */
@RestController
@RequestMapping("/rabbit-mq-test")
public class RabbitMqTestController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostMapping("/send")
    public String send(String me) {
        // 发送到的交换机
        String exchange = "exampleExchange";
        // 发送到的队列路由键
        String routingKey = "exampleRoutingKey";
        // 发送的消息
        String message = me;
        rabbitTemplate.convertAndSend(exchange, routingKey, message);
        return "发送成功";
    }
}

结果成功
在这里插入图片描述