手记

【金秋打卡】第14天 RabbitMQ消息中间件极速入门与实战 04

课程信息:

- 课程名称:RabbitMQ消息中间件极速入门与实战

- 章节名称:第二章 RabbitMQ整合 SpringBoot 2 生产端发送消息

- 讲师姓名:阿神


课程内容:


 1. SpringBoot与RabbitMQ集成非常简单,不需要做任何的额外配置,只需要两步即可:

  • step1:引入相关依赖

  • step2:对application.properties进行配置

2. 新建SpringBoot项目:

配置相关依赖:

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

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

   <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-amqp -->
   <!-- RabbitMQ 相关依赖 -->
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-amqp</artifactId>
      <version>2.7.2</version>
   </dependency>
   <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
   </dependency>
   <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
      <scope>provided</scope>
   </dependency>
   <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>2.0.17</version>
   </dependency>
   <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.6</version>
   </dependency>
   <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
   </dependency>
</dependencies>

编写配置文件:

spring.rabbitmq.addresses=127.0.0.1
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.virtual-host=/
spring.rabbitmq.connection-timeout=15000

# 服务相关
server.servlet.context-path=/
server.port=8001

server.servlet.encoding.charset=utf-8
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
spring.jackson.default-property-inclusion=NON_NULL

3. 建表

4. 编写实体类

这里一定要继承Serializable,是因为:Order类是通过网络传输到RabbitMQ上的。

package com.lcc.rabbitmq.entity;

import java.io.Serializable;

public class Order implements Serializable {
    
    private int id;
    private String name;
    private String messageId;  // 存储消息发送的唯一标识

    public Order() {
    }

    public Order(int id, String name, String messageId) {
        this.id = id;
        this.name = name;
        this.messageId = messageId;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMessageId() {
        return messageId;
    }

    public void setMessageId(String messageId) {
        this.messageId = messageId;
    }
}

5. 发送消息

@Component
public class OrderSender {
    @Autowired
    private RabbitTemplate rabbitTemplate; //自动装配rabbitMQ 模板

    public void sendOrder (Order order) {
        rabbitTemplate.convertAndSend(
                "order-exchange", // exchange 交换机
                "order.abcd",    // routingKey
                order,   // 消息体内容
                new CorrelationData(order.getMessageId())  // correlationData 消息唯一ID
        );
    }
}

学习心得:

通过本次学习我了解了,RabbitMQ的一些基本操作,通过与SpringBoot整合可以很简单的实现RabbitMQ的功能。

课程截图:


0人推荐
随时随地看视频
慕课网APP