HM-RocketMQ2.2【SpringBoot整合RocketMQ】

1 前置条件

相关依赖包:下载rocketmq-spring项目

将rocketmq-spring安装到本地仓库

mvn install -Dmaven.skip.test=true

image-20221108105844657

image-20221108105917387

image-20221108142938910

image-20221108143252047

2 消息生产者

创建空项目shop-project

在空项目shop-project下创建空的maven模块springboot-rocketmq-producer

2.1 添加依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
</parent>

<properties>
    <rocketmq-spring-boot-starter-version>2.0.3</rocketmq-spring-boot-starter-version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.apache.rocketmq</groupId>
        <artifactId>rocketmq-spring-boot-starter</artifactId>
        <version>${rocketmq-spring-boot-starter-version}</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.6</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

</dependencies>

2.2 配置文件

# application.properties
rocketmq.name-server=192.168.115.15:9876;192.168.115.16:9876
rocketmq.producer.group=my-group

2.3 启动类

package com.irun2u;

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

/**
 * @Author: haifei
 * @Date: 2022/11/8 14:53
 */
@SpringBootApplication
public class MQProducerApplication {
    public static void main(String[] args) {
        SpringApplication.run(MQProducerApplication.class);
    }
}

2.4 测试类

package com.irun2u;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @Author: haifei
 * @Date: 2022/11/8 14:57
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {MQProducerApplication.class})
@Slf4j
public class ProducerTest {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Test
    public void testSendMsg(){
        rocketMQTemplate.convertAndSend("springboot-rocketmq", "hello springboot and rocketmq");
        //主题、消息
        log.info("消息发送成功");
    }
}


参考HM-RocketMQ1.3【RocketMQ集群搭建】 - yub4by - 博客园 (cnblogs.com)启动rocketmq-console集群监控平台

运行上述测试方法

image-20221108152352464

查看平台中信息

image-20221108152550527

image-20221108152605890

3 消息消费者

在空项目shop-project下创建空的maven模块springboot-rocketmq-consumer

3.1 添加依赖

同上

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

3.2 配置文件

同上

rocketmq.consumer.group=my-group

3.3 启动类

package com.irun2u;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @Author: haifei
 * @Date: 2022/11/8 16:24
 */
@Slf4j
@SpringBootApplication
public class MQConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(MQConsumerApplication.class);
        log.info("消费者启动成功");
    }
}

3.4 消息监听器

package com.irun2u.listener;

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

/**
 * @Author: haifei
 * @Date: 2022/11/8 16:31
 */
@Component
@RocketMQMessageListener(
        topic = "springboot-rocketmq",
        //consumeMode = ConsumeMode.ORDERLY, //广播CONCURRENTLY;负载均衡ORDERLY(默认)
        consumerGroup = "${rocketmq.consumer.group}"
)
public class Consumer implements RocketMQListener<String> {
    @Override
    public void onMessage(String s) {
        System.out.println("接收到消息:" + s);
    }
}


image-20221108163906258

image-20221108164053283

image-20221108164245170

posted @ 2022-11-08 16:47  yub4by  阅读(19)  评论(0编辑  收藏  举报