Prometheus+Grafana实现服务性能监控:windows主机监控、Spring Boot监控、Spring Cloud Alibaba Seata监控

发布时间 2023-11-02 16:36:42作者: sowler

1、Prometheus介绍

Prometheus使用Go语言开发,中文名称叫:普罗 米修斯。Prometheus是一个开源系统最初在SoundCloud构建的监控和警报工具包。自 2012 年成立以来,许多公司和组织已经采用了 Prometheus,它现在是一个独立的开源项目并独立于任何公司进行维护。Prometheus于2016年加入云原生计算基金会作为继 Kubernetes之后的第二个托管项目。Prometheus主要用于抓取目标数据,并记录所有抓取的目标数据。

官网地址

https://prometheus.io/

Github地址

https://github.com/prometheus/prometheus

2、下载Prometheus并启动

官网下载:

https://prometheus.io/download/

Github下载:

https://github.com/prometheus/prometheus/releases

通过访问上面的地址,可以选择下载Prometheus的版本。这里选择下载windows版本:

下载成功后,解压压缩包,进入prometheus-2.44.0-rc.0.windows-386目录我们就可以启动程序了。

启动程序:prometheus.exe

程序启动成功后,访问端口9090进入系统:

http://localhost:9090/graph

成功进入系统后,我们可以访问Targets来查看接入的目标数据,由于我们还没有配置其他目标数据采集,目前只能检测到自己的信息:

3、安装可视化工具Grafana

通过上面启动Prometheus,会发现上面的展示信息都是数据而不是通过图表展示出来,这个看起来非常费力。所以需要通过第三方工具通过图表的方式用来把Prometheus收集的数据展现出来。

官网地址:

https://grafana.com/

进入官网后,可以在下载里面进行下载程序或者直接点击链接下载

https://grafana.com/grafana/download?pg=get&plcmt=selfmanaged-box1-cta1

Github地址:

https://github.com/grafana/grafana

Github ChangeLog:

https://github.com/grafana/grafana/blob/main/CHANGELOG.md

选择版本和操作系统进行下载,下载成功后进入grafana文件里面进入/bin目录,启动程序:

启动程序:grafana-server.exe

等待窗口加载完毕,访问地址:http://localhost:3000  默认账号密码:admin/admin 就可以登录系统了,首次登录需要修改admin密码。

在grafana中添加数据源

点击添加数据源,选择Prometheus:

配置连接信息:

配置成功后点击保存,这样我们的数据源就配置成功了。

4、Grafana官网模板

在grafana中我们可以自己配置仪表盘展示信息也可以通过引入官方模板进行展示。模板地址:

https://grafana.com/grafana/dashboards/

记录一下常用模板信息:

模板编号ID:
9276:主机基础监控(cpu,内存,磁盘,网络)
12900:SpringBoot Dashboard
4701:JVM模板
10467:windows_exporter-Windows Server监控模板导入
11587:OhmGraphite-0.21.0-Ohm Windows Desktop

5、使用Prometheus+Grafana实现windows主机监控

要想监控windows系统,首先我们需要一个windows监控工具,然后把这个监控地址的连接集成到Prometheus中,最后在通过Grafana进行展示出来。这里介绍两个window监控工具

第一个windows_exporter下载地址:

https://github.com/prometheus-community/windows_exporter/releases

选择版本进行下载,下载windows运行程序就可以了,直接启动即可。启动完成端口为:9182,如果启动成功访问:http://localhost:9182/metrics 就可以以数据的格式展示出我们当前主机的配置信息。

接下来就需要把地址配入到Prometheus中,让Prometheus进行管理。进入Prometheus目录,修改Prometheus配置文件prometheus.yml并重新启动Prometheus

重新启动Prometheus后,再次查看Targets标签,发现windows_exporter接入成功,状态为 UP(正常)

最后我们就需要把数据通过Grafana以图表形式展现出来,首先登录我们安装成功的grafana系统,点击+号导入官方提供的模板。

