Skip to content

KafkaAdmin clusterId configuration is ignored when observability is enabled and bootstrap supplier is not set #3402

Closed
@varmenise

Description

@varmenise

Version: org.springframework.kafka:spring-kafka:jar:3.2.0

Describe the bug

If you enable observability and define clutserId inside the KafkaAdmin configuration, unless you specify an observability supplier, which matches the producer bootstrap server, the code will re-create a KafkaAdmin passing the defined properties as well as the producer bootstrap config.

		if (this.kafkaAdmin != null) {
			Object producerServers = this.producerFactory.getConfigurationProperties()
					.get(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG);
			String adminServers = this.kafkaAdmin.getBootstrapServers();
			if (!producerServers.equals(adminServers)) {
				Map<String, Object> props = new HashMap<>(this.kafkaAdmin.getConfigurationProperties());
				props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, producerServers);
				int opTo = this.kafkaAdmin.getOperationTimeout();
				this.kafkaAdmin = new KafkaAdmin(props);
				this.kafkaAdmin.setOperationTimeout(opTo);
			}
		}

See https://github.com/spring-projects/spring-kafka/blob/main/spring-kafka/src/main/java/org/springframework/kafka/core/KafkaTemplate.java#L493

Unfortunately if you have specified a cluster id with

kafkaAdmin.setClusterId(clusterId);

this will not be part of the property map, so the code above will basically override this configuration and clusterId will be null.

To Reproduce

  1. enable observability

  2. create a KafkaAdminConfiguration and specify bootstrap property or no property at all, just a clusterId

    @bean

    public KafkaAdmin kafkaAdmin() {
        Map<String, Object> configs = new HashMap<>();
        configs.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServer);
        KafkaAdmin kafkaAdmin = new KafkaAdmin(configs);
        kafkaAdmin.setClusterId(clusterId);
        //kafkaAdmin.setBootstrapServersSupplier(() -> bootstrapServer);
        return kafkaAdmin;
    }
  1. send an event

Expected behavior

A kafka admin with properties, bootstrap and clusterId is set whereas clusterId is null.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions