User-Driver System with Spring Boot and Kafka


In modern applications, real-time data streaming is a critical feature for services like cab booking, food delivery, and more. In this blog, weโll build a User Driver System using Spring Boot and Apache Kafka. This system will simulate real-time communication between drivers (producers) and users (consumers).
By the end, youโll have a distributed system where:
Drivers update their location in real-time.
Users receive these updates instantly through Kafka.
๐ 1. Introduction: What are we building and why?
The User Driver System consists of two microservices:
Driver Service: Publishes cab location updates to a Kafka topic.
User Service: Consumes location updates from the Kafka topic.
Key features:
Real-Time Location Updates: Drivers send location updates (latitude, longitude) every second.
Kafka Integration: Ensures reliable and scalable communication between services.
Microservices Architecture: Two independent Spring Boot applications.
This project is ideal for:
Learning Spring Boot Kafka integration.
Building real-time systems with distributed microservices.
Understanding producer-consumer communication in Kafka.
๐ 2. Architecture Overview
Components:
Kafka Broker: The central hub for message streaming.
Driver Service (Producer):
- Sends location updates to the Kafka topic.
User Service (Consumer):
- Listens to location updates from the Kafka topic.
๐ป 3. Driver Service
The Driver Service simulates a driver sending periodic location updates to Kafka.
Kafka Configuration
The KafkaConfig
class creates a Kafka topic named cab-location
where location updates will be published.
@Configuration
public class KafkaConfig {
@Bean
public NewTopic topic() {
return TopicBuilder.name(AppConstant.CAB_LOCATION).build();
}
}
Controller
The CabLocationController
simulates location updates. It sends random coordinates to the Kafka topic every second.
@RestController
@RequestMapping("/location")
public class CabLocationController {
@Autowired
private CabLocationService cabLocationService;
@PutMapping
public ResponseEntity updateLocation() throws InterruptedException {
int range = 100;
while (range > 0) {
cabLocationService.updateLocation(Math.random() + "," + Math.random());
Thread.sleep(1000);
range--;
}
return new ResponseEntity(Map.of("message", "location updated"), HttpStatus.OK);
}
}
Service
The CabLocationService
sends location updates to the Kafka topic.
@Service
public class CabLocationService {
@Autowired
private KafkaTemplate<String, String> kafkaTemplate;
public void updateLocation(String location) {
kafkaTemplate.send("cab-location", location);
}
}
Driver Service Dependencies
The pom.xml
includes dependencies for Spring Boot Kafka and web modules.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
</dependencies>
๐ก 4. User Service
The User Service listens to location updates from the Kafka topic and prints them to the console.
Kafka Listener
The LocationService
class consumes messages from the cab-location
topic.
@Service
public class LocationService {
@KafkaListener(topics = "cab-location", groupId = "user-group")
public void cabLocation(String location) {
System.out.println(location);
}
}
User Service Dependencies
The pom.xml
includes dependencies for Kafka consumption.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
</dependencies>
๐ 5. How to Run the System
Prerequisites:
Apache Kafka running locally.
Java 21 installed.
Steps:
Start Kafka Broker:
bin/zookeeper-server-start.sh config/zookeeper.properties bin/kafka-server-start.sh config/server.properties
Start Driver Service:
cd cab-book-driver ./mvnw spring-boot:run
Start User Service:
cd cab-book-user ./mvnw spring-boot:run
Update Locations: Send a PUT request to
http://localhost:8080/location
to start sending location updates.View Updates: Check the User Service console for real-time location updates.
๐ 6. GitHub Repository
The complete source code for this project is available on GitHub. Feel free to clone, fork, or contribute to the repository:
GitHub Link: User Driver with Kafka
๐ 7. Conclusion
This project demonstrates how to use Apache Kafka with Spring Boot for real-time data streaming. Youโve learned:
How to build producers and consumers with Kafka.
How to integrate Kafka into Spring Boot applications.
How to simulate real-time communication in a distributed system.
Whatโs Next?
Add a database to store historical location data.
Implement error handling for Kafka messages.
Build a UI dashboard to display real-time locations on a map.
Happy coding! ๐๐
Subscribe to my newsletter
Read articles from Mayur Patil directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Mayur Patil
Mayur Patil
Skilled in Java & Spring Boot , Backedn Enthusiast