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

RabbitMQ与SpringBoot整合

RabbitMQ与SpringBoot整合

搭建生产者工程

实现步骤:

    创建生产者SpringBoot工程引入依赖坐标编写yml配置,基本信息配置定义交换机,队列以及绑定关系的配置类注入RabbitTemplate,调用方法,完成消息发送项目名字 :producer-springboot

添加依赖 


    org.springframework.boot
    spring-boot-starter-parent
    2.2.2.RELEASE



    
    
        org.springframework.boot
        spring-boot-starter-amqp
    
    
        org.springframework.boot
        spring-boot-starter-test
    

配置整合

创建application.yml连接参数等配置文件;

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

 创建配置类

package com.Zh.config;

import com.rabbitmq.client.AMQP;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;



//配置类:声明交换机,队列,以及交换机和队列的绑定
@Configuration
public class RabbitMQConfig {
    public static final String EXCHANGE_NAME = "boot_topic_exchange";//交换机名字
    public static final String QUEUE_NAME = "boot_queue";//队列名字
    //1.交换机
    @Bean(value = "topicExchange")
    public Exchange topicExchange() {
        return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
    }

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

    //3.队列和交换机绑定关系 Binding
    
    @Bean
    public Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue,
                                     @Qualifier("topicExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
    }

}

创建入口类

@SpringBootApplication
public class ProducerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProducerApplication.class);
    }
}

 发送消息

创建测试文件 

package com.Zh;

import com.Zh.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;


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

    @Autowired
    private RabbitTemplate rabbitTemplate;

    
    @Test
    public void testSend(){
        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME,"boot.haha","mq hello");
    }
}
搭建消费者工程

实现步骤

    创建消费者SpringBoot工程引入start,依赖坐标编写yml配置,基本信息配置定义监听类,使用@RabbitListener注解完成队列监听。

 创建工程consumber-springboot

添加依赖

    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.2.RELEASE
    
    
        
        
            org.springframework.boot
            spring-boot-starter-amqp
        
    

配置整合

创建application.yml连接参数等配置文件;

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

 消息监听器

队列监听器

创建 RabbimtMQListener

package com.Zh.listener;

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

@Component
public class RabbimtMQListener {

    @RabbitListener(queues = "boot_queue")//要监听的队列
    public void listenerQueue(Message message){
        System.out.println(new String(message.getBody()));
    }
}

创建入口类

@SpringBootApplication
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }
}

运行applicaion:

小结:

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

 

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

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

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