This microservice is part of the Videogame Library ecosystem. Its responsibility is to consume domain events, process information, and generate analytics about user preferences (favorite genres, most used platforms, preferred release years).
- Hexagonal Architecture: Decoupled and maintainable code.
- Event-Driven: Consumes
FavoriteGameEventfrom Kafka in real-time. - Reactive: Uses
WebClientto communicate non-blocking with the Main Backend. - Persistence: Stores aggregated statistics in a persistent H2 database (file-based).
- REST API: Exposes endpoints for the frontend to consume analytics.
- Dockerized: Ready for container deployment.
- Java 25
- Spring Boot 3.x / 4.x
- Spring Kafka
- Spring Data JPA
- H2 Database (File mode)
- Docker
- Maven
- Listen: The service listens to the
favorite-games-topicin Kafka. - Enrichment: When an event is received (user marks/unmarks favorite), it queries the Main Backend (
http://localhost:8080) to get game details (genres, platforms, year). - Processing: Calculates and updates counters in its local database.
- Query: The Frontend queries this service's REST API to display charts and statistics.
- Java 25 (or compatible with the Docker image)
- Maven
- Docker (optional)
- A Kafka instance running on
localhost:9092 - The Main Backend running on
localhost:8080
mvn spring-boot:runThe application will start on port 8081.
The H2 database will be saved in ~/data/videogame-analytics.mv.db.
-
Build the image:
docker build -t videogame-analytics-service . -
Run the container:
docker run -p 8081:8081 --network host videogame-analytics-service
(Note:
--network hostis used on Linux to access localhost. On Mac/Windows, additional configuration may be required to connect to Kafka on the host).
All endpoints are prefixed with /analytics.
Returns the user's favorite genres sorted by frequency.
- URL:
GET /analytics/{userId}/genres - Response:
[ { "id": 1, "userId": "uuid...", "genre": "RPG", "count": 5 }, { "id": 2, "userId": "uuid...", "genre": "Adventure", "count": 3 } ]
Returns the most used platforms.
- URL:
GET /analytics/{userId}/platforms - Response:
[ { "id": 1, "userId": "uuid...", "platform": "PC", "count": 10 } ]
Returns the distribution of games by release year.
- URL:
GET /analytics/{userId}/release-years - Response:
[ { "id": 1, "userId": "uuid...", "releaseYear": 2023, "count": 8 } ]
The project includes:
- Unit Tests: For business logic (
AnalyticsService). - Integration Tests: For the web layer and persistence (
AnalyticsController).
Run tests:
mvn test