Lecture on Creating Microservices with Spring Boot and Spring Cloud
Introduction
- Today's focus: Building small services, not diving into Spring Cloud yet.
- Understanding microservices challenges by developing them.
- Use Spring Cloud to solve microservices challenges in later sessions.
- Goal: Create multiple microservices, ensure they communicate, and understand issues before introducing Spring Cloud.
Creating Microservices
-
Employee Service
- Create an endpoint
/employee/{id} for GET requests.
- Fetch employee by ID and return employee details JSON.
- Deploy on Tomcat using Spring Boot (embedded Tomcat).
- Employee data stored in
employee database table.
-
Address Service
- Create an endpoint
/address/{employeeId} for GET requests.
- Fetch address by employee ID and return address details JSON.
- Deploy on Tomcat using Spring Boot (embedded Tomcat).
- Address data stored in
address database table.
Implementation Steps
Setting Up Employee Microservice
- Dependencies: Spring Web, Dev Tools, MySQL driver, Spring Data JPA.
- Database: Create
employee table with columns ID, NAME, EMAIL, BLOODGROUP.
- Application Properties: Setup MySQL connection details.
- Entity Class: Create
Employee entity mapping to employee table.
- Repository: Create
EmployeeRepository extending JPA repository.
- Service Layer: Implement
EmployeeService to handle business logic.
- Controller: Create
EmployeeController to handle HTTP requests.
Setting Up Address Microservice
- Dependencies: Similar to Employee Service (Spring Web, Dev Tools, MySQL, Data JPA, ModelMapper).
- Database: Create
address table with columns ID, LANE1, LANE2, STATE, ZIP, EMPLOYEE_ID (foreign key).
- Application Properties: Setup MySQL connection details.
- Entity Class: Create
Address entity mapping to address table.
- Repository: Create
AddressRepository with custom query for finding address by employee ID.
- Service Layer: Implement
AddressService to handle business logic.
- Controller: Create
AddressController to handle HTTP requests.
Key Points During Implementation
- Controller Layer: Avoid direct returns of entities; use response models.
- Service Layer: Business logic and model conversion (using ModelMapper).
- Repository Layer: Custom queries using JPA annotations when necessary.
- ModelMapper: Helps in converting entities to response models.
- ResponseEntity: Controls HTTP status codes and response body.
Upcoming Topics
- Microservices Communication: Using Feign Client for REST calls between services.
- Anti-Patterns: Single database for multiple microservices is not a best practice.
- Spring Cloud: Introduce Spring Cloud features to enhance microservices architecture.
- RestTemplate vs Feign Client: Discussing the shift to Feign Client and HTTP client approaches in microservices.
- Reactive Programming: Introduction to WebClient and WebFlux for non-blocking requests.
Summary
- Created two basic microservices (
Employee and Address) with proper REST endpoints.
- Ensured separation of concerns by dividing responsibilities across controllers, services, and repositories.
- Set the stage for microservices communication and introduction of Spring Cloud in upcoming sessions.
Next Steps: Enhance microservices by enabling inter-service communication using Feign Client and exploring Spring Cloud capabilities.