Apache Kafka with Spring Boot
🛰️ What is Apache Kafka?
Kafka is a high-performance, distributed messaging system used for real-time data streaming.
It works like a distributed queue, but is faster, scalable, and durable — used by giants like LinkedIn, Netflix, and Uber.
✅ Why Use Kafka Instead of JMS?
Feature | Kafka | JMS (e.g., ActiveMQ) |
---|---|---|
Performance | Very High (millions/sec) | Moderate |
Scalability | Horizontally scalable | Limited |
Storage | Persistent by default | Mostly in-memory (configurable) |
Use Case | Event streaming & microservices | Traditional enterprise messaging |
🚀 Spring Boot + Kafka Example (Producer & Consumer)
🔗 Step 1: Add Kafka Dependency
⚙️ Step 2: Configuration (application.properties
)
📤 Step 3: Kafka Producer (Send Message)
📥 Step 4: Kafka Consumer (Receive Message)
🧪 Step 5: Test Controller
📦 Output Example:
-
Call:
http://localhost:8080/send
-
Console:
✅ Summary
Component | Annotation / Class |
---|---|
Producer | KafkaTemplate.send() |
Consumer | @KafkaListener |
Broker | Apache Kafka |
Topic | Named pipe between services |