What is Spring Boot Actuator

The Spring Boot Actuator module provides production-level functions such as health check, auditing, indicator collection, AND HTTP tracking, helping us monitor and manage Spring Boot applications, Bean loading, environment variables, log information, thread information, and JVM heap information. This module is a collection of internal application information to expose to the external module, the above functions can be accessed through HTTP and JMX.

Because of its ability to expose internal information, the Actuator can also be integrated with external application monitoring systems (Prometheus, Graphite, DataDog, Influx, Wavefront, New Relic, etc.). These monitoring systems provide excellent dashboards, graphics, analytics and alerts that help you monitor and manage your applications through a unified and friendly interface

Important Endpoint resolution

5.1 / health endpoint

The /health endpoint aggregates your application’s health metrics to check the health of your application. Application health information exposed by endpoints depends on:

Management. The endpoint. Health. The show – the details = always this attribute configuration can use one of the following values:

Don’t show detailed information, the state of the up or down, the default configuration According to the above configuration, configuration as always, we start the project, visit http://localhost:8080/actuator/health port, you can see the message:

Does it feel like health information is a little sparse? Don’t worry, that’s because we created a basic Demo project that didn’t rely on many components.

The /health endpoint has many automatically configured health indicators: components such as Redis, RabbitMQ, DB, and so on. These health indicators are automatically assembled to collect information when your project has dependencies on corresponding components. The diskSpace node information above is the DiskSpaceHealthIndicator in action.

The above screenshots are taken from the official documentation

This is the/Health endpoint information for another of my projects.

If one component is abnormal, the overall status of the application service is Down. We can also configure to disable health monitoring for a component.

Management. Health. Mongo. Enabled: false or disable all automatic configuration of health indicators:

management.health.defaults.enabled: False ⭐ Customize HealthIndicator You can also customize a HealthIndicator by implementing the HealthIndicator interface or AbstractHealthIndicator class.

/ * *

  • @author Richard_yyf
  • @ 2020/1/16 version 1.0

*/ @Component public class CustomHealthIndicator extends AbstractHealthIndicator {

@override protected void doHealthCheck(health.builder Builder) throws Exception {// Use Builder to create Health status information With an exception, status will be set to DOWN, The exception message is logged builder.up().withdetail ("app", "this project is healthy ").withdetail ("error", "Nothing, I'm very good"); }Copy the code

} Final effect:

5.2 / metrics endpoint

The /metrics endpoint is used to return various important metrics for the current application, such as memory information, thread information, garbage collection information, Tomcat, database connection pool, and so on.

{ “names”: [ “tomcat.threads.busy”, “jvm.threads.states”, “jdbc.connections.active”, “jvm.gc.memory.promoted”, “http.server.requests”, “hikaricp.connections.max”, “hikaricp.connections.min”, “jvm.memory.used”, “jvm.gc.max.data.size”, “jdbc.connections.max”, …. ] } Unlike 1.x, the interface does not show specific indicators, but only displays a list of indicators. To obtain detailed information about a metric, we can request specific metric information like this:

{MetricName} for example, I visit http://localhost:8080/actuator/metrics/ / physical/metrics/JVM memory. Max, return information is as follows:

You can also view individual sections using Query Param. For example you can access/physical/metrics/JVM memory. Max? Tag = id: Metaspace. The result:

5.3 / loggers endpoint

The /loggers endpoint exposes information about all loggers configured internally for our program. We visit /actuator/loggers and you can see,

You can also access a single logger in the following way,

http://localhost:8080/actuator/loggers/ {name} now I visit the root logger, for example, http://localhost:8080/actuator/loggers/root

{“configuredLevel”: “INFO”, “effectiveLevel”: “INFO”} ⭐ Change the log level at runtime/loGGERS endpoints For example, we can change the log level of the root Logger in the following way. We only need to initiate a POST request URL for http://localhost:8080/actuator/loggers/root, POST a message is as follows:

{ “configuredLevel”: “DEBUG” }

Think about how useful this feature is. If, in production, you want your application to output Debug messages so that you can diagnose exceptions, you can do this without restarting the application.

If you want to reset to the default value, change value to null

5.4 / info endpoint

The /info endpoint can be used to display information about your application. I understand it to be the basic information of the program. And you can customize it to your needs in the application.properties configuration file (by default, this endpoint will only return an empty JSON content). :

Info.app. name= skeleton-test-demo info.app.encoding=UTF-8 info.app.java.source=1.8 info.app.java.target=1.8 In Maven Apply to the maven project you can directly use the following values of the properties of info. App. Encoding = @ project. Build. SourceEncoding @ info. App. Java. Java source = @. Version @ Info. App. Java. Target = @ Java version @ startup project, visit http://localhost:8080/actuator/info:

{” app “: {” encoding” : “utf-8”, “Java” : {” source “:” 1.8.0 comes with _131 “, “target” : “1.8.0 comes with _131”}, “name” : “actuator-test-demo” } }

5.5 / beans endpoint

The/Beans endpoint returns the aliases, types, singletons, dependencies, and so on of all beans in the Spring container.

Visit http://localhost:8080/actuator/beans to return as follows:

5.6 / heapdumps endpoint

Visit: http://localhost:8080/actuator/heapdump will automatically generate a Jvm heap heapdumps file. You can open this file to view a memory snapshot using VisualVM, the Jvm monitoring tool that comes with the JDK.

5.7 / threaddump endpoint

This endpoint is one that I personally find particularly useful for looking at threads during routine problem location. It mainly shows the thread name, thread ID, thread status, whether to wait for the lock resource, thread stack and other information. It just might not be intuitive to look at. Visit http://localhost:8080/actuator/threaddump to return to the following:

5.8 / shutdown endpoint

This endpoint belongs to the operation control class endpoint and can gracefully close the Spring Boot application. To use this feature you first need to enable it in the configuration file:

Management. The endpoint. Shutdown. Enabled = true due to shutdown interface default only supports the POST request, we start the Demo program, To http://localhost:8080/actuator/shutdown launched a POST request. Return message:

{ “message”: “Shutting down, bye…” } The application is then closed.

As the operation of opening and closing applications itself is very dangerous, we need to add some protection mechanisms when using them online, such as customizing the endpoint path of the Actuator and integrating Spring Security for Security verification. (Don’t open this endpoint unless it’s absolutely necessary.)