Loslegen
Javaneer
Zurück zur Stufe
Modul 8·Produktion & modernes Spring

Observability mit Actuator

Health, Metriken und Einblick in eine laufende App: Spring-Boot-Actuator-Endpunkte und Micrometer-Metriken, angebunden an Prometheus.

15 Min. LesezeitFortgeschritten

Deutsche Übersetzung in Arbeit

Diese Lektion ist noch nicht ins Deutsche übersetzt und wird daher auf Englisch angezeigt. Der Rest der Seite ist vollständig lokalisiert.

Auf dieser Seite

A demo runs on your laptop where you can watch the console. A production service runs on a machine you can't see, serving users you'll never meet - and when it misbehaves at 3am, you need it to tell you what's wrong. Observability is that: the ability to understand a running system from the outside. Spring Boot ships most of it in one dependency.

Actuator: the built-in ops layer

Spring Boot Actuator adds production-ready endpoints under /actuator that expose the app's internals - health, metrics, environment, mappings, and more - without you writing any of it:

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

With it on the classpath you immediately get, for example:

curl localhost:8080/actuator/health     # {'status':'UP'} - is the app healthy?
curl localhost:8080/actuator/metrics    # a list of every metric being collected
curl localhost:8080/actuator/info       # build/version info you choose to expose

The health endpoint is the one your orchestrator (Kubernetes, a load balancer) polls to decide whether to send traffic to this instance or restart it. Health is composed from indicators - the database, disk space, and any custom checks you add - so UP means the things the app depends on are reachable, not merely that the process is alive.

Actuator exposes internals - lock it down

Endpoints like /actuator/env and /actuator/heapdump reveal configuration and memory. By default only health is exposed over HTTP; expose more deliberately (management.endpoints.web.exposure.include) and protect the actuator endpoints with security (or bind them to a separate, internal-only port) in production.

Metrics with Micrometer

Behind /actuator/metrics is Micrometer - a vendor-neutral metrics facade (think 'SLF4J, but for metrics'). Your code, and Spring itself, record measurements against Micrometer; Micrometer then exports them to whatever monitoring system you use - Prometheus, Datadog, CloudWatch - by adding one registry dependency:

<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

Now /actuator/prometheus serves metrics in Prometheus' text format, which Prometheus scrapes on a schedule and stores as time series you graph in Grafana. You get JVM memory, GC, HTTP request latency, and datasource pool stats for free, and you can add your own:

// count something meaningful to your domain
meterRegistry.counter('bookvault.loans.created').increment();
The dashboard of a car

You don't open the hood at speed to check the engine. The dashboard surfaces what matters - speed, fuel, temperature, warning lights - continuously, from the outside. Actuator + Micrometer are your service's dashboard: health as the warning lights, metrics as the gauges. You watch the dashboard, and only open the hood (the logs, a debugger) when a light comes on.

The three pillars

Observability rests on metrics (numeric trends - 'latency is climbing'), logs (discrete events - 'this request failed, here's why'), and traces (one request across services - 'the time went here'). Actuator/Micrometer give you the metrics pillar; the next lessons and the tracing lesson cover the other two.

Is it healthy, really?

Your service's process is running, but its database is unreachable, so every request fails. A naive health check that just returns 'UP' because the process is alive would keep traffic flowing to a broken instance. Explain how Actuator's health endpoint avoids this, and what an orchestrator does with the result.

What does Spring Boot Actuator provide, and what is Micrometer's role?

Key takeaways

  • Observability is understanding a running system from the outside - essential once the app leaves your laptop.
  • Spring Boot Actuator adds production endpoints (/actuator/health, /metrics, /info, ...) with no code.
  • The health endpoint is composed from indicators (DB, disk, custom) and is what orchestrators poll to route or restart.
  • Micrometer is a vendor-neutral metrics facade; add a registry (e.g. Prometheus) to export JVM, HTTP, and custom metrics.
  • Actuator endpoints expose internals - restrict exposure and secure them in production.
  • Metrics, logs, and traces are the three pillars; Actuator/Micrometer cover metrics.
War diese Lektion hilfreich?
Diese Seite auf GitHub bearbeiten