SpringBoot使用prometheus监控

1.关于Prometheus
Prometheus是一个根据应用的metrics来进行监控的开源工具。相信很多工程都在使用它来进行监控,有关详细介绍可以查看官网:https://prometheus.io/docs/introduction/overview/
2.有关Grafana
Grafana是一个开源监控利器,如图所示。

从图中就可以看出来,使用Grafana监控很高大上,提供了很多可视化的图标。
官网地址:https://grafana.com/
3.SpringBoot使用Prometheus
3.1 依赖内容
在SpringBoot中使用Prometheus其实很简单,不需要配置太多的东西,在pom文件中加入依赖,完整内容如下所示。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.dalaoyang</groupId>
    <artifactId>springboot2_prometheus</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot2_prometheus</name>
    <description>springboot2_prometheus</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
            <version>1.1.3</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

3.2 配置文件
配置文件中加入配置,这里就只进行一些简单配置,management.metrics.tags.application属性是本文配合Grafana的Dashboard设置的,如下所示:

spring.application.name=springboot_prometheus
management.endpoints.web.exposure.include=*
management.metrics.tags.application=${spring.application.name}

3.3 设置application
修改启动类,如下所示.

@SpringBootApplication
public class Springboot2PrometheusApplication {

    public static void main(String[] args) {
        SpringApplication.run(Springboot2PrometheusApplication.class, args);
    }
    @Bean
    MeterRegistryCustomizer<MeterRegistry> configurer(
            @Value("${spring.application.name}") String applicationName) {
        return (registry) -> registry.config().commonTags("application", applicationName);
    }
}

SpringBoot项目到这里就配置完成了,启动项目,访问http://localhost:8080/actuator/prometheus,如图所示,可以看到一些度量指标。

4.Prometheus配置
4.1 配置应用
在prometheus配置监控我们的SpringBoot应用,完整配置如下所示。

# my global config
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global ‘evaluation_interval‘.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it‘s Prometheus itself.
scrape_configs:
  - job_name: ‘prometheus‘
    static_configs:
    - targets: [‘127.0.0.1:9090‘]
###以下内容为SpringBoot应用配置
  - job_name: ‘springboot_prometheus‘
    scrape_interval: 5s
    metrics_path: ‘/actuator/prometheus‘
    static_configs:
      - targets: [‘127.0.0.1:8080‘]

4.2 启动Prometheus
启动Prometheus,浏览器访问,查看Prometheus页面,如图所示。

点击如图所示位置,可以查看Prometheus监控的应用。


列表中UP的页面为存活的实例,如图所示。

也可以查看很多指数,如下所示。

5.Grafana配置
启动Grafana,配置Prometheus数据源,这里以ID是4701的Doshboard为例(地址:https://grafana.com/dashboards/4701)如图

在Grafana内点击如图所示import按钮

在如图所示位置填写4701,然后点击load。

接下来导入Doshboard。

导入后就可以看到我们的SpringBoot项目对应的指标图表了,如图。

6.源码
源码地址:https://gitee.com/dalaoyang/springboot_learn/tree/master/springboot2_prometheus

原文地址:https://blog.51cto.com/13981400/2400973

时间: 2024-07-31 05:55:18

SpringBoot使用prometheus监控的相关文章

使用Prometheus监控SpringBoot应用

通过之前的文章我们使用Prometheus监控了应用服务器node_exporter,数据库mysqld_exporter,今天我们来监控一下你的应用.(本文以SpringBoot 2.1.9.RELEASE 作为监控目标) 编码 添加依赖 使用Prometheus监控SpringBoot应用只需要在pom文件添加如下两个依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId

基于prometheus监控k8s集群

本文建立在你已经会安装prometheus服务的基础之上,如果你还不会安装,请参考:prometheus多维度监控容器 如果你还没有安装库k8s集群,情参考: 从零开始搭建基于calico的kubenetes 前言 kubernetes显然已成为各大公司亲睐的容器编排工具,各种私有云公有云平台基于它构建,那么,我们怎么监控集群中的所有容器呢?目前有三套方案: heapster+influxDB heapster为k8s而生,它从apiserver获取节点信息,每个节点kubelet内含了cAdv

Prometheus监控 Redis , redis-cli_exporter

用Prometheus监控Redis 的时候,发现redis_exporter不能对redis里面的队列(list)长度进行监控,Google了一下,发现官网也没有redis-cli_exporter.索性就自己用Python写了一个Rediscli_exporter. #!/usr/bin/env python #coding:utf-8 #Author: [email protected] import prometheus_client import redis from promethe

prometheus监控第二篇之grafana

prometheus监控第二篇 ??1. 使用prometheus监控kube-state-metrics ??上篇博文我们已经成功安装了prometheus.并且使用prometheus成功监控了redis应用.同时使用prometheus监控了kubernetes-service-endpoints.kubernetes-nodes.kubernetes-kubelet.kubernetes-cadvisor.kubernetes-apiserveres等.现在开始使用prometheus监

prometheus监控第二篇之告警alertmanager

kubernetes之prometheus监控第二篇-alertmanager监控告警:   在前期的博文中,我已经简单的介绍过了prometheus的安装,以及通过grafana来实施监控.这篇博文,我们更深入的介绍一下prometheus的监控.本篇博文主要分为以下几个知识点: 1. 使用prometheus监控ceph存储: 2. 学习简单的PromQL语言,在grafana里面根据业务自定义dashboard; 3. alertmanager自定义告警的配置:讲述邮件告警和企业微信告警:

基于k8s集群部署prometheus监控etcd

目录 基于k8s集群部署prometheus监控etcd 1.背景和环境概述 2.修改prometheus配置 3.检查是否生效 4.配置grafana图形 基于k8s集群部署prometheus监控etcd 1.背景和环境概述 本文中涉及到的环境中.prometheus监控和grafana基本环境已部署好.etcd内置了metrics接口供收集数据,在etcd集群任意一台节点上可通过ip:2379/metrics检查是否能正常收集数据. curl -L http://localhost:237

Prometheus 监控K8S Node监控

Prometheus 监控K8S Node监控 Prometheus社区提供的NodeExporter项目可以对主机的关键度量指标进行监控,通过Kubernetes的DeamonSet可以在各个主机节点上部署有且仅有一个NodeExporter实例,实现对主机性能指标数据的监控,但由于容器隔离原因,使用容器NodeExporter并不能正确获取到宿主机磁盘信息,故此本课程将NodeExporter部署到宿主机. node_exporter:用于*NIX系统监控,使用Go语言编写的收集器 使用文档

Prometheus(二):Prometheus 监控Windows机器

一.安装wmi-exporter 首先在需要监控的Windows机器上安装wmi_exporter.wmi_exporter下载地址:https://github.com/martinlindhe/wmi_exporter/releases 下载后,双击即可完成安装. 完成安装后,会自动创建一个开机自启的服务 验证服务是否启动,默认wmi-exporter端口为9182 浏览器访问  http://192.168.56.1:9182/metrics  (Windows系统IP地址:9182端口)

Prometheus(三):Prometheus监控交换机(snmp)

默认已安装Prometheus服务,服务地址:192.168.56.200 一.获取交换机snmp信息 snmp服务IP(交换机IP):172.20.2.83 snmp community:dfetest 二.部署 snmp_exporter 2.1 下载snmp_exporter 下载snmp_exporter安装包,下载地址:https://github.com/prometheus/snmp_exporter/releases 下载完成后,上传至机器的  /usr/local 目录下 解压