<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies>
dependencies { compile("org.springframework.boot:spring-boot-starter-actuator") }
SpringActuatorApplication.java
.
SpringActuatorApplication
running on Tomcat. You can also see that the actuator endpoints are available at /actuator.
018-11-09 20:00:29.346 INFO 8338 --- [ restartedMain] osbwembedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2018-11-09 20:00:29.354 INFO 8338 --- [ restartedMain] nbjsSpringActuatorApplication : Started SpringActuatorApplication in 9.273 seconds (JVM running for 11.823) 2018-11-09 20:00:29.190 INFO 8338 --- [ restartedMain] osbaeweb.EndpointLinksResolver : Exposing 2 endpoint(s) beneath base path '/actuator'.
ID | Description |
---|---|
auditevents
| Provides information about events
audit for the current application. |
beans | Displays a complete list of all
Spring beans in the application. |
caches | Cache information
|
conditions | Shows the conditions that
were computed for configuration classes and autoconfigurations, and reasons for which they matched or not matched. |
configprops | Displays a list of all
@ConfigurationProperties |
env | Displays properties from
ConfigurableEnvironment. |
flyway | Shows database migrations
Flyway that have been applied. |
health | Displays health information
applications. |
httptrace | Displays trace information
HTTP (default is the last 100 HTTP requests-responses). |
info | Displays additional information
about the application. |
integrationgraph | Spring Integration graph.
|
loggers | Displays and allows
change logger configuration in application. |
liquibase | Shows applied migrations
Liquibase database. |
metrics | Shows information about metrics
for the current application. |
mappings | Displays a list of all paths.
@RequestMapping. |
scheduledtasks | Displays scheduled tasks
(scheduled tasks). |
sessions | Allows to retrieve and delete
user sessions from repositories, Supported Spring Session. Not available when using spring session for jet web applications. |
shutdown | Allows the app to correctly
to finish work. |
threaddump | Displays information about streams.
|
shutdown
. To enable the endpoint, use the following property in the application.properties
file.
management.endpoint.<code><</code>id<code>></code>.enabled
Translator's note: by default, access to all endpoints is only via JMX, there is no access via HTTP to all endpoints (see below).
shutdown
endpoint, we need to make the following entry in the application.properties
file:
management.endpoint.shutdown.enabled=true
info
will be disabled.
management.endpoints.enabled-by-default=false management.endpoint.info.enabled=true
health
and info
endpoints are shown here. Because these are the only endpoints that are accessible by default via http. Access via http to other endpoints is closed by default for security reasons, as they may contain confidential information and, therefore, may be compromised.
application.properties
file.
management.endpoints.web.exposure.include=< ><a href="http://localhost:8080/actuator"></a>
management.endpoints.web.exposure.include= health,info,env
application.properties
, let's go again to http: // localhost: 8080 / actuator
env
also included.
*
sign, as shown below.
management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.include=* management.endpoints.web.exposure.exclude=env
application.properties
file:
management.server.port=-1
management.endpoints.web.exposure.exclude=*
/actuator
by addresses of the form /actuator/{id}
. However, you can change the base path of the /actuator
using the following property in the application.properties
.
management.endpoints.web.base-path
/monitor
instead of /actuator
you can do this as follows:
management.endpoints.web.base-path=/monitor
/monitor/{id}
instead of /actuator/{id}
health
gives the overall status of the application: running and running or not. This is very important for monitoring the state of the application when it is in production. This endpoint can be integrated with monitoring applications and will be very useful for determining the health of applications in real time.
health
endpoint depends on the management.endpoint.health.show-details
property in the application.properties
file.
management.endpoint.health.show-details=never
, then no additional information is displayed. In this case, you will only see the following (this is the default behavior).
management.endpoint.health.show-details=always
, then additional information is displayed to all users. As we see in the answer below, we have information about disk space (diskSpace). If your application is connected to a database, then you will also see information about the state of the database.
management.endpoint.health.show-details=when-authorized
, then additional information will be shown only to authorized users. Authorization can be configured using the management.endpoint.health.roles
property.
DiskspaceHealthIndicator
provides disk space information. If you are using MongoDB, then MongoHealthIndicator
will check the operability of the Mongo database (whether the server is running or not) and display the relevant information. By default, the final status of the app is determined by HealthAggregator
, which simply sorts the list of statuses provided by each HealthIndicator
. The first status in the sorted list is used as the final status of the application.
management.health.defaults.enabled=false
HealthIndicator
, as shown below, for example, to disable disk space checking:
management.health.diskspace.enabled=false
HealthIndicator
will be the name of the bean without the suffix HealthIndicator
.
DiskSpaceHealthIndicator diskspace MongoHealthIndicator mongo CassandraHealthIndicator cassandra DataSourceHealthIndicator datasource
HealthIndicator
provided by Spring Boot Actuator, we can create our own state indicators. To do this, you need to create a class that implements the HealthIndicator
interface, implement its health()
method and return Health
as a response with the appropriate information, as shown below:
import org.springframework.boot.actuate.health.Health; import org.springframework.boot.actuate.health.HealthIndicator; import org.springframework.stereotype.Component; @Component public class CustomHealthIndicator implements HealthIndicator { @Override public Health health() { int errorCode = 0; // In the above line,I am simple assigning zero,but you can call Health check related code like below commented line and that method can return the appropriate code. // int errorCode = performHealthCheck(); if (errorCode != 0) { return Health.down().withDetail("Error Code", errorCode).build(); } return Health.up().build(); } }
info
endpoint provides general information about the application, which it receives from files such as build-info.properties
or git.properties
, or from the properties specified in application.properties
.
META-INF/build-info.properties
. This project information file is created by build time by the purpose of build-info
. Here you can also add an arbitrary number of additional properties.
build-info
target for the spring-boot-maven-plugin
to pom.xm
l.
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.1.0.RELEASE</version> <executions> <execution> <goals> <goal>build-info</goal> </goals> <configuration> <additionalProperties> <encoding.source>UTF-8</encoding.source> <encoding.reporting>UTF-8</encoding.reporting> <java.source>${maven.compiler.source}</java.source> <java.target>${maven.compiler.target}</java.target> </additionalProperties> </configuration> </execution> </executions> </plugin>
info
endpoint again and see the build information as shown below:
info
key in application.properties
, as shown below, and it will be displayed at the end point /info
.
info.application.name=spring-actuator info.application.description=spring boot actuator application info.application.version=0.0.1-SNAPSHOT
beans
endpoint shows all the beans
defined in the Spring container with the following information about each bean:
aliases : scope : type : resource : (), dependencies :
TestController
and injected a component named TestService
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { @Autowired private TestService testService; @GetMapping("/messages") public String getMessage() { return "Hello"; } } import org.springframework.context.annotation.Configuration; @Configuration public class TestService { }
configProps
shows all bins annotated by @ConfigurationProperties
.
@ConfigurationProperties
and, therefore, are displayed at this endpoint.
HttpTraceProperties
, annotated by @ConfigurationProperties
.
env
endpoint provides all environmental information in the following order:
System properties
| depends on JVM (platform independent)
|
---|---|
System environment or variables
surroundings | depends on operating
system (platform dependent) |
Application Level Settings
| defined in
application.properties |
loggers
provides application loggers with information about their configured logging level (configuredLevel) and effective level (effectiveLevel). If for the logger and its parent the configured level is not specified (null), then the effective level will be the level of the root logger.
level
property indicates which logging levels are supported by the logging framework.
/loggers
end point, as shown below:
metrics
shows all the metrics that you can track for your application.
/metrics
, as shown below:
Source: https://habr.com/ru/post/452624/