SpringBoot 2.7教程:SpringBoot + Web 项目搭建及实践应用-2022年最新图文版本
SpringBoot 2.7教程:SpringBoot + Web 项目搭建,异常捕获处理-2022年最新图文版本
SpringBoot 2.7教程:SpringBoot + Mysql 项目应用-2022年最新图文版本
SpringBoot 2.7教程:SpringBoot 集成 jsp 页面开发 -2022年最新图文版本
SpringBoot 2.7教程:SpringBoot 实现文件上传,图片上传并显示功能-2022年最新图文版本
SpringBoot 2.7教程:springboot 设置全局字符编码,解决乱码问题-2022年最新图文版
SpringBoot 2.7教程:SpringBoot mybatis 多数据源的整合方法-2022年最新图文版本
SpringBoot 2.7教程:SpringBoot 整合 RocketMQ 项目搭建-2022年最新图文版本
SpringBoot 2.7教程:SpringBoot 整合 RabbitMQ 项目搭建-2022年最新图文版本
SpringBoot 2.7教程:SpringBoot 整合 MongoDB 项目搭建-2022年最新图文版本
SpringBoot 2.7教程:SpringBoot 整合 Redisson 项目搭建-2022年最新图文版本
SpringBoot 2.7教程:SpringBoot 整合 Redis 项目搭建-2022年最新图文版本
目录
一、新建项目rocketmq-sample,项目结构如下
二、配置pom文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>4.9.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
三、配置application.yml文件
#Rocketmq Config
rocketmq:
producer:
#简单的消息发送配置
sample:
name: sampleProducer
groupName: ProducerGroup
namesrvAddr: ip:9876
instanceName: ProducerGroup
topic: topic2022
tag: test
maxMessageSize: 131072
sendMsgTimeout: 10000
consumer:
#简单的消息消费配置
sample:
isOnOff: on
# 发送同一类消息设置为同一个group,保证唯一默认不需要设置,rocketmq会使用ip@pid(pid代表jvm名字)作为唯一标识
groupName: sampleGroup
# mq的nameserver地址
namesrvAddr: ip:9876
# 消费者订阅的主题topic和tags(*标识订阅该主题下所有的tags),格式: topic~tag1||tag2||tags3;
topic: topic2022
tag: "*"
# 消费者线程数据量
consumeThreadMin: 5
consumeThreadMax: 32
# 设置一次消费信息的条数,默认1
consumeMessageBatchMaxSize: 1
四、配置消费消息类
/***
* @date: 2022/7/18
* @author: 微信公众号:深入技术架构
* @description: TODO
*/
@Slf4j
@Data
@Configuration
@ConfigurationProperties(prefix = "rocketmq.consumer.sample")
public class SampleConsumerConfig {
/**
* 消费者分组名称
*/
private String groupName;
/**
* 链接地址
*/
private String namesrvAddr;
/**
* 单个topic
*/
private String topic;
/**
* 单个tag
*/
private String tag;
/**
* 消费者最小线程数
*/
private Integer consumeThreadMin;
/**
* 消费者最大线程数
*/
private Integer consumeThreadMax;
/**
* 每一批次拉取的最大数量
*/
private Integer consumeMessageBatchMaxSize;
/**
* mq 消费者配置
*
* @return
* @throws MQClientException
*/
// @Bean(initMethod = "start", destroyMethod = "shutdown")
@Bean( destroyMethod = "shutdown")
public DefaultMQPushConsumer sampleConsumerGroup() throws MQClientException {
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(groupName);
consumer.setInstanceName(groupName);
consumer.setNamesrvAddr(namesrvAddr);
consumer.setConsumeThreadMin(consumeThreadMin);
consumer.setConsumeThreadMax(consumeThreadMax);
consumer.setConsumeMessageBatchMaxSize(consumeMessageBatchMaxSize);
consumer.setAllocateMessageQueueStrategy(new AllocateMessageQueueAveragely());
/**
* 设置consumer第一次启动是从队列头部开始还是队列尾部开始
* 如果不是第一次启动,那么按照上次消费的位置继续消费
*/
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET);
/**
* 设置消费模型,集群还是广播,默认为集群
*/
consumer.setMessageModel(MessageModel.CLUSTERING);
// 设置监听类,只能设置一个
consumer.registerMessageListener(new ConsumerListener());
// 设置订阅的topic 和 tag
consumer.subscribe(topic, tag);
consumer.start();
log.info("================>消费者配置完成,ConsumerGroupName:{}, Topics:{}, namesrvAddr:{}", groupName, topic, namesrvAddr);
return consumer;
}
}
/***
* @date: 2022/7/18
* @author:微信公众号:深入技术架构
* @description: TODO
*/
@Slf4j
@Component
public class ConsumerListener implements MessageListenerConcurrently {
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> extList, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
MessageExt ext = extList.get(0);
try {
//处理消息业务 收到的body(消息体),字节类型,需转为String
String result = new String(ext.getBody());
log.info("监听到了消息,Msgid" + ext.getMsgId() + ",topic为:" + ext.getTopic() + ",tags:" + ext.getTags() + ",keys" + ext.getKeys() + "消息内容为:" + result);
// Thread.sleep(20);
} catch (Exception e) {
e.printStackTrace();
//处理失败返回状态
return ConsumeConcurrentlyStatus.RECONSUME_LATER;
}
//如果处理成功返回
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
}
五、配置生产消息类
/**
* @description 微信公众号:深入技术架构
* @className RocketMQProducer
*/
@Slf4j
@Data
@Configuration
@ConfigurationProperties(prefix = "rocketmq.producer.sample")
public class SampleProducerConfig {
private String groupName;
private String namesrvAddr;
private String instanceName;
private int maxMessageSize;
private int sendMsgTimeout;
@Bean(destroyMethod = "shutdown")
public DefaultMQProducer sampleProducer() throws MQClientException {
DefaultMQProducer producer = new DefaultMQProducer(groupName);
producer.setNamesrvAddr(namesrvAddr);
producer.setInstanceName(instanceName);
producer.setMaxMessageSize(maxMessageSize);
producer.setSendMsgTimeout(sendMsgTimeout);
producer.setVipChannelEnabled(false);
producer.start();
log.info("================>生产者配置完成,ProducerGroupName{}<================", groupName);
return producer;
}
}
六、controller
@Slf4j
@RestController
public class SendController {
@Resource
DefaultMQProducer sampleProducer;
@RequestMapping("sendMsg")
public String sendMsg(@PathParam("topic") String topic){
if(StringUtils.isBlank(topic)){
return "param topic is must not null !";
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String body = "hi, 普通消息, 时间::" + sdf.format(new Date());
MsgEntity msgEntity = MsgEntity.builder().userId(111).orderId(222).build();
Message message = new Message(topic, "test", "keys", body.getBytes());
SendResult sendResult = null;
try {
//同步发送消息
sendResult = sampleProducer.send(message);
log.info(sendResult.toString());
} catch (Exception e) {
e.printStackTrace();
}
return "sendMsg succ";
}
}