Bootstrap

RocketMq发送和接收带tag的消息

本案例的Rocket是和SpringBoot整合在一起的

项目使用的springboot版本是2.1.9.RELEASE

 项目使用的spring cloud版本是Greenwich.SR1

	<dependencyManagement>
		<dependencies>
			<!--整合spring cloud-->
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Greenwich.SR1</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

1、添加依赖

		<dependency>
			<groupId>com.alibaba.cloud</groupId>
			<artifactId>spring-cloud-starter-stream-rocketmq</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.rocketmq</groupId>
			<artifactId>rocketmq-spring-boot-starter</artifactId>
			<version>2.0.3</version>
		</dependency>

 2、RocketMq发送消息

	/**
	 * 发送RocketMq消息demo,topic=say-msg,携带tag=shop
	 * @param msg
	 * @return
	 */
	@RequestMapping("rocket-mq-send-msg-with-tag/{msg}")
    public String  testRocketMQSendMsgWithTag(@PathVariable("msg") String msg){
    	this.rocketMQTemplate.convertAndSend("say-msg:shop", msg);
    	return "success";
	}

3、接收tag=shop,topic=say-msg的消息

注意:要接收多个tag,可以在selectorExpression中填写多个tag以 || 分割开来

package com.codex.terry.rocketmq;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.annotation.SelectorType;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Service;

/**
 * 文件名称: SayMsgListener.java
 * 编写人: yh.zeng
 * 编写时间: 2024/7/1 11:37
 * 文件描述: 接收RocketMq消息demo
 */
@Service
@Slf4j
@RocketMQMessageListener(consumerGroup="hello-consumer-group2", topic = "say-msg",selectorExpression = "shop || shop2",selectorType = SelectorType.TAG)
public class SayMsgWithTagListener implements RocketMQListener<String>
{
    @Override
    public void onMessage(String s) {
       log.info("收到消息tag=shop或shop2,内容为:{}", s);
    }
}

悦读

道可道,非常道;名可名,非常名。 无名,天地之始,有名,万物之母。 故常无欲,以观其妙,常有欲,以观其徼。 此两者,同出而异名,同谓之玄,玄之又玄,众妙之门。

;