Skip to content

Latest commit

Β 

History

21 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🎬 Movie Catalog Microservices

A demonstration of microservices architecture using Spring Boot and Spring Cloud, featuring inter-service communication, service discovery with Eureka, and fault tolerance with Hystrix.

Architecture Diagram


πŸ“‹ Table of Contents


🎯 Overview

This project demonstrates a movie catalog system built with microservices architecture. Users can retrieve a personalized movie catalog that aggregates:

  • Movie information from The Movie Database (TMDb) API
  • User-specific ratings for each movie

The system showcases:

  • Inter-service communication using RestTemplate and WebClient
  • Service discovery with Netflix Eureka
  • Fault tolerance with Netflix Hystrix circuit breakers
  • Load balancing with client-side load balancing

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                      Eureka Discovery Server                     β”‚
β”‚                         (Port: 8761)                             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                β–²
           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
           β”‚                    β”‚                    β”‚
           β–Ό                    β–Ό                    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Movie Catalog   β”‚  β”‚   Movie Info     β”‚  β”‚  Ratings Data    β”‚
β”‚    Service       │◄──    Service       β”‚  β”‚    Service       β”‚
β”‚   (Port: 8081)   β”‚  β”‚   (Port: 8082)   β”‚  β”‚   (Port: 8083)   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚                      β”‚
        β”‚                      β–Ό
        β”‚              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β”‚              β”‚   TMDb API       β”‚
        β”‚              β”‚  (External)      β”‚
        β”‚              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        β–Ό
   Client Request
   GET /catalog/{userId}

πŸ”§ Services

Service Port Description
Discovery Server 8761 Eureka server for service registration and discovery
Movie Catalog Service 8081 Aggregates movie info and ratings; provides user catalog
Movie Info Service 8082 Fetches movie details from TMDb API
Ratings Data Service 8083 Provides user ratings for movies

πŸ› οΈ Tech Stack

  • Java 11 / Java 8
  • Spring Boot 2.1.2
  • Spring Cloud Greenwich
  • Netflix Eureka - Service Discovery
  • Netflix Hystrix - Circuit Breaker & Fault Tolerance
  • RestTemplate - Synchronous HTTP Client
  • WebClient - Reactive HTTP Client
  • Maven - Build Tool

πŸš€ Getting Started

Prerequisites

Installation

  1. Clone the repository

    git clone https://github.com/SaketKothari/microservices-communication-discovery-and-fault-tolerance.git
    cd microservices-communication-discovery-and-fault-tolerance
  2. Configure TMDb API Key

    Update the API key in movie-info-service/src/main/resources/application.properties:

    api.key=YOUR_TMDB_API_KEY
  3. Start the services in order

    # Terminal 1: Start Discovery Server (must be first)
    cd discovery-server
    ./mvnw spring-boot:run
    
    # Terminal 2: Start Movie Info Service
    cd movie-info-service
    ./mvnw spring-boot:run
    
    # Terminal 3: Start Ratings Data Service
    cd ratings-data-service
    ./mvnw spring-boot:run
    
    # Terminal 4: Start Movie Catalog Service
    cd movie-catalog-service
    ./mvnw spring-boot:run
  4. Verify services are registered

    Open http://localhost:8761 to see the Eureka dashboard.


πŸ“‘ API Endpoints

Movie Catalog Service (Port 8081)

Method Endpoint Description
GET /catalog/{userId} Get movie catalog for a user

Example Request:

curl http://localhost:8081/catalog/user123

Example Response:

[
  {
    "name": "The Dark Knight",
    "desc": "When the menace known as the Joker wreaks havoc...",
    "rating": 4
  },
  {
    "name": "Inception",
    "desc": "A thief who steals corporate secrets through...",
    "rating": 5
  }
]

Movie Info Service (Port 8082)

Method Endpoint Description
GET /movies/{movieId} Get movie details by TMDb ID

Ratings Data Service (Port 8083)

Method Endpoint Description
GET /ratingsdata/{movieId} Get rating for a movie
GET /ratingsdata/users/{userId} Get all ratings for a user

Hystrix Dashboard (Port 8081)

URL Description
http://localhost:8081/hystrix Hystrix Dashboard UI
http://localhost:8081/actuator/hystrix.stream Metrics stream

πŸ“š Key Concepts

Service Discovery with Eureka

Services register themselves with the Eureka server, allowing other services to discover them by name instead of hardcoded URLs.

// Instead of: http://localhost:8082/movies/123
// Use service name: http://movie-info-service/movies/123
Movie movie = restTemplate.getForObject(
    "http://movie-info-service/movies/" + movieId,
    Movie.class
);

RestTemplate with Load Balancing

The @LoadBalanced annotation enables client-side load balancing:

@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
    return new RestTemplate();
}

Fault Tolerance with Hystrix

Circuit breakers prevent cascading failures by providing fallback responses:

@HystrixCommand(fallbackMethod = "getFallbackCatalogItem")
public CatalogItem getCatalogItem(Rating rating) {
    Movie movie = restTemplate.getForObject(
        "http://movie-info-service/movies/" + rating.getMovieId(),
        Movie.class
    );
    return new CatalogItem(movie.getName(), movie.getDescription(), rating.getRating());
}

// Fallback when movie-info-service is unavailable
public CatalogItem getFallbackCatalogItem(Rating rating) {
    return new CatalogItem("Movie name not found", "", rating.getRating());
}

βš™οΈ Configuration

Discovery Server

server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

Client Services

# Movie Catalog Service
spring.application.name=movie-catalog-service
server.port=8081
management.endpoints.web.exposure.include=hystrix.stream

# Movie Info Service
spring.application.name=movie-info-service
server.port=8082
api.key=YOUR_TMDB_API_KEY

# Ratings Data Service
spring.application.name=ratings-data-service
server.port=8083

πŸ“Š Monitoring

Access the Hystrix Dashboard to monitor circuit breaker status:

  1. Navigate to http://localhost:8081/hystrix
  2. Enter the stream URL: http://localhost:8081/actuator/hystrix.stream
  3. Click "Monitor Stream"

πŸ™ Acknowledgments

About

Creating a few microservices and have them communicate with each other. In the process, we'll understand how inter-service communication work, and we'll implement service discovery with Eureka to facilitate services finding each other to communicate.

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages