SpringBoot+Prometheus+Grafana实现系统可视化监控

场景

SpringBoot中集成Actuator实现监控系统运行状态:

SpringBoot中集成Actuator实现监控系统运行状态_springboot actuator 获取系统运行时长_霸道流氓气质的博客-CSDN博客

基于以上Actuator实现系统监控,还可采用如下方案。

Prometheus

Prometheus,是一个开源的系统监控和告警的工具包,其采用Pull方式采集时间序列的度量数据(也支持push方式),

通过Http协议传输。它的工作方式是被监控的服务需要公开一个Prometheus端点,这端点是一个HTTP接口,

该接口公开了度量的列表和当前的值,然后Prometheus应用从此接口定时拉取数据,一般可以存放在时序数据库中,

然后通过可视化的Dashboard(e.g.Grafana)进行数据展示。

Grafana

grafana,是一个开源的dashboard展示工具,可以支持很多主流数据源,包括时序性的和非时序性的。

其提供的展示配置以及可扩展性能满足绝大部分时间序列数据展示需求,是一个比较优秀的工具。

注:

博客:
霸道流氓气质_C#,架构之路,SpringBoot-CSDN博客

扫描二维码关注公众号,回复: 16440002 查看本文章

实现

1、新建SpringBoot项目并添加依赖

        <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>
        <!-- prometheus support -->
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
        </dependency>

2、修改yml文件,添加如下配置

management:
  metrics:
    export:
      prometheus:
        enabled: true
        step: 1m
        descriptions: true
  web:
    server:
      auto-time-requests: true
  endpoints:
    prometheus:
      id: springmetrics
    web:
      exposure:
        include: health,info,env,prometheus,metrics,httptrace,threaddump,heapdump,springmetrics

# 开发环境配置
server:
  # 服务器的HTTP端口,默认为8080
  port: 996
  servlet:
    # 应用的访问路径
    context-path: /
  tomcat:
    # tomcat的URI编码
    uri-encoding: UTF-8
    # tomcat最大线程数,默认为200
    max-threads: 800
    # Tomcat启动初始化的线程数,默认值25
    min-spare-threads: 30

注意这里配置的端口是996

3、启动项目,访问

http://127.0.0.1:996/actuator/prometheus

即可以看到暴露的信息

 

4、Prometheus下载安装

下载地址:

Download | Prometheus

这里是在windows上,所以选择对应windows的zip包

当然还要其他下载安装的方式,比如docker等方式。

如果官网迟迟下载不下来,可从网络上搜索对应windows的包下载并解压。

解压之后找到prometheus.yml修改配置文件中metrics_path为对应的路径以及targets为对应的ip和端口。

 

更多配置说明参考官方文档

https://github.com/prometheus/prometheus/blob/main/documentation/examples/prometheus.yml

修改完配置文件之后,双击prometheus.exe启动,然后等待启动成功并访问默认的9090端口

http://localhost:9090

在Expression中输入

jvm_memory_used_bytes

然后查看监控效果

5、Grafana下载安装

下载地址:

Index of grafana-local

下载之后并解压,找到bin下grafana-server.exe双击启动

 

启动成功后访问

http://127.0.0.1:3000/login

 

默认账户admin,密码也是admin

6、登录并提示修改密码之后,点击DATA SOURCES

然后添加数据源的类型为Prometheus

 

输入上面启动的对应的地址

7、点击右上角新建New dashboard

 

然后点击Add visualization

 

然后选择刚添加的数据源

 

然后下方选择一个参数比如jvm_buffer_count_buffers,其它参数根据自己需要设置,保存即可查看。

猜你喜欢

转载自blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/131931722