23

用InfluxDB+Grafana监控Springboot应用

 3 years ago
source link: https://www.pkslow.com/archives/springboot-monitored-by-influxdb-grafana
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

1 前言

Springboot 的运维监控是微服务系统必备的,不然就感觉是瞎子跑步,随时可能摔跤。本文通过 Springboot 整合 InfluxDB+Grafana 的方式进行监控,以保证系统可靠运行。

InfluxDB 的介绍可以参考: InfluxDB入门及使用,一个优秀的时序数据库

Springboot 监控相关文章:

用Springboot Admin监控你的微服务应用

用Prometheus+Grafana监控Springboot应用

2 Springboot整合InfluxDB

对于 InfluxDB 的安装,前面已经有文章讲过,不再讲解了。 Springboot 整合 InfluxDB 是非常简单的,先引入依赖:

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

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

<dependency>
  <groupId>io.micrometer</groupId>
  <artifactId>micrometer-registry-influx</artifactId>
</dependency>

需要开启 actuator ,然后引入 influx 的依赖。

很多东西 Springboot 都会帮我们做好,我们只需要配置相关信息就可以了,配置文件 application.yaml 如下:

spring:
  application:
    name: springboot-influxdb
server:
  port: 8080
management:
  metrics:
    export:
      influx:
        enabled: true
        db: pkslow
        uri: http://localhost:8086
        user-name:
        password:
        connect-timeout: 1s
        read-timeout: 10s
        auto-create-db: true
        step: 1m
        num-threads: 2
        consistency: one
        compressed: true
        batch-size: 1000

这里我没有配置用户名和密码,因为安装的时候就没有指定,实际项目中要根据实际情况配置。

3 查看数据

整合启动应用后,等一段时间就会有数据源源不断地写入 InfluxDB 中。会为我们自动创建相关的 measurement ,如下:

> show measurements
name: measurements
name
----
jvm_buffer_count
jvm_buffer_memory_used
jvm_buffer_total_capacity
jvm_classes_loaded
jvm_classes_unloaded
jvm_gc_live_data_size
jvm_gc_max_data_size
jvm_gc_memory_allocated
jvm_gc_memory_promoted
jvm_gc_pause
jvm_memory_committed
jvm_memory_max
jvm_memory_used
jvm_threads_daemon
jvm_threads_live
jvm_threads_peak
jvm_threads_states
logback_events
process_cpu_usage
process_files_max
process_files_open
process_start_time
process_uptime
system_cpu_count
system_cpu_usage
system_load_average_1m
tomcat_sessions_active_current
tomcat_sessions_active_max
tomcat_sessions_alive_max
tomcat_sessions_created
tomcat_sessions_expired
tomcat_sessions_rejected
visits

我们随便找一个数据查看一下,如 jvm_memory_used

Fn6rMbA.png!mobile

4 通过Grafana展示

Springboot 的运行数据写进 InfluxDB 了,但通过命令行查看肯定是不方便监控运维的。所以还是需要做数据可视化,在这方面, Grafana 是非常不错的选择。

启动 Grafana

docker run -d -p 3000:3000 grafana/grafana

登陆 Grafana ,配置数据源如下:

aymQjiV.png!mobile

配置图表, Grafana 提供的图表很多,我们这选择最常用的 Graph

eUbu2y.png!mobile

创建后,编辑 Graph

jm63Yri.png!mobile

关联数据:

YJFvey.png!mobile

配置数据如下:

ARfyq2F.png!mobile

配置完成后,展示的数据如下:

VN3aquy.png!mobile

类似的流程,配置一个 Gauge 如下:

aURF3aI.png!mobile

5 总结

本文介绍了如何一步步把 Springboot 指标写入 InfluxDB ,再通过 Grafana 展示数据。

项目的代码在: https://github.com/LarryDpk/pkslow-samples

欢迎关注微信公众号< 南瓜慢说 >,将持续为你更新...

Y7z2U3.png!mobile


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK