Skip to main content

Overview

The Customer Service (jea-cliente) manages customer master data including personal information, contact details, and customer lifecycle. It provides customer information to Sales and Orders services. Database: PostgreSQL (cliente-db) Port: Dynamic (configured via Eureka)

Core Entities

Cliente

Customer master data. Location: jea-cliente/src/main/java/com/example/mscliente/entity/Cliente.java:12 Fields:
  • id (Long) - Primary key
  • dni (String) - National identification number (unique, required)
  • nombre (String) - First name (required)
  • apellido (String) - Last name
  • direccion (String) - Address
  • telefono (String) - Phone number
  • email (String) - Email address (unique, required)
  • fechaRegistro (LocalDateTime) - Registration timestamp (auto-generated)
  • activo (Boolean) - Active status (default: true)
Table: cliente Unique Constraints:
  • dni - Each customer must have unique identification
  • email - Each customer must have unique email
Lifecycle Hooks:
  • @PrePersist - Sets fechaRegistro to current timestamp and activo to true

Key Components

Controllers

ClienteController

Location: jea-cliente/src/main/java/com/example/mscliente/controller/ClienteController.java:13 Endpoints:
  • POST /cliente - Create new customer
  • GET /cliente/{id} - Get customer by ID
  • GET /cliente/dni/{dni} - Get customer by DNI
  • GET /cliente - List all customers
  • PUT /cliente/{id} - Update customer
  • DELETE /cliente/{id} - Delete customer
Features:
  • DNI-based customer lookup
  • Full CRUD operations
  • Standard REST conventions

Services

ClienteService

Location: jea-cliente/src/main/java/com/example/mscliente/service/ClienteService.java Implementation: ClienteServiceImpl.java Key Methods:
  • guardarCliente() - Create new customer
  • obtenerClientePorId() - Find by ID
  • obtenerClientePorDni() - Find by DNI
  • obtenerTodosLosClientes() - List all customers
  • actualizarCliente() - Update customer information
  • eliminarCliente() - Delete customer

Repositories

ClienteRepository

Location: jea-cliente/src/main/java/com/example/mscliente/repository/ClienteRepository.java Extends JpaRepository<Cliente, Long> Provides custom query methods:
  • findByDni() - Lookup customer by DNI
  • findByEmail() - Lookup customer by email

Dependencies

Spring Boot Version: 3.1.3 Spring Cloud Version: 2022.0.2 Key Dependencies:
  • spring-boot-starter-data-jpa - JPA/Hibernate
  • spring-boot-starter-web - REST API
  • spring-boot-starter-validation - Bean validation
  • spring-cloud-starter-netflix-eureka-client:4.0.3 - Service discovery
  • spring-cloud-starter-config:4.0.4 - Centralized configuration
  • springdoc-openapi-starter-webmvc-ui:2.0.2 - OpenAPI/Swagger UI
  • postgresql - PostgreSQL driver
  • lombok - Code generation

Configuration

Config File: config-data/jea-cliente-service.yml Key Settings:
server:
  port: ${PORT:${SERVER_PORT:0}}

spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/cliente-db
    username: postgres
    password: ""
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

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

Database Schema

Main Tables:
  • cliente - Customer master data
    • Primary key: id
    • Unique constraints: dni, email
    • Indexes: On dni and email for fast lookups
Note: This service uses PostgreSQL instead of MySQL, demonstrating polyglot persistence in the microservices architecture.

Key Features

  • Customer master data management
  • Unique identification by DNI and email
  • Active/inactive status tracking
  • Registration timestamp tracking
  • DNI-based customer search
  • Email uniqueness validation
  • PostgreSQL database (vs MySQL in other services)
  • OpenAPI/Swagger documentation
  • Eureka service discovery integration
  • Centralized configuration via Config Server

Integration Points

Consumed by:

  • Sales Service - Fetches customer data for sales transactions via ClienteFeign
  • Orders Service - Retrieves customer information for orders via ClienteFeign

APIs Exposed:

  • Customer CRUD operations
  • Customer lookup by ID
  • Customer search by DNI
  • Customer listing

Usage Pattern

Other services reference customers by ID:
// In Venta or Pedido entity
@Column(name = "cliente_id")
private Long clienteId;

@Transient
private Cliente cliente; // Retrieved via Feign client
Services use Feign clients to fetch customer details:
@FeignClient(name = "jea-cliente-service")
public interface ClienteFeign {
    @GetMapping("/cliente/{id}")
    Cliente obtenerPorId(@PathVariable Long id);
    
    @GetMapping("/cliente/dni/{dni}")
    Cliente obtenerPorDni(@PathVariable String dni);
}

Validation Rules

DNI (National ID)

  • Must be unique across all customers
  • Required field
  • Used for customer identification and lookup

Email

  • Must be unique across all customers
  • Required field
  • Used for customer communication

Customer Status

  • activo = true - Customer can make purchases and orders
  • activo = false - Customer is deactivated (soft delete)

Data Privacy Considerations

Personal Information

The Customer service stores sensitive personal data:
  • National identification (DNI)
  • Contact information (phone, email, address)
  • Must comply with data protection regulations

Best Practices

  • Implement access control at API Gateway level
  • Log access to customer data for audit trails
  • Consider encryption for sensitive fields (DNI)
  • Implement data retention policies
  • Support GDPR/data privacy requests (right to be forgotten)

Database Choice: PostgreSQL

Why PostgreSQL?

  1. Data Integrity - Strong ACID compliance for customer data
  2. Advanced Features - Better support for complex queries if needed
  3. JSON Support - Can store additional customer metadata
  4. Polyglot Persistence - Demonstrates flexibility in microservices

Comparison with Other Services

ServiceDatabaseReason
CustomerPostgreSQLStrong data integrity, personal data
SupplierPostgreSQLSimilar to customer requirements
OthersMySQLTransactional data, lighter weight

Future Enhancements

Potential additions:
  • Customer segmentation (VIP, regular, etc.)
  • Customer credit limits
  • Multiple addresses (billing, shipping)
  • Customer notes or comments
  • Purchase history summary (from Sales service)
  • Customer loyalty points
  • Marketing preferences (newsletter opt-in)
  • Customer groups or classifications

Build docs developers (and LLMs) love