Complete guide for setting up the Delivery Tracking App backend with Kafka KRaft (no Zookeeper), Prometheus, and Grafana.
- Prerequisites
- Step 1: Kafka KRaft Setup
- Step 2: Prometheus Setup
- Step 3: Grafana Setup
- Step 4: Spring Boot Application
- API Reference
- Troubleshooting
- Java 17 or higher
- Maven 3.8+
- Windows/Linux/MacOS
Kafka KRaft mode eliminates the need for Zookeeper, simplifying the setup.
# Windows PowerShell
$kafkaVersion = "3.6.1"
Invoke-WebRequest -Uri "https://downloads.apache.org/kafka/$kafkaVersion/kafka_2.13-$kafkaVersion.tgz" -OutFile "kafka.tgz"
tar -xzf kafka.tgz
Rename-Item "kafka_2.13-$kafkaVersion" "kafka"Or download manually from Apache Kafka Downloads
cd kafka
bin\windows\kafka-storage.bat random-uuidSave the generated UUID (e.g., abc123def456ghi789)
bin\windows\kafka-storage.bat format -t <YOUR_CLUSTER_ID> -c config\kraft\server.propertiesEdit config/kraft/server.properties:
# Listeners
listeners=PLAINTEXT://localhost:9092,CONTROLLER://localhost:9093
advertised.listeners=PLAINTEXT://localhost:9092
# Log directories
log.dirs=C:/kafka/kraft-logs
# Node settings
node.id=1
process.roles=broker,controller
controller.quorum.voters=1@localhost:9093
# Topic settings
auto.create.topics.enable=true
num.partitions=1
default.replication.factor=1
min.insync.replicas=1bin\windows\kafka-server-start.bat config\kraft\server.propertiesVerify Kafka is running by checking port 9092.
# In a new terminal
bin\windows\kafka-topics.bat --create --topic delivery-location --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1
# Verify topic creation
bin\windows\kafka-topics.bat --list --bootstrap-server localhost:9092$promVersion = "2.48.0"
Invoke-WebRequest -Uri "https://github.com/prometheus/prometheus/releases/download/v$promVersion/prometheus-$promVersion.windows-amd64.zip" -OutFile "prometheus.zip"
Expand-Archive -Path "prometheus.zip" -DestinationPath "."
Rename-Item "prometheus-$promVersion.windows-amd64" "prometheus"Create prometheus.yml in the prometheus folder:
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: 'delivery-app'
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['localhost:8080']
scrape_interval: 5s
- job_name: 'kafka'
static_configs:
- targets: ['localhost:9092']cd prometheus
.\prometheus.exe --config.file=prometheus.ymlAccess Prometheus UI at: http://localhost:9090
$grafanaVersion = "10.2.3"
Invoke-WebRequest -Uri "https://dl.grafana.com/oss/release/grafana-$grafanaVersion.windows-amd64.zip" -OutFile "grafana.zip"
Expand-Archive -Path "grafana.zip" -DestinationPath "."cd grafana-$grafanaVersion\bin
.\grafana-server.exeAccess Grafana UI at: http://localhost:3000
- Default username:
admin - Default password:
admin
-
Add Prometheus Data Source:
- Go to Configuration → Data Sources
- Click "Add data source"
- Select "Prometheus"
- URL:
http://localhost:9090 - Click "Save & Test"
-
Import Delivery Dashboard:
- Go to Create → Import
- Use dashboard ID:
1860(Node Exporter) or create custom - Or import the JSON below:
{
"dashboard": {
"title": "Delivery Tracking Dashboard",
"panels": [
{
"title": "Active Deliveries",
"type": "stat",
"targets": [
{
"expr": "delivery_in_transit_count"
}
]
},
{
"title": "Delivered Today",
"type": "stat",
"targets": [
{
"expr": "delivery_delivered_count"
}
]
},
{
"title": "Average Delivery Time",
"type": "graph",
"targets": [
{
"expr": "rate(delivery_time_seconds_sum[5m]) / rate(delivery_time_seconds_count[5m])"
}
]
}
]
}
}Edit src/main/resources/application.properties:
spring:
application:
name: delivery-tracking-app
kafka:
bootstrap-servers: localhost:9092
consumer:
group-id: delivery-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
server:
port: 8080
management:
endpoints:
web:
exposure:
include: prometheus,health,info,metrics
endpoint:
prometheus:
enabled: true
metrics:
export:
prometheus:
enabled: true# Build
.\mvnw clean package -DskipTests
# Run
.\mvnw spring-boot:run
# Or run the JAR
java -jar target\Backend-1-0.0.1-SNAPSHOT.jar- Health Check: http://localhost:8080/actuator/health
- Metrics: http://localhost:8080/actuator/prometheus
- API Base: http://localhost:8080/api/delivery
POST /api/delivery/update
Content-Type: application/json
{
"orderId": "ORD001",
"latitude": 18.5074,
"longitude": 73.8077,
"status": "in-transit",
"etaMinutes": 15.5,
"speedKmh": 25.3,
"driverName": "John Doe",
"vehicleId": "VH001"
}GET /api/delivery/history/{orderId}GET /api/delivery/history/{orderId}/range?from=2024-01-15T10:00:00Z&to=2024-01-15T11:00:00ZGET /api/delivery/activeGET /api/delivery/summary/{orderId}GET /api/delivery/summariesGET /api/delivery/ordersDELETE /api/delivery/history/{orderId}| Metric Name | Type | Description |
|---|---|---|
delivery_placed_count |
Gauge | Orders in placed state |
delivery_in_transit_count |
Gauge | Orders in transit |
delivery_delivered_count |
Gauge | Delivered orders |
delivery_failed_count |
Gauge | Failed deliveries |
delivery_time_seconds |
Summary | Delivery duration |
Problem: Kafka fails to start with "Address already in use" Solution:
# Find and kill process using port 9092
netstat -ano | findstr :9092
taskkill /PID <PID> /FProblem: "Failed to load properties from config/kraft/server.properties"
Solution: Use absolute paths in log.dirs configuration
Problem: "connection refused" when scraping
Solution: Ensure Spring Boot app is running and management.endpoints.web.exposure.include includes prometheus
Problem: "Connection to node -1 could not be established"
Solution: Verify Kafka is running on localhost:9092
Problem: WebSocket connection fails
Solution: Check CORS configuration in DeliveryController.java
# List topics
bin\windows\kafka-topics.bat --list --bootstrap-server localhost:9092
# Describe topic
bin\windows\kafka-topics.bat --describe --topic delivery-location --bootstrap-server localhost:9092
# Consume messages
bin\windows\kafka-console-consumer.bat --topic delivery-location --from-beginning --bootstrap-server localhost:9092
# Delete topic
bin\windows\kafka-topics.bat --delete --topic delivery-location --bootstrap-server localhost:9092# Clean build
.\mvnw clean package
# Run tests
.\mvnw test
# Run with profile
.\mvnw spring-boot:run -Dspring-boot.run.profiles=devBackend/
├── src/main/java/com/example/tracking/
│ ├── config/ # Configuration classes
│ │ ├── JacksonConfig.java
│ │ ├── KafkaConfig.java
│ │ └── WebSocketConfig.java
│ ├── controller/ # REST API controllers
│ │ └── DeliveryController.java
│ ├── metrics/ # Micrometer metrics
│ │ └── DeliveryMetrics.java
│ ├── model/ # Data models
│ │ └── DeliveryLocation.java
│ ├── service/ # Business logic
│ │ ├── DeliveryConsumer.java
│ │ ├── DeliveryHistoryService.java
│ │ ├── DeliveryProducer.java
│ │ ├── DeliverySimulator.java
│ │ ├── EnhancedDeliverySimulator.java
│ │ └── OsrmRouteService.java
│ └── util/ # Utility classes
│ └── PolylineDecoder.java
├── src/main/resources/
│ └── application.properties
├── pom.xml
└── README.md