Springboot整合RabbitMQ生产端和消费端

365 阅读3分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第18天,点击查看活动详情

Springboot整合RabbitMQ(生产端)

步骤如下

1.创建生产者SpringBoot项目

2.引入依赖



<dependencies>

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter</artifactId>

    </dependency>



    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-test</artifactId>

        <scope>test</scope>

    </dependency>



    <!--RabbitMQ依赖-->

 <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-amqp</artifactId>

    </dependency>

    <!--单元测试-->

 <dependency>

        <groupId>junit</groupId>

        <artifactId>junit</artifactId>

        <scope>test</scope>

    </dependency>



</dependencies>

3.编写yml配置基本信息

 #配置rabbitmq基本信息 ip port  username password...

spring:

    rabbitmq:

        #ip 默认localhost 远程172.16.98.133

  host: localhost

        #密码  默认guest 游客

  password: guest

        #端口

  port: 5672

        #用户名 默认guest 游客

  username: guest

        #虚拟机 默认/

  virtual-host: /

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

package com.wyh.rabbitmq.config;



import org.springframework.amqp.core.*;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;



 /**

 *  @program:  SpringBoot_RabbitMQ_Procuder

 *  @description:  RabbitMQ配置类

 *  @author:  魏一鹤

 *  @createDate:  2022-03-31 23:49

 **/



 //用于定义配置类,可替换xml配置文件

@Configuration

public class RabbitMQConfig {



    //定义常量

  public static final String EXCHANGE_NAME = "boot_topic_exchange" ;

    public static final String QUEUE_NAME = "boot_queue" ;



    //1 交换机 exchange

 //定义Bean

 @Bean( "bootExchange" )

    public Exchange bootExchange(){

        //构建并且返回通配符规则的交换机               durable持久化

  return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();

    }



    //2 队列 queue

 //定义Bean

 @Bean( "bootQueue" )

    public Queue bootQueue() {

        //持久化队列

  return QueueBuilder.durable(QUEUE_NAME).build();

    }



    //3 队列和队列的绑定关系 binding

 /**

 * 1 知道哪个交换机

 * 2 知道哪个队列

 * 3 设置routingkey 路由键

 **/

 @Bean

    //把交换机和队列进行注入作为参数 @Qualifier精准注入某一个bean

  public Binding bindQueueExchange(@Qualifier( "bootQueue" )Queue queue,@Qualifier( "bootExchange" ) Exchange exchange){

       //指定交换机和队列的绑定 并且指定路由key #0个或者多个单词  noargs不指定参数

  return BindingBuilder.bind(queue).to(exchange).with( "boot.#" ).noargs();

    }

}

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

package com.wyh.test;



import com.wyh.rabbitmq.config.RabbitMQConfig;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.amqp.rabbit.core.RabbitTemplate;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.test.context.junit4.SpringRunner;



import javax.annotation.Resource;

import javax.annotation.security.RunAs;



 /**

 *  @program:  SpringBoot_RabbitMQ_Procuder

 *  @description:  测试rabbitmq生产者

 *  @author:  魏一鹤

 *  @createDate:  2022-04-02 23:26

 **/



@SpringBootTest

@RunWith(SpringRunner.class)

public class ProducerTest {

    //1 注入RabbitTemplate类

 @Resource

    private RabbitTemplate rabbitTemplate;

    //测试

 @Test

    public void send() {

        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME, "boot.weiyihe" , "boot mq hello!!!" );

    }

}

6测试

登录guest(配置的那个账户).查看对应的虚拟机(配置的那个虚拟机),查看队列以及发送的消息

17 Springboot整合RabbitMQ(消费端)

步骤如下

1 创建生产者SpringBoot项目

2 引入依赖



<dependencies>

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter</artifactId>

    </dependency>



    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-test</artifactId>

        <scope>test</scope>

    </dependency>



    <!--RabbitMQ依赖-->

 <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-amqp</artifactId>

    </dependency>

    <!--单元测试-->

 <dependency>

        <groupId>junit</groupId>

        <artifactId>junit</artifactId>

        <scope>test</scope>

    </dependency>



</dependencies>

3 编写yml配置基本信息

 #配置rabbitmq基本信息 ip port  username password...

spring:

  rabbitmq:

    #ip 默认localhost 远程172.16.98.133

  host: localhost

    #密码  默认guest 游客

  password: weiyihe

    #端口

  port: 5672

    #用户名 默认guest 游客

  username: weiyihe

    #虚拟机 默认/

  virtual-host: /itcast_wyh

4 定义监听器,使用@RabbitListener注解完成队列监听

package com.wyh.listener;



import com.sun.org.apache.xpath.internal.operations.String;

import org.springframework.amqp.core.Message;

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

import org.springframework.stereotype.Component;



 /**

 *  @program:  SpringBoot_RabbitMQ_Procuder

 *  @description:  RabbitMQ消费者监听器

 *  @author:  魏一鹤

 *  @createDate:  2022-04-03 22:39

 **/



 //表明这是一个配置文件

@Component

public class RabbitMQListener {



    //监听器注解 里面是监听的队列的名称

 @RabbitListener(queues = "boot_queue" )

    public void Listener(Message message) {

        //由于消息以及被消费了 想要得到纤细只能重新发送

 System.out.println(message); //(Body:'boot mq hello!!!' MessageProperties [headers={}, contentType=text/plain, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=true, receivedExchange=boot_topic_exchange, receivedRoutingKey=boot.weiyihe, deliveryTag=1, consumerTag=amq.ctag-S-FylSWagEzp7TP8BkTvpA, consumerQueue=boot_queue])



 }

}

5 测试

SpingBoot整合RabbitMQ总结

1 SpringBoot提供了快速整合RabbitMQ的方式.需要引入AMQP依赖

2 基本信息在YML中配置,队列交换机以及绑定关系在配置类中使用Bean方式配置

3 生产端直接注入RabbitTemplate完成消息发送

4 消费端直接引用@RabbitListener完成消息接收