# arivan-amin/clean-architecture-microservices architecture

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

- Interactive diagram: https://gitdiagram.com/arivan-amin/clean-architecture-microservices
- Repository: https://github.com/arivan-amin/clean-architecture-microservices

## Overview

##### Project Type and Purpose
- The project is a modern microservices back-end solution built using Java and Spring Boot.
- It follows Clean Architecture principles and implements best practices such as CQRS, AOP for audit logging, centralized error handling (via RestControllerAdvice), and service discovery.
- The system is designed to serve as a clean template for microservices with components for API gateway, service discovery, patient management, audit logging, SSO (authentication), notifications, and a shared Core module.

##### File Structure Indicators
- Top-level directories represent distinct microservices: Api-Gateway, Audit-Service, Core, Discovery-Server, Notification-Service, Patient-Service, and SSO-Service.
- The consistent package naming (com.arivanamin.healthcare/backend/…) and separation into subdirectories (application, core, endpoints, storage, configuration) show that each service has its own clean layering.
- Presence of Docker, docker-compose.yml, and additional directories for Grafana and keycloak indicate an environment with extra external components like authentication servers and monitoring tools.
- Testing and architectural rule-checking (ArchUnit tests) are also incorporated, which reinforces the adherence to strict architectural boundaries.

##### Read the README for Impactful Architectural Insights
- The README highlights key system components and the overall data flow: Clients communicate with the API Gateway (secured with rate limiting and security configurations) which in turn communicates with backend services.
- A discovery server (using Eureka) provides dynamic registration of services.
- Audit logging is implemented using Spring AOP to intercept API calls and then send events via Kafka to the Audit Service.
- External components include Keycloak for authentication, MySQL and optionally MongoDB for persistence, and observability tools like Grafana, Prometheus, log systems, and tracing through Tempo.
- Architectural patterns such as CQRS (splitting commands versus queries) and separation of core entities from persistence-specific annotations are clearly in use.

a. Main Components:
- External Clients: Represent user agents (web/mobile/other systems).
- API Gateway: The central entry point for client requests.
- Discovery Server (Eureka): Handles dynamic service registration and discovery.
- Microservices: Include Patient Service, Audit Service, SSO Service, and Notification Service (with Core module supporting shared utilities).
- External Systems: Keycloak (Auth Server), Message Broker (Kafka for event-driven communication), persistence databases (MySQL for transactional data, MongoDB for audit logs), and monitoring tools (Grafana, Prometheus, Loki, Tempo).

b. Relationships and Interactions:
- API Gateway forwarding authenticated requests to different back-end services.
- Registration of microservices with the Discovery Server.
- External systems like Keycloak with API Gateway and SSO Service for authentication/authorization.
- Monitoring flows from services (and Docker volumes) to Grafana, Prometheus, etc.

c. Highlighting Architectural Patterns:

d. Relevant Technologies:
- Mention that the Core module provides shared configurations, domain entities, and utilities used across services.

##### Final Detailing
- Provide detailed labels including technology names (Java, Spring Boot, Maven, Docker) and responsibilities (e.g., API Gateway handles security and rate-limiting, Audit Service captures events via Kafka).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Client Layer"
        CL["External Clients"]:::client
    end

    %% API Layer
    subgraph "API Layer"
        AG["API Gateway (Spring Boot)"]:::microservice
    end

    %% Service Discovery Layer
    subgraph "Service Discovery"
        DS["Discovery Server (Eureka)"]:::infrastructure
    end

    %% Business Services Layer
    subgraph "Business Services"
        PS["Patient Service (REST, Liquibase)"]:::microservice
        AS["Audit Service (Kafka, MongoDB)"]:::microservice
        SS["SSO Service (Keycloak integration)"]:::microservice
        NS["Notification Service"]:::microservice
        CM["Core Module (Shared Config, Domain)"]:::microservice
    end

    %% External Systems Layer
    subgraph "External Systems"
        KC["Keycloak (Auth)"]:::external
        KF["Kafka (Event Broker)"]:::external
        MS["MySQL (Transactional DB)"]:::database
        MG["MongoDB (Audit Logs)"]:::database
        MO["Grafana/Prometheus/Loki/Tempo (Monitoring)"]:::external
    end

    %% Connections
    CL -->|"sends_requests"| AG
    AG -->|"forwards_request"| PS
    AG -->|"forwards_request"| SS
    AG -->|"forwards_request"| NS
    AG -->|"registers_with"| DS

    PS -->|"registers_with"| DS
    AS -->|"registers_with"| DS
    SS -->|"registers_with"| DS
    NS -->|"registers_with"| DS

    PS -->|"sends_audit_event"| KF
    KF -->|"delivers_event"| AS

    PS -->|"stores_data"| MS
    AS -->|"stores_logs"| MG

    AG -->|"auth_via"| KC
    SS -->|"auth_support"| KC

    AG -->|"metrics_logs"| MO
    PS -->|"metrics_logs"| MO
    AS -->|"metrics_logs"| MO
    SS -->|"metrics_logs"| MO
    NS -->|"metrics_logs"| MO

    PS -.->|"uses"| CM
    AS -.->|"uses"| CM
    SS -.->|"uses"| CM
    NS -.->|"uses"| CM

    %% Click Events
    click AG "https://github.com/arivan-amin/clean-architecture-microservices/tree/master/Api-Gateway/src/main/java/com/arivanamin/healthcare/backend/api/gateway"
    click AS "https://github.com/arivan-amin/clean-architecture-microservices/tree/master/Audit-Service/src/main/java/com/arivanamin/healthcare/backend/audit"
    click CM "https://github.com/arivan-amin/clean-architecture-microservices/tree/master/Core/src/main/java/com/arivanamin/healthcare/backend/base"
    click DS "https://github.com/arivan-amin/clean-architecture-microservices/tree/master/Discovery-Server/src/main/java/com/arivanamin/healthcare/backend/discovery"
    click NS "https://github.com/arivan-amin/clean-architecture-microservices/tree/master/Notification-Service/src/main/java/com/arivanamin/healthcare/backend/notification"
    click PS "https://github.com/arivan-amin/clean-architecture-microservices/tree/master/Patient-Service/src/main/java/com/arivanamin/healthcare/backend/patient"
    click SS "https://github.com/arivan-amin/clean-architecture-microservices/tree/master/SSO-Service/src/main/java/com/arivanamin/healthcare/backend/sso"

    %% Styles
    classDef client fill:#B2EBF2,stroke:#00796B,stroke-width:2px;
    classDef microservice fill:#C8E6C9,stroke:#388E3C,stroke-width:2px;
    classDef external fill:#FFCDD2,stroke:#C62828,stroke-width:2px;
    classDef database fill:#FFE082,stroke:#F57F17,stroke-width:2px;
    classDef infrastructure fill:#D1C4E9,stroke:#512DA8,stroke-width:2px;
```
