原文地址:http://blog.csdn.net/wh_ouyangshuang/article/details/48048111

spring-boot-actuator模块提供了一个监控和管理生产环境的模块,可以使用http、jmx、ssh、telnet等拉管理和监控应用。审计(Auditing)、 
健康(health)、数据采集(metrics gathering)会自动加入到应用里面。

首先,写一个最基本的spring boot项目。

基于Maven的项目添加‘starter’依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
ID 描述 敏感(Sensitive)
autoconfig 显示一个auto-configuration的报告,该报告展示所有auto-configuration候选者及它们被应用或未被应用的原因 true
beans 显示一个应用中所有Spring Beans的完整列表 true
configprops 显示一个所有@ConfigurationProperties的整理列表 true
dump 执行一个线程转储 true
env 暴露来自Spring ConfigurableEnvironment的属性 true
health 展示应用的健康信息(当使用一个未认证连接访问时显示一个简单的’status’,使用认证连接访问则显示全部信息详情) false
info 显示任意的应用信息 false
metrics 展示当前应用的’指标’信息 true
mappings 显示一个所有@RequestMapping路径的整理列表 true
shutdown 允许应用以优雅的方式关闭(默认情况下不启用) true
trace 显示trace信息(默认为最新的一些HTTP请求) true

health

比如:http://localhost:8080/health 
你可以得到结果

{
status: "UP",
}

在应用配置加上

endpoints.health.sensitive=false

再次访问http://localhost:8080/health

{
status: "UP",
diskSpace: {
status: "UP",
free: 32516145152,
threshold: 10485760
},
db: {
status: "UP",
database: "Microsoft SQL Server",
hello: 1440729256277
}
}

自定义当然也可以,你可以注册实现了HealthIndicator接口的Spring beans,Health响应需要包含一个status和可选的用于展示的详情。

import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component; @Component
public class MyHealth implements HealthIndicator { @Override
public Health health() {
int errorCode = check(); // perform some specific health check
if (errorCode != 0) {
return Health.down().withDetail("Error Code", errorCode).build();
}
return Health.up().build();
}
}

trace

访问http://localhost:8080/trace可以看到结果,默认为最新的一些HTTP请求

[
{
timestamp: 1440728799269,
info: {
method: "GET",
path: "/health",
headers: {
request: {
host: "localhost:7231",
connection: "keep-alive",
accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
user-agent: "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36",
accept-encoding: "gzip,deflate,sdch",
accept-language: "zh-CN,zh;q=0.8,en;q=0.6",
ra-ver: "3.0.7",
ra-sid: "74E754D8-20141117-085628-93e7a4-1dd60b"
},
response: {
X-Application-Context: "executecount:integration:7231",
Content-Type: "application/json;charset=UTF-8",
Transfer-Encoding: "chunked",
Content-Encoding: "gzip",
Vary: "Accept-Encoding",
Date: "Fri, 28 Aug 2015 02:26:39 GMT",
status: "200"
}
}
}
}
]

看看 InMemoryTraceRepository,默认是100个事件,如果需要可以定义自己的 InMemoryTraceRepository 实例。如果需要,你可以创建自己的替代 TraceRepository 实现。

public class InMemoryTraceRepository implements TraceRepository {

    private int capacity = 100;

    private boolean reverse = true;
}

如果需要追踪其他的事件,你可以将一个TraceRepository注入到你的Spring Beans中

info

当执行 http://localhost:8080/info 的时候,结果什么没有 
但是,单加入加入一些配置

info.app.name=ecs
info.app.version=1.0.0 info.build.artifactId=@project.artifactId@
info.build.version=@project.version@

执行/info

{
app: {
version: "1.0.0",
name: "ecs"
},
build: {
artifactId: "execute-count-server",
version: "0.0.3"
}
}

/info 是用来在构建的时候,自动扩展属性的。对于Maven项目,可以通过 @..@ 占位符引用Maven的’project properties’。

env

通过/env可以访问环境信息

