Home | 简体中文 | 繁体中文 | 杂文 | 知乎专栏 | Github | OSChina 博客 | 云社区 | 云栖社区 | Facebook | Linkedin | 视频教程 | 打赏(Donations) | About

5.42. Spring Boot Actuator

健康检查、审计、统计和监控

5.42.1. Maven 依赖

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

5.42.2. 与 Spring Boot Actuator 有关的配置

application.properties

跨域配置

		
management.endpoints.web.cors.allowed-origins=https://example.com
management.endpoints.web.cors.allowed-methods=GET,POST		
		
		

5.42.2.1. 禁用HTTP端点

如果您不想通过HTTP公开端点,则可以将管理端口设置为-1,如以下示例所示:

			
management.server.port=-1			
			
			

5.42.3. actuator 接口

		
neo@MacBook-Pro ~ % curl -s http://localhost:8080/actuator | jq
{
  "_links": {
    "self": {
      "href": "http://localhost:8080/actuator",
      "templated": false
    },
    "health": {
      "href": "http://localhost:8080/actuator/health",
      "templated": false
    },
    "health-component": {
      "href": "http://localhost:8080/actuator/health/{component}",
      "templated": true
    },
    "health-component-instance": {
      "href": "http://localhost:8080/actuator/health/{component}/{instance}",
      "templated": true
    },
    "info": {
      "href": "http://localhost:8080/actuator/info",
      "templated": false
    }
  }
}
		
		
		

5.42.4. 健康状态

curl localhost:8080/actuator/health

				
neo@MacBook-Pro ~ % curl -s http://localhost:8080/actuator/health | jq
{
  "status": "UP"
}
		
		

5.42.4.1. 健康状态

详细的健康状态信息

			
management.endpoint.health.show-details=always			
			
			

			
neo@MacBook-Pro ~ % curl -s http://localhost:8080/actuator/health | jq
{
  "status": "UP",
  "details": {
    "diskSpace": {
      "status": "UP",
      "details": {
        "total": 250790436864,
        "free": 23556112384,
        "threshold": 10485760
      }
    }
  }
}			
			
			

5.42.5. 关机

配置文件中加入

		
management.endpoint.shutdown.enabled=true		
		
		
		
curl -X POST localhost:8080/actuator/shutdown	
		
		

5.42.6. info 配置信息

返回 application.properties 文件中定义的 info 配置信息,如:

		
# info端点信息配置
info.app.name=spring-boot-example
info.app.version=v1.0.0		
		
		

		
neo@MacBook-Pro ~ % curl -s http://localhost:8080/actuator/info | jq   
{
  "app": {
    "name": "spring-boot-example",
    "version": "v1.0.0"
  }
}
		
		

5.42.7. 计划任务

		
http://localhost:8080/actuator/scheduledtasks