关于java:Spring-Boot-Prometheus-Grafana-打造可视化监控一目了然

3次阅读

共计 4729 个字符,预计需要花费 12 分钟才能阅读完成。

作者:烟味 i \
链接:https://www.cnblogs.com/2YSP/…

一、背景

Spring Boot 的利用监控计划比拟多,SpringBoot + Prometheus + Grafana 是目前比拟罕用的计划之一。

它们三者之间的关系大略如下图:

二、开发 SpringBoot 利用

首先,创立一个 SpringBoot 我的项目,pom 文件如下:

<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.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

<!-- https://mvnrepository.com/artifact/io.prometheus/simpleclient_spring_boot -->
<dependency>
    <groupId>io.prometheus</groupId>
    <artifactId>simpleclient_spring_boot</artifactId>
    <version>0.8.1</version>
</dependency>

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

举荐一个 Spring Boot 基础教程及实战示例:
https://github.com/javastacks…

留神: 这里的 SpringBoot 版本是 1.5.7.RELEASE,之所以不必最新的 2.X 是因为最新的 simpleclient_spring_boot 只反对 1.5.X,不确定 2.X 版本的是否反对。

MonitorDemoApplication 启动类减少注解

package cn.sp;

import io.prometheus.client.spring.boot.EnablePrometheusEndpoint;
import io.prometheus.client.spring.boot.EnableSpringBootMetricsCollector;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnablePrometheusEndpoint
@EnableSpringBootMetricsCollector
@SpringBootApplication
 public class MonitorDemoApplication {public static void main(String[] args) {SpringApplication.run(MonitorDemoApplication.class, args);
     }

 }

配置文件 application.yml

server:
  port: 8848
spring:
  application:
    name: monitor-demo

security:
  user:
    name: admin
    password: 1234
  basic:
    enabled: true
    # 平安门路列表,逗号分隔,此处只针对 /admin 门路进行认证
    path: /admin

# actuator 裸露接口的前缀
management:
  context-path: /admin
  # actuator 裸露接口应用的端口,为了和 api 接口应用的端口进行拆散
  port: 8888
  security:
    enabled: true
    roles: SUPERUSER

测试代码 TestController

@RequestMapping("/heap/test")
@RestController
public class TestController {public static final Map<String, Object> map = new ConcurrentHashMap<>();

    @RequestMapping("")
    public String testHeapUsed() {for (int i = 0; i < 10000000; i++) {map.put(i + "", new Object());
        }
        return "ok";
    }
}

这里的逻辑就是在申请这个接口后,创立大量对象保留到 map 中减少堆内存使用量,不便前面测试邮件报警。

启动我的项目后,能够在 IDEA 中看到有很多 Endpoints,如图:

开始我的 IDEA 是不显示这个 Endpoints,起初发现是我应用的 idea 版本太老了, 还是 2017.1 的,
而这个须要 idea2017.2 版本以上能力看到。
起初只好从新下载安装,弄了良久。。。。

启动结束,拜访 http://localhost:8888/admin/p… 就能够看到服务裸露的那些监控指标了。

留神:

因为开启了平安认证,所以拜访这个 URL 的须要提醒输出账号 / 明码,如果提醒 404 请查看下你的申请地址是否正确,如果不设置 management.context-path 则默认地址是 http://ip:port/prometheus

三、装置 Prometheus

下载地址点击这里,本文下载的是 Windows 版本 prometheus-2.17.2.windows-amd64.tar.gz。

解压后批改 prometheus.yml 文件,配置数据采集的指标信息。

scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  # - job_name: 'prometheus'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    # static_configs:
    # - targets: ['localhost:9090']
  - job_name: 'monitor-demo'
    scrape_interval: 5s # 刮取的工夫距离
    scrape_timeout: 5s
    metrics_path: /admin/prometheus
    scheme: http
    basic_auth: #认证信息
      username: admin
      password: 1234
    static_configs:
      - targets:
        - 127.0.0.1:8888  #此处填写 Spring Boot 利用的 IP + 端口号

更多配置信息请查看官网文档。

当初能够启动 Prometheus 了,命令行输出:prometheus.exe –config.file=prometheus.yml
拜访 http://localhost:9090/targets,查看 Spring Boot 采集状态是否失常。

四、装置 Grafana

下载地址点击这里,本文用到的是 Windows 版本 grafana-6.3.3.windows-amd64.zip。

解压后运行 bin 目录下的 grafana-server.exe 启动,游览器拜访 http://localhost:3000 即可看到登录页面,默认账号密码是 admin/admin。

当初开始创立本人的可视化监控面板。

1. 设置数据源

2. 创立一个 Dashboard

3. 填写采集的指标点

留神: 这里的指标点不能轻易填,必须是已有的能够在 Prometheus 看到。

4. 抉择图表款式

5. 填写题目形容

最初点击右上角的保留,输出 Dashboad 的名称即可。

Tips: 这里的图表布局是能够用鼠标拖动的

五、增加邮件报警

在理论我的项目中当监控的某的个指标超过阈值(比方 CPU 使用率过高),心愿监控零碎主动通过短信、钉钉和邮件等形式报警及时告诉运维人员,Grafana 就反对该性能。

第一步: 点击 [Alerting]——>[Notification channels] 增加告诉通道

创立通道

这里的 Type 有很多选项,包含 webhook、钉钉等,这里以邮件为例。

第二步: 邮箱配置

Grafana 默认应用 conf 目录下 defaults.ini 作为配置文件运行,依据官网的倡议咱们不要更改 defaults.ini 而是在同级目录下新建一个配置文件 custom.ini。

以腾讯企业邮箱为例,配置如下:

#################################### 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

而后须要重启 Grafana,命令 grafana-server.exe -config=E:\file\grafana-6.3.3\conf\custom.ini

第三步: 为指标增加 alert

配置预警规定

配置告诉形式和信息

Evaluate every

示意检测评率,这里为了测试成果,改为 1 秒

For

如果警报规定配置了 For,并且查问违反了配置的阈值,那么它将首先从 OK 变为 Pending。从 OK 到 Pending Grafana 不会发送任何告诉。一旦警报规定的触发工夫超过持续时间,它将更改为 Alerting 并发送警报告诉。

Conditions

when 示意什么工夫,of 示意条件,is above 示意触发值
同时,设置了 is above 后会有一条红线。

If no data or all values are null

如果没有数据或所有值都为空,这里抉择触发报警

If execution error or timeout

如果执行谬误或超时,这里抉择触发报警

留神: 下一次触发,比方 10 秒后,它不会再次触发,避免报警风暴产生!

第四步: 测试

申请 http://localhost:8848/heap/test 接口后,内存升高大于设置的阈值,而后就收到报警邮件。

这里图片没有显示进去,搞不懂为什么。

六、总结

这套监控性能还是挺弱小的,就是 Prometheus 的表达式有点多。

近期热文举荐:

1.1,000+ 道 Java 面试题及答案整顿(2022 最新版)

2. 劲爆!Java 协程要来了。。。

3.Spring Boot 2.x 教程,太全了!

4. 别再写满屏的爆爆爆炸类了,试试装璜器模式,这才是优雅的形式!!

5.《Java 开发手册(嵩山版)》最新公布,速速下载!

感觉不错,别忘了顺手点赞 + 转发哦!

正文完
 0