Springboot 集成Prometheus 数据采集 使用grafana 监控报告告警 邮件配置

本文详细介绍了如何在Springboot项目中配置Pom文件,引入Actuator和Prometheus依赖以收集应用指标。接着,展示了如何配置Prometheus.yml文件来抓取数据,并在Grafana中设置监控模板和告警规则,包括邮件接收人的设定和SMTP配置。此外,还提到了安全配置的非必选情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

Springboot 相关

Pom

重点包

如果有需要可以增加安全包-一般内部机房没啥事-(非必选)

Application.yml配置文件-(非必选)

Application.properties

management.endpoints.web.exposure.include介绍

启动类

查看监控信息

Prometheus

Prometheus.yml 配置

如果使用类安全包-(非必选)

启动就可以看到了

Grafana 模板 12900

一、报告模板内容

二、设置告警邮件接收人

三、邮箱发送人配置(找个自己常用的邮箱开启smtp相关权限配置即可)

然后专门配置几个告警规则 走走测试验证下即可


 

Springboot 相关

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 https://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.2.4.RELEASE</version>

       <relativePath/> <!-- lookup parent from repository -->

    </parent>

    <groupId>com.example</groupId>

    <artifactId>springboot2demo</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <name>springboot2demo</name>

    <description>Demo project for Spring Boot</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>io.micrometer</groupId>

           <artifactId>micrometer-registry-prometheus</artifactId>

           <version>1.1.4</version>

       </dependency>



       <dependency>

           <groupId>org.springframework.boot</groupId>

           <artifactId>spring-boot-starter-test</artifactId>

           <scope>test</scope>

           <exclusions>

              <exclusion>

                  <groupId>org.junit.vintage</groupId>

                  <artifactId>junit-vintage-engine</artifactId>

              </exclusion>

           </exclusions>

       </dependency>

    </dependencies>



    <build>

       <plugins>

           <plugin>

              <groupId>org.springframework.boot</groupId>

              <artifactId>spring-boot-maven-plugin</artifactId>

           </plugin>

       </plugins>

    </build>



</project>

重点包
<dependency>

           <groupId>org.springframework.boot</groupId>

           <artifactId>spring-boot-starter-actuator</artifactId>

       </dependency>

      

       <dependency>

           <groupId>io.micrometer</groupId>

           <artifactId>micrometer-registry-prometheus</artifactId>

       </dependency>

如果有需要可以增加安全包-一般内部机房没啥事-(非必选)

 

 <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-security</artifactId>

        </dependency>

Application.yml配置文件-(非必选)
security: 
  user: 
    name: admin 
    password: 1234 
  basic: 
    enabled: true 
    # 安全路径列表,逗号分隔,此处只针对/actuator路径进行认证 
    path: /actuator
 

Application.properties

server.port=8101

spring.application.name=springBootDemo

# 打开所有 Actuator 服务

management.endpoints.web.exposure.include=*

# 将应用名称添加到计量器的 tag 中去

# 以便 Prometheus 根据应用名区分不同服务

management.metrics.tags.application=${spring.application.name}

management.endpoints.web.exposure.include介绍

路径    描述

/autoconfig   提供了一份自动配置报告,记录哪些自动配置条件通过了,哪些没通过

/beans 描述应用程序上下文里全部的Bean,以及它们的关系

/env    获取全部环境属性

/configprops 描述配置属性(包含默认值)如何注入Bean

/dump 获取线程活动的快照

/health     报告应用程序的健康指标,这些值由HealthIndicator的实现类提供

/info    获取应用程序的定制信息,这些信息由info打头的属性提供

/mappings     描述全部的URI路径,以及它们和控制器(包含Actuator端点)的映射关系

/metrics    报告各种应用程序度量信息,比如内存用量和HTTP请求计数

/shutdown     关闭应用程序,要求endpoints.shutdown.enabled设置为true

/trace  提供基本的HTTP请求跟踪信息(时间戳、HTTP头等)

/prometheus

启动类

package com.example.springboot2demo;



import io.micrometer.core.instrument.MeterRegistry;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.context.annotation.Bean;



@SpringBootApplication

public class Springboot2demoApplication {



    public static void main(String[] args) {

        SpringApplication.run(Springboot2demoApplication.class, args);

    }



    @Bean

    MeterRegistryCustomizer<MeterRegistry> configurer(

            @Value("${spring.application.name}") String applicationName) {

        return (registry) -> registry.config().commonTags("application", applicationName);

    }

}

查看监控信息

http://localhost:8101/actuator/prometheus

 

Prometheus

Prometheus.yml 配置

- job_name: " actuator-demo"

    metrics_path: "/actuator/prometheus"

    static_configs:

      - targets: ["localhost:8101"]

如果使用类安全包-(非必选)

- job_name: 'monitor-demo'

    scrape_interval: 5s # 刮取的时间间隔

    scrape_timeout: 5s 

    metrics_path: /actuator/prometheus

    scheme: http 

    basic_auth: #认证信息

      username: admin

      password: 1234

    static_configs:

      - targets:

        - 127.0.0.1: 8101 #此处填写 Spring Boot 应用的 IP + 端口号

启动就可以看到了

 

Grafana 模板 12900

一、报告模板内容

程序运行  Jvm   tomcat 请求响应  日志

 

 

 

 

 

 

 

 

 

二、设置告警邮件接收人

 

三、邮箱发送人配置(找个自己常用的邮箱开启smtp相关权限配置即可)

Grafana默认使用conf目录下defaults.ini作为配置文件运行 在这直接改就ok

 

#################################### SMTP / Emailing #####################

[smtp]

enabled = true

host = smtp.exmail.qq.com:465

user = xxxx@ininin.com

# If the password contains # or ; you have to wrap it with triple quotes. Ex """#password;"""

password = XXX

cert_file =

key_file =

skip_verify = true

from_address = xxxx@ininin.com

from_name = Grafana

ehlo_identity = ininin.com

然后专门配置几个告警规则 走走测试验证下即可

 

 

ok

持续更新

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值