Skip to content

If cluster id is null and observation is enabled, KafkaTemplate tries to retrieve it on each produce #2853

@m-burst

Description

@m-burst

In what version(s) of Spring for Apache Kafka are you seeing this issue?

3.0.11

Describe the bug

If observation is enabled on a KafkaTemplate, whenever it produces a message, it tries to retrieve the cluster ID from the Kafka cluster.
Normally the cluster ID is retrieved once and then cached, but if the cluster does not have a cluster ID configured, the cached value remains null, and the request is made again on each next produce.
This is highly inefficient, because an AdminClient is created every time for this purpose.

See KafkaTemplate#clusterId(), which in turn calls KafkaAdmin#clusterId()

To Reproduce

  1. Run a Kafka cluster without cluster ID configured
  2. KafkaTemplate.setObservationEnabled(true)
  3. Produce multiple messages
  4. Observe the application logs: an AdminClient is being created for each produce attempt, and its config is printed

Expected behavior

KafkaTemplate can distinguish between the situations where it did not yet retrieve the cluster ID and when it is not configured on the cluster.

Sample

No sample yet, but I can prepare one if needed.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions