Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | 51CTO学院 | CSDN程序员研修院 | OSChina 博客 | 腾讯云社区 | 阿里云栖社区 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏多维度架构

5.33. Spring Boot Actuator

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

5.33.1. Maven 依赖

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

5.33.2. 与 Spring Boot Actuator 有关的配置

application.properties

跨域配置

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

5.33.2.1. 禁用HTTP端点

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

			
management.server.port=-1			
			
			

5.33.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.33.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
    }
  }
}
		
		
		

暴漏所有接口

		
management:
  endpoints:
    web:
      exposure:
        include: "*"		
		
		

再次访问 /actuator

		
neo@MacBook-Pro-Neo ~/w/Architect (master)> curl -s https://www.netkiller.cn/actuator/ | jq
{
  "_links": {
    "self": {
      "href": "http://pre.ejiayou.com/ensd-channel-service/monitor",
      "templated": false
    },
    "archaius": {
      "href": "https://www.netkiller.cn/actuator/archaius",
      "templated": false
    },
    "nacosconfig": {
      "href": "https://www.netkiller.cn/actuator/nacosconfig",
      "templated": false
    },
    "nacosdiscovery": {
      "href": "https://www.netkiller.cn/actuator/nacosdiscovery",
      "templated": false
    },
    "auditevents": {
      "href": "https://www.netkiller.cn/actuator/auditevents",
      "templated": false
    },
    "beans": {
      "href": "https://www.netkiller.cn/actuator/beans",
      "templated": false
    },
    "caches-cache": {
      "href": "https://www.netkiller.cn/actuator/caches/{cache}",
      "templated": true
    },
    "caches": {
      "href": "https://www.netkiller.cn/actuator/caches",
      "templated": false
    },
    "health-component": {
      "href": "https://www.netkiller.cn/actuator/health/{component}",
      "templated": true
    },
    "health": {
      "href": "https://www.netkiller.cn/actuator/health",
      "templated": false
    },
    "health-component-instance": {
      "href": "https://www.netkiller.cn/actuator/health/{component}/{instance}",
      "templated": true
    },
    "conditions": {
      "href": "https://www.netkiller.cn/actuator/conditions",
      "templated": false
    },
    "configprops": {
      "href": "https://www.netkiller.cn/actuator/configprops",
      "templated": false
    },
    "env-toMatch": {
      "href": "https://www.netkiller.cn/actuator/env/{toMatch}",
      "templated": true
    },
    "env": {
      "href": "https://www.netkiller.cn/actuator/env",
      "templated": false
    },
    "info": {
      "href": "https://www.netkiller.cn/actuator/info",
      "templated": false
    },
    "loggers-name": {
      "href": "https://www.netkiller.cn/actuator/loggers/{name}",
      "templated": true
    },
    "loggers": {
      "href": "https://www.netkiller.cn/actuator/loggers",
      "templated": false
    },
    "heapdump": {
      "href": "https://www.netkiller.cn/actuator/heapdump",
      "templated": false
    },
    "threaddump": {
      "href": "https://www.netkiller.cn/actuator/threaddump",
      "templated": false
    },
    "metrics": {
      "href": "https://www.netkiller.cn/actuator/metrics",
      "templated": false
    },
    "metrics-requiredMetricName": {
      "href": "https://www.netkiller.cn/actuator/metrics/{requiredMetricName}",
      "templated": true
    },
    "scheduledtasks": {
      "href": "https://www.netkiller.cn/actuator/scheduledtasks",
      "templated": false
    },
    "httptrace": {
      "href": "https://www.netkiller.cn/actuator/httptrace",
      "templated": false
    },
    "mappings": {
      "href": "https://www.netkiller.cn/actuator/mappings",
      "templated": false
    },
    "refresh": {
      "href": "https://www.netkiller.cn/actuator/refresh",
      "templated": false
    },
    "features": {
      "href": "https://www.netkiller.cn/actuator/features",
      "templated": false
    },
    "service-registry": {
      "href": "https://www.netkiller.cn/actuator/service-registry",
      "templated": false
    }
  }
}		
		
		

5.33.4. 健康状态

curl localhost:8080/actuator/health

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

5.33.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.33.5. 关机

配置文件中加入

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

5.33.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.33.7. 计划任务

		
http://localhost:8080/actuator/scheduledtasks		
		
		

5.33.8. 修改 actuator 地址

		
management:
  endpoints:
    web:
      base-path: /monitor		
		
		

5.33.9. metrics

		
neo@MacBook-Pro-Neo ~/w/Architect (master)> curl -s https://www.netkiller.cn/actuator/metrics/ | jq
{
  "names": [
    "jvm.threads.states",
    "process.files.max",
    "jvm.gc.memory.promoted",
    "hikaricp.connections.max",
    "hikaricp.connections.min",
    "jvm.memory.committed",
    "system.load.average.1m",
    "http.server.requests",
    "jvm.memory.used",
    "jvm.gc.max.data.size",
    "jdbc.connections.max",
    "jdbc.connections.min",
    "hikaricp.connections.usage",
    "jvm.gc.pause",
    "system.cpu.count",
    "hikaricp.connections.timeout",
    "tomcat.global.sent",
    "jvm.buffer.memory.used",
    "tomcat.sessions.created",
    "jvm.memory.max",
    "jvm.threads.daemon",
    "hikaricp.connections.acquire",
    "system.cpu.usage",
    "jvm.gc.memory.allocated",
    "tomcat.global.request.max",
    "tomcat.global.request",
    "tomcat.sessions.expired",
    "jvm.threads.live",
    "jvm.threads.peak",
    "tomcat.global.received",
    "process.uptime",
    "tomcat.sessions.rejected",
    "process.cpu.usage",
    "tomcat.threads.config.max",
    "jvm.classes.loaded",
    "jvm.classes.unloaded",
    "tomcat.global.error",
    "tomcat.sessions.active.current",
    "tomcat.sessions.alive.max",
    "jvm.gc.live.data.size",
    "log4j2.events",
    "hikaricp.connections.idle",
    "tomcat.threads.current",
    "hikaricp.connections.pending",
    "process.files.open",
    "jvm.buffer.count",
    "hikaricp.connections",
    "jvm.buffer.total.capacity",
    "tomcat.sessions.active.max",
    "hikaricp.connections.active",
    "hikaricp.connections.creation",
    "tomcat.threads.busy",
    "process.start.time"
  ]
}		
		
		
		
neo@MacBook-Pro-Neo ~/w/Architect (master)> curl -s https://www.netkiller.cn/actuator/metrics/tomcat.threads.config.max |jq
{
  "name": "tomcat.threads.config.max",
  "description": null,
  "baseUnit": "threads",
  "measurements": [
    {
      "statistic": "VALUE",
      "value": 4096
    }
  ],
  "availableTags": [
    {
      "tag": "name",
      "values": [
        "http-nio-8080"
      ]
    }
  ]
}
neo@MacBook-Pro-Neo ~/w/Architect (master)> curl -s https://www.netkiller.cn/actuator/metrics/tomcat.threads.current |jq
{
  "name": "tomcat.threads.current",
  "description": null,
  "baseUnit": "threads",
  "measurements": [
    {
      "statistic": "VALUE",
      "value": 24
    }
  ],
  "availableTags": [
    {
      "tag": "name",
      "values": [
        "http-nio-8080"
      ]
    }
  ]
}