专栏名称: hryou0922
目录
相关文章推荐
大众网青岛  ·  知名男星宣布当爸! ·  昨天  
江西旅游广播  ·  42岁热播剧演员在街头摆摊刷鞋!本人回应 ·  2 天前  
51好读  ›  专栏  ›  hryou0922

Spring Boot系列十三 Spring Boot集成RabbitMQ

hryou0922  · 掘金  ·  · 2018-03-21 02:05

正文

1. 概述

之前我有 一系列关于RabbitMQ文章 介绍了RabbitMQ的用法,本篇我们介绍如何在Spring Boot中集成RabbitMQ。本篇主要内容如下:

  • 在Spring boot中消息的发送和接收的两种方式的demo
  • 配置使用MessageConverter对消息序列化

2. Demo工程公共部分介绍

工程名称: rabbitmq

需要在我们的工程中的pom.xml引入新jar

 <!-- spring boot: 此版本使用的amqp-client的4.x版本,所有需要注释掉上面的amqp-client配置  -->
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-amqp</artifactId>
 </dependency>

配置application-boot.yml

spring:
  # 配置rabbitMQspring:
  rabbitmq:
    host: 10.240.80.134
    username: spring-boot
    password: spring-boot
    virtual-host: spring-boot-vhost

下面的代码都在这个工程中

3. 简单的应用demo 一

3.1. RabbitConfigure2

@Configuration
public class RabbitConfigure2 {

    // 队列名称
    public final static String SPRING_BOOT_QUEUE = "spring-boot-queue-2";
    // 交换机名称
    public final static String SPRING_BOOT_EXCHANGE = "spring-boot-exchange-2";
    // 绑定的值
    public static final String SPRING_BOOT_BIND_KEY = "spring-boot-bind-key-2";
}

3.2. 发送消息

在spring boot默认会生成AmqpAdmin和AmqpTemplate 供我们和RabbitMQ交互。 AmqpTemplate 的默认实例是RabbitTemplate,AmqpAdmin 默认实例是RabbitAdmin,通过源码发现其内部实现实际是RabbitTemplate。所以AmqpAdmin和AmqpTemplate两者本质是相同的

发送者代码: SendMsg2

@Component
public class SendMsg2 {

    // 此接口默认实现只有一个,且是RabbitAdmin,通过源码发现其内部实现实际是RabbitTemplate。所以AmqpAdmin和AmqpTemplate当前两者本质是相同的
    @Autowired
    private AmqpAdmin amqpAdmin;

    // 此接口的默认实现是RabbitTemplate,目前只有一个实现,
    @Autowired
    private AmqpTemplate amqpTemplate;

    /**
     * 发送消息
     *
     * @param msgContent
     */
    public void send_2(String msgContent) {
        amqpTemplate.convertAndSend(RabbitConfigure2.SPRING_BOOT_EXCHANGE, RabbitConfigure2.SPRING_BOOT_BIND_KEY, msgContent);
    }
}

3.3. 接收消息:

通过@RabbitListener注解要接收消息的方法,在此注解中可以定义

  • @QueueBinding:定义要本次要监听的消息绑定
  • @Queue:定义队列,如果RabbitMQ没有这个队列,则创建,如果有且配置参数相同,则忽略,否则抛出异常
  • @Exchange:定义交换机,如果RabbitMQ没有这个交换机,则创建,如果有且配置参数相同,则忽略,否则抛出异常

接收者代码: ReceiveMsg2

@Component
public class ReceiveMsg2 {

    /**
     * === 在RabbitMQ上创建queue,exchange,binding 方法二:直接在@RabbitListener声明 begin ===
     * 接收
     * @param content
     */
    @RabbitListener(containerFactory = "rabbitListenerContainerFactory",
            bindings = @QueueBinding(
            value = @Queue(value = RabbitConfigure2.SPRING_BOOT_QUEUE+"3", durable = "true", autoDelete="true"),
            exchange = @Exchange(value = RabbitConfigure2.SPRING_BOOT_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = RabbitConfigure2.SPRING_BOOT_BIND_KEY)
    )
    public void receive_2(String content) {
        // ...
        System.out.println("[ReceiveMsg-2] receive msg: " + content);
    }

}

3.4. 启动类和测试类

启动类: SpringBootRabbitApplication2
测试类: SimpleTest2

@RunWith(SpringRunner.class)
@SpringBootTest(classes= SpringBootRabbitApplication2.class, value = "spring.profiles.active=boot")
public class SimpleTest2 {

    @Autowired
    private ReceiveMsg2 receiveMsg2;

    @Autowired
    private SendMsg2 sendMsg2;

    @Test
    public void sendAndReceive_2(){
        String testContent = "send msg via spring boot -2";
        sendMsg2.send_2(testContent);
        try {
            Thread.sleep(1000 * 10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

输出结果

[ReceiveMsg-2] receive msg: send msg via spring boot -2

4. 简单的应用demo 二

4.1. RabbitConfigure1

在第一个例子中我们在@RabbitListener注解上声明队列、交换机、绑定关系,这里我们使用@Bean来声明这张对象。详细见RabbitConfigure1

@Configuration
public class RabbitConfigure1 {

    // 队列名称
    public final static String SPRING_BOOT_QUEUE = "spring-boot-queue-1";
    // 交换机名称
    public final static String SPRING_BOOT_EXCHANGE = "spring-boot-exchange-1";
    // 绑定的值
    public static final String SPRING_BOOT_BIND_KEY = "spring-boot-bind-key-1";


    // === 在RabbitMQ上创建queue,exchange,binding 方法一:通过@Bean实现 begin ===
    /**
     * 定义队列:
     * @return
     */
    @Bean
    Queue queue() {
        return new Queue(SPRING_BOOT_QUEUE, false);
    }

    /**
     * 定义交换机
     * @return
     */
    @Bean
    TopicExchange exchange() {
        return new TopicExchange(SPRING_BOOT_EXCHANGE);
    }

    /**
     * 定义绑定
     * @param queue
     * @param exchange
     * @return
     */
    @Bean
    Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(SPRING_BOOT_BIND_KEY );
    }

4.2. 消息接收类

接收类,还是使用@RabbitListener,但是只需要配置queues ,queues 除了支持完全匹配还支持正则匹配







请到「今天看啥」查看全文