【RocketMQ】同一个项目中,同一个topic,可以存在多个消费者么?
一、问题答案
是不可以的
而且后注册的会替换前注册的,MqConsumer2会替换MqConsumer,并且只结束tag-2的消息
/**
* @date 2019/05/28
*/
@Component
@Slf4j
public class MqConsumer implements MessageConsumer { @Override
@Transactional(rollbackFor = Throwable.class, propagation = Propagation.REQUIRED)
public void onMessage(String msg) {
log.info("接收到的库存MQ消息:{}", msg);
log.info("接收到的库存MQ消息:{}", msg);
log.info("接收到的库存MQ消息:{}", msg);
} @Override
public String getTopic() {
return "topic-1";
} @Override
public String getTag() {
return "tag-1";
}
}
@Component
@Slf4j
public class MqConsumer2 implements MessageConsumer { @Override
@Transactional(rollbackFor = Throwable.class, propagation = Propagation.REQUIRED)
public void onMessage(String msg) {
log.info("接收到的库存MQ消息:{}", msg);
log.info("接收到的库存MQ消息:{}", msg);
log.info("接收到的库存MQ消息:{}", msg);
} @Override
public String getTopic() {
return "topic-1";
} @Override
public String getTag() {
return "tag-2";
}
}
二、为什么呢?
我们从源码的角度来分析下
1.订阅消息的方法 public void subscribe(String topic, String subExpression, MessageListener listener) ,其中subExpression即为tag
package com.aliyun.openservices.ons.api.impl.rocketmq;
....
@Generated("ons-client")
public class ConsumerImpl extends ONSConsumerAbstract implements Consumer {
private final ConcurrentHashMap<String, MessageListener> subscribeTable = new ConcurrentHashMap<String, MessageListener>(); public ConsumerImpl(final Properties properties) {
super(properties);
boolean postSubscriptionWhenPull = Boolean.parseBoolean(properties.getProperty(PropertyKeyConst.PostSubscriptionWhenPull, "false"));
this.defaultMQPushConsumer.setPostSubscriptionWhenPull(postSubscriptionWhenPull); String messageModel = properties.getProperty(PropertyKeyConst.MessageModel, PropertyValueConst.CLUSTERING);
this.defaultMQPushConsumer.setMessageModel(MessageModel.valueOf(messageModel));
} @Override
public void start() {
this.defaultMQPushConsumer.registerMessageListener(new MessageListenerImpl());
super.start();
} @Override
public void subscribe(String topic, String subExpression, MessageListener listener) {
if (null == topic) {
throw new ONSClientException("topic is null");
} if (null == listener) {
throw new ONSClientException("listener is null");
}
this.subscribeTable.put(topic, listener);
super.subscribe(topic, subExpression);
} .....
}
从上面的类中我们可以从this.subscribeTable.put(topic, listener);看到subscribeTable这样的一个Map,该Map与tag无关
2.我们再看super.subscribe(topic, subExpression)方法,属于ONSConsumerAbstract类中
protected void subscribe(String topic, String subExpression) {
try {
this.defaultMQPushConsumer.subscribe(topic, subExpression);
} catch (MQClientException e) {
throw new ONSClientException("defaultMQPushConsumer subscribe exception", e);
}
}
DefaultMQPushConsumer中:
@Override
public void subscribe(String topic, String subExpression) throws MQClientException {
this.defaultMQPushConsumerImpl.subscribe(withNamespace(topic), subExpression);
}
DefaultMQPushConsumerImpl中:
public void subscribe(String topic, String subExpression) throws MQClientException {
try {
//此处用来构建订阅数据,并且指定了tag
SubscriptionData subscriptionData = FilterAPI.buildSubscriptionData(this.defaultMQPushConsumer.getConsumerGroup(),
topic, subExpression);
//此处将topic和该topic的订阅数据存放到subscriptionInner这个Map中
// protected final ConcurrentMap<String /* topic */, SubscriptionData> subscriptionInner = new ConcurrentHashMap<String, SubscriptionData>();
this.rebalanceImpl.getSubscriptionInner().put(topic, subscriptionData);
if (this.mQClientFactory != null) {
this.mQClientFactory.sendHeartbeatToAllBrokerWithLock();
}
} catch (Exception e) {
throw new MQClientException("subscription exception", e);
}
}
三、总结
从上面简单的源码可以看到,有用到两个Map,
subscribeTable 和 subscriptionInner ,并且Map的key都为topic。所以我们可以笃定,RocketMQ在同一个项目中,只支持注册一个topic消费者,那么也就只能指定一个tag
【RocketMQ】同一个项目中,同一个topic,可以存在多个消费者么?的更多相关文章
- django同一个项目中连接多个数据库
一.场景与思路 同一个项目中需要连接多个数据库. 二.代码 代码中主要是三个部分,settings.models以及自己写的一个类. 1.自己写的文件:database_app_router.py ...
- Swift & Objc 在同一个项目中的使用
在WWDC大会中发布了Swift让人眼前一亮.终于加了很多的现代编程语言该有的东西.很早年以前玩C#3.0+的时候这些差不多类似的 已经用的烂熟的东西终于一点一点的在看Swift Programmin ...
- 项目中同一个页面引入不同的jQuery版本的不冲突问题
在写项目的过程中,如果需要使用jQuery时,时长会遇到需要引入不同版本的jQuery,可能上一个负责该项目的人用到的是老版本的jQuery,而你去添加功能时用的是新版本的,这个问题很难避免掉,如果去 ...
- 在同一个项目中灵活运用application/json 和application/x-www-form-urlencoded 两种传输格式(配合axios,同时配置loading)
'use strict' import axios from 'axios' // import qs from 'qs' import { Notification} from 'element-u ...
- 项目中同一个dll的x86和x64同时引用
<ItemGroup Condition=" '$(Platform)' == 'x86' "> <Reference Include="System. ...
- IntelliJ IDEA导入多个eclipse项目到同一个workspace下
IntelliJ IDEA 与eclipse在新建项目上工作区的叫法略有不同,区别见下图. 我们在eclipse都是在新建的workspace目录下新建我们的项目,但是在IDEA中没有workspac ...
- 如何在 ETL 项目中统一管理上百个 SSIS 包的日志和包配置框架
一直准备写这么一篇有关 SSIS 日志系统的文章,但是发现很难一次写的很完整.因为这篇文章的内容可扩展的性太强,每多扩展一部分就意味着需要更多代码,示例和理论支撑.因此,我选择我觉得比较通用的 LOG ...
- MVC3中在同一解决方案的不同项目中实现Area功能
1.背景 微软在MVC中引入了Area概念,用于复杂项目的分工开发.如一个MVC项目中Controller过多时,就会导致项目中包含大量的Controller+View+Model,无论是查 ...
- class类名在webpack项目中的两种引用方式
一.问题描述 在项目工程中,我们通常既用到css module,也用到普通的less文件引用方式,代码及webpack配置如下,运行时,发现只有css module起作用,如何让两者都起作用呢? // ...
随机推荐
- 【GMT43智能液晶模块】例程十五:LAN_TCPC实验——以太网数据传输
源代码下载链接: 链接:https://pan.baidu.com/s/1bFX8_UpUlML29oqoDGaw5g提取码:mrf5 复制这段内容后打开百度网盘手机App,操作更方便哦 GMT43购 ...
- LeetCode 101. Symmetric Tree(镜像树)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- maven多模块和继承
https://blog.csdn.net/mafan121/article/details/50477852 1.maven 打包Could not resolve dependencies for ...
- PHP urlencode空格被转为+的问题
我想既然各位点进来,绝大数是因为你遇到了空格被转为+号所带来的坑 不过没关系,解决方案很简单:使用rawurlencode()来进行编码即可~此函数遵循RFC 3986标准,空格会被转为%20 同时, ...
- centos7安装配置MariaDB10
1:添加 MariaDB yum 仓库 vi /etc/yum.repos.d/MariaDB.repo在该文件中添加以下内容保存: [mariadb] name = MariaDB baseurl ...
- 使用Docker Compose搭建Service Mesh
使用Docker Compose搭建Service Mesh 本文将介绍如何使用Docker Compose搭建Istio.Istio号称支持多种平台(不仅仅Kubernetes).然而,官网上非基于 ...
- Java课堂笔记1
1. Java严格区分大小写 2. 一个源文件public主类名必须和文件名完全一致 3. 命名规则严格要求,字母.数字.下划线.美元符号$.下划线_组成,其中不能以数字开头,也不能使用Java的 ...
- imposm模块安装
imposm安装 pip install imposm 报错,参考:https://imposm.org/docs/imposm/2.5.0/install.html 并安装依赖: sudo ap-g ...
- [转帖]使用 Vagrant 打造跨平台开发环境
使用 Vagrant 打造跨平台开发环境 https://segmentfault.com/a/1190000000264347 Vagrant 是一款用来构建虚拟开发环境的工具,非常适合 php/p ...
- 手撕面试官系列(五):Tomcat+Mysql+设计模式面试专题
Tomcat (面试题+答案领取方式见侧边栏) Tomcat 的缺省端口是多少,怎么修改? tomcat 有哪几种 Connector 运行模式(优化)? Tomcat 有几种部署方式? tomcat ...