监控与管理
在微服务架构中,我们将原本庞大的单体系统拆分成多个提供不同服务的应用。 虽然
各个应用的内部逻辑因分解而得以简化,但是由于部署应用的数量成倍增长,使得系统的维护复杂度大大提升。 对于运维人员来说,随着应用的不断增多,系统集群中出现故障的频率也变得越来 越高,虽然在高可用机制的保护下,个别故障不会影响系统的对外服务,但是这些频繁出现的故障需要被及时发现和处理才能长期保证系统处千健康可用状态。 为了能对这些成倍增长的应用做到高效运维,传统的运维方式显然是不合适的,所以我们需要实现一套自动化的监控 运维机制,而这套机制的运行基础就是不间断地收集各个微服务应用的各项 指标情况,并根据这些基础指标信息来制定监控和预警规则,更进一步甚至做到一些自动化的运维操作等。为了让运维系统能够获取各个微服务应用的相关指标以及实现一些常规操作控制,我们需要开发一套专门用于植入各个微服务应用的接口供监控 系统采集信息。 而这些接口往往有很大一 部分指标都是类似的, 比如环境变量、 垃圾收集信息、 内存信息、 线程池信息等。 既然这些信息那么通用,难道就没有一个标准化的实现框架吗?
当我们决定用Spring Boot来作为微服务框架时,除了它强大的快速开发功能之外,还因
为它在Starter POMs中提供了一个特殊依赖模块spring-boot-starter-actuor 。引入该模块能够自动为 Spring Boot 构建的应用提供 一系列用千监控的端点。 同时, SpringCloud 在实现各个微服务组件的时候, 进一步为该模块做了不少扩展, 比如, 为原生端点增加了更多的指标和度量信息(比如在整Eureka 的时候会为/health 端点增加相关的信息), 并且根据不同的组件还提供了更多有空的端点(比如, 为 API 网关组件 Zuul 提供了 /routes 端点来返回路由信息)。
spring-boot-starter-actuator 模块的实现对千实施微服务的中小团队来说,可以有效地省去或大大减少监控系统在采集应用指标时的开发量。 当然, 它也并不是万能的, 有时候也需要对其做 一些简单的扩展来帮助我们实现自身系统个性化的监控需求。 所以,在本节将详细介绍一些关于 spring-boo七-starter-acuator 模块的内容, 包括原生提供的端点以及一些常用的扩展和配置方式等。
初识actuator
下面, 我们通过对 “ 快速入门 ” 小节中实现的 Spring Boot 2.0以上 应用增加 spring-boot-starer-actuator 模块功能, 来对它有一个直观的认识。
配置Actuator
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- 浏览器打开链接http://localhost:8080/actuator/
可以看到所有支持的连接,默认只有
/actuator
/actuator/health
/actuator/health/{component}
/actuator/health/{component}/{instance}
/actuator/info
{
"contexts":{
"application":{
"beans":{
"endpointCachingOperationInvokerAdvisor":{
"aliases":[
],
"scope":"singleton",
"type":"org.springframework.boot.actuate.endpoint.invoker.cache.CachingOperationInvokerAdvisor",
"resource":"class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/EndpointAutoConfiguration.class]",
"dependencies":[
"environment"
]
}
}
}
}
}
{
"activeProfiles":[
],
"propertySources":[
{
"name":"server.ports",
"properties":{
"local.server.port":{
"value":8080
}
}
},
{
"name":"servletContextInitParams",
"properties":{
}
},
{
"name":"systemProperties",
"properties":{
"java.vendor":{
"value":"Oracle Corporation"
},
"sun.java.launcher":{
"value":"SUN_STANDARD"
},
"catalina.base":{
"value":"C:%users\timxia\AppData\Local\Temp\tomcat.2979281870254394426.8080"
}
}
}
]
}
常用配置
- 如果要看到所有支持的状态查询,需要配置
management.endpoints.web.exposure.include=*
- 显示所有健康状态,需要加配置
management.endpoint.health.show-details=always
结果
{
"status":"UP",
"details":{
"db":{
"status":"UP",
"details":{
"database":"MySQL",
"hello":1
}
},
"diskSpace":{
"status":"UP",
"details":{
"total":335067213824,
"free":241971175424,
"threshold":10485760
}
},
"redis":{
"status":"UP",
"details":{
"version":"3.2.12"
}
}
}
}
启用端点
默认情况下,除shutdown以外的所有端点均已启用。要配置单个端点的启用,请使用management.endpoint.<id>.enabled
属性。以下示例启用shutdown
端点:
management.endpoint.shutdown.enabled=true
另外可以通过management.endpoints.enabled-by-default
来修改全局端口默认配置,以下示例启用info端点并禁用所有其他端点:
management.endpoints.enabled-by-default=false
management.endpoint.info.enabled=true
注意
禁用的端点将从应用程序上下文中完全删除。如果您只想更改端点公开(对外暴露)的技术,请改为使用include和exclude属性,详情见下文
暴露端点
要更改公开哪些端点,请使用以下技术特定的include和exclude属性:
Property | Default |
---|---|
management.endpoints.jmx.exposure.exclude | * |
management.endpoints.jmx.exposure.include | * |
management.endpoints.web.exposure.exclude | * |
management.endpoints.web.exposure.include | info, health |
include属性列出了公开的端点的ID,exclude属性列出了不应该公开的端点的ID
exclude属性优先于include属性。包含和排除属性都可以使用端点ID列表进行配置。
注意
这里的优先级是指同一端点ID,同时出现在include
属性表和exclude
属性表里,exclude
属性优先于include
属性,即此端点没有暴露
- 例如,要停止通过JMX公开所有端点并仅公开
health
和info
端点,请使用以下属性:
management.endpoints.jmx.exposure.include=health,info
*
可以用来选择所有端点。例如,要通过HTTP公开除env
和beans
端点之外的所有内容,请使用以下属性:
management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env,beans
参考
补充1:Endpoints
内置Endpoints
id | desc | Sensitive |
---|---|---|
auditevents | 显示当前应用程序的审计事件信息 | Yes |
beans | 显示应用Spring Beans的完整列表 | Yes |
caches | 显示可用缓存信息 | Yes |
conditions | 显示自动装配类的状态及及应用信息 | Yes |
configprops | 显示所有 @ConfigurationProperties 列表 | Yes |
env | 显示 ConfigurableEnvironment 中的属性 | Yes |
flyway | 显示 Flyway 数据库迁移信息 | Yes |
health | 显示应用的健康信息(未认证只显示status ,认证显示全部信息详情) | No |
info | 显示任意的应用信息(在资源文件写info.xxx即可) | No |
liquibase | 展示Liquibase 数据库迁移 | Yes |
metrics | 展示当前应用的 metrics 信息 | Yes |
mappings | 显示所有 @RequestMapping 路径集列表 | Yes |
scheduledtasks | 显示应用程序中的计划任务 | Yes |
sessions | 允许从Spring会话支持的会话存储中检索和删除用户会话。 | Yes |
shutdown | 允许应用以优雅的方式关闭(默认情况下不启用) | Yes |
threaddump | 执行一个线程dump | Yes |
httptrace | 显示HTTP跟踪信息(默认显示最后100个HTTP请求 - 响应交换) | Yes |
补充2:SpringBoot中启动是忽略某项检测
在搭建springboot框架时,启动会检测某些连接是否正常,比如redis,rabbitmq等等组件,此时如果没有启动相关服务,或者你这个组件没有用到此组件,则不希望检测此项.
可以在启动文件中填入
management.health.redis.enabled: false ##启动不检测redis
management.health.rabbit.enabled: false ##启动不检查rabbit
management.health.mail.enabled
management.health.mongo.enabled
management.health.solr.enabled
management.health.elasticsearch.enabled
原文地址:https://www.jianshu.com/p/8bfac9289c7e
参考文章:https://www.jianshu.com/p/14d10481845e