/rocketmq-spring-boot-starter

Spring Boot starter for RocketMQ

Primary LanguageJavaGNU General Public License v3.0GPL-3.0

spring boot starter for RocketMQ Build Status Coverage Status

Maven CentralGitHub release

1. 添加maven依赖:

<dependency>
    <groupId>com.maihaoche</groupId>
    <artifactId>spring-boot-starter-rocketmq</artifactId>
    <version>0.0.5</version>
</dependency>

2. 添加配置:

rocketmq:
  name-server-address: 172.21.10.111:9876
  # 可选, 如果无需发送消息则忽略该配置
  producer-group: local_pufang_producer
  # 发送超时配置毫秒数, 可选, 默认3000
  send-msg-timeout: 5000
  # 追溯消息具体消费情况的开关
  #trace-enabled: true

3. 入口添加注解开启自动装配

在springboot应用主入口添加@EnableMQConfiguration注解开启自动装配:

@SpringBootApplication
@EnableMQConfiguration
class CamaroDemoApplication {
}

4. 定义消息体

可以通过@MQKey注解将消息POJO中的对应字段设置为消息key,通过prefix定义key的前缀:

data class DemoMessage(
        @MQKey(prefix = "sku_")
        val skuId:Long,
        val skuType:String)

5. 创建发送方

详见wiki

@MQProducer
class DemoProducer : AbstractMQProducer() {
}

6. 创建消费方

详见wiki

@MQConsumer(consumerGroup = "local_pufang_test_consumer", topic = "suclogger")
class DemoConsumer : AbstractMQPushConsumer<DemoMessage>() {
    override fun process(message: DemoMessage?, extMap: MutableMap<String, Any>?): Boolean {
        // extMap 中包含messageExt中的属性和message.properties中的属性
        println("message id : ${extMap!![MessageExtConst.PROPERTY_EXT_MSG_ID]}")
        return true
    }

}

7. 发送消息:


// 注入发送者
@Autowired
lateinit var demoProducer:DemoProducer
    
...
    
// 发送
demoProducer.syncSend("suclogger", DemoMessage(1, "plain_message"))