栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 数据挖掘与分析

SpringBoot集成rabbitMQ

SpringBoot集成rabbitMQ

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

生产端

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

消费端

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

1.创建一个SpringBoot工程

2.导入相关jar



    4.0.0
    com.itheima
    producer-springboot
    1.0-SNAPSHOT
    
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.4.RELEASE
    
    
        
        
            org.springframework.boot
            spring-boot-starter-amqp
        
        
            org.springframework.boot
            spring-boot-starter-test
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

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
    
    @Bean
    public Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue, @Qualifier("bootExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
    }


}

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

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. 引入依赖坐标


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.4.RELEASE
        
    
    com.itheima
    consumer-springboot
    0.0.1-SNAPSHOT
    
        1.8
    
    
        
        
            org.springframework.boot
            spring-boot-starter-amqp
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

  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()));
    }

}

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/278701.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号