Springboot 集成 RocketMq(入门)

1.RocketMq安装部署

Linux 安装 RocketMq-CSDN博客

2.添加依赖包

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

3.配置

rocketmq:
  name-server: 127.0.0.1:9876
  # 生产者
  producer:
    group: group_one
    # 消息发送超时时间
    send-message-timeout: 30000
    # 消息最大长度4M
    max-message-size: 4096
    # 消息发送失败重试次数
    retry-times-when-send-failed: 3
    # 异步消息发送失败重试次数
    retry-times-when-send-async-failed: 2
  # 消费者
  consumer:
    # 每次提取的最大消息数
    pull-batch-size: 5

4.生成者代码

@RestController
@Slf4j
public class ProducerController {

    @Resource
    private RocketMQTemplate rocketMqTemplate;
    @Resource
    private DefaultMQProducer defaultMqProducer;

    @GetMapping("/send/msg1/{messageBody}")
    public String sendMsg1(@PathVariable("messageBody") String messageBody) {
        // 发送消息
        rocketMqTemplate.convertAndSend("topic1", messageBody);
        return "OK";
    }

    @GetMapping("/send/msg2/{messageBody}")
    public String sendMsg2(@PathVariable("messageBody") String messageBody) {
        // 构建消息对象
        Message message = new Message();
        message.setTopic("topic2");
        message.setTags("boot-mq-tag");
        message.setKeys("boot-mq-key");
        message.setBody(messageBody.getBytes());
        // 发送消息,打印日志
        SendResult sendResult = null;
        try {
            sendResult = defaultMqProducer.send(message);
            log.info("sendMsg2 msgId:{},sendStatus:{}", sendResult.getMsgId(), sendResult.getSendStatus());
        } catch (Exception e) {
            log.info("sendMsg2 error", e);
            return "FAIL";
        }
        return "OK";
    }
}

5.消费者

@Component
@RocketMQMessageListener(topic = "topic1", consumerGroup = "topic1")
@Slf4j
public class MyTopic1Consumer implements RocketMQListener<String> {

    @Override
    public void onMessage(String message) {
        // 处理消息的逻辑
        log.info("Received topic1 message: {}", message);
    }

}
@Component
@RocketMQMessageListener(topic = "topic2", consumerGroup = "topic2")
@Slf4j
public class MyTopic2Consumer implements RocketMQListener<Message> {

    @Override
    public void onMessage(Message message) {
        // 处理消息的逻辑
        log.info("Received topic2 message: {}", new String(message.getBody()));
    }

}





Springboot 集成 RocketMQ(进阶-消息)-CSDN博客

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot集成RocketMQ可以通过引入RocketMQ的相关依赖和配置来实现。在启动类中,需要添加@SpringBootApplication注解,并在main方法中调用SpringApplication.run方法来启动应用。同时,还需要在启动类中添加@ComponentScan注解来指定需要扫描的包路径。\[1\]\[2\] 在集成RocketMQ的应用中,可以创建一个消息控制器类,使用@RestController注解来标识该类为控制器类。在该类中,可以使用@Autowired注解来注入RocketMQTemplate对象,用于发送消息。通过@RequestMapping注解来定义接口路径和请求方法,可以在方法中调用rocketMQTemplate.convertAndSend方法来发送消息。\[3\] 需要注意的是,在使用RocketMQ的时候,还需要配置JmsConfig.TOPIC,即主题名,确保发送消息和消费消息的主题名一致。\[3\] 总结起来,SpringBoot集成RocketMQ的步骤包括引入依赖、配置启动类、创建消息控制器类和配置主题名。 #### 引用[.reference_title] - *1* *2* [Spring Boot整合Rocketmq](https://blog.csdn.net/qq_18603599/article/details/81172866)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Spring Boot集成RocketMq(一看就会)](https://blog.csdn.net/weixin_44039105/article/details/121755023)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值