Spring Boot 简明教程

Spring Boot - Actuator

Spring Boot Actuator 为监视和管理您的 Spring Boot 应用程序提供了安全端点。默认情况下,所有 actuator 端点都是安全的。本章将详细介绍如何为您的应用程序启用 Spring Boot actuator。

Spring Boot Actuator provides secured endpoints for monitoring and managing your Spring Boot application. By default, all actuator endpoints are secured. In this chapter, you will learn in detail about how to enable Spring Boot actuator to your application.

Enabling Spring Boot Actuator

要为 Spring Boot 应用程序启用 Spring Boot actuator 端点,我们需要在我们的构建配置文件中添加 Spring Boot Starter actuator 依赖项。

To enable Spring Boot actuator endpoints to your Spring Boot application, we need to add the Spring Boot Starter actuator dependency in our build configuration file.

Maven 用户可以在您的 pom.xml 文件中添加以下依赖项。

Maven users can add the below dependency in your pom.xml file.

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

Gradle 用户可以在 build.gradle 文件中添加下面的依赖关系。

Gradle users can add the below dependency in your build.gradle file.

compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator'

在 application.properties 文件中,我们需要禁用执行器端点的安全性。

In the application.properties file, we need to disable the security for actuator endpoints.

management.security.enabled = false

YAML 文件用户可以在 application.yml 文件中添加以下属性。

YAML file users can add the following property in your application.yml file.

management:
   security:
      enabled: false

如果您想使用单独的端口号来访问 Spring 引导执行器端点,请在 application.properties 文件中添加管理端口号。

If you want to use the separate port number for accessing the Spring boot actutator endpoints add the management port number in application.properties file.

management.port = 9000

YAML 文件用户可以在 application.yml 文件中添加以下属性。

YAML file users can add the following property in your application.yml file.

management:
   port: 9000

现在,您可以创建一个可执行 JAR 文件,并使用以下 Maven 或 Gradle 命令运行 Spring 引导应用程序。

Now, you can create an executable JAR file, and run the Spring Boot application by using the following Maven or Gradle commands.

对于 Maven,可以使用以下命令:

For Maven, you can use the following command −

mvn clean install

“BUILD SUCCESS”之后,您可以在目标目录中找到 JAR 文件。

After “BUILD SUCCESS”, you can find the JAR file under the target directory.

对于 Gradle,可以使用以下命令:

For Gradle, you can use the following command −

gradle clean build

“BUILD SUCCESSFUL”之后,您可以在 build/libs 目录中找到 JAR 文件。

After “BUILD SUCCESSFUL”, you can find the JAR file under the build/libs directory.

现在,您可以使用以下命令运行 JAR 文件:

Now, you can run the JAR file by using the following command −

java –jar <JARFILE>

现在,应用程序已在 Tomcat 端口 8080 上启动。请注意,如果您指定了管理端口号,则同一个应用程序将运行在两个不同的端口号上。

Now, the application has started on the Tomcat port 8080. Note that if you specified the management port number, then same application is running on two different port numbers.

started application on tomcat port

下面给出了几个重要的 Spring 引导执行器端点。您可以在 Web 浏览器中输入它们并监控您的应用程序行为。

Some important Spring Boot Actuator endpoints are given below. You can enter them in your web browser and monitor your application behavior.

ENDPOINTS

USAGE

/metrics

To view the application metrics such as memory used, memory free, threads, classes, system uptime etc.

/env

To view the list of Environment variables used in the application.

/beans

To view the Spring beans and its types, scopes and dependency.

/health

To view the application health

/info

To view the information about the Spring Boot application.

/trace

To view the list of Traces of your Rest endpoints.