简体   繁体   English

Apache Kafka Producer Broker连接

[英]Apache Kafka Producer Broker Connection

I have a set of Kafka broker instances running as a cluster. 我有一组作为集群运行的Kafka代理实例。 I have a client that is producing data to Kafka: 我有一个向Kafka生成数据的客户端:

props.put("metadata.broker.list", "broker1:9092,broker2:9092,broker3:9092");

When we monitor using tcpdump, I can see that only the connections to broker1 and broker2 are ESTABLISHED while for the broker3, there is no connection from my producer. 当我们使用tcpdump监控时,我可以看到只有与broker1和broker2的连接是ESTABLISHED,而对于broker3,我的生产者没有连接。 I have a single topic with just one partition. 我只有一个分区的单个主题。

My questions: 我的问题:

  1. How is the relation between number of brokers and topic partitions? 经纪人数与主题分区之间的关系如何? Should I always have number of brokers = number of partitons? 我应该总是有经纪人数=分数吗?

  2. Why in my case, I'm not able to connect to broker3? 为什么在我的情况下,我无法连接到broker3? or atleast my network monitoring does not show that a connection from my Producer is established with broker3? 或至少我的网络监控没有显示我的生产者与broker3建立的连接?

It would be great if I could get some deeper insight into how the connection to the brokers work from a Producer stand point. 如果我能从生产者的立场深入了解与经纪人的联系是如何工作的,那将是很棒的。

Obviously, your producer does not need to connect to broker3 :) 显然,你的制作人不需要连接到broker3 :)

I'll try to explain you what happens when you are producing data to Kafka: 我将尝试向您解释当您向Kafka生成数据时会发生什么:

  1. You spin up some brokers, let's say 3, then create some topic foo with 2 partitions, replication factor 2. Quite simple example, yet could be a real case for someone. 你启动了一些经纪人,让我们说3,然后用2个分区创建一些主题foo ,复制因子2.非常简单的例子,但可能是某个人的真实案例。
  2. You create a producer with metadata.broker.list (or bootstrap.servers in new producer) configured to these brokers. 您可以使用配置到这些代理的metadata.broker.list (或新生产者中的bootstrap.servers )创建生产者。 Worth mentioning, you don't necessarily have to specify all the brokers in your cluster, in fact you can specify only 1 of them and it will still work. 值得一提的是,您不一定要指定群集中的所有代理,实际上您只能指定其中的一个代理,它仍然可以工作。 I'll explain this in a bit too. 我也会解释一下。
  3. You send a message to topic foo using your producer. 您使用生产者向主题foo发送消息。
  4. The producer looks up its local metadata cache to see what brokers are leaders for each partition of topic foo and how many partitions does your foo topic have. 生产者查找其本地元数据缓存,以查看哪些代理是主题foo每个分区的领导者,以及您的foo主题有多少分区。 As this is the first send to the producer, local cache contains nothing. 由于这是第一次发送给生产者,因此本地缓存不包含任何内容。
  5. Producer sends a TopicMetadataRequest to each broker in metadata.broker.list sequentially until first successful response. 生产者发送TopicMetadataRequest到的每个代理metadata.broker.list顺序直到第一成功响应。 That's why I mentioned 1 broker in that list would work as long as it's alive. 这就是为什么我提到该列表中的1个经纪人只要活着就会工作。
  6. Returned TopicMetadataResponse will contain the information about requested topics, in your case it's foo and brokers in the cluster. 返回的TopicMetadataResponse将包含有关所请求主题的信息,在您的情况下,它是群集中的foo和代理。 Basically, this response contains the following: 基本上,此响应包含以下内容:
    • list of brokers in the cluster, where each broker has an ID, host and port. 集群中的代理列表,其中每个代理都有ID,主机和端口。 This list may not contain the entire list of brokers in the cluster, but should contain at least the list of brokers that are responsible for servicing the subject topic. 此列表可能不包含群集中的整个代理列表,但至少应包含负责服务主题主题的代理列表。
    • list of topic metadata, where each entry has topic name, number of partitions, leader broker ID for each partition and ISR broker IDs for each partition. 主题元数据列表,其中每个条目都有主题名称,分区数,每个分区的领导者代理ID以及每个分区的ISR代理ID。
  7. Based on TopicMetadataResponse your producer builds up its local cache and now knows exactly that the request for topic foo partition 0 should go to broker X. 基于TopicMetadataResponse您的生产者构建其本地缓存,现在确切地知道主题foo分区0的请求应该转到代理X.
  8. Based on number of partitions in a topic, producer partitions your message and accumulates it with the knowledge that it should be sent as a part of batch to some broker. 根据主题中的分区数量,生产者对您的消息进行分区并累积它,并知道它应作为批处理的一部分发送给某个代理。
  9. When the batch is full or linger.ms timeout passes, your producer flushes the batch to the broker. 当批处理已满或linger.ms超时通过时,生产者将批处理刷新到代理。 By "flushes" I mean "opens a new connection to a broker or reuses an existing one, and sends the ProduceRequest ". 通过“刷新”我的意思是“打开与代理的新连接或重用现有的连接,并发送ProduceRequest ”。

