关于kafka Consumer参数:exclude.internal.topics

之前对这个参数一直理解有误,彻底搞清楚后就写个笔记记录一下。(基于kafka1.0)

先看官方文档的解释:Whether records from internal topics (such as offsets) should be exposed to the consumer. If set to true the only way to receive records from an internal topic is subscribing to it.

大意是:这个参数用于是否把内部topic的信息(例如offset)暴露给cosumer,如果设置为true,就只能通过订阅的方式来获取内部topic的数据。

也正是官网的这个解释让我有了错误理解,从参数的字面意思来看是“排除内部topic”,所以一直以为是设置为true就是排除内部topic,设置为false就是暴露内部topic,但实际测试却有不是这样的,于是看了相关的源码:

1.  //ConsumerCoordinator.java
2.  public void updatePatternSubscription(Cluster cluster) {
3.      final Set<String> topicsToSubscribe = new HashSet<>();
4.      //主要是下面这一句
5.      for (String topic : cluster.topics())
6.          if (subscriptions.subscribedPattern().matcher(topic).matches() &&
7.                  !(excludeInternalTopics && cluster.internalTopics().contains(topic)))
8.              topicsToSubscribe.add(topic);
9.  
10.     subscriptions.subscribeFromPattern(topicsToSubscribe);
11. 
    // note we still need to update the topics contained in the metadata. Although we have
    // specified that all topics should be fetched, only those set explicitly will be retained
        metadata.setTopics(subscriptions.groupSubscription());
}

从第6行看,原来还可以用正则的方式来订阅topic (孤陋寡闻了,从来就这么用过),加上第7行的两个条件,就明白了这个参数的意图。

__consumer_offsets是kafka server中一个默认的topic名,当用户有一个topic叫sumer_off,且用正则的方式来订阅topic,如下所示:

Pattern pattern = Pattern.compile(".*umer_of.*");
consumer.subscribe(pattern);

那么正则会匹配到__consumer_offsets这个内部topic,所以就用这个参数来限制是否暴露内部topic,当设置为true是,就算正则匹配中了内部topic,也不会消息,此时只能通过订阅的方式来消费内部topic,当这个参数设置为false时,如果正则匹配到内部topic,就会消费到内部topic的数据。

个人认识官方文档应该提及这个参数与正则订阅方式的联系,否则单从文档上很难看懂这个参数的意思。

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值