SpringBoot整合RocketMQ实现消息发送和接收的详细步
我们使⽤主流的SpringBoot框架整合RocketMQ来讲解,使⽤⽅便快捷;
最终项⽬结构如下:
具体步骤如下:
第⼀步:新建SpringBoot项⽬rocketmq-test,引⼊rocketmq依赖,以及项⽬配置
<dependency>
<groupId>ketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
完整l
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /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>ketmq</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>
spring framework是什么框架的</plugins>
</build>
</project>
另外我们项⽬配置⽂件需要指定nameserver地址,以及消息⽣产者和消息消费者组;
所以配置l:
rocketmq:
name-server: 127.0.0.1:9876
producer:
group: producer-demo1
consumer:
group: consumer-demo1
第⼆步:新建消息⽣产者Service类ProducerService
package ketmq;
import RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* 消息⽣产者
* @author java1234_⼩锋
* @site www.java1234
* @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++){
}
}
}
SpringBoot给我们提供了RocketMQTemplate模板类,我们利⽤这个类可以以多种形式发送消息;另外这个类我们要加下@Component注解,让Spring来管理实例,⽅便其他地⽅获取bean来使⽤;发送⽅法指定Topic主题java1234-rocketmq;
第三步:启动类获取ProducerService实例,调⽤发送消息⽅法
package com.java1234;
import ketmq.ProducerService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import t.ConfigurableApplicationContext;
@SpringBootApplication
public class RocketmqTestApplication {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(RocketmqTestApplication.class, args);
ProducerService producerService = (ProducerService) Bean("producerService");
producerService.sendMessage();
}
}
我们获取ProducerService实例,调⽤sendMessage⽅法发送消息;
第四步:新建消息消费者Service类ConsumerService,监听消息,消费消息
package ketmq;
import ketmq.spring.annotation.RocketMQMessageListener;
import RocketMQListener;
import org.springframework.stereotype.Component;
/**
* 消息消费者
* @author java1234_⼩锋
* @site www.java1234
* @company 南通⼩锋⽹络科技有限公司
* @create 2021-08-22 22:40
*/
@RocketMQMessageListener(topic = "java1234-rocketmq",consumerGroup ="${up}" )
@Component
public class ConsumerService implements RocketMQListener<String> {
@Override
public void onMessage(String s) {
System.out.println("收到消息内容:"+s);
}
}
消费者类要实现RocketMQListener接⼝,以及动态指定消息类型String。
类上要加上**@RocketMQMessageListener**注解,指定topic主题java1234-rocketmq,以及消费者组${up}
同样这个类上也要加上@Component注解,让Spring来管理bean实例;
第五步:再次启动,测试消息消费
测试OK,成功消费!
到此这篇关于SpringBoot整合RocketMQ实现消息发送和接收的⽂章就介绍到这了,更多相关SpringBoot整合RocketMQ消息内
容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。