- 本文springboot版本:
1.5.4.RELEASE
- 如果使用springboot2.x氮采,請(qǐng)參考springboot2.x-actuator
spring-boot-actuator模塊提供了一個(gè)監(jiān)控和管理生產(chǎn)環(huán)境的模塊,可以使用http郑象、jmx精耐、ssh、telnet等來(lái)管理和監(jiān)控應(yīng)用。審計(jì)(Auditing)桃熄、 健康(health)、數(shù)據(jù)采集(metrics gathering)會(huì)自動(dòng)加入到應(yīng)用里面型奥。
添加依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
可以在你的application.yml
配置文件配置:
##運(yùn)行狀態(tài) actuator監(jiān)控
endpoints:
enabled: true
info:
sensitive: false
health:
sensitive: false
management:
##服務(wù)路徑
context-path: /
##服務(wù)端口
port: 8081
如果你使用application.properties
瞳收,可以做類似的配置:
endpoints.enabled=true
endpoints.info.sensitive=false
endpoints.health.sensitive=false
management.context-path=/
management.port=8081
啟動(dòng)后可以看到以下日志:
2017-03-15 15:50:58.775 INFO 9544 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Detected ResponseBodyAdvice bean in org.springframework.boot.actuate.autoconfigure.EndpointWebMvcHypermediaManagementContextConfiguration$ActuatorEndpointLinksAdvice
2017-03-15 15:50:58.956 INFO 9544 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/dump || /dump.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-03-15 15:50:58.957 INFO 9544 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/configprops || /configprops.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-03-15 15:50:58.958 INFO 9544 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/metrics/{name:.*}],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.MetricsMvcEndpoint.value(java.lang.String)
2017-03-15 15:50:58.958 INFO 9544 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/metrics || /metrics.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-03-15 15:50:58.959 INFO 9544 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/health || /health.json],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint.invoke(java.security.Principal)
2017-03-15 15:50:58.960 INFO 9544 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/autoconfig || /autoconfig.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-03-15 15:50:58.960 INFO 9544 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/mappings || /mappings.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-03-15 15:50:58.961 INFO 9544 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/beans || /beans.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-03-15 15:50:58.963 INFO 9544 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/heapdump || /heapdump.json],methods=[GET],produces=[application/octet-stream]}" onto public void org.springframework.boot.actuate.endpoint.mvc.HeapdumpMvcEndpoint.invoke(boolean,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) throws java.io.IOException,javax.servlet.ServletException
2017-03-15 15:50:58.964 INFO 9544 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/actuator || /actuator.json],produces=[application/json]}" onto public org.springframework.hateoas.ResourceSupport org.springframework.boot.actuate.endpoint.mvc.HalJsonMvcEndpoint.links()
2017-03-15 15:50:58.965 INFO 9544 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/info || /info.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-03-15 15:50:58.973 INFO 9544 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/env/{name:.*}],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpoint.value(java.lang.String)
2017-03-15 15:50:58.973 INFO 9544 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/env || /env.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-03-15 15:50:58.974 INFO 9544 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/trace || /trace.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
接口
接口 | 描述 | 敏感 |
---|---|---|
actuator | 列出所有可用接口 | true |
autoconfig | 顯示一個(gè)auto-configuration的報(bào)告,該報(bào)告展示所有auto-configuration候選者及它們被應(yīng)用或未被應(yīng)用的原因 | true |
beans | 顯示一個(gè)應(yīng)用中所有Spring Beans的完整列表 | true |
configprops | 顯示一個(gè)所有@ConfigurationProperties的整理列表 | true |
dump | 顯示當(dāng)前應(yīng)用線程狀態(tài)信息 | true |
env | 顯示Spring的ConfigurableEnvironment屬性 | true |
health | 展示應(yīng)用的健康信息(當(dāng)使用一個(gè)未認(rèn)證連接訪問(wèn)時(shí)顯示一個(gè)簡(jiǎn)單的’status’厢汹,使用認(rèn)證連接訪問(wèn)則顯示全部信息詳情) | false |
info | 顯示應(yīng)用信息 | false |
metrics | 展示當(dāng)前應(yīng)用的’指標(biāo)’信息 | true |
mappings | 顯示一個(gè)所有@RequestMapping路徑的整理列表 | true |
shutdown | 允許應(yīng)用以優(yōu)雅的方式關(guān)閉(默認(rèn)情況下不啟用) | true |
trace | 顯示trace信息(默認(rèn)情況下是最后100個(gè)HTTP請(qǐng)求) | true |
loggers | 提供顯示和修改應(yīng)用程序中l(wèi)oggers配置的功能 | true |
測(cè)試幾個(gè)接口
/health
訪問(wèn)http://127.0.0.1:8081/health 可以看到
{
"status":"UP",
"hello":{
"status":"UP"
},
"diskSpace":{
"status":"UP",
"total":116333809664,
"free":86622515200,
"threshold":10485760
},
"db":{
"status":"UP",
"database":"Oracle",
"hello":"Hello"
}
}
自動(dòng)配置的HealthIndicators
下面的HealthIndicators會(huì)被Spring Boot自動(dòng)配置(在合適的時(shí)候):
名字 | 描述 |
---|---|
CassandraHealthIndicator | 檢查Cassandra database是否正常 |
DiskSpaceHealthIndicator | 低磁盤空間檢測(cè) |
DataSourceHealthIndicator | 檢查數(shù)據(jù)庫(kù)連接是否正常 |
ElasticsearchHealthIndicator | 檢查Elasticsearch cluster是否正常 |
JmsHealthIndicator | 檢查JMS broker是否正常 |
MailHealthIndicator | 檢查mail server是否正常 |
MongoHealthIndicator | 檢查Mongo database是否正常 |
RabbitHealthIndicator | 檢查Rabbit server是否正常 |
RedisHealthIndicator | 檢查Redis server是否正常 |
SolrHealthIndicator | 檢查Solr server是否正常 |
編寫自定義HealthIndicators
想提供自定義健康信息螟深,你可以注冊(cè)實(shí)現(xiàn)了HealthIndicator接口的Spring beans。你需要提供一個(gè)health()方法的實(shí)現(xiàn)烫葬,并返回一個(gè)Health響應(yīng)界弧。Health響應(yīng)需要包含一個(gè)status和可選的用于展示的詳情。比如上面/health
接口中的hello就是再下面定義的
@Component
public class HelloHealthIndicator 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();
}
int check(){
return 0;
}
}
/metrics
{
"counter.status.200.root": 20,
"counter.status.200.metrics": 3,
"counter.status.200.star-star": 5,
"counter.status.401.root": 4,
"gauge.response.star-star": 6,
"gauge.response.root": 2,
"gauge.response.metrics": 3,
"classes": 5808,
"classes.loaded": 5808,
"classes.unloaded": 0,
"heap": 3728384,
"heap.committed": 986624,
"heap.init": 262144,
"heap.used": 52765,
"nonheap": 0,
"nonheap.committed": 77568,
"nonheap.init": 2496,
"nonheap.used": 75826,
"mem": 986624,
"mem.free": 933858,
"processors": 8,
"threads": 15,
"threads.daemon": 11,
"threads.peak": 15,
"threads.totalStarted": 42,
"uptime": 494836,
"instance.uptime": 489782,
"datasource.primary.active": 5,
"datasource.primary.usage": 0.25
}
此處我們可以看到基本的 memory 搭综, heap 垢箕, class loading , processor 和 thread pool 信息兑巾,連同一些HTTP指標(biāo)条获,還可以使用 / /{name:.} 訪問(wèn)單個(gè)屬性。在該實(shí)例中蒋歌, root ('/')帅掘, /metrics URLs分別返回20次,3次 HTTP 200 響應(yīng)堂油。同時(shí)可以看到 root URL返回了4次 HTTP 401 (unauthorized)響應(yīng)修档。雙asterix(star-star)來(lái)自于被Spring MVC /* 匹配到的一個(gè)請(qǐng)求(通常為一個(gè)靜態(tài)資源)。
gauge 級(jí)別展示了一個(gè)請(qǐng)求的最后響應(yīng)時(shí)間称诗。所以萍悴, root 的最后請(qǐng)求被響應(yīng)耗時(shí)2毫秒, /metrics 耗時(shí)3毫秒寓免。
Spring Boot提供以下系統(tǒng)指標(biāo):
系統(tǒng)內(nèi)存總量(mem)癣诱,單位:Kb
空閑內(nèi)存數(shù)量(mem.free),單位:Kb
處理器數(shù)量(processors)
系統(tǒng)正常運(yùn)行時(shí)間(uptime)袜香,單位:毫秒
應(yīng)用上下文(就是一個(gè)應(yīng)用實(shí)例)正常運(yùn)行時(shí)間(instance.uptime)撕予,單位:毫秒
系統(tǒng)平均負(fù)載(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)
還會(huì)為你應(yīng)用中定義的支持的DataSource提供 以下指標(biāo):
最大連接數(shù)(datasource.xxx.max)
最小連接數(shù)(datasource.xxx.min)
活動(dòng)連接數(shù)(datasource.xxx.active)
連接池的使用情況( .xxx.usage)
所有的數(shù)據(jù)源指標(biāo)共用 datasoure. 前綴啄清。該前綴對(duì)每個(gè)數(shù)據(jù)源都非常合適:
如果是主數(shù)據(jù)源(唯一可用的數(shù)據(jù)源或存在的數(shù)據(jù)源中被@Primary標(biāo)記的)前綴為datasource.primary
如果數(shù)據(jù)源bean名稱以dataSource結(jié)尾六水,那前綴就是bean的名稱去掉dataSource的部分(例如,batchDataSource的前
綴是datasource.batch)
其他情況使用bean的名稱作為前綴
記錄自己的指標(biāo)
想要記錄你自己的指標(biāo)辣卒,只需將CounterService
或GaugeService
注入到你的bean中掷贾。CounterService
提供increment
:將指定的計(jì)數(shù)器增加1。荣茫,decrement
:將指定的計(jì)數(shù)器減1想帅。和reset
:復(fù)位指定的計(jì)數(shù)器。 方法啡莉;GaugeService
提供一個(gè)submit
方法港准。
@Service
public class UserService {
@Autowired
UserMapper mapper;
@Autowired
CounterService counterService;
@Autowired
GaugeService gaugeService;
public Map<Object, Object> findById(String id) {
this.counterService.increment("services.system.userService.findById.invoked");
gaugeService.submit("a", 1.0);
return mapper.findById(id);
}
}
/trace
訪問(wèn)http://127.0.0.1:8081/trace
[
{
"timestamp":1489568887768,
"info":{
"method":"GET",
"path":"/",
"headers":{
"request":{
"host":"127.0.0.1:8080",
"connection":"keep-alive",
"accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"upgrade-insecure-requests":"1",
"user-agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.22 Safari/537.36 SE 2.X MetaSr 1.0",
"accept-encoding":"gzip, deflate, sdch",
"accept-language":"zh-CN,zh;q=0.8"
},
"response":{
"X-Application-Context":"youyu:localdev:8080",
"status":"404"
}
}
}
},
{
"timestamp":1489568886967,
"info":{
"method":"GET",
"path":"/",
"headers":{
"request":{
"host":"127.0.0.1:8080",
"connection":"keep-alive",
"accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"upgrade-insecure-requests":"1",
"user-agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.22 Safari/537.36 SE 2.X MetaSr 1.0",
"accept-encoding":"gzip, deflate, sdch",
"accept-language":"zh-CN,zh;q=0.8"
},
"response":{
"X-Application-Context":"youyu:localdev:8080",
"status":"404"
}
}
}
}
]
/loggers
訪問(wèn)http://127.0.0.1:8081/loggers
返回:
{
"levels":[
"OFF",
"ERROR",
"WARN",
"INFO",
"DEBUG",
"TRACE"
],
"loggers":{
"ROOT":{
"configuredLevel":"INFO",
"effectiveLevel":"INFO"
},
"com":{
"configuredLevel":null,
"effectiveLevel":"INFO"
},
"com.caiyi":{
"configuredLevel":null,
"effectiveLevel":"INFO"
}
...
}
}
可以通過(guò)post
請(qǐng)求http://127.0.0.1:8081/loggers/{elephant}
修改不同包下的日志級(jí)別。
舉個(gè)栗子:訪問(wèn)
http:127.0.0.1:8081/manage/loggers/com.caiyi
請(qǐng)求body中傳參數(shù):
{
"configuredLevel": "debug"
}
就能修改com.caiyi
下面的日志級(jí)別為debug
咧欣,用get
請(qǐng)求一下http://127.0.0.1:8081/manage/loggers/com.caiyi
可看到修改成功了:
{
"configuredLevel": "DEBUG",
"effectiveLevel": "DEBUG"
}