The producer does not need to open unnecessary connections to all brokers, as the topic you are producing to may not be serviced by some brokers, and your cluster could be quite large. 生产者不需要打开与所有代理的不必要的连接,因为您生成的主题可能无法由某些代理提供服务,并且您的群集可能非常大。 Imagine a 1000 broker cluster with lots of topics, but one of topics has just one partition - you only need that one connection, not 1000. 想象一下1000个代理群集有很多主题,但其中一个主题只有一个分区 - 你只需要一个连接,而不是1000个。

In your particular case I'm not 100% sure why you have 2 open connections to brokers, if you have just a single partition, but I assume one connection was opened during metadata discovery and was cached for reusing, and the second one is the actual broker connection to produce data. 在您的特定情况下,我不能100%确定为什么您有两个开放连接到代理,如果您只有一个分区,但我假设在元数据发现期间打开了一个连接并缓存以便重用,第二个是实际的代理连接产生数据。 However, I might be wrong in this case. 但是,在这种情况下我可能错了。

But anyway, there is no need at all to have a connection for the third broker. 但无论如何,根本没有必要为第三个经纪人建立连接。

Regarding your question about "Should I always have number of brokers = number of partitons?" 关于你的问题“我应该总是有多少经纪人=分数?” the answer is most likely no. 答案很可能是没有。 If you explain what you are trying to achieve, maybe I'll be able to point you to the right direction, but this is too broad to explain in general. 如果你解释一下你想要实现的目标,也许我能够指出你正确的方向,但这一点太宽泛而无法解释。 I recommend reading this to clarify things. 我建议阅读此内容以澄清事情。

UPD to answer the question in comment: UPD回答评论中的问题:

Metadata cache is updated in 2 cases: 元数据缓存更新为2种情况:

  1. If producer fails to communicate with broker for any reason - this includes the case when the broker is not reachable at all and when broker responds with an error (like "I'm not leader for this partition anymore, go away") 如果生产者由于任何原因未能与代理进行通信 - 这包括代理根本无法访问且代理响应错误的情况(例如“我不再是该分区的领导者,请离开”)

  2. If no failures happen, the client still refreshes metadata every metadata.max.age.ms ( https://github.com/apache/kafka/blob/trunk/clients/src/main/java/org/apache/kafka/clients/CommonClientConfigs.java#L42-L43 ) to discover new brokers and partitions itself. 如果没有发生故障,客户端仍会刷新元数据每个metadata.max.age.mshttps://github.com/apache/kafka/blob/trunk/clients/src/main/java/org/apache/kafka/clients /CommonClientConfigs.java#L42-L43 )发现新的代理和分区本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM