A demonstration of microservices architecture using Spring Boot and Spring Cloud, featuring inter-service communication, service discovery with Eureka, and fault tolerance with Hystrix.
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
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β 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}
| 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 |
- 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
- Java 8 or higher
- Maven 3.x
- TMDb API Key (get one at themoviedb.org)
-
Clone the repository
git clone https://github.com/SaketKothari/microservices-communication-discovery-and-fault-tolerance.git cd microservices-communication-discovery-and-fault-tolerance -
Configure TMDb API Key
Update the API key in
movie-info-service/src/main/resources/application.properties:api.key=YOUR_TMDB_API_KEY -
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
-
Verify services are registered
Open http://localhost:8761 to see the Eureka dashboard.
| Method | Endpoint | Description |
|---|---|---|
| GET | /catalog/{userId} |
Get movie catalog for a user |
Example Request:
curl http://localhost:8081/catalog/user123Example 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
}
]| Method | Endpoint | Description |
|---|---|---|
| GET | /movies/{movieId} |
Get movie details by TMDb ID |
| Method | Endpoint | Description |
|---|---|---|
| GET | /ratingsdata/{movieId} |
Get rating for a movie |
| GET | /ratingsdata/users/{userId} |
Get all ratings for a user |
| URL | Description |
|---|---|
http://localhost:8081/hystrix |
Hystrix Dashboard UI |
http://localhost:8081/actuator/hystrix.stream |
Metrics stream |
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
);The @LoadBalanced annotation enables client-side load balancing:
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}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());
}server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false# 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=8083Access the Hystrix Dashboard to monitor circuit breaker status:
- Navigate to
http://localhost:8081/hystrix - Enter the stream URL:
http://localhost:8081/actuator/hystrix.stream - Click "Monitor Stream"
- Java Brains - Tutorial inspiration
- Netflix OSS - Eureka & Hystrix
- The Movie Database (TMDb) - Movie data API
