Skip to main content

Infrastructure Overview

Sistema de Ventas uses Spring Cloud infrastructure components to provide service discovery, API gateway, and centralized configuration management.

API Gateway

Spring Cloud Gateway for routing and authentication

Service Registry

Eureka Server for service discovery

Config Server

Centralized configuration management

API Gateway (jea-gateway-server)

Overview

Spring Cloud Gateway serves as the single entry point for all client requests, providing routing, load balancing, and security.

Technical Configuration

server:
  port: 8085

eureka:
  client:
    serviceUrl:
      defaultZone: ${EUREKA_URI:http://localhost:8090/eureka}
  instance:
    instance-id: ${spring.application.name}:${spring.application.instance_id:${random.value}}

CORS Configuration

Configured to allow requests from the Angular frontend:
spring:
  cloud:
    gateway:
      globalcors:
        corsConfigurations:
          '[/**]':
            allowedOrigins: "http://localhost:4200"
            allowedHeaders: "*"
            allowedMethods:
              - GET
              - POST
              - PUT
              - DELETE

Route Configuration

The gateway defines explicit routes for each microservice:

Authentication Routes (No Filter)

- id: jea-auth-service
  uri: lb://jea-auth-service
  predicates:
    - Path=/auth/**, /usuario/**, /rol/**

Catalogo Service Routes

- id: jea-catalogo-service
  uri: lb://jea-catalogo-service
  predicates:
    - Path=/categoria/**, /producto/**, /imagenes/**
  filters:
    - AuthFilter

Cliente Service Routes

- id: jea-cliente-service
  uri: lb://jea-cliente-service
  predicates:
    - Path=/cliente/**
  filters:
    - AuthFilter

Pagos Service Routes

- id: jea-pagos-service
  uri: lb://jea-pagos-service
  predicates:
    - Path=/pagos/**
  filters:
    - AuthFilter

Venta Service Routes

- id: jea-venta-service
  uri: lb://jea-venta-service
  predicates:
    - Path=/venta/**
  filters:
    - AuthFilter

Pedido Service Routes

- id: jea-pedido-service
  uri: lb://jea-pedido-service
  predicates:
    - Path=/pedido/**
  filters:
    - AuthFilter

Compra Service Routes

- id: jea-compra-service
  uri: lb://jea-compra-service
  predicates:
    - Path=/compra/**
  filters:
    - AuthFilter

Proveedor Service Routes

- id: jea-proveedor-service
  uri: lb://jea-proveedor-service
  predicates:
    - Path=/proveedor/**
  filters:
    - AuthFilter

Gateway Features

Load Balancing

  • Uses Eureka service discovery
  • Client-side load balancing
  • lb:// URI scheme for service lookup
  • Automatic failover

Authentication Filter

  • JWT token validation
  • Applied to all routes except /auth/**
  • Custom AuthFilter implementation
  • Unauthorized request rejection

Service Discovery

  • Dynamic route resolution
  • No hardcoded service endpoints
  • Automatic service detection
  • Health-based routing

Path-Based Routing

  • Routes based on URL patterns
  • RESTful path matching
  • Wildcard support (/**)
  • Multiple paths per service

Request Flow

Gateway Benefits

  • Single Entry Point: Simplifies client configuration
  • Security: Centralized authentication enforcement
  • Abstraction: Hides internal service structure
  • Flexibility: Easy to add/remove services
  • Monitoring: Single point for request logging
  • Rate Limiting: Can add rate limiting filters

Service Registry (jea-registry-server)

Overview

Netflix Eureka Server provides service discovery, enabling microservices to find and communicate with each other dynamically.

Technical Configuration

server:
  port: 8090

eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://localhost:8090/eureka

Service Registration

All microservices register with Eureka on startup:
eureka:
  client:
    serviceUrl:
      defaultZone: ${EUREKA_URI:http://localhost:8090/eureka}
  instance:
    instance-id: ${spring.application.name}:${spring.application.instance_id:${random.value}}
    prefer-ip-address: true

Registered Services

Business Services

  • jea-auth-service
  • jea-catalogo-service
  • jea-cliente-service
  • jea-venta-service
  • jea-compra-service
  • jea-pedido-service
  • jea-pagos-service
  • jea-proveedor-service

Infrastructure Services

  • jea-gateway-server
  • Config Server (if registered)

Service Discovery Flow

Instance Identification

Each service instance has a unique ID:
{spring.application.name}:{random.value}
Example: jea-venta-service:8f3a2b1c

Eureka Dashboard

Access the Eureka dashboard at:
http://localhost:8090
The dashboard shows:
  • Registered services
  • Instance count per service
  • Health status
  • Service metadata
  • Recent registrations/cancellations

High Availability

For production environments:
  • Deploy multiple Eureka servers
  • Configure peer-to-peer replication
  • Update defaultZone with all Eureka instances
eureka:
  client:
    serviceUrl:
      defaultZone: http://eureka1:8090/eureka,http://eureka2:8091/eureka

Service Registry Benefits

Dynamic Discovery

  • No hardcoded service URLs
  • Automatic service detection
  • Supports multiple instances
  • Zero-downtime deployments

Health Management

  • Automatic health checks
  • Failed instance removal
  • Self-healing architecture
  • Service availability tracking

Load Balancing

  • Client-side load balancing
  • Round-robin by default
  • Instance selection strategies
  • Weighted routing support

Resilience

  • Handles service failures
  • Quick failure detection
  • Instance eviction
  • Self-preservation mode

Config Server (jea-config-server)

Overview

Spring Cloud Config Server provides centralized external configuration management for all microservices.

Configuration Repository

All service configurations are stored in YAML files:
config-data/
├── jea-auth-service.yml
├── jea-catalogo-service.yml
├── jea-cliente-service.yml
├── jea-compra-service.yml
├── jea-gateway-service.yml
├── jea-pagos-service.yml
├── jea-pedido-service.yml
├── jea-proveedor-service.yml
├── jea-registry-service.yml
└── jea-venta-service.yml

Configuration Structure

Each service configuration includes:

Server Configuration

server:
  port: ${PORT:${SERVER_PORT:0}}  # Dynamic port assignment

Database Configuration

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/database-name
    username: root
    password: ""
    driver-class-name: com.mysql.cj.jdbc.Driver
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

Service Discovery Configuration

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8090/eureka
  instance:
    hostname: localhost

Database Configuration by Service

  • Auth: auth-jea
  • Catalogo: catalogo-ms
  • Venta: venta-jea
  • Compra: compra-jea
  • Pedido: pedido-jea
  • Pagos: pagos-ms

Environment-Specific Configuration

Supports multiple environments:
  • Development (local)
  • Testing
  • Production
Configuration naming:
{service-name}-{profile}.yml
Example:
  • jea-venta-service.yml (default)
  • jea-venta-service-dev.yml (development)
  • jea-venta-service-prod.yml (production)

Dynamic Port Assignment

All services use dynamic ports by default:
server:
  port: ${PORT:${SERVER_PORT:0}}
  • 0 = random available port
  • Set PORT or SERVER_PORT environment variable for fixed port
  • Gateway uses fixed port 8085
  • Registry uses fixed port 8090

Config Server Benefits

Centralization

  • Single source of truth
  • All configs in one place
  • Easy to manage and update
  • Version control friendly

Environment Management

  • Profile-based configuration
  • Environment-specific settings
  • Easy environment promotion
  • Consistent configuration structure

Dynamic Updates

  • Configuration refresh without restart
  • Spring Cloud Bus integration
  • Webhook support
  • Immediate propagation

Security

  • Encrypted properties support
  • Secret management
  • Access control
  • Audit trail

Infrastructure Architecture

Deployment Considerations

Local Development

  1. Start Eureka Server (Port 8090)
  2. Start Config Server
  3. Start API Gateway (Port 8085)
  4. Start Business Services (dynamic ports)
  5. Start Frontend (Port 4200)

Docker Deployment

services:
  eureka-server:
    ports:
      - "8090:8090"
  
  config-server:
    environment:
      - EUREKA_URI=http://eureka-server:8090/eureka
  
  gateway:
    ports:
      - "8085:8085"
    environment:
      - EUREKA_URI=http://eureka-server:8090/eureka
  
  # Business services...

Environment Variables

  • EUREKA_URI: Eureka server URL
  • PORT or SERVER_PORT: Service port (optional)

Monitoring and Management

Health Endpoints

All services expose Spring Boot Actuator endpoints:
  • /actuator/health: Service health
  • /actuator/info: Service information
  • /actuator/metrics: Service metrics

Circuit Breaker Monitoring

Resilience4j health indicators:
  • Circuit breaker states
  • Failure rates
  • Call statistics

API Documentation

Each service provides Swagger UI:
  • Path: /doc/swagger-ui.html
  • Accessible via Gateway

Next Steps

Database Design

Explore database schemas and initialization

Microservices

Review all business microservices

Build docs developers (and LLMs) love