Springboot2 Metrics之actuator集成influxdb, Grafana提供监控和报警
到目前为止,各种日志收集,统计监控开源组件数不胜数,即便如此还是会有很多人只是tail -f查看一下日志文件。随着容器化技术的成熟,日志和metrics度量统计已经不能仅仅靠tail -f来查看了,你甚至都不能进入部署的机器。因此,日志收集和metrics统计就必不可少。日志可以通过logstash或者filebeat收集到ES中用来查阅。对于各种统计指标,springboot提供了actuator组件,可以对cpu, 内存,线程,request等各种指标进行统计,并收集起来。本文将粗略的集成influxdb来实现数据收集,以及使用Grafana来展示。
最终dashboard模板: https://github.com/Ryan-Miao/boot-metrics-exporter/blob/master/grafana/grafana-dashboard-template.json
最终获得如下统计报表:
对于redis cache命中率的统计:
对于单独重要request的统计
基于health check的alert
安装influxdb和Grafana
安装influxdb:
https://www.cnblogs.com/woshimrf/p/docker-influxdb.html
安装Grafana:
https://www.cnblogs.com/woshimrf/p/docker-grafana.html
Springboot配置
可以直接使用封装好的starter:
https://github.com/Ryan-Miao/boot-metrics-exporter
或者:
引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-influx</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
定义MeterConfig, 用来统一设置一些tag,比如instance id
@Component
public class MeterConfig implements MeterRegistryCustomizer {
private static final Logger LOGGER = LoggerFactory.getLogger(MeterConfig.class);
@Override
public void customize(MeterRegistry registry) {
try {
String hostAddress = InetAddress.getLocalHost().getHostAddress();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("设置metrics实例id为ip:" + hostAddress);
}
registry.config().commonTags("instance-id", hostAddress);
} catch (UnknownHostException e) {
String uuid = UUID.randomUUID().toString();
registry.config().commonTags("instance-id", uuid);
LOGGER.error("获取实例ip失败,设置实例id为uuid:" + uuid, e);
}
}
}
添加对应的配置:
management:
metrics:
export:
influx:
db: my-db
uri: http://192.168.5.9:8086
user-name: admin
password: admin
enabled: true
web:
server:
auto-time-requests: true
tags:
app: ${spring.application.name}
这里选择将metric export到influxdb,还有很多其他存储方案可选。
网络配置
grafana和influxdb可能部署在某个vpc,比如monitor集群。而需要收集的业务系统则遍布在各个业务线的vpc内,因此需要业务集群打通访问influxdb的网络和端口。
自定义Metrics
Springboot actuator暴露的health接口只有up/down的选择,在grafana如何使用这个来判断阈值,我还没找到,于是转换成了数字。
自定义MeterBinder
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.binder.MeterBinder;
import lombok.Data;
@Data
public class HealthMetrics implements MeterBinder {
/**
* 100 up
* 0 down
* 0 unknown
*/
private Integer health = 100;
@Override
public void bindTo(MeterRegistry registry) {
Gauge.builder("health", () -> health)
.register(registry);
}
}
定义每30s更新一下状态:
public abstract class AbstractHealthCheckStatusSetter {
private final HealthMetrics healthMetrics;
protected AbstractHealthCheckStatusSetter(HealthMetrics healthMetrics) {
this.healthMetrics = healthMetrics;
}
/**
* 修改health的状态定义。修改HealthMetrics.health的value。
*/
public abstract void setHealthStatus(HealthMetrics h);
/**
* 定时更新health统计.
*/
@PostConstruct
void doSet() {
ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(1);
scheduledExecutorService.scheduleWithFixedDelay(
() -> setHealthStatus(healthMetrics), 30L, 30L, TimeUnit.SECONDS);
}
}
实现类
public class HealthCheckStatusSetter extends AbstractHealthCheckStatusSetter {
private final HealthEndpoint healthEndpoint;
public HealthCheckStatusSetter(HealthMetrics healthMetrics, HealthEndpoint healthEndpoint) {
super(healthMetrics);
this.healthEndpoint = healthEndpoint;
}
@Override
public void setHealthStatus(HealthMetrics healthMetrics) {
Health health = healthEndpoint.health();
if (health != null) {
Status status = health.getStatus();
switch (status.getCode()) {
case "UP": {
healthMetrics.setHealth(100);
break;
}
case "DOWN":
;
case "UNKNOWN":
;
default: {
healthMetrics.setHealth(0);
break;
}
}
}
}
}
加入配置
@Bean
@ConditionalOnMissingBean
public HealthMetrics healthMetrics() {
return new HealthMetrics();
}
/**
* 这里采用healthEndpoint来判断系统的健康状况。如果有别的需要,可以实现AbstractHealthCheckStatusSetter,自己设置health.
*/
@Bean
@ConditionalOnMissingBean
@ConditionalOnBean(HealthEndpoint.class)
public AbstractHealthCheckStatusSetter healthCheckSchedule(HealthEndpoint healthEndpoint, HealthMetrics healthMetrics) {
return new HealthCheckStatusSetter(healthMetrics, healthEndpoint);
}
Redis cache命中率统计
整套metrics监控是基于Spring boot actuator来实现的,而actuator是通过io.micrometer来做统计的。那么就可以通过自定义micrometer metrics的方式来添加各种metric。比如我们常用redis作为缓存,那么缓存的命中率是我们所关注的。可以自己写一套counter来记录:命中hit+1,没命中miss+1.
也可以直接使用redisson。
我们使用RedissonCache来集成spring cache, 此时cache的命中统计指标就已经被收集好了。
Cache基本统计指标的定义:
然而,统计的结果是按行存储的:
怎么基于此计算命中率呢?
hit-rate= sum(hit)/sum(hit+miss)
因此,我手动对这个序列做了整合:
DROP CONTINUOUS QUERY cq_cache_hit ON my-db
DROP CONTINUOUS QUERY cq_cache_miss ON my-db
DROP measurement cache_hit_rate
CREATE CONTINUOUS QUERY "cq_cache_hit" ON "my-db" RESAMPLE EVERY 10m BEGIN SELECT sum("value") AS hit INTO "cache_hit_rate" FROM "rp_30days"."cache_gets" WHERE ( "result" = 'hit') GROUP BY time(10m),"app", "cache" fill(0) END
CREATE CONTINUOUS QUERY "cq_cache_miss" ON "my-db" RESAMPLE EVERY 10m BEGIN SELECT sum("value") AS miss INTO "cache_hit_rate" FROM "rp_30days"."cache_gets" WHERE ( "result" = 'miss') GROUP BY time(10m),"app", "cache" fill(0) ENDD
监控告警
Grafana提供了alert功能,当查询的指标不满足阈值时,发出告警。
选择influxdb or Prometheus ?
关于收集metric指标的存储方案,大多数教程都是Prometheus, 生态比较完整。我当时之所以选择influxdb,仅仅是因为容器的网络问题。Prometheus需要访问实例来拉取数据,需要允许Prometheus访问业务网络,那我就得不停打通网络,而且,k8s集群不同的网络是不通的,没找到网络打通方案。而influx这种只要实例push数据。同样的,还可以选择es。
influxdb有单点局限性,以及数量大之后的稳定性等问题。需要合理的计算时间间隔的数据。比如,对于几天几个月等查询,提前汇总细粒度的统计。
还有一种据说可以无限扩展的方案就是OpenTSDB. 暂未研究。
会遇到的问题
当前demo是influxdb单点,极其脆弱,稍微长点的时间间隔查询就会挂掉,也只能用来做demo,或者只是查看最近15min这种简单的实时查看。对于近几个月,一年这种长时间聚合,只能提前做好聚合函数进行粗粒度的统计汇总。
参考
Springboot2 Metrics之actuator集成influxdb, Grafana提供监控和报警的更多相关文章
- Spring Boot 微服务应用集成Prometheus + Grafana 实现监控告警
Spring Boot 微服务应用集成Prometheus + Grafana 实现监控告警 一.添加依赖 1.1 Actuator 的 /prometheus端点 二.Prometheus 配置 部 ...
- jmeter+influxdb+grafana性能测试监控
背景: 话说Jmeter原生的监控确实太丑了,听大佬们在讨论Jmeter+InfluxDb+Grafana的监控,于是,为了有一个漂亮的测试报告,就手动开始进行部署. 安装步骤: 1.influxdb ...
- influxDB1.6版安装与配置(windows环境)、Jmeter+influxDB+Grafana性能监控
influxDB1.6版安装与配置(windows环境).Jmeter+influxDB+Grafana性能监控 来源:https://blog.csdn.net/SwTesting/article/ ...
- 微服务监控之二:Metrics+influxdb+grafana构建监控平台
系统开发到一定的阶段,线上的机器越来越多,就需要一些监控了,除了服务器的监控,业务方面也需要一些监控服务.Metrics作为一款监控指标的度量类库,提供了许多工具帮助开发者来完成自定义的监控工作. 使 ...
- 利用Metrics+influxdb+grafana构建监控平台(转)
转自http://www.jianshu.com/p/fadcf4d92b0e 这里再配合Influxdb和Grafana可以构建一个非常漂亮的实时监控界面. Grafana监控界面 采集数据(Met ...
- influxdb + Grafana可视化监控平台
在centos6.5上influxdb + Grafana监控平台配置: 1.RedHat and CentOS users can install the latest stable version ...
- 利用Metrics+influxdb+grafana构建监控平台
https://blog.csdn.net/fishmai/article/details/51817429
- Spring Boot Actutaur + Telegraf + InFluxDB + Grafana 构建监控平台
完成一套精准,漂亮图形化监控系统从这里开始第一步 Telegraf是收集和报告指标和数据的代理 它是TICK堆栈的一部分,是一个用于收集和报告指标的插件驱动的服务器代理.Telegraf拥有插件或集成 ...
- Spring Boot Actutaur + Telegraf + InFluxDB + Grafana 构建监控平台之应用数据分析
本节将引入完美的granafa仪表板,在上节的基础上,并提出自己的一些监控数据的总结和看法 你可以有一个类似于这个的Dashboard,会引入监控Zimbra协作 本节环境采用的是centos7系统, ...
随机推荐
- 定时任务之SpringSchedule的注意事项
在我们现在的项目中,或多或少的都会涉及到定时任务,Spring在3.0之后引入了SpringSchedule,这让我们在使用Spring的时候,可以很容易的整合SpringSchedule.但是好用归 ...
- java war包 路径--解决war包中文件路径问题
https://blog.csdn.net/u013409283/article/details/51480948 转自:http://free-chenwei.iteye.com/blog/1507 ...
- 朋友想玩下百度的ORC我鼓捣鼓捣thinkphp3集成百度sdk
他想玩的是文字识别 那就玩下 咱们开始 1 先到百度文字识别 添加个应用 这样就有了APPID API KEY SECRET KEY https://console.bce.baidu.com ...
- .net 通过反射实现两个相同结构实体类的转换
public static T2 CopyToModel<T1, T2>(T1 source) { T2 model = default(T2); PropertyInfo[] pi = ...
- 终于我还是没忍住,用Python爬了一波女神
你学爬虫,最终不还是为了爬妹子 啥也不说,开始福利赠送~ 女神大会 不是知道有多少人知道“懂球帝”这个 APP(网站),又有多少人关注过它的一个栏目“女神大会”,在这里,没有足球,只有女神哦. 画风是 ...
- flex布局开发
flex布局开发 布局原理 flex时flexible Box的缩写,意为"弹性布局",用来为盒子模型提供最大的灵活性,任何一个容器都可以定位flex布局 [注意] 当我们为父盒子 ...
- 作用域,作用域链,垃圾收集,js解析
变量中包含基本数据类型和引用数据类型,基本类型指简单的数据值,引用类型由多个值构成的对象. 引用类型可以为其添加属性和方法,也可以改变和删除属性和方法. 复制变量值: 基本类型:一个变量向另一 ...
- 移动OA办公——Smobiler第一个开源应用解决方案,快来get吧
产品简介 SmoONE是一款移动OA类的开源解决方案,通过Smobiler平台开发,包含了注册.登陆.用户信息等基本功能.集成了OA中使用场景较多的报销.请假.部门管理.成本中心等核心功能. 免费获取 ...
- git设置多账户
1.设置公司gitlab 0.先给git 设置一个全局的账户, 如果是公司的电脑环境, 全局的账户当然是用你在公司的邮箱了 git config --global user.name "yo ...
- day35作业
1. 查询所有大于60分的学生的姓名和学号 (DISTINCT: 去重) -- 2.查询每个老师教授的课程数量 和 老师信息 -- 3. 查询学生的信息以及学生所在的班级信息 -- 4.学生中男生的个 ...