AI Hints
Home›Spring Boot Tutorial›Actuator, Health and Info and Packaging a Jar
Spring Boot · Lesson 110 of 120

Health and Info

What is inside.

⏱ 2 min read▶ Video at 2:26:22🎓 Free
Watch this lesson in the Spring Boot courseWatch on YouTube ↗Full Spring Boot course

A single UP is nice, but what exactly is healthy? The database? The disk? And how do we add our own checks?

  • What is healthy?
  • Database, disk
  • Our own checks

Health components

Show components lists every check, that makes up the health. Run it. The database is up. There's enough disk space. And the app is live, and ready for traffic. If any check is down, the overall status is down too.

application.propertiesini
management.endpoint.health.show-components=always
Output
{
  "components":{
    "db":{"status":"UP"},
    "diskSpace":{"status":"UP"},
    "livenessState":{"status":"UP"},
    "ping":{"status":"UP"},
    "readinessState":{"status":"UP"},
    "ssl":{"status":"UP"}
  },
  "groups":["liveness","readiness"],
  "status":"UP"
}
Also uses BookController.java
@RestController
public class BookController {

    @GetMapping("/books")
    public String books() { return "Book list"; }
}
Also uses _curl.sh
curl localhost:8080/actuator/health

Health indicators

Each check is a Health Indicator bean. Spring Boot adds them for the database, the disk, and more. You can write your own, for anything your app depends on. Its name comes from the class name, without Health Indicator.

keyvalue
Each checka HealthIndicator bean
Built indb, diskSpace, ping
Your ownimplement health()
NameBookStock from BookStockHealthIndicator

Your own health check

This indicator counts the books, in the repository. With no books, the shop is down. Otherwise, it's up, with the count as a detail. Run it. Our check appears, under its own path. It's up, with eight books.

BookStockHealthIndicator.javajava
@Component
public class BookStockHealthIndicator implements HealthIndicator {

    private final BookRepository books;

    public BookStockHealthIndicator(BookRepository books) { this.books = books; }

    @Override
    public Health health() {
        long count = books.count();
        if (count == 0) {
            return Health.down().withDetail("books", 0).build();
        }
        return Health.up().withDetail("books", count).build();
    }
}
Output
{"details":{"books":8},"status":"UP"}
Also uses Book.java
@Entity
public class Book {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String title;
    private String author;
    private double price;

    protected Book() {}

    public Book(String title, String author, double price) {
        this.title = title; this.author = author; this.price = price;
    }

    public Long getId() { return id; }
    public String getTitle() { return title; }
    public String getAuthor() { return author; }
    public double getPrice() { return price; }
    public void setPrice(double price) { this.price = price; }

    @Override
    public String toString() { return "Book " + id + ": " + title + " (" + price + ")"; }
}
Also uses BookRepository.java
public interface BookRepository extends JpaRepository<Book, Long> {
}
Also uses data.sql
INSERT INTO book (title, author, price) VALUES
  ('Dune', 'Frank Herbert', 9.99),
  ('The Hobbit', 'J.R.R. Tolkien', 8.99),
  ('Circe', 'Madeline Miller', 10.99),
  ('Children of Dune', 'Frank Herbert', 8.49),
  ('Song of Achilles', 'Madeline Miller', 9.49),
  ('The Fellowship of the Ring', 'J.R.R. Tolkien', 11.99),
  ('Emma', 'Jane Austen', 6.99),
  ('Persuasion', 'Jane Austen', 7.49);
Also uses application.properties
spring.jpa.defer-datasource-initialization=true
management.endpoint.health.show-details=always
Also uses _curl.sh
curl localhost:8080/actuator/health/bookStock

Describing the app

Info shows anything we like. Properties starting with info dot appear in the info endpoint. Run it. The name, version, and owner come back, as JSON. Monitoring tools show this, next to the health.

application.propertiesini
management.endpoints.web.exposure.include=health,info
management.info.env.enabled=true
info.app.name=Bookshop API
info.app.version=1.0.0
info.app.owner=Bookshop team
Output
{"app":{"name":"Bookshop API","version":"1.0.0","owner":"Bookshop team"}}
Also uses BookController.java
@RestController
public class BookController {

    @GetMapping("/books")
    public String books() { return "Book list"; }
}
Also uses _curl.sh
curl localhost:8080/actuator/info

Summary

Health is made of indicators, and you can add your own. Info describes your app, from info properties. Next, packaging the app, as a jar.

Key takeaways
  • show-components
  • HealthIndicator
  • info.app.*
Next up: Packaging a Jar — one file to run anywhere