{
profiles: [
"integration"
],
servletContextInitParams: { },
systemProperties: {
java.runtime.name: "Java(TM) SE Runtime Environment",
java.vm.version: "25.25-b02",
java.vm.vendor: "Oracle Corporation",
java.vendor.url: "http://java.oracle.com/",
path.separator: ";",
idea.launcher.port: "7532",
java.vm.name: "Java HotSpot(TM) 64-Bit Server VM",
file.encoding.pkg: "sun.io",
user.country: "CN",
user.script: "",
sun.java.launcher: "SUN_STANDARD",
sun.os.patch.level: "Service Pack 1",
PID: "7416",
java.vm.specification.name: "Java Virtual Machine Specification", ...

通过/env/{name:.*}可以访问特定的环境属性,如:/env/java.*,获取java相关的项

metrics

/metrics显示了应用当前的指标信息

{
mem: 348672,
mem.free: 264831,
processors: 4,
instance.uptime: 1859584,
uptime: 1870818,
systemload.average: -1,
heap.committed: 348672,
heap.init: 124928,
heap.used: 83840,
heap: 1773568,
threads.peak: 23,
threads.daemon: 21,
threads: 23,
classes: 7269,
classes.loaded: 7272,
classes.unloaded: 3,
gc.ps_scavenge.count: 7,
gc.ps_scavenge.time: 379,
gc.ps_marksweep.count: 2,
gc.ps_marksweep.time: 283,
httpsessions.max: -1,
httpsessions.active: 0,
datasource.primary.active: 0,
datasource.primary.usage: 0,
counter.status.200.autoconfig: 1,
counter.status.200.dump.root: 1,
counter.status.200.env: 2,
counter.status.200.env.name:.-star-: 4,
counter.status.200.env.root: 3,
counter.status.200.health: 2,
counter.status.200.info: 1,
counter.status.200.mappings: 1,
counter.status.200.trace: 1,
counter.status.404.env.name:.-star-: 1,
gauge.response.autoconfig: 43,
gauge.response.dump.root: 89,
gauge.response.env: 21,
gauge.response.env.name:.-star-: 14,
gauge.response.env.root: 17,
gauge.response.health: 19,
gauge.response.info: 5,
gauge.response.mappings: 13,
gauge.response.trace: 52
}
  • 系统内存总量(mem),单位:Kb
  • 空闲内存数量(mem.free),单位:Kb
  • 处理器数量(processors)
  • 系统正常运行时间(uptime),单位:毫秒
  • 应用上下文(就是一个应用实例)正常运行时间(instance.uptime),单位:毫秒
  • 系统平均负载(systemload.average)
  • 堆信息(heap,heap.committed,heap.init,heap.used),单位:Kb
  • 线程信息(threads,thread.peak,thead.daemon)
  • 类加载信息(classes,classes.loaded,classes.unloaded)
  • 垃圾收集信息(gc.xxx.count, gc.xxx.time)
  • 最大连接数(datasource.xxx.max)
  • 最小连接数(datasource.xxx.min)
  • 活动连接数(datasource.xxx.active)
  • 连接池的使用情况(datasource.xxx.usage)

也可以使用 /metrics/{name:.*} 访问单个属性。

dump

/dump执行一个线程转储,这是一个例子。

[
{
threadName: "http-nio-7231-exec-10",
threadId: 32,
blockedTime: -1,
blockedCount: 0,
waitedTime: -1,
waitedCount: 3,
lockName: "java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject@7cf8ed03",
lockOwnerId: -1,
lockOwnerName: null,
inNative: false,
suspended: false,
threadState: "WAITING",
stackTrace: [
{
methodName: "park",
fileName: null,
lineNumber: -2,
className: "sun.misc.Unsafe",
nativeMethod: true
}, ...

mappings

/mappings显示一个所有@RequestMapping路径的整理列表,一切尽在眼中。

{
/webjars/**: {
bean: "resourceHandlerMapping"
},
/**: {
bean: "resourceHandlerMapping"
},
/**/favicon.ico: {
bean: "faviconHandlerMapping"
},
{[/mappings],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}: {
bean: "endpointHandlerMapping",
method: "public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()"
},
{[/info],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}: {
bean: "endpointHandlerMapping",
method: "public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()"
},
{[/env/{name:.*}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}: {
bean: "endpointHandlerMapping",
method: "public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpoint.value(java.lang.String)"
},
{[/env],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}: {
bean: "endpointHandlerMapping",
method: "public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()"
}, ...

autoconfig

autoconfig显示一个auto-configuration的报告,该报告展示所有auto-configuration候选者及它们被应用或未被应用的原因

{
positiveMatches: {
AuditAutoConfiguration.AuditEventRepositoryConfiguration: [
{
condition: "OnBeanCondition",
message: "@ConditionalOnMissingBean (types: org.springframework.boot.actuate.audit.AuditEventRepository; SearchStrategy: all) found no beans"
}
],
EndpointAutoConfiguration#autoConfigurationAuditEndpoint: [
{
condition: "OnBeanCondition",
message: "@ConditionalOnBean (types: org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport; SearchStrategy: all) found the following [autoConfigurationReport] @ConditionalOnMissingBean (types: org.springframework.boot.actuate.endpoint.AutoConfigurationReportEndpoint; SearchStrategy: current) found no beans"
}
], ...

configprops

/configprops显示一个所有@ConfigurationProperties的整理列表.

{
management.health.status.CONFIGURATION_PROPERTIES: {
prefix: "management.health.status",
properties: {
order: null
}
},
spring.datasource.CONFIGURATION_PROPERTIES: {
prefix: "spring.datasource",
properties: {
schema: null,
data: null,
xa: {
dataSourceClassName: null,
properties: { }
},
separator: ";",
url: "jdbc:sqlserver://192.168.x.x:xxxx;DatabaseName=xxxx;sendStringParametersAsUnicode=false",
platform: "all",
continueOnError: false,
jndiName: null,
sqlScriptEncoding: null,
password: "******",
driverClassName: "com.microsoft.sqlserver.jdbc.SQLServerDriver",
initialize: false,
username: "Monitor"
}
}, ...

spring boot监控--actuator的更多相关文章

  1. Spring Boot|监控-Actuator

    Spring Boot 为我们提供了一个生产级特性-Actuator,包含很多实际有用的API,下面我们就一起来看看这些API. 一.Actuator 首先在程序中引入Actuator <!-- ...

  2. Spring Boot整合actuator实现监控管理

    Spring Boot使用actuator监控管理 1.在pom文件中导入相关的依赖 <dependency> <groupId>org.springframework.boo ...

  3. Spring Boot (27) actuator服务监控与管理

    actuaotr是spring boot项目中非常强大的一个功能,有助于对应用程序进行监控和管理,通过restful api请求来监管.审计.收集应用的运行情况,针对微服务而言它是必不可少的一个环节. ...

  4. 笔记:Spring Boot 监控与管理

    在微服务架构中,我们将原本庞大的单体系统拆分为多个提供不同服务的应用,虽然,各个应用的内部逻辑因分解而简化,但由于部署的应用数量成倍增长,使得系统的维护复杂度大大提升,为了让运维系统能够获取各个为服务 ...

  5. Spring Boot 监控利器 —— Actutor

    参考 CSDN-学习Spring Boot:(二十七)Spring Boot 2.0 中使用 Actuator 使用Actuator监控Spring Boot应用 程序猿DD-Spring Boot ...

  6. Spring Boot 监控与管理

    在微服务架构中,我们将原本庞大的单体系统拆分为多个提供不同服务的应用,虽然,各个应用的内部逻辑因分解而简化,但由于部署的应用数量成倍增长,使得系统的维护复杂度大大提升,为了让运维系统能够获取各个为服务 ...

  7. 从Unauthorized 401错误学习Spring Boot的Actuator

    之前用Spring Boot都是别人搭好的框架,然后自己在里面写就行了.对原理.细节上都怎么涉及,毕竟需求都做不完.但是昨天一个访问RESTful接口的401问题搞了我2个小时.网上找的很多用: ma ...

  8. Spring Boot (28) actuator与spring-boot-admin

    在上一篇中,通过restful api的方式查看信息过于繁琐,也不直观,效率低下.当服务过多的时候看起来就过于麻烦,每个服务都需要调用不同的接口来查看监控信息. SBA SBA全称spring boo ...

  9. spring boot之actuator简介

    当我们的开发工作进入尾声,部署上线之后,对于一个程序而言,可能才刚刚开始,对程序的运行情况的监控要伴随着整个生命周期. 如果这个工作由程序员自己来开发,也未尝不可,但本着不重复制造轮子的思想,我们尽量 ...

随机推荐

  1. Spring Boot Docker 实战

    Spring Boot Docker 开发环境 开发工具: Intellij IDEA 2018.2.6 springboot: 2.0.6.RELEASE jdk: 1.8.0_192 maven: ...

  2. python使用tcp实现一个简单的下载器

    上一篇中介绍了tcp的流程,本篇通过写一个简单的文件下载器程序来巩固之前学的知识. 文件下载器的流程如下: 客户端: 输入目标服务器的ip和port 输入要下载文件的名称 从服务器下载文件保存到本地 ...

  3. 什么是Docker Volume?

    摘要:Docker Volume,通常翻译为数据卷,用于保存持久化数据.当我们将数据库例如MySQL运行在Docker容器中时,一般将数据通过Docker Volume保存在主机上,这样即使删除MyS ...

  4. angular 设置年份选择下拉框,并默认今年

    <select ng-model="selectedYear" ng-change="yearChange(selectedYear)"> < ...

  5. Spring 切入点配置

    有关各种拦截的切入点配置举例 (1)只对返回值为String的方法进行拦截 @Pointcut("execution (java.lang.String com.zzdr.spring.se ...

  6. Java虚拟机(四)垃圾收集算法

    前言 在本系列上一篇文章中我讲到了垃圾标记算法,垃圾被标记后,GC就会对垃圾进行收集,垃圾收集有很多种算法,这篇文章就来介绍常用的垃圾收集算法的思想. 1.标记-清除算法 标记-清除算法(Mark-S ...

  7. MySQL中lock tables和unlock tables浅析

    MySQL中lock tables和unlock tables浅析   在MySQL中提供了锁定表(lock tables)和解锁表(unlock tables)的语法功能,ORACLE与SQL Se ...

  8. mysql练习----More JOIN operations

    This tutorial introduces the notion of a join. The database consists of three tables movie , actor a ...

  9. [20181015]为什么是3秒.txt

    [20181015]为什么是3秒.txt --//以前测试:连接http://blog.itpub.net/267265/viewspace-2144765/=>为什么是12秒.txt.--// ...

  10. C#-事件(十八)

    概述 事件(Event) 基本上说是一个用户操作,如按键.点击.鼠标移动 使用事件,可以很方便地确定程序执行顺序 事件在类中声明且生成,且通过使用同一个类或其他类中的委托与事件处理程序关联 包含事件的 ...