Microservices Overview :
Microservices is an architectural style in which applications are developed as a collection of small, loosely coupled, and independently deployed services. This approach contrasts with monolithic architecture, where all components are tightly integrated.
Comparison of Monolithic and Microservices Architectures :
Monolithic Application
- A monolithic application consists of multiple components (e.g., App1 and App2) that share:
- Business Logic Layer
- Data Access Layer
- Single Centralized Database
- Characteristics:
- Tightly coupled components
- Difficult to scale individual components
- A failure in one part can affect the entire system
- All components must be deployed together
Microservices Application :
- A microservices-based application is structured into independent microservices, each with its own:
- Database (DB)
- Business Logic
- Characteristics:
- Loosely coupled components
- Independent scalability of each service
- More fault-tolerant (failure in one service does not impact others)
- Easier to update and deploy individual components
Characteristics of Microservices :
- Small and Focused – Each service is designed to handle a specific function.
- Independently Deployable – Services can be updated and deployed without affecting others.
- Organized Around Business Capabilities – Instead of being based on technical layers, services align with business functions.
- Decentralized Data Management – Each microservice has its own database, reducing dependencies.
- Lightweight Communication – Services communicate using lightweight protocols such as HTTP/REST.
Challenges of Microservices :
- Increased Complexity – Managing multiple services is more complex than handling a single monolithic system.
- Distributed System Issues – Challenges such as:
- Latency – Communication between multiple services may introduce delays.
- Load Balancing – Requires careful scaling and balancing.
- Network Reliability – Services must handle network failures gracefully.
- Consistency – Managing data consistency across multiple services can be difficult.
- Managing Transactions and Consistency Across Services – Requires techniques like REST, gRPC, and messaging.
- Monitoring and Logging – A centralized monitoring and logging solution is necessary.
What is Service Discovery ?
In a microservices architecture, each microservice is a standalone application that needs to communicate with others to function as a complete system. Service Discovery helps to
Maintain a record of service locations and Enable services to find and communicate with each other dynamically.
Spring Cloud Eureka :
Eureka is a REST-based service registry primarily used for tracking information about services that need to communicate with each other. It is also known as the Eureka Server.
Services that register themselves in the Eureka server to obtain information about other services are called Eureka Clients.
Eureka is an essential part of service discovery in a microservices architecture.
Basic Architecture :
- Eureka Server (Discovery Service) – Maintains a registry of services and their instances.
- Eureka Clients (Service A, Service B, etc.) – Register themselves with the Eureka Server and query it to discover other services.
- Service Registration & Discovery – Clients register their details (IP, port) and query the Eureka Server for other registered services.
How Eureka Works ?
1. Service Registration
When a service starts, it registers itself with the Eureka Server.
The registration includes metadata such as IP address, port, and status.
2. Heartbeat Mechanism
Once registered, the service sends periodic heartbeats to the Eureka Server.
This renews its lease and informs Eureka that the service is still active.
3. Service Discovery
Other services query the Eureka Server to find the location (IP/port) of registered services.
4. Health Check
Eureka monitors service health to ensure only active services are discoverable.
5. Eviction Process
If a service stops sending heartbeats, Eureka removes it from the registry after the lease expires.
This ensures fault tolerance and prevents failed services from being discovered.
Configuring the Eureka Server :
Application.properties
spring.application.name=discovery-service
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
spring.application.name – Assigns a name to the Eureka Server.
server.port=8761 – Eureka Server typically runs on port 8761.
eureka.client.register-with-eureka=false – Prevents Eureka Server from registering itself.
eureka.client.fetch-registry=false – Disables registry fetching as the server doesn't query itself.
Dependencies for Eureka Server :
To use Eureka Server, include the following dependency in pom.xml:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
Enable Eureka Server :
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
Configuring Eureka Clients :
Each microservice (e.g., Inventory Service) that wants to register with Eureka needs to configure its properties.
Application.properties for a Eureka Client
spring.application.name=inventory-service
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
spring.application.name=inventory-service – Assigns a name to the microservice.
eureka.client.service-url.defaultZone – Specifies the Eureka Server URL.
Dependencies for Eureka Client
To use Eureka Client, add the following dependencies:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
Enable Eureka Client
@SpringBootApplication
@EnableEurekaClient
public class InventoryServiceApplication {
public static void main(String[] args) {
SpringApplication.run(InventoryServiceApplication.class, args);
}
}
Post a Comment
0 Comments