Skip to content

b. Microservices

Each microservice needs two changes: the required Maven dependencies in pom.xml and the actuator endpoint configuration in application.yaml.

1 Dependencies

Add the following dependencies to the pom.xml of each microservice (gateway-service, auth-service, and account-service):

<!-- metrics collection -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<!-- export in prometheus format -->
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
    <scope>runtime</scope>
</dependency>
  • spring-boot-starter-actuator enables production-ready management endpoints, including health checks and metrics.
  • micrometer-registry-prometheus exports those metrics in the text format that Prometheus expects to scrape.

2 Configuration

Configure each service's application.yaml to expose the Prometheus endpoint under the service's own context path. This keeps all endpoints reachable through the single load balancer port without conflicts:

management:
  endpoints:
    web:
      base-path: /gateway/actuator
      exposure:
        include: ['prometheus']

spring:
  cloud:
    gateway:
      metrics:
        enabled: true
management:
  endpoints:
    web:
      base-path: /auth/actuator
      exposure:
        include: ['prometheus']
management:
  endpoints:
    web:
      base-path: /accounts/actuator
      exposure:
        include: ['prometheus']

Why different base paths?

Each microservice uses its own context prefix so the Prometheus endpoint becomes reachable as /gateway/actuator/prometheus, /auth/actuator/prometheus, and /accounts/actuator/prometheus — without port conflicts and without exposing internal ports directly.

The metrics_path of each job in prometheus.yml (Prometheus) must be this base-path followed by /prometheus, character by character; otherwise the scrape gets a 404 and the target shows DOWN:

flowchart LR
    yml["prometheus.yml\nmetrics_path: /accounts/actuator/prometheus"] -->|"GET account:8080"| svc{"base-path is\n/accounts/actuator?"}
    svc -->|yes| up["200 with the metrics\ntarget UP"]
    svc -->|no| down["404 Not Found\ntarget DOWN"]