# sqshq/piggymetrics architecture

> Architecture diagram of the GitHub repository sqshq/piggymetrics, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-07). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sqshq/piggymetrics
- Repository: https://github.com/sqshq/piggymetrics

## Overview

##### Main Components
- Functional Microservices:
– Account Service (deals with account data, validation, and user inputs).
– Statistics Service (performs financial calculations, tracks time series data).
– Notification Service (manages user notification settings and email reminders).
- Infrastructure and Supporting Services:
– Auth Service (handles OAuth2 authentication and authorization using both password and client credentials).
– API Gateway (using Zuul) that routes incoming requests to the proper service.
– Config Service (centralized configuration management using Spring Cloud Config; note the “shared” configuration files shown in the file tree).
– Service Registry (Eureka Server for service discovery and registration).
– Monitoring Service (collects Hystrix metrics, with support from Turbine for aggregating stream data).
– Docker & Deployment: Each microservice is containerized (as indicated by Dockerfiles for each service) and orchestrated via docker-compose.
– Supporting Components: MongoDB instances are set up for persistence in each service, and there is also a RabbitMQ instance for Spring Cloud Bus (as noted in the README under log analysis and configuration sections).

##### Relationships and Interactions
- The Service Registry (Eureka) is used by all services for service discovery and load balancing (via Ribbon). Illustrate how each service registers itself and also makes calls to Eureka.
- Monitoring elements: Include a Monitoring or Hystrix Dashboard component (with Turbine Stream service as its companion) that collects circuit breaker metrics from all services.
- Optionally, include the messaging system (RabbitMQ) used by Spring Cloud Bus to propagate configuration changes and refresh events.

##### Architectural Patterns and Technologies
- Microservice architectural pattern with independently deployable services.
- RESTful API communication between services using Feign clients, with integrated mechanisms such as Ribbon (client-side load balancing) and Hystrix (circuit breaker for fault tolerance).
- Centralized configuration management (Spring Cloud Config) and service discovery via Eureka.
- Security implemented using OAuth2 through a dedicated Auth Service.
- All services are containerized using Docker and orchestrated with docker-compose.
- The separation between configuration (Config Service), authentication (Auth Service), routing (Gateway), and business functionalities (Account, Statistics, Notification) is a key design principle here.

