SpringBoot集成rabbitMQ

该博客详细介绍了如何在SpringBoot应用中集成RabbitMQ,包括生产端和消费端的配置。生产端通过配置yml文件和RabbitTemplate发送消息,而消费端则利用@RabbitListener进行消息监听和接收。

⚫ SpringBoot提供了快速整合RabbitMQ的方式
⚫ 基本信息再yml中配置,队列交互机以及绑定关系在配置类中使用Bean的方式配置
⚫ 生产端直接注入RabbitTemplate完成消息发送
⚫ 消费端直接使用@RabbitListener完成消息接收

生产端

  1. 创建生产者SpringBoot工程
  2. 引入start,依赖坐标
    < dependency>
    < groupId > org.springframework.boot < /groupId >
    < artifactId>spring-boot-starter-amqp </ artifactId>
    </ dependency>
  3. 编写yml配置,基本信息配置
  4. 定义交换机,队列以及绑定关系的配置类
  5. 注入RabbitTemplate,调用方法,完成消息发送

消费端

  1. 创建消费者SpringBoot工程
  2. 引入start,依赖坐标
    < dependency>
    < groupId>org.springframework.boot < /groupId>
    < artifactId>spring-boot-starter-amqp< /artifactId>
    </ dependency>
  3. 编写yml配置,基本信息配置
  4. 定义监听类,使用@RabbitListener注解完成队列监听

生产端

1.创建一个SpringBoot工程

在这里插入图片描述

2.导入相关jar

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.itheima</groupId>
    <artifactId>producer-springboot</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--
        1. 父工程依赖
    -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
    </parent>
    <dependencies>
        <!--2. rabbitmq-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

3.编写yml配置,基本信息配置

在这里插入图片描述
相当于在spring中配置RabbitMQ的核心xml文件中的创建Connection连接:
在这里插入图片描述

4.定义交换机,队列以及绑定关系的配置类

在com.xxx.rabbitmq包下定义一个RabbitMQConfig 配置类。

这里采用的是topic交换机模式。

@Configuration
public class RabbitMQConfig {

    public static final String EXCHANGE_NAME = "boot_topic_exchange";
    public static final String QUEUE_NAME = "boot_queue";

    //1.交换机
    @Bean("bootExchange")
    public Exchange bootExchange(){
        return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
    }


    //2.Queue 队列
    @Bean("bootQueue")
    public Queue bootQueue(){
        return QueueBuilder.durable(QUEUE_NAME).build();
    }

    //3. 队列和交互机绑定关系 Binding
    /*
        1. 知道哪个队列
        2. 知道哪个交换机
        3. routing key
     */
    @Bean
    public Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue, @Qualifier("bootExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
    }


}

相当于在spring中配置RabbitMQ的核心xml文件中的声明减交换机和队列:
相当于在spring中配置RabbitMQ的核心xml文件中的创建Connection连接:

5.注入RabbitTemplate,调用方法,完成消息发送

在com.xxx.rabbitmq测试包下面书写测试类


@SpringBootTest
@RunWith(SpringRunner.class)
public class ProducerTest {

    //1.注入RabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    public void testSend(){

        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME,"boot.haha","boot mq hello~~~");
    }
}

消费端

  1. 创建消费者SpringBoot工程

在这里插入图片描述

  1. 引入依赖坐标
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/>
    </parent>
    <groupId>com.itheima</groupId>
    <artifactId>consumer-springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--RabbitMQ 启动依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
  1. 编写yml配置,基本信息配置

在这里插入图片描述

  1. 定义监听类,使用@RabbitListener注解完成队列监听

在com.xxx.rabbitmq.listener包下面定义一个RabbimtMQListener类。

/**
 * 定义一个监听类
 */
@Component
public class RabbimtMQListener {
    //生产者端定义队列的名称
    @RabbitListener(queues = "boot_queue")
    public void ListenerQueue(Message message){
        //System.out.println(message);
        System.out.println(new String(message.getBody()));
    }

}

最后分别启动生产者端的测试文件发送消息,启动消费者端启动类接收消息

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值