1. 引用起步

gradle

dependencies {
    //设置程序监控
    compile("org.springframework.boot:spring-boot-starter-actuator")
}

maven

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

2. 基本使用

2.1 获得Bean装配报告

http://localhost:8080/beans端点提供的Spring应用程序上下文Bean信息

{
"bean": "application",//Bean的ID
"dependencies": [ //当前Bean注入的Bean ID列表
    "readingListRepository",
    "amazonProperties"
    ],
"resource": "URL [jar:file:/../readinglist-0.0.1-SNAPSHOT.jar!",//.class文件的物理位置
"scope": "singleton",//Bean的作用域
"type": "readinglist.Application$$EnhancerBySpringCGLIB$$f363c202"//Java类型
}

出于安全考虑,默认不能访问,需要将安全选项关掉。

management.security.enabled=false
2.2 /env端点报告所有可用的属性

基本上,任何能给Spring Boot应用程序提供属性的属性源都会列在/env的结果里,同时会显示具体的属性。内容很多,不方便具体的查看。

springboot提供了单个属性值得查看。只需要在请求时在/env后加上属性名即可。举例来说,查看文件编码的配置:http://localhost:8080/env/file.encoding

{
"file.encoding": "UTF-8"
}
2.3 /mappings映射所有路径

在应用程序相对较小的时候,很容易搞清楚控制器都映射到了哪些端点上。如果Web界面的 控制器和请求处理方法数量多,那最好能有一个列表,罗列出应用程序发布的全部端点。

{[/User/Add],//路径
methods=[GET],//请求方式
produces=[application/json]}//返回Json
: {
"bean": "requestMappingHandlerMapping",
"method": "public java.lang.String com.maxrocky.sso.controller.user.UserController.index()"//方法名
}
2.4 /metrics端点提供运行时数据

举个例子,了解应用程序的内存情况 (可用或空闲)有助于决定给JVM分配多少内存。

{
"mem": 930065,
"mem.free": 328999,
"processors": 8,
"instance.uptime": 943842,
"uptime": 1002818,
"systemload.average": 2.3662109375,
"heap.committed": 847872,
"heap.init": 262144,
"heap.used": 518872,
"heap": 3728384,
"nonheap.committed": 83904,
"nonheap.init": 2496,
"nonheap.used": 82193,
"nonheap": 0,
"threads.peak": 32,
"threads.daemon": 26,
........
}

作用

前缀

垃圾收集器

gc.*

内存

mem.*


heap.*

类加载器

classes.*

系统

processors、uptime instance.uptime、

线程池

threads.*

数据源

datasource.*

Tomcat会话

httpsessions.*

HTTP

counter.status.* 、 gauge.response.*

2.5 /trace端点会记录下Web请求的细节

/trace端点能报告所有Web请求的详细信息,包括请求方法、路径、时间戳以及请求和响应的 头信息。

[
    {
        "timestamp": 1426378239775,
        "info": {
          "method": "GET",
          "path": "/metrics",
          "headers": {
            "request": {
              "accept": "*/*",
              "host": "localhost:8080",
              "user-agent": "curl/7.37.1"
            },
            "response": {
              "X-Content-Type-Options": "nosniff",
              "X-XSS-Protection": "1; mode=block",
              "Cache-Control":
                        "no-cache, no-store, max-age=0, must-revalidate",
              "Pragma": "no-cache",
              "Expires": "0",
              "X-Frame-Options": "DENY",
              "X-Application-Context": "application",
              "Content-Type": "application/json;charset=UTF-8",
              "Transfer-Encoding": "chunked",
              "Date": "Sun, 15 Mar 2015 00:10:39 GMT",
              "status": "200"
} }
} }
]
2.6 /dump端点提供了应用程序线程的快照
{
    "threadName": "container-0",
    "threadId": 19,
    "blockedTime": -1,
    "blockedCount": 0,
    "waitedTime": -1,
    "waitedCount": 64,
    "lockName": null,
    "lockOwnerId": -1,
    "lockOwnerName": null,
    "inNative": false,
    "suspended": false,
    "threadState": "TIMED_WAITING",
    "stackTrace": [
      {
        "className": "java.lang.Thread",
        "fileName": "Thread.java",
        "lineNumber": -2,
        "methodName": "sleep",
        "nativeMethod": true
}, {
} ],
    "lockedMonitors": [],
    "lockedSynchronizers": [],
    "lockInfo": null
},
... ]
2.7 /health监控应用程序健康情况
{"status":"UP"}

/health端点输出的某些信息可能涉及内容,因此对未经授权的请求只能提供简单的健康状 态。

但是我们已经将安全策略关闭了,所以可以看到更多的信息

{
    "status": "UP",
    "diskSpace": {
    "status": "UP",
    "total": 249769230336,
    "free": 179990880256,
    "threshold": 10485760
},
"db": {
    "status": "UP",
    "ssoDataSource": {
    "status": "UP",
    "database": "MySQL",
    "hello": 1
},
"configurationDataSource": {
    "status": "UP",
    "database": "MySQL",
    "hello": 1
}
}
}
2.8 /shutdown关闭应用程序(post请求)

post,post,post

很显然,关闭运行中的应用程序是件危险的事情,因此这个端点默认是关闭的。如果没有显 式地开启这个功能,那么POST请求的结果是这样的:

{"message":"This endpoint is disabled"}

要开启该端点,可以将endpoints.shutdown.enabled设置为true。

# application.properties,远程关闭程序
endpoints.shutdown.enabled=true
2.9 /info端点展示应用信息

默认只是一个空的json,你可以通过配置带有info前缀的属性向/info端点
的响应添加内容。

例如,你希望在响应中添加联系邮箱。

可以在application.properties里设置名为 info.contactEmail的属性:

info.contactEmail=kingboyworld@163.com

现在再访问/info端点,就能得到如下响应:

{
"contactEmail":"support@myreadinglist.com" 
}
2.10 更改原有的映射路径

例如,shutdown端口比较敏感,可以更改为localhost:8080/mykill等其他路径。(实际使用中一般用security来进行安全控制)。

只需要在配置文件中增加一行:

endpoints.shutdown.id=mykill
2.11 启用和禁用端点

启用/禁用:

endpoints.metrics.enabled=true/false

只启用某一个

#先关全部
endpoints.enabled=false
#再打开一个
endpoints.metrics.enabled=true

至于如何创建自定义的端点,用的人应该不多。需要的话可以看springboot实战的第七章,写的很详细。

一般来说,要对这些端点进行保护,总不能谁都可以查看或者操作这些信息,这就要结合spring security来进行保护了,后续再写了。