##### Final Touches
- That any dependencies are depicted, including failure safeguards (e.g., circuit breakers in the connections between services as implemented by Hystrix).
- If space allows, include a small inset or sidebar that indicates the flow of Continuous Delivery/CI (Travis CI building Docker images that are then deployed) since that is part of the overall deployment architecture.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer
    subgraph "Presentation Layer"
        CLIENT("User/Client"):::frontend
        GATEWAY("API Gateway (Zuul)"):::service
    end

    %% Business Services Layer
    subgraph "Business Services"
        ACCOUNT("Account Service"):::service
        STAT("Statistics Service"):::service
        NOTIF("Notification Service"):::service
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Services"
        AUTH("Auth Service"):::service
        CONFIG("Config Service"):::infra
        EUREKA("Eureka Registry"):::infra
        MON("Monitoring Dashboard"):::infra
        TURBINE("Turbine Stream"):::infra

        subgraph "Databases"
            MONGO_ACC("MongoDB (Account)"):::database
            MONGO_STAT("MongoDB (Statistics)"):::database
            MONGO_NOTIF("MongoDB (Notification)"):::database
        end

        RABBIT("RabbitMQ"):::messaging
    end

    %% Deployment & Containerization
    subgraph "Containerization & Deployment"
        DC("Docker Compose"):::deployment
        DCD("Docker Compose Dev"):::deployment
        DF_ACC("Account Dockerfile"):::deployment
        DF_STAT("Statistics Dockerfile"):::deployment
        DF_NOTIF("Notification Dockerfile"):::deployment
        DF_AUTH("Auth Dockerfile"):::deployment
        DF_GATE("Gateway Dockerfile"):::deployment
        DF_CONFIG("Config Dockerfile"):::deployment
        DF_EUREKA("Registry Dockerfile"):::deployment
        DF_MON("Monitoring Dockerfile"):::deployment
        DF_TURBINE("Turbine Dockerfile"):::deployment
    end

    %% Connections from Presentation to Business & Infrastructure
    CLIENT -->|"sends request"| GATEWAY
    CLIENT -->|"authenticates"| AUTH

    %% Gateway routes requests to business services
    GATEWAY -->|"routes to"| ACCOUNT
    GATEWAY -->|"routes to"| STAT
    GATEWAY -->|"routes to"| NOTIF

    %% Gateway uses Auth Service for token validation
    GATEWAY -->|"validates token"| AUTH

    %% Business Services intercommunication
    ACCOUNT -->|"calls via Feign"| STAT
    NOTIF -->|"communicates with"| ACCOUNT

    %% Service registration with Eureka and config retrieval
    GATEWAY -->|"registers with"| EUREKA
    ACCOUNT -->|"registers with"| EUREKA
    STAT -->|"registers with"| EUREKA
    NOTIF -->|"registers with"| EUREKA
    AUTH -->|"registers with"| EUREKA

    GATEWAY -->|"fetches config"| CONFIG
    ACCOUNT -->|"fetches config"| CONFIG
    STAT -->|"fetches config"| CONFIG
    NOTIF -->|"fetches config"| CONFIG
    AUTH -->|"fetches config"| CONFIG

    %% Config Service publishes config changes through RabbitMQ
    CONFIG -->|"broadcasts updates"| RABBIT

    %% Each business service interacts with its dedicated database
    ACCOUNT -->|"writes/reads"| MONGO_ACC
    STAT -->|"writes/reads"| MONGO_STAT
    NOTIF -->|"writes/reads"| MONGO_NOTIF

    %% Monitoring: services send metrics
    GATEWAY -->|"sends metrics"| MON
    ACCOUNT -->|"sends metrics"| MON
    STAT -->|"sends metrics"| MON
    NOTIF -->|"sends metrics"| MON
    AUTH -->|"sends metrics"| MON

    %% Turbine Stream collects circuit breaker data via monitoring
    MON -->|"aggregates data"| TURBINE

    %% Class definitions for styling
    classDef service fill:#add8e6,stroke:#000,stroke-width:2px;
    classDef infra fill:#90ee90,stroke:#000,stroke-width:2px;
    classDef frontend fill:#87CEFA,stroke:#000,stroke-width:2px;
    classDef database fill:#FFA500,stroke:#000,stroke-width:2px;
    classDef messaging fill:#dda0dd,stroke:#000,stroke-width:2px;
    classDef deployment fill:#d3d3d3,stroke:#000,stroke-width:2px;

    %% Click events for component mapping
    click ACCOUNT "https://github.com/sqshq/piggymetrics/tree/master/account-service/"
    click STAT "https://github.com/sqshq/piggymetrics/tree/master/statistics-service/"
    click NOTIF "https://github.com/sqshq/piggymetrics/tree/master/notification-service/"
    click AUTH "https://github.com/sqshq/piggymetrics/tree/master/auth-service/"
    click GATEWAY "https://github.com/sqshq/piggymetrics/tree/master/gateway/"
    click CONFIG "https://github.com/sqshq/piggymetrics/tree/master/config/"
    click EUREKA "https://github.com/sqshq/piggymetrics/tree/master/registry/"
    click MON "https://github.com/sqshq/piggymetrics/tree/master/monitoring/"
    click TURBINE "https://github.com/sqshq/piggymetrics/tree/master/turbine-stream-service/"
    click DF_ACC "https://github.com/sqshq/piggymetrics/tree/master/account-service/Dockerfile"
    click DF_STAT "https://github.com/sqshq/piggymetrics/tree/master/statistics-service/Dockerfile"
    click DF_NOTIF "https://github.com/sqshq/piggymetrics/tree/master/notification-service/Dockerfile"
    click DF_AUTH "https://github.com/sqshq/piggymetrics/tree/master/auth-service/Dockerfile"
    click DF_GATE "https://github.com/sqshq/piggymetrics/tree/master/gateway/Dockerfile"
    click DF_CONFIG "https://github.com/sqshq/piggymetrics/tree/master/config/Dockerfile"
    click DF_EUREKA "https://github.com/sqshq/piggymetrics/tree/master/registry/Dockerfile"
    click DF_MON "https://github.com/sqshq/piggymetrics/tree/master/monitoring/Dockerfile"
    click DF_TURBINE "https://github.com/sqshq/piggymetrics/tree/master/turbine-stream-service/Dockerfile"
    click DC "https://github.com/sqshq/piggymetrics/blob/master/docker-compose.yml"
    click DCD "https://github.com/sqshq/piggymetrics/blob/master/docker-compose.dev.yml"
    click MONGO_ACC "https://github.com/sqshq/piggymetrics/tree/master/mongodb/"
    click MONGO_STAT "https://github.com/sqshq/piggymetrics/tree/master/mongodb/"
    click MONGO_NOTIF "https://github.com/sqshq/piggymetrics/tree/master/mongodb/"
```
