SpringBoot整合RocketMQ實現消息發送和接收的詳細步驟

我們使用主流的SpringBoot框架整合RocketMQ來講解,使用方便快捷;

最終項目結構如下:

在這裡插入圖片描述

具體步驟如下:

第一步:新建SpringBoot項目rocketmq-test,引入rocketmq依賴,以及項目配置

<dependency>
	<groupId>org.apache.rocketmq</groupId>
	<artifactId>rocketmq-spring-boot-starter</artifactId>
	<version>2.2.0</version>
</dependency>

完整pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.java1234</groupId>
    <artifactId>rocketmq-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>rocketmq-test</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.rocketmq</groupId>
            <artifactId>rocketmq-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>
        
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

另外我們項目配置文件需要指定nameserver地址,以及消息生產者和消息消費者組;

所以配置application.yml

rocketmq:
  name-server: 127.0.0.1:9876
  producer:
    group: producer-demo1
  consumer:
    group: consumer-demo1

第二步:新建消息生產者Service類ProducerService

package com.java1234.rocketmq;

import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 消息生產者
 * @author java1234_小鋒
 * @site www.java1234.com
 * @company 南通小鋒網絡科技有限公司
 * @create 2021-08-22 22:16
 */
@Component("producerService")
public class ProducerService {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 發送簡單消息
     */
    public void sendMessage(){
        for(int i=0;i<1000;i++){
            rocketMQTemplate.convertAndSend("java1234-rocketmq","rocketmq大爺,你好!"+i);
        }
    }
}

SpringBoot給我們提供瞭RocketMQTemplate模板類,我們利用這個類可以以多種形式發送消息;

另外這個類我們要加下@Component註解,讓Spring來管理實例,方便其他地方獲取bean來使用;

發送方法指定Topic主題java1234-rocketmq

第三步:啟動類獲取ProducerService實例,調用發送消息方法

package com.java1234;

import com.java1234.rocketmq.ProducerService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class RocketmqTestApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(RocketmqTestApplication.class, args);
        ProducerService producerService = (ProducerService) run.getBean("producerService");
        producerService.sendMessage();
    }

}

我們獲取ProducerService實例,調用sendMessage方法發送消息;

第四步:新建消息消費者Service類ConsumerService,監聽消息,消費消息

package com.java1234.rocketmq;

import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

/**
 * 消息消費者
 * @author java1234_小鋒
 * @site www.java1234.com
 * @company 南通小鋒網絡科技有限公司
 * @create 2021-08-22 22:40
 */
@RocketMQMessageListener(topic = "java1234-rocketmq",consumerGroup ="${rocketmq.consumer.group}" )
@Component
public class ConsumerService implements RocketMQListener<String> {

    @Override
    public void onMessage(String s) {
        System.out.println("收到消息內容:"+s);
    }
}

消費者類要實現RocketMQListener接口,以及動態指定消息類型String。

類上要加上**@RocketMQMessageListener**註解,指定topic主題java1234-rocketmq,以及消費者組${rocketmq.consumer.group}

同樣這個類上也要加上@Component註解,讓Spring來管理bean實例;

第五步:再次啟動,測試消息消費

在這裡插入圖片描述

測試OK,成功消費!

到此這篇關於SpringBoot整合RocketMQ實現消息發送和接收的文章就介紹到這瞭,更多相關SpringBoot整合RocketMQ消息內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: