Project-based learning through [2024] Java Spring Boot Microservices with k8s, Docker, AWS | Monolithic to Microservices [PART 1].
The job application is responsible for managing jobs. Jobs will be posted by companies, and these companies will have reviews about their workplaces, typically posted by employees who use the website.
- Built
REST APIs
to create endpoints for managing jobs, companies, and reviews resources. - Defined
JPA (Java Persistence API)
entities and repositories for data management withHibernate
, usingH2
for development andPostgreSQL
for production. - Containerized the
spring boot
job application and ran it inside aDocker
container. - Leveraged
Spring Boot Actuator
endpoints to monitor and manage the job application, including/health
,/info
,/metrics
,/loggers
,/beans
, and/shutdown
. - Ran
PostgreSQL
andPGAdmin
insideDocker
containers, and configuredDocker Networks
to enable communication between them. - Utilized
Docker Compose
to manage multi-containerDocker
applications. - Refactored the
monolithic
job application into separatemicroservices
, each running on different ports and using separate databases. - Implements
RESTful
interactions between microservices usingRestTemplate
. - Applied the
DTO (Data Transfer Object)
Pattern to design the structure of the data exposed through the API.
We initially built the backend using monolithic architecture
, and later transformed it into microservice architecture
.
-
Monolithic Architecture
The monolithic form includes
company component
,review component
andjob component
within a single project, all running on one server. Accessing the application from the browser is essentially sending requests to this single server, where the interconnected components interact with a shared database. -
Microservice Architecture
We refactored the monolithic application into three microservices:
company microservice
,job microservice
, andreview microservice
, each responsible for its specific functionality. Each microservice has its own database, runs on its own server, and can independently receive requests.These microservices are separate
Spring Boot
projects, each with its own dependencies configured. For the end user, it will appear as a single application, but on the backend, three microservices will run on different ports:- Company microservice on port 8081
- Job microservice on port 8082
- Review microservice on port 8083
We established inter-service communication between the
job microservice
and thecompany microservice
, using aData Transfer Object (DTO)
to encapsulate data from both services. When a user calls the API to fetch all jobs from thejob microservice
, the response will include detailed company information alongside each job, instead of just the company ID.
Each of the three services (job, company and review) has three main layers: the presentation layer, the service layer, and the data access layer.
-
Presentation Layer
The presentation layer presents data and application features to the user, essentially acting as the frontend view. This layer contains all the
controller
classes, which handle user requests, validate inputs, and pass the data to the service layer. -
Service Layer
The service layer contains the business logic of the application. This is where evaluations, decision-making, and data processing occur. It interacts with both the presentation and data access layers, serving as an intermediary that communicates between them.
-
Data Access Layer
The data access layer houses all
repository
classes responsible for database interactions, such as reading, retrieving, and saving data. This layer manages access to and operations on the underlying databases, which can be either relational orNoSQL
databases.
GET /jobs
- Retrieve all jobsPOST /jobs
- Create a new jobGET /jobs/{id}
- Retrieve a specific jobPUT /jobs/{id}
- Update a specific jobDELETE /jobs/{id}
- Delete a specific job
GET /companies
- Retrieve all companiesPOST /companies
- Create a new companyGET /companies/{id}
- Retrieve a specific companyPUT /companies/{id}
- Update a specific companyDELETE /companies/{id}
- Delete a specific company
GET /reviews?companyId={id}
- Retrieve all reviews for a specific companyPOST /reviews?companyId={id}
- Create a new review for a specific companyGET /reviews/{id}
- Retrieve a specific reviewPUT /reviews/{id}
- Update a specific reviewDELETE /reviews/{id}
- Delete a specific review
- Spring Boot
- Maven