<dependency><groupId>org.apache.rocketmq</groupId><artifactId>rocketmq-spring-boot-starter</artifactId><version>2.2.2</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:8080# Producer configurationsproducer:# Producer group namegroup: group111# Role keyaccess-key: eyJrZXlJZC....# Name of the authorized rolesecret-key: admin# Common configurations for the consumerconsumer:# Role keyaccess-key: eyJrZXlJZC....# Name of the authorized rolesecret-key: admin# Custom configurationsproducer1:topic: testdev1consumer1:group: group111topic: testdev1subExpression: TAG1consumer2:group: group222topic: testdev1subExpression: TAG2
Parameter | Description |
name-server | Cluster access address in the basic information of the cluster. Select either the private network or public network access address as needed. |
group | Producer group name, which can be copied from the Group tab on the console. |
secret-key | Role name, which can be copied from SecretKey on the Cluster Permission page. |
access-key | Role key, which can be copied from accessKey on the Cluster Permission page. |
topic | Topic name, which can be copied from the Topic tab on the console. |
subExpression | Used to set the message tag. |
RcoketMQTemplate
into the class in the message that needs to be sent.@Value("${rocketmq.producer1.topic}")private String topic; // Topic name@Autowiredprivate RocketMQTemplate rocketMQTemplate;
SendResult sendResult = rocketMQTemplate.syncSend(destination, message);/*------------------------------------------------------------------------*/rocketMQTemplate.syncSend(destination, MessageBuilder.withPayload(message).build())
/*** Description: Message producer*/@Servicepublic class SendMessage {// Concatenate the topic name because the full name is required. Alternatively, you can set it by yourself. Format: topic name@Value("${rocketmq.producer1.topic}")private String topic;@Autowiredprivate RocketMQTemplate rocketMQTemplate;/*** Synchronously sending** @param message message content* @param tags subscription tags*/public void syncSend(String message, String tags) {// springboot does not support the use of headers to pass tags. Tags must be appended to the topic name as required. formats: `topicName:tags`. No concatenation indicates that there are no tags.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.consumer1.group}", // Consumer group. Format: group name// Concatenate full topic name because the full name is required. Alternatively, you can set it by yourself. Format: topic nametopic = "${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 has received the message:" + message);}}
Was this page helpful?