How to start Spring Apache Kafka Application with SpringBoot Auto-Configuration

The Spring Apache Kafka (spring-kafka) provides a high-level abstraction for Kafka-based messaging solutions. And Spring Boot 1.5 includes auto-configuration support for Apache Kafka via the spring-kafka project. So in the tutorial, JavaSampleApproach will guide how to start Spring Apache Kafka Application with SpringBoot.

Related Articles:
How to start Apache Kafka
How to start Spring Kafka Application with Spring Boot
Spring JMS – Explicitly Configure Spring ActiveMQ ConnectionFactory | SpringBoot

I. Technologies

– Java 8
– Maven build
– Spring Boot
– Apache Kafka
– Spring Tool Suite editor

II. Overview

As above mention, SpringBoot supports auto-configuration for Apache Kafka development:
– Use spring.kafka.* in file to modify external configuration.

Spring Apache Kafka Application

– Use Spring auto-configured KafkaTemplate to send Kafka-based messages:

– Use @KafkaListener to setup a Kafka listener:

III. Practice

We create a SpringBoot project with 2 main services: KafkaProducer and KafkaConsumer for sending and receiving messages from Apache Kafka cluster.
And export 2 RestAPIs {‘/producer’, ‘/consumer’} for interaction.

SpringBoot Kafka Application - project structure

Step to do:
– Create a SpringBoot project
– Create Kafka Producer and Consumer
– Add Apache Kafka external configuration
– Export some RestAPIs
– Deployment

1. Create a SpringBoot project

Use SpringToolSuite to create a SpringBoot project, then add dependencies {spring-kafka, spring-boot-starter-web}:

2. Create Kafka Producer and Consumer

– Create a KafkaProducer service:

– Create a KafkaConsumer service:

About MessageStorage, it is an additional implement to store Kafka-based messages after received. See details the implementation of MessageStorage:

3. Add Apache Kafka external configuration

Open file, add the configurations:

spring.kafka.bootstrap-servers is used to indicate the Kafka Cluster address. is used to indicate the consumer-group-id.
jsa.kafka.topic is an additional configuration. In the the tutorial, we use jsa.kafka.topic to define a Kafka topic name to produce and receive messages.

4. Export some RestAPIs

Create a Web Controller to export 2 RestAPIs {‘/producer’, ‘/consumer’}

/producer is used to send messages from browser to KafkaProducer service.
/consumer is used to get all recieved messages that are buffered in MessageStorage.

5. Deployment

Start Apache Kafka Cluster:
– Start a ZooKeeper:

– Start the Apache Kafka server:

>>> More details at: How to start Apache Kafka

Build and Install the SpringBoot project with commandlines: mvn clean install and mvn spring-boot:run

– Make a producer request: http://localhost:8080/jsa/kafka/producer?data=Hello World
-> Logs:

– Make another producer request: http://localhost:8080/jsa/kafka/producer?data=This is a SpringBoot Kafka Application

-> Logs:

– Make a consumer request: http://localhost:8080/jsa/kafka/consumer, result:

Spring Appache Kafka - consumer request

IV. Sourcecode


Related Posts

1 thought on “How to start Spring Apache Kafka Application with SpringBoot Auto-Configuration”

Got Something To Say:

Your email address will not be published. Required fields are marked *