Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About
知乎專欄多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者”

5.40. Spring Boot Actuator

健康檢查、審計、統計和監控

5.40.1. Maven 依賴

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

5.40.2. 與 Spring Boot Actuator 有關的配置

application.properties

跨域配置

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

5.40.2.1. 禁用HTTP端點

如果您不想通過HTTP公開端點,則可以將管理連接埠設置為-1,如以下示例所示:

			
management.server.port=-1			
			
			

5.40.2.2. 安全配置

			
security.basic.enabled=true
security.basic.path=/admin    #針對/admin路徑進行認證
security.user.name=admin     #認證使用的用戶名
security.user.password=password   #認證使用的密碼
management.security.roles=SUPERUSER

management.port=11111   #actuator暴露介面使用的連接埠,為了和api介面使用的連接埠進行分離
management.context-path=/admin   #actuator暴露介面的首碼
management.security.enabled=true   #actuator是否需要安全保證

endpoints.metrics.sensitive=false   #actuator的metrics介面是否需要安全保證
endpoints.metrics.enabled=true

endpoints.health.sensitive=false  #actuator的health介面是否需要安全保證
endpoints.health.enabled=true			
			
			

5.40.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.40.4. 健康狀態

curl localhost:8080/actuator/health

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

5.40.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.40.5. 關機

配置檔案中加入

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

5.40.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.40.7. 計劃任務

		
http://localhost:8080/actuator/scheduledtasks