Skip to main content

How to: Integrate Kafka into a service

An end-to-end checklist for wiring Apache Kafka into a jEAP-based microservice using the jEAP Messaging Library, spanning the Message Type Registry, the Error Handling Service and the library itself. For settings reference (topics, consumers, producers, partitioning), see Kafka and Kafka topics & client configuration.

Checklist​

  1. Define Message Types according to the business requirements.
  2. Order Kafka topics from the party responsible for the cluster (usually one topic per event, though several events per topic are possible), following the Naming Conventions.
  3. Define the type descriptor and Avro schema, and publish them on a feature branch in the Message Type Registry.
  4. Set up an Error Handling Service — don't forget to request the roles needed to authorize the service.
  5. Add jeap-messaging-infrastructure-kafka and the generated message-type dependencies.
  6. Import the Kafka cluster certificates and the schema registry's root certificate into the truststore.
  7. Declare the consumer/producer message contracts for the microservice using annotations, as described under jeap-messaging — Message contracts.
  8. Implement the producer / consumer, following the jme-messaging-example and applying the idempotent-receiver pattern on the consumer side (see below).
  9. Define the Kafka configuration in the service, following the example in application.yml in the jme-messaging-example repository.
  10. Develop and test the event-driven service, both locally (see below) and on dev.
  11. Once the event schema is stable, merge the feature branch in the Message Type Registry into master via a pull request.

Local development with Kafka​

A trimmed docker-compose.yml with a KRaft-mode broker, a schema registry and a Postgres database for the error-handling service:

services:
broker:
image: confluentinc/cp-kafka:latest
hostname: broker
ports:
- "9092:9092"
environment:
KAFKA_NODE_ID: 1
KAFKA_PROCESS_ROLES: broker,controller
KAFKA_CONTROLLER_QUORUM_VOTERS: 1@broker:9093
KAFKA_LISTENERS: SASL_PLAINTEXT://broker:9092,CONTROLLER://broker:9093
KAFKA_ADVERTISED_LISTENERS: SASL_PLAINTEXT://localhost:9092
KAFKA_CONTROLLER_LISTENER_NAMES: CONTROLLER
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: SASL_PLAINTEXT:SASL_PLAINTEXT,CONTROLLER:PLAINTEXT
KAFKA_SASL_ENABLED_MECHANISMS: SCRAM-SHA-512
KAFKA_SASL_MECHANISM_INTER_BROKER_PROTOCOL: SCRAM-SHA-512
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1

schema-registry:
image: confluentinc/cp-schema-registry:latest
depends_on:
- broker
ports:
- "8081:8081"
environment:
SCHEMA_REGISTRY_HOST_NAME: schema-registry
SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS: SASL_PLAINTEXT://broker:9092
SCHEMA_REGISTRY_LISTENERS: http://0.0.0.0:8081

errorhandling-db:
image: postgres:latest
ports:
- "5432:5432"
environment:
POSTGRES_DB: errorhandling
POSTGRES_USER: errorhandling
POSTGRES_PASSWORD: errorhandling

See also Kafka — Starting Kafka locally for notes on schema-registry port clashes and the Confluent Control Center license.

Acknowledge / commit​

jEAP Messaging sets enable.auto.commit=false (Kafka client) and AckMode=MANUAL (Spring Kafka). A listener must therefore always call Acknowledgment.acknowledge() itself, at the very end of event processing — never before processing or in a finally block, or an early acknowledgment can lose the message on failure. The jEAP error handler acknowledges a failed record itself, after publishing its failure event:

@KafkaListener(topics = {TopicConfiguration.NAME})
public void consume(final JmeDeclarationCreatedEvent event, Acknowledgment ack) {
log.debug("Received event {}", event.getIdentity().getEventId());
// ... processing ...

ack.acknowledge();
log.debug("Acknowledged event {}", event.getIdentity().getEventId());
}

Idempotent consumer​

At-least-once delivery means a consumer may receive the same record (and hence the same domain event) more than once — also via an error-handler resubmit. Consumers must therefore be implemented idempotently. The message identity's idempotenceId is the basis for deduplication; see jeap-messaging — Idempotent message handler for the automatic approach, or deduplicate manually:

@KafkaListener(topics = {TopicConfiguration.NAME})
public void consume(final JmeDeclarationCreatedEvent event, Acknowledgment ack) {
log.debug("Received event {}", event.getIdentity().getEventId());
if (repository.containsOrderWithIdempotenceId(event.getIdentity().getIdempotenceId())) {
log.debug("Received duplicate event with idempotenceId {}, ignoring", event.getIdentity().getIdempotenceId());
ack.acknowledge();
return;
}
// ... processing ...
ack.acknowledge();
}