Skip to content

manvinderjit/react-springboot-kafka-apps

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

16 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Ad Event Tracking System

A distributed real-time ad event tracking system built with Spring Boot and Apache Kafka. This application demonstrates event-driven architecture for capturing, streaming, and analyzing advertisement interaction data.

πŸ—οΈ Architecture

The system consists of two Spring Boot microservices:

  • Frontend Service (Port 8081): Web interface for generating ad events and data visualization
  • Backend Service (Port 8080): Consumer service that processes Kafka messages and provides REST APIs
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    Kafka Topics     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    MySQL    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Frontend  β”‚ ──────────────────► β”‚   Backend   β”‚ ──────────► β”‚  Database   β”‚
β”‚  (Producer) β”‚   ad_viewed         β”‚ (Consumer)  β”‚             β”‚             β”‚
β”‚             β”‚   ad_clicked        β”‚             β”‚             β”‚             β”‚
β”‚             β”‚   ad_closed         β”‚             β”‚             β”‚             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜             β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

✨ Recent Updates

Enhanced Event Tracking (Latest)

  • Enriched Data Model: Events now include ad company and category information
  • Improved UI Consistency: Unified navigation and styling across all pages
  • Better Accessibility: Enhanced contrast for close buttons and consistent button layouts
  • Streamlined Interface: Removed external visit buttons for cleaner ad interaction focus

UI/UX Improvements

  • Consistent Navigation: Top-positioned navigation buttons on all pages
  • Optimized Button Layout: All ad action buttons aligned in single row
  • Enhanced Data Display: Company and category columns in raw data view
  • Professional Styling: Gradient headers and consistent visual design

πŸš€ Features

  • Real-time Event Streaming: Kafka-based message processing for ad interactions
  • Interactive Ad Display: Visual ad showcase with real product advertisements
  • Event Tracking: Automatic tracking of ad views, clicks, and closures with company and category metadata
  • Data Visualization: Interactive charts showing event distribution, trends, and ad performance analytics
  • REST API: Comprehensive endpoints for accessing event data, statistics, and ad-specific analytics
  • Persistent Storage: MySQL database for event history with enriched ad metadata (company, category)
  • Responsive Design: Mobile-friendly interface for ad interactions
  • Containerized Deployment: Docker support with Kubernetes manifests

πŸ“Έ Screenshots

Ad Showcase - Interactive Display

Ad Showcase Main interface showing interactive ad cards with View, Click, and Close tracking buttons

Analytics Dashboard - Performance Insights

Analytics Dashboard Comprehensive analytics with event distribution charts and detailed ad performance metrics

Raw Event Logs - Complete Data View

Raw Event Logs Detailed event history with timestamps, event types, and ad metadata including company and category information

πŸ› οΈ Technology Stack

  • Java 17 - Programming language
  • Spring Boot 3.5.5 - Application framework
  • Apache Kafka - Event streaming platform
  • MySQL - Database for event persistence
  • Thymeleaf - Template engine for web UI
  • Maven - Build and dependency management
  • Docker - Containerization
  • Kubernetes - Container orchestration

πŸ“‹ Prerequisites

  • Java 17 or higher
  • Maven 3.6+
  • Apache Kafka < 4.0 (local or remote)
  • MySQL 8.0+
  • Docker (optional, for containerized deployment)

πŸƒβ€β™‚οΈ Quick Start

1. Clone the Repository

git clone <repository-url>
cd ad-event-tracking-system

2. Start Infrastructure Services

Option A: Local Setup

  • Start Kafka on localhost:9092
  • Start MySQL on localhost:3306 with database ads_db

Option B: Docker Compose (if available)

docker-compose up -d kafka mysql

3. Configure Database

Create MySQL database and user:

CREATE DATABASE ads_db;
CREATE USER 'ads_user'@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON ads_db.* TO 'ads_user'@'%';

4. Run Applications

Backend Service:

cd backend
./mvnw spring-boot:run

Frontend Service:

cd frontend
./mvnw spring-boot:run

5. Access the Application

πŸ”§ Kafka Setup & Management

Prerequisites

  • Apache Kafka version < 4.0 (tested with Kafka 3.8)
  • Kafka must be running on localhost:9092 for default configuration

Starting Kafka Services

1. Start Zookeeper (in first terminal):

# Navigate to your Kafka installation directory
cd /path/to/kafka

# Start Zookeeper (runs on port 2181)
bin/zookeeper-server-start.sh config/zookeeper.properties

2. Start Kafka Broker (in second terminal):

# Start Kafka server (runs on port 9092)
bin/kafka-server-start.sh config/server.properties

Topic Management

Create Required Topics (the application will auto-create these, but you can create them manually):

# Create ad_viewed topic
bin/kafka-topics.sh \
  --create \
  --topic ad_viewed \
  --bootstrap-server localhost:9092 \
  --partitions 1 \
  --replication-factor 1

# Create ad_clicked topic
bin/kafka-topics.sh \
  --create \
  --topic ad_clicked \
  --bootstrap-server localhost:9092 \
  --partitions 1 \
  --replication-factor 1

# Create ad_closed topic
bin/kafka-topics.sh \
  --create \
  --topic ad_closed \
  --bootstrap-server localhost:9092 \
  --partitions 1 \
  --replication-factor 1

List All Topics:

bin/kafka-topics.sh --list --bootstrap-server localhost:9092

Testing Kafka Setup

Monitor Ad Events (useful for debugging):

# Monitor ad_viewed events
bin/kafka-console-consumer.sh \
  --bootstrap-server localhost:9092 \
  --topic ad_viewed \
  --from-beginning

