RabbitMQ是一个被广泛使用的开源消息队列。它是轻量级且易于部署的,它能支持多种消息协议。RabbitMQ可以部署在分布式和联合配置中,以满足高规模、高可用性的需求。

RabbitMQ安装

1、安装Erlang,下载地址:http://erlang.org/download/otp_win64_21.3.exe

springboot 消费队列 springboot集成消息队列_Test

2、安装RabbitMQ,下载地址:https://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.7.14/rabbitmq-server-3.7.14.exe

springboot 消费队列 springboot集成消息队列_Test_02

3、安装完成后,进入RabbitMQ安装目录下的sbin目录

springboot 消费队列 springboot集成消息队列_自定义_03

4、在地址栏输入cmd并回车启动命令行,然后输入以下命令启动管理功能:

rabbitmq-plugins enable rabbitmq_management

springboot 消费队列 springboot集成消息队列_自定义_04

5、访问地址查看是否安装成功:http://localhost:15672/

springboot 消费队列 springboot集成消息队列_Test_05

6、输入账号密码并登录:guest guest

springboot 消费队列 springboot集成消息队列_Test_06

下面开始springboot整合,总体流程就是 先创建交换器,然后创建队列,然后将两者绑定到一起之后进行消息操作
1、创建一个项目,选择RabbitMQ和web

springboot 消费队列 springboot集成消息队列_springboot 消费队列_07

2、配置MQ相关数据源

springboot 消费队列 springboot集成消息队列_自定义_08

3、进入后台 http://localhost:15672/#/exchanges 添加自定义交换器

springboot 消费队列 springboot集成消息队列_springboot 消费队列_09

4、然后添加队列

springboot 消费队列 springboot集成消息队列_System_10

5、点进来交换器绑定队列

springboot 消费队列 springboot集成消息队列_System_11

6、绑定完之后测试

发送:

/**
 * 1、单播(点对点)
 * exchange.direct 交换器
 * xianyu.news 队列
 * new Book 对象内容
 */
@Test
public void contextLoads() {
    Map<String,Object> map = new HashMap<>();
    map.put("msg","这是第一个消息");
    map.put("data", Arrays.asList("helloworld",123,true));
    //对象被默认序列化以后发送出去
    rabbitTemplate.convertAndSend("exchange.direct","xianyu.news", map);

}
序列化存储

springboot 消费队列 springboot集成消息队列_Test_12

接收(自动反序列化) :
@Test
public void receive(){
    Object o = rabbitTemplate.receiveAndConvert("xianyu.news");
    System.out.println(o.getClass());
    System.out.println(o);
}
没有问题

springboot 消费队列 springboot集成消息队列_System_13

如果想要使用Json形式存储,需要修改消息转换器

springboot 消费队列 springboot集成消息队列_Test_14

@Configuration
public class MyAMQPConfig {

    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }
}
再次运行:第一条是配置前发的消息,序列化存储进去,第二条是加了配置之后发的消息,是一个jSON格式数据

springboot 消费队列 springboot集成消息队列_Test_15

一样可以取出

springboot 消费队列 springboot集成消息队列_自定义_16


广播形式发送:调用fanout交换器
/**
 * 广播形式发送:调用fanout交换器
 */
@Test
public void sendMsg(){
    rabbitTemplate.convertAndSend("exchange.fanout","",new Book("三体 ","刘慈欣"));
}
发送之后,每个与之绑定的队列都会受到消息

springboot 消费队列 springboot集成消息队列_Test_17

随便打开一条

springboot 消费队列 springboot集成消息队列_springboot 消费队列_18


监听:启动项加注解 @EnableRabbit

springboot 消费队列 springboot集成消息队列_自定义_19

创建Service测试
@Service
public class BookServiceImpl implements BookService {

    /**
     * queues是数组形式,可以监听多个队列
     */
    @RabbitListener(queues = "xianyu.news")
    public void receive(Book book){
        System.out.println("收到消息" + book);
    }
}
重启服务,发现启动服务后自动监听到了队列消息

springboot 消费队列 springboot集成消息队列_System_20

当然我们也可以自定义监听消息
/**
 * 自定义监听,消息头等等
 * @param message
 */
@RabbitListener(queues = "xianyu.emps")
public void receive02(Message message){
    System.out.println(message.getBody());
    System.out.println(message.getMessageProperties());
}

springboot 消费队列 springboot集成消息队列_springboot 消费队列_21


AmqAdmin:使用代码创建和删除Queue,Exchange,Bingding
@Test
    public void createExchange(){

//    amqpAdmin.declareExchange(new DirectExchange("amqpadmin.exchange"));
//    System.out.println("创建完成");

        //创建队列
//    amqpAdmin.declareQueue(new Queue("amqpadmin.queue",true));

      //创建绑定规则
      amqpAdmin.declareBinding(new Binding("amqpadmin.queue", Binding.DestinationType.QUEUE,"amqpadmin.exchange","amqp.haha",null));

      //各种删除
        //amqpAdmin.de 
    }

创建交换器
amqpAdmin.declareExchange(new DirectExchange("amqpadmin.exchange"));

springboot 消费队列 springboot集成消息队列_springboot 消费队列_22


创建队列
amqpAdmin.declareQueue(new Queue("amqpadmin.queue",true));

springboot 消费队列 springboot集成消息队列_自定义_23


交换器绑定队列
amqpAdmin.declareBinding(new Binding("amqpadmin.queue", Binding.DestinationType.QUEUE,"amqpadmin.exchange","amqp.haha",null));

springboot 消费队列 springboot集成消息队列_springboot 消费队列_24