Skip to main content

Support for Multiple Kafka Clusters

Introduction​

jEAP Messaging supports connecting to more than one Kafka cluster since version 6. This feature is primarily intended to support hybrid cloud use cases. See jEAP Messaging Library — Configuration for a reference of all available configuration properties and more code examples.

Also, the jme-messaging-examples repository contains a multi-cluster example, jme-messaging-multicluster-service — the module path referenced in the original documentation could not be verified against the current jme-messaging-example repository on GitHub (see migration notes in the summary).

Example​

For example, a Spring Boot service might connect to two different Kafka clusters and schema registries:

PlantUML diagram (waiting to render)

For the configuration, the bean-injection pattern per cluster and the Lombok caveat, see jeap-messaging — Configuration reference: Clusters.

Spring Kafka Bean Autoconfiguration in jeap-messaging​

When the dependency jeap-messaging-infrastructure is on the classpath, it will activate the Spring autoconfiguration support for jEAP Messaging.

  1. The autoconfiguration class KafkaConfiguration in jeap-messaging will activate the import bean registrar JeapKafkaBeanRegistrar.
  2. JeapKafkaBeanRegistrar iterates over each cluster configured under jeap.messaging.kafka.cluster.<clustername>.
  3. If a cluster is marked with default-cluster=true, this will be the default cluster. Otherwise, the first cluster in the YAML configuration will be the designated default cluster.
  4. For each configured cluster, beans will be registered:
    1. Default cluster: bean names will not be prefixed (for example, kafkaTemplate). The registered beans will be marked as @Primary. Spring will prefer primary beans at injection points without a qualifier.
      • The following beans are marked @Primary for a cluster other than the default cluster if default-producer-cluster-override=true (supported from jEAP Messaging 7.10.0) is set for a specific cluster:
        1. KafkaTemplate
        2. KafkaProducerFactory
        3. SenderOptions
        4. ReactiveKafkaProducerTemplate
        5. TransactionalOutbox (+ internal outbox beans)
    2. Other clusters: bean names will be prefixed with the cluster name (for example, awsKafkaTemplate). The registered beans will be marked as @Qualifier("clusterName"). Spring will inject these beans at injection points when a matching qualifier is annotated.

This graph shows the most relevant Spring beans that are relevant for cluster-specific producers and consumers:

Spring Kafka Beans for Multiple Clusters

See also​