Two basic synchronous microservices with CRUD operations:
company-servicemanages companiesemployee-servicemanages employees and callscompany-servicesynchronously usingRestClientandRequestEntity
- 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/...
- Company service:
http://localhost:8081/api/v1/companies - Employee service:
http://localhost:8082/api/v1/employees
- Docker and Docker Compose installed
# Build JAR files
mvn clean package -DskipTests
# Start all services with PostgreSQL databases
docker-compose up --buildThe services will be available at:
- Company service:
http://localhost:8081/api/v1/companies - Employee service:
http://localhost:8082/api/v1/employees
docker-compose down- Start company service
cd company-service
mvn spring-boot:run- Start employee service in another terminal
cd employee-service
mvn spring-boot:runNote: Make sure PostgreSQL is running locally on port 5432 with username postgres and password postgres.
+--------------+ +--------------------+ +---------------------------+
| Client | -----> | employee-service | -----> | employee PostgreSQL DB |
| (Postman) | CRUD | :8082 | CRUD | |
+--------------+ +---------+----------+ +---------------------------+
|
| synchronous call (RestClient)
v
+--------------------+ +---------------------------+
| company-service | -----> | company PostgreSQL DB |
| :8081 | CRUD | |
+--------------------+ +---------------------------+
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
- Start company service
cd company-service
mvn spring-boot:run- Start employee service in another terminal
cd employee-service
mvn spring-boot:run- Create company
curl -X POST http://localhost:8081/api/v1/companies \
-H "Content-Type: application/json" \
-d '{"name":"Acme","address":"NY"}'- 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}'- Fetch employee (includes company summary from sync call)
curl http://localhost:8082/api/v1/employees/1