RabbitMQ(四)

SpringBoot整合RabbitMQ

SpringBoot整合

1、生产者工程

①创建module

②配置POM

添加如下依赖:

xml 复制代码
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
</dependencies>

③YAML

yaml 复制代码
spring: 
  rabbitmq: 
    host: 192.168.xxx.xxx
    port: 5672 
    username: guest 
    password: 123456 
    virtual-host: /

④主启动类

java 复制代码
package com.xxx.mq;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @ClassName: RabbitMQProducerMainType
 * @Package: com.xxx.mq
 * @Author: 
 * @CreateDate: 
 * @Version: V1.0.0
 * @Description:
 */

@SpringBootApplication
public class RabbitMQProducerMainType {

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

}

⑤测试程序

在src目录下的test目录内新建测试类:

java 复制代码
package com.xxx.mq.test;

import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * @ClassName: RabbitMQTest
 * @Package: com.xxx.mq.test
 * @Author: 
 * @CreateDate: 
 * @Version: V1.0.0
 * @Description:
 */

@SpringBootTest
public class RabbitMQTest {

    public static final String EXCHANGE_DIRECT = "exchange.direct.order";
    public static final String ROUTING_KEY = "order";

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    public void test01SendMessage() {
        rabbitTemplate.convertAndSend(EXCHANGE_DIRECT, ROUTING_KEY, "Hello Rabbit!SpringBoot!");
    }

}

2、消费者工程

①创建module

②配置POM

添加如下依赖:

xml 复制代码
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

③YAML

增加日志打印的配置:

新建名为application的yml文件。

文件内配置:
yaml 复制代码
spring:
  rabbitmq:
    host: 192.168.xxx.xxx
    port: 5672
    username: guest
    password: 123456
    virtual-host: /
logging:
  level:
    com.xxx.mq.listener.MyMessageListener: info

将host修改为自己的地址。

④主启动类

java 复制代码
package com.xxx.mq;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @ClassName: RabbitMQConsumerMainType
 * @Package: com.xxx.mq
 * @Author: 
 * @CreateDate: 
 * @Version: V1.0.0
 * @Description:
 */

@SpringBootApplication
public class RabbitMQConsumerMainType {

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

}

⑤监听器

新建子包listener,并编写监听类:

java 复制代码
package com.xxx.mq.listener;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @ClassName: MyMessageListener
 * @Package: com.xxx.mq.listener
 * @Author: 
 * @CreateDate: 
 * @Version: V1.0.0
 * @Description:
 */

@Component
@Slf4j
public class MyMessageListener {

    public static final String EXCHANGE_DIRECT = "exchange.direct.order";
    public static final String ROUTING_KEY = "order";
    public static final String QUEUE_NAME = "queue.order";

    //    写法一:监听 + 在 RabbitMQ 服务器上创建交换机、队列
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = QUEUE_NAME, durable = "true"),
            exchange = @Exchange(value = EXCHANGE_DIRECT),
            key = {ROUTING_KEY}
        )
    )
//    写法二:监听
//    @RabbitListener(queues = {QUEUE_NAME})
    public void processMessage(String dataString, Message message, Channel channel) {
        log.info("消费端接收到了消息:" + dataString);
    }
}

3、@RabbitListener注解属性对比

①bindings属性

  • 表面作用:
    • 指定交换机和队列之间的绑定关系
    • 指定当前方法要监听的队列
  • 隐藏效果:如果RabbitMQ服务器上没有这里指定的交换机和队列,那么框架底层的代码会创建它们

②queues属性

java 复制代码
@RabbitListener(queues = {QUEUE_TEST})
  • 作用:指定当前方法要监听的队列
  • 注意:此时框架不会创建相关交换机和队列,必须提前创建好

先启动生产者端代码,此时会立即执行完成。然后执行消费者端代码,等待消息。

在生产者端module下的test内有创建好的测试代码,执行test01SendMessage测试方法,结果如图所示:

可以看到收到了生产者测试代码中的消息。前面的报错是因为我先启动了消费者端代码,此时找不到对应的交换机以及消息队列,当启动生产者端代码后就不会报错了,可以正确的接收到消息。

相关推荐
懒虫虫~5 分钟前
基于SpringBoot解决RabbitMQ消息丢失问题
spring boot·rabbitmq
java干货1 小时前
深度解析:Spring Boot 配置加载顺序、优先级与 bootstrap 上下文
前端·spring boot·bootstrap
sclibingqing2 小时前
SpringBoot项目接口集中测试方法及实现
java·spring boot·后端
KK溜了溜了4 小时前
JAVA-springboot log日志
java·spring boot·logback
我命由我123455 小时前
Spring Boot 项目集成 Redis 问题:RedisTemplate 多余空格问题
java·开发语言·spring boot·redis·后端·java-ee·intellij-idea
面朝大海,春不暖,花不开5 小时前
Spring Boot消息系统开发指南
java·spring boot·后端
hshpy5 小时前
setting up Activiti BPMN Workflow Engine with Spring Boot
数据库·spring boot·后端
jay神5 小时前
基于Springboot的宠物领养系统
java·spring boot·后端·宠物·软件设计与开发
不知几秋6 小时前
Spring Boot
java·前端·spring boot
howard20057 小时前
5.4.2 Spring Boot整合Redis
spring boot·整合redis