输入模板ID:10467,点击Load:

选择数据源为Prometheus

点击导入,我们就可以查看到windows系统的配置信息

通过上面的步骤,我们就成功配置成功了一个监控信息了。下面介绍windows的另一个监控软件:OhmGraphite,下载地址:

https://github.com/nickbabcock/OhmGraphite/releases

需要下载压缩包文件,下载成功后进行解压。解压成功后需要进入OhmGraphite-0.21.0目录,编辑OhmGraphite.exe.config文件,增加prometheus作为接收数据源。支持Graphite、InfluxDB、Prometheus、Timescale / Postgres。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="host" value="localhost" />
    <add key="port" value="2003" />
    <add key="interval" value="5" />
	  <!--开启prometheus-->
	<add key="type" value="prometheus" />
    <add key="prometheus_port" value="4000" />
	<add key="prometheus_host" value="*" />
  </appSettings>
</configuration>

通过cmd命令行执行:

OhmGraphite.exe install

启动:

OhmGraphite.exe start

启动成功后访问:http://127.0.0.1:4000/metrics 就可以访问的监控数据。后面步骤就和第一次集成一致修改Prometheus配置文件在scrape_configs中添加新的路径,导入Grafana。导入模板ID:11587:OhmGraphite-0.21.0-Ohm Windows Desktop

 #被监控端的配置,目前只有一个节点,就是prometheus本身
scrape_configs:
  - job_name: "prometheus"
    static_configs:
      - targets: ["localhost:9090"]
  - job_name: "windows_exporter"
    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
    static_configs:
    - targets: ["localhost:9182"] 
  - job_name: "windows_ohm_graphite"
    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
    static_configs:
    - targets: ["localhost:4000"] 

6、SpringBoot监控

首先在SpringBoot项目中导入依赖

        <!--prometheus 监控平台 集成micrometer,将监控数据存储到 prometheus-->
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
        </dependency>
        <!--监控-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

在配置文件bootstrap.yml或application.yml中加入配置暴露端点

#暴露端点
management:
  endpoints:
    web:
      exposure:
        include: '*'

配置成功后,启动项目访问地址:http://IP:PORT/actuator/prometheus

http://localhost:8810/actuator/prometheus

就可以查看到监控数据

数据获取成功后,后面就和原来的步骤一样了,修改Prometheus配置文件在scrape_configs中添加新的路径,导入Grafana。

  - job_name: "spring-boot-order-8810"
    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
    metrics_path: "/actuator/prometheus" #路径变了,记得要更改访问路径
    static_configs:
    - targets: ["localhost:8810"]

这里还是使用官方提供的模板,模板ID:12900:SpringBoot Dashboard

7、Spring Cloud Alibaba Seata事务监控

Seata支持在TC开启Metrics数据采集并输出到Prometheus监控系统中。默认是关闭状态,需要开启 metrics 的采集配置。进入\seata\conf,修改或新增metrics

## metrics configuration, only used in server side
metrics {
  enabled = true
  registryType = "compact"
  # multi exporters use comma divided
  exporterList = "prometheus"
  exporterPrometheusPort = 9898
  registry-type: compact
}

修改成功后,重新启动seata。输入http://IP:9898/metrics,即可获得最新的Metrics数据

http://localhost:9898/metrics

修改Prometheus配置文件添加seata:

查看Targets

在Grafana中点击新建控制台,添加seata信息:

选择数据源

搜索seata_transactional

点击Run queries查询,就可以成功获取seata信息数据

获取成功保存图表即可。我们点击图表下面的颜色标签,也可以查看不同状态下seata事务的使用情况以及事务提交状况。好了,本次关于Prometheus+Grafana的学习就到这里,以后如果会遇到其他使用情况也会做相关的记录,方便以后再次遇到相同的问题时再次快速查找。最后感谢阅读本篇的读友,让我们一起成长、共同进步。