# Monitor ad_clicked events
bin/kafka-console-consumer.sh \
  --bootstrap-server localhost:9092 \
  --topic ad_clicked \
  --from-beginning

# Monitor ad_closed events
bin/kafka-console-consumer.sh \
  --bootstrap-server localhost:9092 \
  --topic ad_closed \
  --from-beginning

Manually Send Test Events (for testing):

# Send test ad_viewed event
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic ad_viewed
# Then type: {"eventType":"ad_viewed","timestamp":"2024-01-01T12:00:00Z","adId":1,"adTitle":"Test Ad","adCompany":"Test Company","adCategory":"Electronics"}

# Send test ad_clicked event
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic ad_clicked
# Then type: {"eventType":"ad_clicked","timestamp":"2024-01-01T12:00:00Z","adId":1,"adTitle":"Test Ad","adCompany":"Test Company","adCategory":"Electronics"}

Stopping Kafka Services

Option 1: Use Ctrl+C in the terminal windows running Zookeeper and Kafka

Option 2: Use stop scripts:

# Stop Kafka server first
bin/kafka-server-stop.sh

# Then stop Zookeeper
bin/zookeeper-server-stop.sh

Kafka Configuration Notes

  • Bootstrap Servers: Default localhost:9092
  • Consumer Group: ad-consumer-group (configured in backend)
  • Auto Topic Creation: Enabled by default
  • Message Format: JSON with event metadata
  • Partitions: Single partition per topic (suitable for development)
  • Replication Factor: 1 (suitable for single-broker setup)

πŸ”§ Configuration

Environment Variables

Backend Service:

SPRING_KAFKA_BOOTSTRAP_SERVERS=localhost:9092
SPRING_DATASOURCE_URL=jdbc:mysql://localhost:3306/ads_db
SPRING_DATASOURCE_USERNAME=ads_user
SPRING_DATASOURCE_PASSWORD=password

Frontend Service:

KAFKA_BOOTSTRAP_SERVERS=localhost:9092
BACKEND_API_URL=http://localhost:8080/api/events
SERVER_PORT=8081

🐳 Docker Deployment

Build Images

# Build backend image
cd backend
docker build -t ad-tracker-backend .

# Build frontend image
cd frontend
docker build -t ad-tracker-frontend .

Run with Docker

# Run backend
docker run -p 8080:8080 \
  -e SPRING_KAFKA_BOOTSTRAP_SERVERS=kafka:9092 \
  -e SPRING_DATASOURCE_URL=jdbc:mysql://mysql:3306/ads_db \
  ad-tracker-backend

# Run frontend
docker run -p 8081:8081 \
  -e KAFKA_BOOTSTRAP_SERVERS=kafka:9092 \
  -e BACKEND_API_URL=http://backend:8080/api/events \
  ad-tracker-frontend

πŸ“Š API Endpoints

Backend Service (Port 8080)

Method Endpoint Description
GET /api/events Get all events
GET /api/events/counts Get event counts by type
GET /api/events/counts/by-ad Get total interactions per ad
GET /api/events/analytics/by-ad Get detailed analytics breakdown per ad with company/category
GET /api/events/top-ads Get top 10 performing ads by interactions

Frontend Service (Port 8081)

Method Endpoint Description
GET / Main event generation page
POST /send-event Send ad event to Kafka
GET /chart Event visualization page
GET /data Raw event data page

πŸ§ͺ Testing

Manual Testing

  1. Access the ad showcase at http://localhost:8081
  2. Interact with displayed ads:
    • View: Click the "πŸ‘οΈ View" button or ad image
    • Click: Click the "πŸ–±οΈ Click" button to simulate ad clicks
    • Close: Click "βœ• Close" to simulate closing an ad
  3. View real-time analytics at /chart and raw data at /data
  4. Check backend API at http://localhost:8080/api/events for all events
  5. Monitor console logs for Kafka message processing

πŸ“ Project Structure

β”œβ”€β”€ backend/                 # Consumer service
β”‚   β”œβ”€β”€ src/main/java/
β”‚   β”‚   └── com/example/backend/
β”‚   β”‚       β”œβ”€β”€ controller/  # REST controllers
β”‚   β”‚       β”œβ”€β”€ entity/      # JPA entities
β”‚   β”‚       β”œβ”€β”€ repository/  # Data access layer
β”‚   β”‚       └── service/     # Business logic
β”‚   └── Dockerfile
β”œβ”€β”€ frontend/                # Producer service & web UI
β”‚   β”œβ”€β”€ src/main/java/
β”‚   β”‚   └── com/example/frontend/
β”‚   β”‚       β”œβ”€β”€ controller/  # Web controllers
β”‚   β”‚       β”œβ”€β”€ model/       # Data models
β”‚   β”‚       └── service/     # Kafka producers
β”‚   β”œβ”€β”€ src/main/resources/templates/  # Thymeleaf templates
β”‚   └── Dockerfile
└── README.md

πŸ” Monitoring

  • Application Logs: Check console output for Kafka message processing
  • Database: Query ad_event table for stored events
  • Kafka Topics: Monitor ad_viewed, ad_clicked, ad_closed topics

πŸ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ†˜ Troubleshooting

Common Issues:

  1. Kafka Connection Failed: Ensure Kafka is running and accessible
  2. Database Connection Error: Verify MySQL is running and credentials are correct
  3. Port Already in Use: Check if ports 8080/8081 are available
  4. Maven Build Fails: Ensure Java 17+ is installed and JAVA_HOME is set

Logs Location:

  • Application logs: Console output
  • Kafka logs: Check Kafka server logs
  • Database logs: MySQL error logs

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published