知乎专栏 | 多维度架构 |
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
application.properties
跨域配置
management.endpoints.web.cors.allowed-origins=https://example.com management.endpoints.web.cors.allowed-methods=GET,POST
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
常规接口
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 } } }
curl localhost:8080/actuator/health
neo@MacBook-Pro ~ % curl -s http://localhost:8080/actuator/health | jq { "status": "UP" }
配置文件中加入
management.endpoint.shutdown.enabled=true
curl -X POST localhost:8080/actuator/shutdown
返回 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" } }
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" ] } ] }