Part 1: Install and Run Kafka on Windows
Prerequisites
Java 8+
Kafka 3.x (includes Zookeeper)
A terminal like Command Prompt, Git Bash, or PowerShell
Step 1: Download Kafka
Go to: https://kafka.apache.org/downloads
Choose a binary (e.g., Kafka 3.6.0 with Scala 2.13)
Extract it to C:\kafka
Step 2: Start Zookeeper
Kafka uses Zookeeper for managing brokers. In a terminal:
cd C:\kafka
bin\windows\zookeeper-server-start.bat config\zookeeper.properties
Keep this terminal open.
Step 3: Start Kafka Server
Open a new terminal:
cd C:\kafka
bin\windows\kafka-server-start.bat config\server.properties
Kafka is now running on localhost:9092.
Part 2: Kafka Commands
Create a Topic
bin\windows\kafka-topics.bat --create --topic test-topic --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1
Check the Topic
bin\windows\kafka-topics.bat --list --bootstrap-server localhost:9092
Part 3: Spring Boot Kafka Project
Step 1: Create a Spring Boot Project
You can use Spring Initializr with the following settings:
Dependencies: Spring Web, Spring for Apache Kafka
Name: kafka-demo
Package: com.example.kafkademo
Project Structure
src/
└── main/
├── java/com/example/kafkademo/
│ ├── KafkaDemoApplication.java
│ ├── config/KafkaConfig.java
│ ├── controller/MessageController.java
│ ├── service/KafkaProducerService.java
│ └── listener/KafkaConsumerListener.java
└── resources/
└── application.yml
🛠 application.yml
spring:
kafka:
bootstrap-servers: localhost:9092
consumer:
group-id: my-group
auto-offset-reset: earliest
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
producer:
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.apache.kafka.common.serialization.StringSerializer
KafkaProducerService.java
@Service
public class KafkaProducerService {
@Autowired
private KafkaTemplate<String, String> kafkaTemplate;
public void sendMessage(String message) {
kafkaTemplate.send("test-topic", message);
}
}
KafkaConsumerListener.java
@Component
public class KafkaConsumerListener {
@KafkaListener(topics = "test-topic", groupId = "my-group")
public void listen(String message) {
System.out.println("Received Message: " + message);
}
}
MessageController.java
@RestController
@RequestMapping("/api/messages")
public class MessageController {
@Autowired
private KafkaProducerService producerService;
@PostMapping
public ResponseEntity<String> sendMessage(@RequestBody String message) {
producerService.sendMessage(message);
return ResponseEntity.ok("Message sent to Kafka");
}
}
Main Application
@SpringBootApplication
public class KafkaDemoApplication {
public static void main(String[] args) {
SpringApplication.run(KafkaDemoApplication.class, args);
}
}
Part 4: Run and Test
Run the App
./mvnw spring-boot:run
Test with Postman or Curl
curl -X POST http://localhost:8080/api/messages -H "Content-Type: text/plain" -d "Hello Kafka!"
## Output
Your consumer will show:
Received Message: Hello Kafka!
Part 5: Clean Up
Stop the Spring Boot app --> Ctrl+C
Stop Kafka and Zookeeper --> Ctrl+C (Ctrl+C in each terminal)
Conclusion
You’ve just created a complete Kafka setup with:
A running Kafka instance on Windows
A Spring Boot REST API to send messages
A Kafka listener to consume them
Top comments (0)