Skip to content

nivleking/ticket-book-microservices

Repository files navigation

Ticket Booking Microservices

Event ticketing system built with Spring Boot microservices architecture, featuring event-driven communication via Kafka and secure API gateway with OAuth2 authentication.

Architecture

┌─────────────┐
│ API Gateway │ :8090 - Entry point with OAuth2 + Circuit Breaker
└──────┬──────┘
       │
       ├──────► Inventory Service :8080 - Event & venue inventory management
       │
       └──────► Booking Service   :8081 - Ticket booking + Kafka producer
                      │
                      │ Kafka (BOOKING_TOPIC)
                      ▼
               Order Service      :8082 - Order processing + Kafka consumer

System Architecture

Tech Stack

  • Java 21
  • Spring Boot 3.5.7
  • MySQL 8.0
  • Apache Kafka
  • Keycloak (OAuth2)
  • Flyway (DB Migration)
  • Resilience4j (Circuit Breaker)

Quick Start

Prerequisites

  • JDK 21
  • Maven 3.6+
  • MySQL 8.0
  • Apache Kafka 3.0+
  • Keycloak 21+ (optional, for API Gateway)

Database Setup

CREATE DATABASE ticketing;

Environment Configuration

Each service needs a .env file. Copy from examples:

cp inventory-service/.env.example inventory-service/.env
cp booking-service/.env.example booking-service/.env
cp order-service/.env.example order-service/.env
cp api-gateway/.env.example api-gateway/.env

Update database credentials in each .env:

DB_USERNAME=root
DB_PASSWORD=your_password

Start Services

Start each service in separate terminals:

# Terminal 1
cd inventory-service && mvn spring-boot:run

# Terminal 2
cd booking-service && mvn spring-boot:run

# Terminal 3
cd order-service && mvn spring-boot:run

# Terminal 4
cd api-gateway && mvn spring-boot:run

Service URLs

Service Port API Docs
API Gateway 8090 http://localhost:8090/swagger-ui.html
Inventory Service 8080 http://localhost:8080/swagger-ui.html
Booking Service 8081 -
Order Service 8082 -

API Flow Example

  1. Get available events (via API Gateway)

    GET http://localhost:8090/api/v1/inventory/events
  2. Book tickets (via API Gateway)

    POST http://localhost:8090/api/v1/booking/book
    {
      "userId": 1,
      "eventId": 1,
      "ticketCount": 2
    }
  3. Booking service publishes event to Kafka topic BOOKING_TOPIC

  4. Order service consumes event and creates order in database

  5. Order service updates inventory via REST call to Inventory Service

Project Structure

ticket-book-microservices/
├── inventory-service/    # Event & venue management
├── booking-service/      # Ticket booking + Kafka producer
├── order-service/        # Order processing + Kafka consumer
├── api-gateway/          # API Gateway with OAuth2

Key Features

Inventory Service

  • Event and venue CRUD operations
  • Real-time capacity tracking
  • REST API for inventory queries

Booking Service

  • Ticket booking validation
  • Kafka event producer
  • Inventory availability check

Order Service

  • Kafka event consumer
  • Order persistence
  • Inventory update via REST client

API Gateway

  • OAuth2 authentication (Keycloak)
  • Circuit breaker pattern
  • Request routing to downstream services
  • Aggregated API documentation

Development

Build All Services

# From root directory
for dir in inventory-service booking-service order-service api-gateway; do
  cd $dir && mvn clean install && cd ..
done

Run Tests

cd <service-name>
mvn test

Database Migrations

Flyway migrations run automatically on application startup. Migration files located in src/main/resources/db/migration/.

Security

  • API Gateway enforces OAuth2 authentication via Keycloak
  • Database credentials stored in .env files (not committed to git)
  • Public endpoints configurable via SECURITY_EXCLUDED_URLS

Monitoring

API Gateway exposes Circuit Breaker health metrics:

GET http://localhost:8090/actuator/health

Releases

No releases published

Packages

No packages published

Languages