RabbitMQ入门:在Spring Boot 应用中整合RabbitMQ

Dear 丶 2023-02-13 11:41 58阅读 0赞

在上一篇随笔中我们认识并安装了RabbitMQ,接下来我们来看下怎么在Spring Boot 应用中整合RabbitMQ。

先给出最终目录结构:

format_png

搭建步骤如下:

  1. 新建maven工程amqp
  2. 修改pom文件,引入spring-boot-starter-amqp和spring-boot-starter-test

    复制代码

    1. <project xmlns="http://maven.apache.org/POM/4.0.0"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0</modelVersion>
    5. <groupId>com.sam</groupId>
    6. <artifactId>amqp</artifactId>
    7. <version>0.0.1-SNAPSHOT</version>
    8. <parent>
    9. <groupId>org.springframework.boot</groupId>
    10. <artifactId>spring-boot-starter-parent</artifactId>
    11. <version>1.5.1.RELEASE</version>
    12. </parent>
    13. <properties>
    14. <javaVersion>1.8</javaVersion>
    15. </properties>
    16. <dependencies>
    17. <!-- 引入amqp依赖,它能很好的支持RabbitMQ -->
    18. <dependency>
    19. <groupId>org.springframework.boot</groupId>
    20. <artifactId>spring-boot-starter-amqp</artifactId>
    21. </dependency>
    22. <!-- 引入test依赖,这次需要用到JUnit -->
    23. <dependency>
    24. <groupId>org.springframework.boot</groupId>
    25. <artifactId>spring-boot-starter-test</artifactId>
    26. </dependency>
    27. </dependencies>
    28. </project>

    复制代码

  3. 新建application.properties配置文件,主要就是配置下连接RabbitMQ的信息:

    1. spring.application.name=rabbitmq-hello
    2. #config rabbitmq info
    3. spring.rabbitmq.host=localhost
    4. spring.rabbitmq.port=5672
    5. spring.rabbitmq.username=guest
    6. spring.rabbitmq.password=guest
  4. 新建启动类,这里没什么特殊的,就是普通的spring boot启动类

    复制代码

    1. /**
    2. * 这里没什么特殊的地方,就是普通的spring boot 配置
    3. *
    4. */
    5. @SpringBootApplication
    6. public class RabbitMQApp {
    7. public static void main(String[] args) {
    8. SpringApplication.run(RabbitMQApp.class, args);
    9. }
    10. }

    复制代码

  5. 创建生产者类,通过AmqpTemplate实现消息的发送,AmqpTemplate接口定义了一套针对AMQP协议的基础操作。在Spring Boot中会根据配置来注入具体的实现。这里我们会产生一个字符串,并发送到名为hello的队列中。

    复制代码

    1. @Component
    2. public class Sender {
    3. @Autowired
    4. AmqpTemplate rabbitmqTemplate;
    5. /**
    6. * 发送消息
    7. */
    8. public void send() {
    9. String content = "Sender says:" + "'hello, I'm sender'";
    10. System.out.println(content);
    11. rabbitmqTemplate.convertAndSend("hello", content);
    12. }
    13. }

    复制代码

  6. 创建消费者类,需要用到@RabbitListener来定义对hello队列的监听,并用@RabbitHandler注解来指定对消息处理的方法。我们这里实现了对hello队列的消费。

    复制代码

    1. /**
    2. * 通过@RabbitListener对hello队列进行监听
    3. *
    4. */
    5. @Component
    6. @RabbitListener(queues="hello")
    7. public class Receiver {
    8. /**
    9. * 通过@RabbitHandler声明的方法,对hello队列中的消息进行处理
    10. */
    11. @RabbitHandler
    12. public void receiver(String str) {
    13. System.out.println("Receiver says:[" + str + "]");
    14. }
    15. }

    复制代码

  7. 编写RabbitMQ的配置类,配置类可以配置队列、交换器、路由等高级信息。我们这里为了简单,只配置队列,其他的采用默认配置。

    复制代码

    1. /**
    2. * rabbitmq配置类,
    3. * 为了简单,我们这里只配置了Queue
    4. * 至于exchanges、brokers等用的默认配置
    5. *
    6. */
    7. @Configuration
    8. public class RabbitConfig {
    9. @Bean
    10. public Queue helloQueue() {
    11. return new Queue("hello");
    12. }
  1. }
  2. ![复制代码][aHR0cHM6Ly9jb21tb24uY25ibG9ncy5jb20vaW1hZ2VzL2NvcHljb2RlLmdpZg]
  1. 编写测试类,用来调用消息生产者

    复制代码

    1. @RunWith(SpringJUnit4ClassRunner.class)
    2. @SpringBootTest(classes=RabbitMQApp.class)
    3. public class HelloTest {
    4. @Autowired
    5. private Sender sender;
    6. /**
    7. * 调用生产者进行消息发送
    8. */
    9. @Test
    10. public void hello() throws Exception{
    11. sender.send();
    12. }
    13. }

    复制代码

  2. 运行启动类,启动后控制台会有下面的提示内容:format_png 1
  1. 执行测试类,在测试类的控制台会打印我们打的log内容

  format_png 2

  

  切换到amqp应用的控制台,能看到打印:

  format_png 3

  

  在管理页面中我们能看到Connections和Channels中包含了当前连接的条目:format_png 4

在整个生产和消费的过程中,生产和消费是一个异步操作,这是分布式系统中要使用消息代理的重要原因。

https://www.cnblogs.com/sam-uncle/p/9051231.html

发表评论

表情:
评论列表 (有 0 条评论,58人围观)

还没有评论,来说两句吧...

相关阅读