Fullstack > Architecture > π Microservices
Microservices is an architectural style that structures an application as a collection of small, loosely coupled services, which can be developed, deployed, and scaled independently. Each service in a microservices architecture focuses on a specific business capability and can be built and deployed independently.
β Why Microservices?
Microservices offer several advantages, particularly for complex, large-scale applications:
- π Scalability: Each service can be scaled independently to meet specific demands.
- π‘οΈ Resilience: Failure in one service doesnβt necessarily affect the entire application.
- β‘ Faster Development: Development teams can work on different services independently, accelerating time-to-market.
- π¨ Flexibility: Microservices allow using different technologies and programming languages for each service.
- π§Ή Easier Maintenance: Each service is smaller and focused on specific functionality, making it easier to understand, maintain, and modify.
π§© Microservices Components
1οΈβ£ Service
A service is a core component of the microservices architecture. It performs a specific business function and is typically responsible for a single module or domain of the application. Each service is designed to be:
- π οΈ Autonomous: Can be deployed, updated, or scaled independently.
- π¦ Self-contained: Has its own database, business logic, and API.
- π Communicating over the network: Communicates with other services via APIs, usually REST or messaging queues.
2οΈβ£ API Gateway
The API Gateway is a crucial component in a microservices architecture. It serves as a single entry point for all client requests and is responsible for routing requests to the appropriate service.
Responsibilities include:
- π Request Routing: Directs requests to the appropriate microservice.
- βοΈ Load Balancing: Balances the load across multiple instances of a microservice.
- π Authentication and Authorization: Handles security concerns such as API authentication, rate limiting, etc.
- π§© Aggregation: Combines responses from multiple services into a single response for the client.
3οΈβ£ Service Registry and Discovery
In a microservices architecture, services need to discover each other to communicate. A Service Registry (e.g., Eureka, Consul) keeps track of the available services and their locations.
- π Dynamic discovery: Services register themselves at startup and remove themselves on shutdown.
- βοΈ Load balancing: Service discovery helps the system balance load among instances of the same service.
4οΈβ£ Database per Service
Each microservice has its own database or persistence layer, which can be a different database type based on the serviceβs needs (SQL, NoSQL, etc.).
Benefits include:
- π Data encapsulation: Each service owns its own data and is decoupled from other servicesβ databases.
- π¨ Technology independence: Each service can choose the best database for its use case.
5οΈβ£ Communication Between Services
Services in a microservices architecture communicate with each other via lightweight communication mechanisms, typically HTTP, REST APIs, or messaging queues (e.g., Kafka, RabbitMQ).
- π Synchronous Communication: Direct API calls using HTTP/REST.
- π© Asynchronous Communication: Message brokers and queues for decoupling services and handling long-running tasks.
π Microservices in Fullstack Applications
Backend (Spring Boot + Microservices)
- π οΈ Service: Individual Spring Boot applications that focus on specific business functions.
- π API Gateway: Often implemented using tools like Spring Cloud Gateway or Zuul.
- π Service Registry: Managed using tools like Eureka or Consul.
- ποΈ Database: Each service can have its own database (SQL/NoSQL) depending on its requirements.
- π‘ Communication: REST API calls, messaging systems like Kafka or RabbitMQ.
Frontend (Angular)
- π§© Model: Angular services for communicating with microservices APIs.
- π¨ View: Angular components and templates that interact with the backend.
- π§ Controller: Component classes managing interactions with backend APIs and frontend logic.
π Folder Structure Example
microservices-app/
βββ backend/
β βββ service-a/
β β βββ src/
β β βββ Dockerfile
β β βββ application.yml
β βββ service-b/
β β βββ src/
β β βββ Dockerfile
β β βββ application.yml
β βββ api-gateway/
β β βββ src/
β β βββ application.yml
β βββ service-registry/
β β βββ src/
β β βββ application.yml
β βββ docker-compose.yml
βββ frontend/
β βββ src/app/
β β βββ components/
β β βββ services/
β βββ app.module.ts
β βββ main.ts
βββ docker-compose.yml