Skip to main content
The Purchase-Sale Service (sgivu-purchase-sale) orchestrates vehicle purchase and sale transactions by coordinating data from User, Client, and Vehicle services.

Service Overview

Port

8084

Database

PostgreSQL

Type

Orchestration

Role

Transaction Management

Key Features

  • Transaction orchestration across multiple services
  • Purchase and sale record management
  • Integration with User, Client, and Vehicle services
  • Database migration with Flyway
  • RESTful API with OpenAPI documentation
  • Internal service authentication

Base Configuration

Server Settings

server:
  port: ${PORT:8084}

JPA Configuration

spring:
  jpa:
    open-in-view: false
Disabling open-in-view enforces proper transaction boundaries in this orchestration service.

Flyway Migration

spring:
  flyway:
    enabled: true
    locations: classpath:db/migration
    baseline-on-migrate: ${FLYWAY_BASELINE_ON_MIGRATE:false}
    baseline-version: 0
    validate-on-migrate: true

Database Configuration

spring:
  datasource:
    url: jdbc:postgresql://${DEV_PURCHASE_SALE_DB_HOST:host.docker.internal}:${DEV_PURCHASE_SALE_DB_PORT:5432}/${DEV_PURCHASE_SALE_DB_NAME}
    username: ${DEV_PURCHASE_SALE_DB_USERNAME}
    password: ${DEV_PURCHASE_SALE_DB_PASSWORD}
    driver-class-name: org.postgresql.Driver
  jpa:
    hibernate:
      ddl-auto: validate
    show-sql: true
    properties:
      hibernate:
        format_sql: true
  flyway:
    baseline-on-migrate: true
    clean-disabled: true
Development enables SQL logging with formatting for debugging transactions.
This service maintains its own database for transaction records while querying other services for related data.

Service Integration

Eureka Registration

