Skip to content

ibatulanandjp/ecommerce-microservices

Repository files navigation

Micro Marketplace: An E-commerce Microservices Application

Solution Overview

Micro Marketplace is a robust e-commerce application built on a microservices architecture using Spring technologies and other open-source tools.

  • This platform leverages the power of Spring Boot, Netflix Eureka, Spring Cloud Gateway, and KeyCloak for service development, discovery, gateway management, and security, respectively.
  • It incorporates Resilience4j for resilient synchronous communication, and Apache Kafka for seamless event-driven asynchronous communication between services.
  • It offers extensive observability into the application using Micrometer and Zipkin for distributed tracing, and Prometheus and Grafana for monitoring and visualization.

With a focus on scalability, resilience, and real-time interaction, Micro Marketplace provides a robust foundation for creating feature-rich online marketplaces.

Solution Architecture

Solution Architecture

Services

  • Product Service: Responsible for managing product information, including creation, retrieval, and updates. It uses a MongoDB database.
  • Order Service: Handles order management, including creating and retrieving orders. It uses a MySQL database.
  • Inventory Service: Manages products inventory. It also uses a MySQL database.
  • Notification Service: A stateless service responsible for sending notifications to users regarding their orders or other relevant updates.

Major Components

  • Discovery Server: Netflix Eureka is employed for service registration and discovery, allowing microservices to locate and interact with each other dynamically.
  • API Gateway: Spring Cloud Gateway is deployed to serve as the entry point for all external requests, effectively routing traffic to the appropriate microservices.
  • Auth Server: For robust authentication and authorization mechanisms, KeyCloak is used to secure the microservices and protect sensitive data.
  • Circuit Breaker: Resilience4j is used to maintain system reliability by preventing cascading failures in microservices through circuit-breaking mechanisms.
  • Message Broker: Apache Kafka forms the backbone of Micro Marketplace's event-driven architecture, facilitating asynchronous notification for orders.
  • Observability Stack: Distributed tracing is implemented to monitor and troubleshoot requests as they traverse different microservices, using Micrometer for metrics and Zipkin for tracing. Moreover, Prometheus is used for collecting metrics, and Grafana for providing a rich dashboard for visualizing and analyzing application performance data.

Tech Stack Used

Languages & Frameworks Java Spring Boot Spring Cloud
Databases & Message Queue MongoDB MySQL Apache Kafka
API Gateway Spring Cloud Gateway
Service Discovery Netflix Eureka
Circuit Breaker Resilience4J
Security KeyCloak
Observability Micrometer Zipkin Prometheus Grafana
Build & Containerization Maven Docker Jib

Getting Started

Prerequisites

  1. Docker and Docker Compose should be installed.
  2. Docker should be running.

Deployment

  1. Navigate to the project directory:

    cd ecommerce-microservices
  2. Start the containers:

    docker compose up -d
  3. Confirm that the containers are up and running:

    docker ps

Usage

Interacting with Application

  • Getting Credentials from KeyCloak

    • Access the KeyCloak Admin UI at http://localhost:8080/
    • Go to the Realm spring-boot-microservices-realm
    • Go to the Client spring-cloud-client
    • Go the the 'Credentials' section, and get the 'Client Secret'
  • Setup Postman Authentication [Required in the next steps]

    • On the Request page, set Authorization:

      • Type: OAuth 2.0
      • Configure New Token with:
        • Token Name: token
        • Grant Type: Client Credentials
        • Access Token URL: http://keycloak:8080/realms/spring-boot-microservices-realm/protocol/openid-connect/token
        • Client ID: spring-cloud-client
        • Client Secret: <client-secret> (which you copied in the last step from KeyCloak)
      • Click on "Get New Access Token" and then click "Use Token"

      NOTE: For getting the access token from the keycloak container with the local machine, it is required to add a row with 127.0.0.1 keycloak in the file: C:\Windows\System32\drivers\etc\hosts or /etc/hosts

  • Accessing API Endpoints

    • POST /api/product

      • Method: POST
      • Endpoint: http://localhost:8181/api/product
      • Authorization: Use the OAuth 2.0 token fetched, following the previous step.
      • Body:
        {
           "name": "Iphone 15",
           "description": "Apple Iphone 15",
           "price": 1500
        } 
      • Output: Postman Zipkin
    • GET /api/product

    • POST /api/order

      • Method: POST
      • Endpoint: http://localhost:8181/api/order
      • Authorization: Use the OAuth 2.0 token fetched, following the previous step.
      • Body:
        {
           "orderLineItemsDtoList": [
             {
                "skuCode": "iphone_15_pro",
                "price": 2000,
                "quantity": 1
              }
           ]
        }  
      • Output: Postman Zipkin Notification Service Logs

Components UI

  • KeyCloak Admin UI

    • Keycloak UI can be accessed on http://localhost:8080/
    • Realm: spring-boot-microservices-realm KeyCloak Realm
    • Client: spring-cloud-client KeyCloak Client
  • Eureka Dashboard

  • Zipkin UI

  • Prometheus UI

    • Prometheus UI can be accessed on http://localhost:9090/
    • Prometheus Graph Query Graph Query
    • Prometheus Targets Health Targets Health
    • Prometheus Service Discovery Status Service Discovery Status
  • Grafana Dashboard

    • Grafana Dashboard can be accessed on http://localhost:3000/
    • To visualize the application, create a 'Data Source' and import the dashboard using grafana-dashboard.json file.
    • Data Source Data Source
    • Dashboard [Collapsed] Dashboard Collapsed
    • Dashboard Dashboard-1 Dashboard-5

Environment Cleanup

  • To completely stop and remove the containers and other resources (network, volume, etc.), run the following command:
    docker compose down -v