Una arquitectura de microservicios completa construida con Spring Boot y Cloud. Implementa patrones modernos de microservicios como circuit breaker, service discovery, y distributed tracing para crear sistemas altamente escalables y resilientes.
// OrderService.java
@Service
public class OrderService {
private final RestTemplate restTemplate;
private final CircuitBreaker circuitBreaker;
public OrderService(RestTemplate restTemplate, CircuitBreaker circuitBreaker) {
this.restTemplate = restTemplate;
this.circuitBreaker = circuitBreaker;
}
@CircuitBreaker(name = "orderService", fallbackMethod = "fallbackMethod")
public Order createOrder(OrderRequest request) {
// Verificar inventario
InventoryResponse inventory = restTemplate.getForObject(
"http://inventory-service/check/" + request.getProductId(),
InventoryResponse.class
);
if (inventory.isAvailable()) {
// Procesar pago
PaymentResponse payment = restTemplate.postForObject(
"http://payment-service/process",
request.getPaymentDetails(),
PaymentResponse.class
);
if (payment.isSuccessful()) {
Order order = new Order();
order.setStatus(OrderStatus.CONFIRMED);
return orderRepository.save(order);
}
}
throw new OrderException("No se pudo procesar la orden");
}
public Order fallbackMethod(OrderRequest request, Exception ex) {
Order order = new Order();
order.setStatus(OrderStatus.FAILED);
return order;
}
}
# application.yml
spring:
cloud:
gateway:
routes:
- id: order-service
uri: lb://order-service
predicates:
- Path=/api/orders/**
filters:
- name: CircuitBreaker
args:
name: orderServiceCircuitBreaker
fallbackUri: forward:/fallback/orders
- name: RequestRateLimiter
args:
redis-rate-limiter.replenishRate: 10
redis-rate-limiter.burstCapacity: 20
- id: inventory-service
uri: lb://inventory-service
predicates:
- Path=/api/inventory/**
filters:
- name: RequestRateLimiter
args:
redis-rate-limiter.replenishRate: 20
redis-rate-limiter.burstCapacity: 40
- id: payment-service
uri: lb://payment-service
predicates:
- Path=/api/payments/**
filters:
- name: CircuitBreaker
args:
name: paymentServiceCircuitBreaker
fallbackUri: forward:/fallback/payments
Explora la documentación y el código: