小强哥博客

小强哥,小强哥博客,技术大咖

ranong项目总结-Spring Boot Actuator(一)

springboot health

actuator是springboot中一个用来做系统健康检测的一个模块,它提供一个resetful的api接口,可以将系统运行过程中的磁盘空间、线程数、以及程序连接的数据库情况通过json返回,然后再结合预警、监控模块进行实时系统监控。

假设我们的app在某一时间产生了大量的运行日志,这些日志占用了大量的磁盘空间,我们的监控系统(zabbix)通过actuator提供的resetful api获取到了当前系统的磁盘数据,然后触发报警,从而避免了一场生产事故,想想这是不是很酷,当然actuator所提供的功能远远不止这些,不扯了,下面进入正题。

在maven项目中增加依赖,如下,截止整理这篇文章springboot的官方最新版本是1.5.1.release,我这里使用的是1.4.3.release

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.eju.ess</groupId>
	<artifactId>ranong</artifactId>
	<version>1.0.0-snapshot</version>
	<packaging>jar</packaging>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.3.RELEASE</version>
	</parent>

	<dependencies>
		<!-- spring plateform -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
			<exclusions>
				<exclusion>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-starter-logging</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-log4j2</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>utf8</encoding>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

其中,比较重要的依赖是,

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

启动spring boot程序,浏览器输入http://127.0.0.1:8080/health,可以看到如下,

{
  "status": "UP",
  "diskSpace": {
    "status": "UP",
    "total": 106145247232,
    "free": 84631035904,
    "threshold": 10485760
  }
}

上面列出了当前的系统状态UP,以及diskSpace的健康指标信息,这是json方式的数据,其它系统可以获取该数据进行分析。

## 自定义health

有的时候我们需要添加一些自己的健康指标,比如系统线程、线程池、队列等,springboot提供了HealthIndicator接口,如下,

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class MongoHealth implements HealthIndicator {
	@Override
	public Health health() {
		return null;
	}

}

MongoHealth是自定义的health类,我们在其中可以实现自己的监控逻辑,这里模拟输出mongo的client数,如下,

@Component
public class MongoHealth implements HealthIndicator {
	@Override
	public Health health() {
		return Health.up().withDetail("client", "100").build();
	}

}

启动spring boot程序,浏览器输入http://127.0.0.1:8080/health,可以看到如下,

{
  "status": "UP",
  "mongoHealth": {
    "status": "UP",
    "client": "100"
  },
  "diskSpace": {
    "status": "UP",
    "total": 106145247232,
    "free": 84628717568,
    "threshold": 10485760
  }
}

可以已经输出了mongoHealth的自定义健康信息。也可以自定义status,如下,

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.actuate.health.Status;
import org.springframework.stereotype.Component;

@Component
public class MongoHealth implements HealthIndicator {
	@Override
	public Health health() {
		return Health.status(new Status("OK","this is mongo client")).withDetail("client", "100").build();
	}

}

启动spring boot程序,浏览器输入http://127.0.0.1:8080/health,可以看到如下,

{
  "status": "UP",
  "mongoHealth": {
    "description": "this is mongo client",
    "status": "OK",
    "client": "100"
  },
  "diskSpace": {
    "status": "UP",
    "total": 106145247232,
    "free": 84628381696,
    "threshold": 10485760
  }
}

关闭默认的健康检查

如下,

{
  "status": "UP",
  "mongoHealth": {
    "description": "this is mongo client",
    "status": "OK",
    "client": "100"
  },
  "diskSpace": {
    "status": "UP",
    "total": 106145247232,
    "free": 84628381696,
    "threshold": 10485760
  }
}

其中,diskSpace为系统默认的健康检查,有的时候我们并不需要看着写系统默认的,可以在application.properties文件中,通过设置management.health.defaults.enabled=false来关闭默认检查,启动spring boot程序,浏览器输入http://127.0.0.1:8080/health,可以看到如下,

{
  "status": "UP",
  "mongoHealth": {
    "status": "UP",
    "client": "100"
  }
}

更改健康检查端口

默认情况下健康检查端口和app端口是使用同一个,如我们之前使用的http://127.0.0.1:8080/health,但是为了安全起见、不影响业务我们最好使用另外的端口进行健康检查,如使用9090端口,在application.properties文件中设置management.port=9090

其他resetful接口

actuator除了health之外还提供,如下接口,

"{[/trace || /trace.json],                 
"{[/env/{name:.*}],                        
"{[/env || /env.json],                     
"{[/configprops || /configprops.json],     
"{[/health || /health.json],               
"{[/metrics/{name:.*}],                    
"{[/metrics || /metrics.json],             
"{[/mappings || /mappings.json],           
"{[/dump || /dump.json],                   
"{[/autoconfig || /autoconfig.json],       
"{[/heapdump || /heapdump.json],           
"{[/beans || /beans.json],                 
"{[/info || /info.json],

/trace

主要检测http请求的每个请求的具体情况,如下,通过浏览器访问http://127.0.0.1:8080/trace会得到我之前http的请求记录,非常详细,对于分析访问来源很有作用。

[
  {
    "timestamp": 1486634253134,
    "info": {
      "method": "GET",
      "path": "/info",
      "headers": {
        "request": {
          "host": "127.0.0.1:8080",
          "connection": "keep-alive",
          "cache-control": "no-cache",
          "user-agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36",
          "postman-token": "b0204694-de81-f20d-24dd-441d9b5726af",
          "accept": "*/*",
          "accept-encoding": "gzip, deflate, sdch",
          "accept-language": "zh-CN,zh;q=0.8"
        },
        "response": {
          "X-Application-Context": "application",
          "status": "404"
        }
      }
    }
  },
  {
    "timestamp": 1486634246615,
    "info": {
      "method": "GET",
      "path": "/info",
      "headers": {
        "request": {
          "host": "127.0.0.1:8080",
          "connection": "keep-alive",
          "cache-control": "no-cache",
          "user-agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36",
          "postman-token": "0ae239ad-5162-e519-01e6-60489d0ba60e",
          "accept": "*/*",
          "accept-encoding": "gzip, deflate, sdch",
          "accept-language": "zh-CN,zh;q=0.8"
        },
        "response": {
          "X-Application-Context": "application",
          "status": "404"
        }
      }
    }
  }
]

/env

用来检测系统的环境信息,非常全,感觉整个机器的信息都被列出来了,我在这里就不粘贴我的信息了。

/env/{name:.*}

是用来检索/en结果的命令。

/configprops

用来显示springboot自动配置的属性,例如:springboot的默认值等。

/metrics

列出整个系统的核心信息,如下,对于做系统诊断很有用,

{
  "mem": 293820,
  "mem.free": 174628,
  "processors": 4,
  "instance.uptime": 925731,
  "uptime": 931825,
  "systemload.average": -1,
  "heap.committed": 237056,
  "heap.init": 122880,
  "heap.used": 62427,
  "heap": 1745920,
  "nonheap.committed": 57856,
  "nonheap.init": 2496,
  "nonheap.used": 56766,
  "nonheap": 0,
  "threads.peak": 37,
  "threads.daemon": 31,
  "threads.totalStarted": 43,
  "threads": 37,
  "classes": 7055,
  "classes.loaded": 7055,
  "classes.unloaded": 0,
  "gc.ps_scavenge.count": 9,
  "gc.ps_scavenge.time": 271,
  "gc.ps_marksweep.count": 2,
  "gc.ps_marksweep.time": 211,
  "httpsessions.max": -1,
  "httpsessions.active": 0,
  "gauge.response.star-star": 1,
  "counter.status.404.star-star": 2
}

/metrics/{name:.*}

使用来检索/metrics的命令,

/mappings

里面包含了Controller的所有mapping信息,可以用来查看某个映射所在的类。

/info

/beans

/heapdump

/dump

参考文章

http://blog.csdn.net/king_is_everyone/article/details/53261354