Skip to content

ashifakram/spring-kafka-impl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

spring-kafka-impl

Spring Boot 3 + Spring Kafka demo showing:

  • String producer/consumer on my-topic
  • JSON producer/consumer for a User payload on user-topic

This project exposes REST endpoints that publish to Kafka, and Kafka listeners that log consumed messages.

Tech Stack

  • Java 17
  • Spring Boot 3.5.7
  • Spring Kafka
  • Maven

Project Structure

  • src/main/java/com/example/springkafka/config
    • KafkaProducerConfig.java: Kafka ProducerFactory and KafkaTemplate beans. Creates topic user-topic.
    • KafkaConsumerConfig.java: Kafka ConsumerFactory and listener container factories.
  • src/main/java/com/example/springkafka/controller
    • KafkaController.java: REST endpoints for sending messages/users.
  • src/main/java/com/example/springkafka/service
    • KafkaProducerService.java: sends string messages to my-topic.
    • KafkaConsumerService.java: consumes string messages from my-topic.
    • JsonProducer.java: sends User objects to user-topic.
    • JsonConsumer.java: consumes User objects from user-topic.
  • src/main/java/com/example/springkafka/model
    • User.java: simple POJO for JSON payloads.
  • src/main/resources/application.properties: Kafka and app configuration.

Kafka Topics

  • my-topic (String)
    • Producer: KafkaProducerService
    • Consumer: KafkaConsumerService
    • Note: there is no NewTopic bean created for my-topic in the config (commented out).
  • user-topic (JSON User)
    • Producer: JsonProducer
    • Consumer: JsonConsumer
    • Created by KafkaProducerConfig#userTopic()

Configuration Notes

There are two places where Kafka settings are defined:

  • application.properties (Spring Boot configuration)
  • KafkaProducerConfig and KafkaConsumerConfig (hard-coded values)

The config classes currently hard-code:

  • bootstrap.servers as localhost:9092
  • group IDs (my-consumer-group, user-consumer-group)

If you want centralized configuration, consider moving those values to application.properties and injecting them into the config classes.

Running the App

Prerequisites:

  • Java 17
  • Kafka broker running on localhost:9092
  • Maven (or use the Maven wrapper included in this repo)

Start Kafka (example):

  1. Start Zookeeper (if using Kafka in ZK mode)
  2. Start Kafka broker on localhost:9092

Run the app:

./mvnw spring-boot:run

Or:

mvn spring-boot:run

The app starts on http://localhost:8080.

REST Endpoints

Send String Message

POST /send?message=hello

Example:

curl -X POST "http://localhost:8080/send?message=hello"

Send User JSON

POST /senduser

Example:

curl -X POST "http://localhost:8080/senduser" \
  -H "Content-Type: application/json" \
  -d "{\"name\":\"Alice\",\"age\":30,\"email\":\"alice@example.com\"}"

Expected Logs

On publish:

  • Message sent: <message>
  • Sent user: User [name=..., age=..., email=...]

On consume:

  • Received message: <message>
  • Received user: User [name=..., age=..., email=...]

Tests

./mvnw test

About

Simple implementation of Apache Kafka in Spring Boot

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages