Microservices Basics
1. What is Microservices?
Microservices is an architecture where application is divided into small independent services communicating via REST or messaging.
2. Monolithic vs Microservices
| Monolithic | Microservices |
|---|---|
| Single application | Multiple small services |
| Single deployment | Independent deployment |
| Tightly coupled | Loosely coupled |
3. Advantages of Microservices
- Independent scaling
- Fault isolation
- Technology flexibility
- Fast deployment
Service Discovery
4. What is Eureka Server?
Eureka is a service registry where microservices register themselves.
5. Register Client with Eureka
@EnableEurekaClient
@SpringBootApplication
public class OrderServiceApp {
public static void main(String[] args){
SpringApplication.run(OrderServiceApp.class,args);
}
}
6. Why Service Discovery?
Helps dynamic location of services instead of hard-coded URLs.
API Gateway
7. What is API Gateway?
Single entry point for all microservices.
8. Spring Cloud Gateway Example
spring:
cloud:
gateway:
routes:
- id: order-service
uri: http://localhost:8081
predicates:
- Path=/order/**
9. Benefits of API Gateway
- Centralized routing
- Security
- Logging
- Rate limiting
Load Balancing
10. What is Load Balancer?
Distributes traffic among multiple instances.
11. Spring Cloud LoadBalancer
@LoadBalanced
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
Inter-Service Communication
12. RestTemplate vs WebClient
| RestTemplate | WebClient |
|---|---|
| Synchronous | Asynchronous |
| Blocking | Non-blocking |
13. Feign Client
@FeignClient(name="user-service")
public interface UserClient{
@GetMapping("/users/{id}")
User getUser(@PathVariable int id);
}
Centralized Configuration
14. Config Server
Externalizes configuration from microservices.
15. Config Server Setup
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApp { }
Fault Tolerance
16. Circuit Breaker
Prevents cascading failure.
17. Resilience4j Example
@CircuitBreaker(name="orderService", fallbackMethod="fallback")
public String callService(){
return restTemplate.getForObject(url,String.class);
}
public String fallback(Exception e){
return "Service down";
}
Security
18. OAuth2 & JWT
Used for authentication and authorization.
19. JWT Flow
- User logs in
- JWT generated
- Client sends token with request
- Service validates token
Distributed Tracing
20. Sleuth & Zipkin
Track request across multiple services.
21. Trace Example
Request ID is passed to all services.
Monitoring
22. Actuator
/actuator/health
/actuator/metrics
23. Prometheus & Grafana
Used for metrics visualization.
Messaging
24. Kafka vs RabbitMQ
| Kafka | RabbitMQ |
|---|---|
| High throughput | Message broker |
| Stream processing | Queue based |
25. Kafka Producer Example
kafkaTemplate.send("topic","Hello Message");
Deployment
26. Docker in Microservices
Each service runs inside container.
27. Dockerfile Example
FROM openjdk:17
COPY target/app.jar app.jar
ENTRYPOINT ["java","-jar","app.jar"]
28. Kubernetes
Used for container orchestration.
Best Practices
29. Database per Service
Each microservice should have its own database.
30. Stateless Services
Services should not store session state.
Source: sureshtechlabs.com