Skip to content

Rajkumar-Sony/Quick-Learn-Microservices

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Quick Learn - Employee and Company Microservices

Two basic synchronous microservices with CRUD operations:

  • company-service manages companies
  • employee-service manages employees and calls company-service synchronously using RestClient and RequestEntity

Tech Used

  • Spring Boot 3
  • Spring Web
  • Spring Data JPA
  • Spring Validation
  • RestClient
  • Java Record DTOs
  • Lombok
  • PostgreSQL database
  • Docker & Docker Compose
  • API versioning via /api/v1/...

Services

  • Company service: http://localhost:8081/api/v1/companies
  • Employee service: http://localhost:8082/api/v1/employees

Run with Docker Compose (Recommended)

Prerequisites

  • Docker and Docker Compose installed

Build and Start

# Build JAR files
mvn clean package -DskipTests

# Start all services with PostgreSQL databases
docker-compose up --build

The services will be available at:

  • Company service: http://localhost:8081/api/v1/companies
  • Employee service: http://localhost:8082/api/v1/employees

Stop Services

docker-compose down

Run Locally (without Docker)

  1. Start company service
cd company-service
mvn spring-boot:run
  1. Start employee service in another terminal
cd employee-service
mvn spring-boot:run

Note: Make sure PostgreSQL is running locally on port 5432 with username postgres and password postgres.

Project Workflow Diagram

+--------------+        +--------------------+        +---------------------------+
| Client       | -----> | employee-service   | -----> | employee PostgreSQL DB    |
| (Postman)    |  CRUD  | :8082              |  CRUD  |                           |
+--------------+        +---------+----------+        +---------------------------+
                                  |
                                  | synchronous call (RestClient)
                                  v
                       +--------------------+        +---------------------------+
                       | company-service    | -----> | company PostgreSQL DB     |
                       | :8081              |  CRUD  |                           |
                       +--------------------+        +---------------------------+

System Design (Synchronous Communication)

Architecture (inside services)

Client
  |
  v
Employee Controller (8082)
  |     Company Service
         |
         v
      Company Repository --> Company PostgreSQL DB

Synchronous flow (Get Employee)

1) Client -> Employee Controller
   GET /api/v1/employees/{id}

2) Employee Service -> Employee DB
   Read employee record and companyId

3) Employee Service -> CompanyClient -> Company Service
   GET /api/v1/companies/{companyId}
   (RestClient synchronous call)

4) Employee Service
   Combines employee + company data

5) Employee Controller -> Client
   Returns single response payload

Run

  1. Start company service
cd company-service
mvn spring-boot:run
  1. Start employee service in another terminal
cd employee-service
mvn spring-boot:run

Example Flow

  1. Create company
curl -X POST http://localhost:8081/api/v1/companies \
  -H "Content-Type: application/json" \
  -d '{"name":"Acme","address":"NY"}'
  1. Create employee referencing that companyId
curl -X POST http://localhost:8082/api/v1/employees \
  -H "Content-Type: application/json" \
  -d '{"name":"John","email":"john@acme.com","companyId":1}'
  1. Fetch employee (includes company summary from sync call)
curl http://localhost:8082/api/v1/employees/1

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors