# anjeyy/grpc-sample architecture

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

- Interactive diagram: https://gitdiagram.com/anjeyy/grpc-sample
- Repository: https://github.com/anjeyy/grpc-sample

## Overview

- grpc-proto – houses the shared .proto files to define the gRPC services and message contracts;
- grpc-server – implements the server-side logic (using grpc-spring-boot-starter) with REST endpoints, controllers, service and DAO layers, plus configuration for exception handling and interceptors;
- grpc-client – serves as the client-side consumer of the gRPC services, with its own APIs to interface with the server.

##### Main Components
a. Modules:
- Proto Module: Contains the .proto files (e.g., DocumentService.proto, HelloService.proto). This is the common contract used by both server and client.
- gRPC Server: Implemented with Spring Boot. It includes REST controllers (e.g., DocumentController) and gRPC service implementations (e.g., GrpcDocumentService, GrpcHelloService). It also includes application configuration, global exception handling, interceptors, and a DAO/data-access layer (document repository, entity, and SQL setup file).
- gRPC Client: Also implemented with Spring Boot that calls the gRPC server. It comprises API controllers and client service classes (e.g., GrpcDocumentServiceClient, GrpcHelloServiceClient).
b. Supporting Infrastructure:
- Deployment Configurations: Kubernetes YAML files for both client and server, Dockerfiles, and scripts for deployment and service removal.
- Build/CI Environment: GitHub workflows under .github for building and pushing Docker images along with Maven wrapper files.
- Configuration Files: Application configuration (application.yml, application-kubernetes.yml) to adjust environment-based behavior.

b. Server component with its internal layers:
- API Layer (Controllers providing REST endpoints for testing or non-gRPC interactions)
- Service Layer (Business logic and interaction with the gRPC framework)
- Data Access Layer (DAO, Repository, using a relational database as indicated by the SQL scripts)
- Infrastructure Layer (Global exception handling, interceptors, configuration setups)
c. Client as the consumer of the gRPC Server:
- Client API module to convert external requests into gRPC calls.
- gRPC Client Services that call the server endpoints using the shared .proto definitions.
d. Deployment elements:
- Docker containers wrapping both the Server and Client modules.
- Kubernetes configuration showing separate load balancers, one for each module. Note the direction for dynamic gRPC load balancing for the client when making calls to the server.

##### Important Architectural Patterns and Technologies
a. Modular Design: Clear separation between service contracts (proto), server implementation, and client consumption.
b. Layered Architecture (especially in the server module): Controllers, Services, and Data Access.
c. Use of gRPC and REST together, enabled by grpc-spring-boot-starter Spring Boot integrations.
d. CI/CD Integration: Automated builds and container deployment via GitHub workflows and Maven wrapper.
e. Containerization and Orchestration via Docker and Kubernetes.

##### What
– Proto Module
– gRPC Server (with sub-layers: API controllers, service layer, data access)
– gRPC Client (including its API layer)
– External systems (e.g., database, load balancers)
– Shared dependency of Server and Client on the Proto Module.
– Requests flowing from the Client to the Server.
– Data flow within the Server from Controllers to Business Services and finally to the Database.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Shared Module
    PM["Proto Module"]:::proto

    %% gRPC Server Module
    subgraph "gRPC Server"
        SSR["REST Controllers"]:::server
        SSV["Service Layer"]:::server
        SDA["Data Access Layer"]:::server
        SINF["Infrastructure"]:::server
    end

    %% gRPC Client Module
    subgraph "gRPC Client"
        CAC["API Controllers"]:::client
        CSS["Client Service Layer"]:::client
        CINF["Infrastructure"]:::client
    end

    %% Deployment & CI/CD Components
    subgraph "Deployment & CI/CD"
        DEPLOY["Deployment Configurations"]:::deploy
        CI["Build/CI Environment"]:::cicd
        CONFIG["Configuration Files"]:::config
    end

    %% External Database
    DB(("Database")):::database

    %% Relationships between modules and shared components
    PM -->|"used_by"| SSR
    PM -->|"used_by"| CAC

    %% Internal Server Flow: REST API to Business Service to Data Access to Database
    SSR -->|"calls"| SSV
    SSV -->|"accesses"| SDA
    SDA -->|"persists_data"| DB

    %% Client interactions: API Controllers to Client Service and gRPC calls to Server
    CAC -->|"invokes"| CSS
    CSS -->|"calls_RPC"| SSV
    CAC -->|"gRPC_call"| SSR
    CINF ---|"handles"| CAC

    %% Deployment & CI/CD connections
    DEPLOY --- SSR
    DEPLOY --- CAC
    CI --- DEPLOY
    CONFIG --- SSR
    CONFIG --- CAC

    %% Click Events for Component Mapping
    click PM "https://github.com/anjeyy/grpc-sample/tree/main/grpc-proto/src/main/proto"
    click SSR "https://github.com/anjeyy/grpc-sample/blob/main/grpc-server/src/main/java/com/github/anjeyy/api/controller/DocumentController.java"
    click SSV "https://github.com/anjeyy/grpc-sample/tree/main/grpc-server/src/main/java/com/github/anjeyy/api/service"
    click SDA "https://github.com/anjeyy/grpc-sample/tree/main/grpc-server/src/main/java/com/github/anjeyy/api/dao"
    click SINF "https://github.com/anjeyy/grpc-sample/blob/main/grpc-server/src/main/java/com/github/anjeyy/infrastructure/config/GlobalRestAdvice.java"
    click CAC "https://github.com/anjeyy/grpc-sample/tree/main/grpc-client/src/main/java/com/github/anjeyy/client/api/controller"
    click CSS "https://github.com/anjeyy/grpc-sample/tree/main/grpc-client/src/main/java/com/github/anjeyy/client/api/service"
    click CINF "https://github.com/anjeyy/grpc-sample/blob/main/grpc-client/src/main/java/com/github/anjeyy/client/infrastructure/GlobalExceptionHandler.java"
    click DEPLOY "https://github.com/anjeyy/grpc-sample/tree/main/deployment"
    click CI "https://github.com/anjeyy/grpc-sample/tree/main/.github/workflows"
    click CONFIG "https://github.com/anjeyy/grpc-sample/tree/main/grpc-server/src/main/resources"

    %% Styles
    classDef proto fill:#ffeb3b,stroke:#333,stroke-width:2px;
    classDef server fill:#3498db,stroke:#fff,stroke-width:2px;
    classDef client fill:#2ecc71,stroke:#fff,stroke-width:2px;
    classDef deploy fill:#e67e22,stroke:#fff,stroke-width:2px;
    classDef cicd fill:#9b59b6,stroke:#fff,stroke-width:2px;
    classDef config fill:#e74c3c,stroke:#fff,stroke-width:2px;
    classDef database fill:#95a5a6,stroke:#333,stroke-width:2px;
```
