spring boot监控与管理

引言

在微服务架构中,咱们把单块系统拆分红多个服务。在部署时,一个服务极可能分布在不一样的主机,甚至是不一样的机房。随着服务的不断增多,服务间相互调用、相互依赖。为了掌握服务的状态及服务所在主机的状况,就显得尤其重要!git

Spring Boot Actuator

spring boot actuator为咱们暴露不少端点(endpoints),能够用来获取环境属性、线程信息、健康情况等。下表列出了经常使用的端点。github

路径 描述
/health 显示应用程序的状态
/metrics 显示内存、GC、线程、堆栈等信息
/trace 显示最近100条请求详细信息
/mappings 显示全部经过@RequestMapping配置的路径
/env 显示所有环境属性,包括profiles、systemProperties、systemEnvironment、applicationConfig
/dump 显示线程快照
/autoconfig 显示自动配置的生效及未生效列表,对了解bean的加载颇有帮助
/beans 查看程序中全部的bean
/shutdown 关闭应用程序,默认是不可用的,能够经过endpoints.shutdown.enabled=true开启

如何开启

引入spring-boot-startr-actuator后自动开启。spring

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

虽然很简单,若是你们不方便本身搭建,能够经过git下载一下例子浏览器

git clone https://github.com/feelgood3000/actuator.git

下载后运行ActuatorApplication的main()方法便可。 安全

在浏览器打开以下地址:http://localhost:8080/metrics,返回内容以下session

{
"mem": 446758,
"mem.free": 316977,
"processors": 4,
"instance.uptime": 39453,
"uptime": 46245,
"systemload.average": 3.32568359375,
"heap.committed": 391680,
"heap.init": 131072,
"heap.used": 74702,
"heap": 1864192,
"nonheap.committed": 55872,
"nonheap.init": 2496,
"nonheap.used": 55079,
"nonheap": 0,
"threads.peak": 25,
"threads.daemon": 21,
"threads.totalStarted": 28,
"threads": 23,
"classes": 7793,
"classes.loaded": 7793,
"classes.unloaded": 0,
"gc.ps_scavenge.count": 10,
"gc.ps_scavenge.time": 170,
"gc.ps_marksweep.count": 2,
"gc.ps_marksweep.time": 130,
"httpsessions.max": -1,
"httpsessions.active": 0,
"datasource.primary.active": 0,
"datasource.primary.usage": 0
}

其余能够本身看一下,返回内容经过字段就能够知道含义。架构

配置

端点开关

支持对每一个端点进行开关,配置为endpoints.[endpointName].enabled=true,endpointName为端点的名字,好比咱们想禁用info端点,在application.properties中添加以下内容便可:app

endpoints.info.enabled=false

若是咱们只想开启其中一两个端点,咱们能够先把全部端点禁用,再单独配置要开启的端点。ide

endpoints.enabled=false
endpoints.info.enabled=true

端点端口号

有时为了安全考虑,咱们不想跟服务占用同一个端口,能够单独给actuator配置端口。spring-boot

server.port=8080
management.port=8888

自定义路径

咱们也能够修改默认的路径,来起到安全的目的。
配置格式为endpoints.[endpointsName].id=[path]
好比咱们想把metrics的访问路径从http://localhost:8080/metrics修改成http://localhost:8080/feelgood3000,则配置为:

endpoints.metrics.id=feelgood3000

添加前缀

咱们能够经过配置修改访问endpoints的路径。

management.context-path=/manage

添加如上配置后,全部端点的路径从原来的http://localhost:8080/endpointName,改成http://localhost:8080/manage/endpointName了。

相关文章
相关标签/搜索