SpringBoot整合RabbitMQ,干净利索!(附项目地址)

大伙可以到我的RabbitMQ专栏获取更多信息

demo示例这里拿

不多bb直接上菜

依赖

生产者和消费者都需要引入该依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

配置

生产者和消费者配置相同,都是连接RabbitMQ的基本连接信息

server:
  port: 2001

spring:
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: xxxxxxxxx
    password: xxxxxxxxx
    virtual-host: /LeoLee

生产者

配置交换机、队列

package com.leolee.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;

/**
 * @ClassName RabbitMQConfig
 * @Description: TODO
 * @Author LeoLee
 * @Date 2020/11/7
 * @Version V1.0
 **/
@Configuration
public class RabbitMQConfig {

    public static final String EXCHANGE_NAME = "boot_topic_exchange";

    public static final String QUEUE_NAME = "boot_queue";

    //交换机
    @Bean("bootExchange")
    public Exchange bootExchange() {

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


    //队列
    @Bean("bootQueue")
    public Queue bootQueue() {

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


    //交换机队列绑定关系
    @Bean
    public Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue, @Qualifier("bootExchange") Exchange exchange) {

        return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();//noargs,不传递参数
    }
}

发送消息

package com.leolee.rabbitmq;

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

/**
 * @ClassName com.leolee.rabbitmq.ProducerTest
 * @Description: TODO
 * @Author LeoLee
 * @Date 2020/11/7
 * @Version V1.0
 **/
@SpringBootTest
@RunWith(SpringRunner.class)
public class ProducerTest {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    public void testSend() {

        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME, "boot.test", "test msg send");
    }
}

消费者

消费者通过配置一个监听器来监听指定queue中的消息,当有消息发送到了监听的queue中,监听器将会执行

package com.leolee.rabbitmq.MsgListener;

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

/**
 * @ClassName TestMsgListener
 * @Description: 监听MQ消息
 * @Author LeoLee
 * @Date 2020/11/7
 * @Version V1.0
 **/
@Component
public class TestMsgListener {

    public static final String QUEUE_NAME = "boot_queue";

    @RabbitListener(queues = "boot_queue")
    public void listenerQueue(Message message) {

        System.out.println(message);
    }
}

 

 


版权声明:本文为qq_25805331原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。