eureka:
  instance:
    instance-id: ${spring.cloud.client.hostname}:${spring.application.name}:${random.value}
  client:
    service-url:
      defaultZone: ${EUREKA_URL:http://sgivu-discovery:8761/eureka}

Service Discovery Map

This service integrates with all business services:
services:
  map:
    sgivu-auth:
      name: sgivu-auth
      url: ${SGIVU_AUTH_URL:http://sgivu-auth:9000}
    sgivu-client:
      name: sgivu-client
      url: ${SGIVU_CLIENT_URL:http://sgivu-client:8082}
    sgivu-user:
      name: sgivu-user
      url: ${SGIVU_USER_URL:http://sgivu-user:8081}
    sgivu-vehicle:
      name: sgivu-vehicle
      url: ${SGIVU_VEHICLE_URL:http://sgivu-vehicle:8083}
  • sgivu-auth: Token validation and authorization
  • sgivu-client: Client (customer) information for transactions
  • sgivu-user: User information for transaction recording
  • sgivu-vehicle: Vehicle details for purchase/sale records
This is the only service that communicates with all other business services, making it a true orchestration layer.

Internal Service Authentication

service:
  internal:
    secret-key: "${SERVICE_INTERNAL_SECRET_KEY}"
The service uses this secret to authenticate API calls to User, Client, and Vehicle services.

Transaction Orchestration

The Purchase-Sale Service coordinates complex transactions:

Purchase Flow Example

  1. Receive purchase request via API
  2. Validate user (call User Service)
  3. Validate client (call Client Service)
  4. Validate vehicle availability (call Vehicle Service)
  5. Create transaction record in local database
  6. Update vehicle status (call Vehicle Service)
  7. Return transaction confirmation

Sale Flow Example

  1. Receive sale request via API
  2. Validate seller (call User Service)
  3. Validate buyer client (call Client Service)
  4. Validate vehicle ownership (call Vehicle Service)
  5. Create sale record in local database
  6. Transfer vehicle ownership (call Vehicle Service)
  7. Generate sale documentation
Consider implementing the Saga pattern for distributed transaction management with compensating actions.

Observability

Actuator Endpoints

management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: always

Distributed Tracing

management:
  tracing:
    sampling:
      probability: 0.1
  zipkin:
    tracing:
      endpoint: http://sgivu-zipkin:9411/api/v2/spans
Distributed tracing is especially valuable here to trace requests across User, Client, and Vehicle services.

Logging

logging:
  level:
    root: INFO

API Documentation

springdoc:
  swagger-ui:
    url: /docs/purchase-sale/v3/api-docs
    configUrl: /docs/purchase-sale/v3/api-docs/swagger-config
openapi:
  server:
    url: ${OPENAPI_SERVER_URL}

Required Environment Variables

All Environments

VariableDescriptionExample
SERVICE_INTERNAL_SECRET_KEYShared secret for internal APIsyour-secret-key

Development

VariableDescriptionDefault
DEV_PURCHASE_SALE_DB_HOSTDatabase hosthost.docker.internal
DEV_PURCHASE_SALE_DB_PORTDatabase port5432
DEV_PURCHASE_SALE_DB_NAMEDatabase nameRequired
DEV_PURCHASE_SALE_DB_USERNAMEDatabase usernameRequired
DEV_PURCHASE_SALE_DB_PASSWORDDatabase passwordRequired

Production

VariableDescription
PROD_PURCHASE_SALE_DB_HOSTDatabase host
PROD_PURCHASE_SALE_DB_PORTDatabase port
PROD_PURCHASE_SALE_DB_NAMEDatabase name
PROD_PURCHASE_SALE_DB_USERNAMEDatabase username
PROD_PURCHASE_SALE_DB_PASSWORDDatabase password
OPENAPI_SERVER_URLPublic API docs URL

Optional

VariableDescriptionDefault
PORTService port8084
EUREKA_URLEureka server URLhttp://sgivu-discovery:8761/eureka
SGIVU_AUTH_URLAuth service URLhttp://sgivu-auth:9000
SGIVU_CLIENT_URLClient service URLhttp://sgivu-client:8082
SGIVU_USER_URLUser service URLhttp://sgivu-user:8081
SGIVU_VEHICLE_URLVehicle service URLhttp://sgivu-vehicle:8083

API Endpoints

Typical Purchase-Sale Service endpoints:
GET    /api/purchase-sales              - List transactions
GET    /api/purchase-sales/{id}         - Get transaction by ID
POST   /api/purchase-sales/purchase     - Create purchase
POST   /api/purchase-sales/sale         - Create sale
PUT    /api/purchase-sales/{id}         - Update transaction
DELETE /api/purchase-sales/{id}         - Cancel transaction
GET    /api/purchase-sales/client/{id}  - Get client transactions
GET    /api/purchase-sales/vehicle/{id} - Get vehicle history
All endpoints require authentication through the gateway.

Transaction Data Model

Typical transaction entity includes:
  • Transaction ID and type (purchase/sale)
  • User ID (who processed the transaction)
  • Client ID (customer)
  • Vehicle ID
  • Transaction amount and currency
  • Transaction date and status
  • Payment details
  • Notes and metadata

Error Handling

The orchestration service must handle failures gracefully:

Circuit Breaker Pattern

Consider implementing circuit breakers for service calls:
resilience4j:
  circuitbreaker:
    instances:
      userService:
        failure-rate-threshold: 50
        wait-duration-in-open-state: 10000
      clientService:
        failure-rate-threshold: 50
        wait-duration-in-open-state: 10000
      vehicleService:
        failure-rate-threshold: 50
        wait-duration-in-open-state: 10000

Retry Strategy

resilience4j:
  retry:
    instances:
      userService:
        max-attempts: 3
        wait-duration: 1000
Always implement timeout and retry logic when calling downstream services to prevent cascade failures.

Service Communication

RestTemplate / WebClient

The service uses Spring’s HTTP clients to call other services:
// Example: Calling Client Service
ResponseEntity<ClientDto> response = restTemplate.exchange(
    clientServiceUrl + "/api/clients/" + clientId,
    HttpMethod.GET,
    httpEntity,
    ClientDto.class
);

Authentication Headers

Include the internal secret in requests:
HttpHeaders headers = new HttpHeaders();
headers.set("X-Internal-Secret", serviceInternalSecretKey);

Performance Considerations

Caching

Cache frequently accessed data from other services:
spring:
  cache:
    type: caffeine
    caffeine:
      spec: maximumSize=500,expireAfterWrite=10m

Parallel Service Calls

When data from multiple services is needed, call them in parallel:
CompletableFuture<User> userFuture = userService.getUserAsync(userId);
CompletableFuture<Client> clientFuture = clientService.getClientAsync(clientId);
CompletableFuture<Vehicle> vehicleFuture = vehicleService.getVehicleAsync(vehicleId);

CompletableFuture.allOf(userFuture, clientFuture, vehicleFuture).join();

Security

Service-to-Service Authentication

All internal API calls must include the shared secret.

Transaction Authorization

Verify user permissions before allowing transaction creation:
  • Check user role (admin, sales, etc.)
  • Validate transaction limits
  • Audit all operations

Monitoring

Key Metrics

Monitor these metrics for the orchestration service:
  • Transaction success/failure rates
  • Service call latencies (User, Client, Vehicle)
  • Database query performance
  • Error rates by service
  • Circuit breaker states

Alerts

Set up alerts for:
  • High failure rates
  • Service unavailability
  • Slow transaction processing
  • Circuit breaker opens

Configuration Files

  • sgivu-purchase-sale.yml - Base configuration
  • sgivu-purchase-sale-dev.yml - Development overrides
  • sgivu-purchase-sale-prod.yml - Production overrides

User Service

User data provider

Client Service

Client data provider

Vehicle Service

Vehicle data provider

Auth Service

Token validation

Build docs developers (and LLMs) love