This guide provides a detailed explanation of the project's architecture, the role of each module, and how they all work together.
This project is a distributed system built on a microservices architecture. It has been intentionally evolved from a single, monolithic application into a collection of smaller, independent services that collaborate to perform the functions of a complete Employee Management System.
Think of it like a company's secure corporate network:
- You have a main gate that checks everyone's ID badge (
gateway-service). - Inside, you have an HR department that manages employee records (
employee-service). - You also have a separate, secure Payroll department (
payroll-service). - And a central company phone book so the departments and the main gate know how to find each other (
discovery-server).
This separation is the core of the project. It demonstrates how to build applications that are more scalable, resilient, and easier to manage.
Three fundamental patterns are at play here:
- Microservices: Instead of one big application, we have multiple small services, each with a single, well-defined responsibility.
- Service Discovery: Services don't have hardcoded knowledge of each other's locations. They use a central registry (the Discovery Server) to find each other by name.
- API Gateway: A single entry point that handles all incoming requests. It is responsible for routing, security, and other cross-cutting concerns, acting as a reverse proxy and security checkpoint.
The project is composed of four distinct modules that run as separate applications.
- Purpose: To act as a dynamic service registry for the entire system.
- Technology: Netflix Eureka.
- How it Works: It maintains a live list of all other services and their network locations. All other modules are clients of this server.
- Purpose: To be the single, unified entry point for all external requests.
- Technology: Spring Cloud Gateway, Eureka Client, JWT.
- Key Responsibilities:
- Routing: It uses the discovery server to dynamically route requests to the correct service (e.g., a request to
/api/employees/**is forwarded to theemployee-service). - Centralized Security: It validates the JWT on all incoming requests (except for login) before they are allowed to proceed to any downstream service.
- Routing: It uses the discovery server to dynamically route requests to the correct service (e.g., a request to
- Purpose: To manage the core, non-sensitive employee data.
- Technology: Spring Web, Spring Data MongoDB, Eureka Client.
- Key Responsibilities:
- CRUD Operations: Provides API endpoints for creating, reading, updating, and deleting employee records.
- Authentication: Exposes the
/auth/loginendpoint to issue JWTs (but does not validate them itself). - Service Discovery: It is a client of the Eureka server, which allows it to find the
payroll-service.
- Purpose: To manage sensitive and structured employee payroll data.
- Technology: Spring Web, Spring Data MongoDB, Eureka Client.
- Key Responsibilities:
- Data Ownership: It is the single source of truth for all payroll-related information.
- Internal API: It exposes an API intended only for internal communication from other trusted services.
Let's trace a request to get an employee's salary:
-
Login: The user sends their credentials to the API Gateway at
http://localhost:8080/auth/login. The gateway routes this request to theemployee-service. Theemployee-servicevalidates the credentials and returns a JWT to the user (via the gateway). -
API Request: The user now makes a
GETrequest to the API Gateway athttp://localhost:8080/api/employees/{id}/salary. They include the JWT in theAuthorization: Bearer <token>header. -
Security Check at the Gateway: The
gateway-serviceintercepts this request. ItsJwtAuthenticationFiltervalidates the JWT. If the token is valid, it allows the request to proceed. -
Routing at the Gateway: The gateway sees that the path
/api/employees/**is mapped to theemployee-service. It asks the Eureka Server for the location ofemployee-serviceand forwards the request there. -
Service Discovery in
employee-service:- The
EmployeeControllerreceives the request and calls theRestTemplateto get salary data. - The
RestTemplatesees the URLhttp://payroll-service/api/payroll/{id}and asks the Eureka Server for the location ofpayroll-service. - Eureka provides the address, and the
RestTemplatemakes a request to thepayroll-service.
- The
-
Response: The
payroll-servicereturns the payroll data to theemployee-service. -
Final Reply: The
employee-servicereturns the data to the API Gateway, which then forwards the final response back to the user.