pom.xml file. Version 2.3.3 (latest) is recommended, as it simultaneously supports Spring Boot 2 and Spring Boot 3.<dependency><groupId>org.apache.rocketmq</groupId><artifactId>rocketmq-spring-boot-starter</artifactId><version>2.3.3</version><exclusions><exclusion><groupId>org.apache.rocketmq</groupId><artifactId>rocketmq-client</artifactId></exclusion><exclusion><groupId>org.apache.rocketmq</groupId><artifactId>rocketmq-acl</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.apache.rocketmq</groupId><artifactId>rocketmq-client</artifactId><version>4.9.7</version></dependency><dependency><groupId>org.apache.rocketmq</groupId><artifactId>rocketmq-acl</artifactId><version>4.9.7</version></dependency>
server:port: 8082# RocketMQ configuration informationrocketmq:# Service access address of TDMQ for RocketMQname-server: rocketmq-xxx.rocketmq.ap-bj.public.tencenttdmq.com:9876# Producer configurationsproducer:# Producer group namegroup: group111# Role tokenaccess-key: eyJrZXlJZC....# Name of the authorized rolesecret-key: admin# Common configurations for the consumerconsumer:# Role tokenaccess-key: eyJrZXlJZC....# Name of the authorized rolesecret-key: admin# Custom configurations based on business needsnamespace: rocketmq-xxx|namespace1producer1:topic: testdev1consumer1:group: group111topic: testdev1subExpression: TAG1consumer2:group: group222topic: testdev1subExpression: TAG2
Parameter | Description |
name-server | Retrieve the cluster access address from the access information module on the console cluster basic information page. ![]() |
group | Producer name. Users can self-define it or use the corresponding Topic name. |
secret-key | Role name, copied from the SecretKey column on the Role Management page in the console. |
access-key | Role key, copied from the AccessKey column on the Role Management page in the console. ![]() |
namespace | Namespace name, copied from the namespace page in the console. If you use a 5.x cluster or 4.x generic cluster, fill in the cluster ID here just. |
topic | Copy the topic name from the Topic management page in the console. |
subExpression | A parameter used to set the message tag. |
RocketMQTemplate into the class that needs to send messages.@Value("${rocketmq.namespace}%${rocketmq.producer1.topic}")private String topic; // Full topic name, which needs to be concatenated.@Autowiredprivate RocketMQTemplate rocketMQTemplate;
org.springframework.messaging.SendResult sendResult = rocketMQTemplate.syncSend(destination, message);/*------------------------------------------------------------------------*/rocketMQTemplate.syncSend(destination, MessageBuilder.withPayload(message).build())
/*** Description: Message producer*/@Servicepublic class SendMessage {// Use the full name of the topic, which can be either customized or concatenated in the format of "full namespace name%topic name".@Value("${rocketmq.namespace}%${rocketmq.producer1.topic}")private String topic;@Autowiredprivate RocketMQTemplate rocketMQTemplate;/*** Sync sending** @param message Message content* @param tags Subscribed tags*/public void syncSend(String message, String tags) {// Spring Boot does not support passing tags by using the header. You must concatenate the topic name and tags with a colon ":" as in `topicName:tags`; otherwise, the topic has no tag for identification.String destination = StringUtils.isBlank(tags) ? topic : topic + ":" + tags;SendResult sendResult = rocketMQTemplate.syncSend(destination,MessageBuilder.withPayload(message).setHeader(MessageConst.PROPERTY_KEYS, "yourKey") // Specify the business key.build());System.out.printf("syncSend1 to topic %s sendResult=%s %n", topic, sendResult);}}
@Service@RocketMQMessageListener(consumerGroup = "${rocketmq.namespace}%${rocketmq.consumer1.group}", // Consumer group in the format of "full namespace name%group name"// Use the full name of the topic, which can be either customized or concatenated in the format of "full namespace name%topic name".topic = "${rocketmq.namespace}%${rocketmq.consumer1.topic}",selectorExpression = "${rocketmq.consumer1.subExpression}" // Subscription expression. If this parameter is not configured, it means subscribing to all messages.)public class MessageConsumer implements RocketMQListener<String> {@Overridepublic void onMessage(String message) {System.out.println("Tag1Consumer receive message:" + message);}}

Feedback