<dependency><groupId>org.apache.rocketmq</groupId><artifactId>rocketmq-spring-boot-starter</artifactId><version>2.3.3</version></dependency>
server:port: 8082#TDMQ for RocketMQ configuration information.rocketmq:# TDMQ for RocketMQ service access address.name-server: rocketmq-xxx.rocketmq.ap-bj.public.tencenttdmq.com:9876// Producer configuration.producer:# Producer group name.group: group111# Role token.access-key: eyJrZXlJZC....# Authorized role name.secret-key: admin# Consumer common configuration.consumer:# Role token.access-key: eyJrZXlJZC....# Authorized role name.secret-key: admin# Custom configuration, which is configured based on the business.namespace: rocketmq-xxx|namespace1producer1:topic: testdev1consumer1:group: group111topic: testdev1subExpression: TAG1consumer2:group: group222topic: testdev1subExpression: TAG2
Parameter | Description |
name-server | Cluster access address. You can obtain the access address from the Access Information module on the Cluster Basic Information page in the console. |
group | Producer name, which is user-defined and can be set to the topic name. |
secret-key | Role name. You can copy the role name from the SecretKey column on the Cluster Permissions page in the console. |
access-key | Role token. You can copy the token from the AccessKey column on the Cluster Permissions page in the console. |
namespace | Namespace name. You can copy the name from the Namespace page in the console. If you are using a 5.x cluster or 4.x general cluster, enter the cluster ID here. |
topic | Topic name. You can copy the name from the Topic Management page in the console. |
subExpression | Tags associated with the message. |
RocketMQTemplate in the class that requires message sending.@Value("${rocketmq.namespace}%${rocketmq.producer1.topic}")private String topic; // Topic name. (The full topic name is required. Therefore, concatenate the topic name here.)@Autowiredprivate RocketMQTemplate rocketMQTemplate;
SendResult sendResult = rocketMQTemplate.syncSend(destination, message);/*------------------------------------------------------------------------*/rocketMQTemplate.syncSend(destination, MessageBuilder.withPayload(message).build())
/*** Description: Message producer.*/@Servicepublic class SendMessage {// The full topic name is required. Therefore, concatenate the topic name. You can also set the format yourself: full namespace name%topic name.@Value("${rocketmq.namespace}%${rocketmq.producer1.topic}")private String topic;@Autowiredprivate RocketMQTemplate rocketMQTemplate;/*** Synchronous sending.** @param message Message content.* @param tags Tags subscribed to.*/public void syncSend(String message, String tags) {// Spring Boot does not support passing tags using headers. As required, it is necessary to concatenate tags after the topic name in the format of `topicName:tags`. If no concatenation identifier is available, no tag is involved.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}", // Specify the consumer group in the format of full namespace name%group name.// The full topic name is required. Therefore, concatenate the topic name. You can also set the format yourself: full namespace name%topic name.topic = "${rocketmq.namespace}%${rocketmq.consumer1.topic}",selectorExpression = "${rocketmq.consumer1.subExpression}" // Subscription expression. If it is not configured, all messages are subscribed to.)public class MessageConsumer implements RocketMQListener<String> {@Overridepublic void onMessage(String message) {System.out.println("Tag1Consumer receive message:" + message);}}
Feedback