SpringBoot + Prometheus + Grafana 打造可视化监控一条龙!真的太顶了!

沙海 2022年6月15日10:06:51Java评论25字数 5078阅读16分55秒阅读模式
摘要

SpringBoot + Prometheus + Grafana 打造可视化监控一条龙!真的太顶了! java版web项目

速蛙云 - 极致体验,强烈推荐!!!

SpringBoot + Prometheus + Grafana 打造可视化监控一条龙!真的太顶了!

java版web项目 文章源自JAVA秀-https://www.javaxiu.com/66688.html

收录于合集文章源自JAVA秀-https://www.javaxiu.com/66688.html

#java版web项目296文章源自JAVA秀-https://www.javaxiu.com/66688.html

#SpringBoot11文章源自JAVA秀-https://www.javaxiu.com/66688.html

#服务监控1文章源自JAVA秀-https://www.javaxiu.com/66688.html

大家好,我是老赵文章源自JAVA秀-https://www.javaxiu.com/66688.html

1.背景文章源自JAVA秀-https://www.javaxiu.com/66688.html

SpringBoot的应用监控方案比较多,SpringBoot+Prometheus+Grafana是目前比较常用的方案之一。它们三者之间的关系大概如下图:文章源自JAVA秀-https://www.javaxiu.com/66688.html

SpringBoot + Prometheus + Grafana 打造可视化监控一条龙!真的太顶了!文章源自JAVA秀-https://www.javaxiu.com/66688.html

关系图文章源自JAVA秀-https://www.javaxiu.com/66688.html

文章源自JAVA秀-https://www.javaxiu.com/66688.html

2.开发SpringBoot应用文章源自JAVA秀-https://www.javaxiu.com/66688.html

首先,创建一个SpringBoot项目,pom文件如下:文章源自JAVA秀-https://www.javaxiu.com/66688.html

<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><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>
文章源自JAVA秀-https://www.javaxiu.com/66688.html

注意:这里的SpringBoot版本是1.5.7.RELEASE,之所以不用最新的2.X是因为最新的simpleclient_spring_boot只支持1.5.X,不确定2.X版本的能否支持。文章源自JAVA秀-https://www.javaxiu.com/66688.html

MonitorDemoApplication启动类增加注解文章源自JAVA秀-https://www.javaxiu.com/66688.html

@EnablePrometheusEndpoint @EnableSpringBootMetricsCollector @SpringBootApplication public class MonitorDemoApplication {      public static void main(String[] args) {         SpringApplication.run(MonitorDemoApplication.class, args);     }  }
文章源自JAVA秀-https://www.javaxiu.com/66688.html

 文章源自JAVA秀-https://www.javaxiu.com/66688.html

配置文件application.yml文章源自JAVA秀-https://www.javaxiu.com/66688.html

文章源自JAVA秀-https://www.javaxiu.com/66688.html

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
文章源自JAVA秀-https://www.javaxiu.com/66688.html

测试代码TestController文章源自JAVA秀-https://www.javaxiu.com/66688.html

文章源自JAVA秀-https://www.javaxiu.com/66688.html

@RequestMapping("/heap/test")@RestControllerpublic 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";    }}
文章源自JAVA秀-https://www.javaxiu.com/66688.html

这里的逻辑就是在请求这个接口后,创建大量对象保存到map中增加堆内存使用量,方便后面测试邮件报警。文章源自JAVA秀-https://www.javaxiu.com/66688.html

启动项目后,可以在IDEA中看到有很多Endpoints,如图:文章源自JAVA秀-https://www.javaxiu.com/66688.html

文章源自JAVA秀-https://www.javaxiu.com/66688.html

SpringBoot + Prometheus + Grafana 打造可视化监控一条龙!真的太顶了!文章源自JAVA秀-https://www.javaxiu.com/66688.html

开始我的IDEA是不显示这个Endpoints,后来发现是我使用的idea版本太老了,还是2017.1的,文章源自JAVA秀-https://www.javaxiu.com/66688.html

而这个需要 idea2017.2版本以上才能看到。文章源自JAVA秀-https://www.javaxiu.com/66688.html

后来只好重新下载安装,弄了好久。文章源自JAVA秀-https://www.javaxiu.com/66688.html

启动完毕,访问http://localhost:8888/admin/prometheus就可以看到服务暴露的那些监控指标了。文章源自JAVA秀-https://www.javaxiu.com/66688.html

SpringBoot + Prometheus + Grafana 打造可视化监控一条龙!真的太顶了!文章源自JAVA秀-https://www.javaxiu.com/66688.html

监控指标文章源自JAVA秀-https://www.javaxiu.com/66688.html

注意:文章源自JAVA秀-https://www.javaxiu.com/66688.html

由于开启了安全认证,所以访问这个URL的需要提示输入账号/密码,如果提示404请检查下你的请求地址是否正确,如果不设置management.context-path则默认地址是http://ip:port/prometheus文章源自JAVA秀-https://www.javaxiu.com/66688.html

3.安装Prometheus文章源自JAVA秀-https://www.javaxiu.com/66688.html

本文下载的是Windows版本prometheus-2.17.2.windows-amd64.tar.gz。文章源自JAVA秀-https://www.javaxiu.com/66688.html

下载地址:https://prometheus.io/download/文章源自JAVA秀-https://www.javaxiu.com/66688.html

解压后修改prometheus.yml文件,配置数据采集的目标信息。文章源自JAVA秀-https://www.javaxiu.com/66688.html

文章源自JAVA秀-https://www.javaxiu.com/66688.html

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 + 端口号
文章源自JAVA秀-https://www.javaxiu.com/66688.html

更多配置信息请查看官方文档。文章源自JAVA秀-https://www.javaxiu.com/66688.html

现在可以启动Prometheus了,命令行输入:文章源自JAVA秀-https://www.javaxiu.com/66688.html

文章源自JAVA秀-https://www.javaxiu.com/66688.html

prometheus.exe --config.file=prometheus.yml
文章源自JAVA秀-https://www.javaxiu.com/66688.html

访问http://localhost:9090/targets,查看Spring Boot采集状态是否正常。文章源自JAVA秀-https://www.javaxiu.com/66688.html

SpringBoot + Prometheus + Grafana 打造可视化监控一条龙!真的太顶了!文章源自JAVA秀-https://www.javaxiu.com/66688.html

采集目标信息文章源自JAVA秀-https://www.javaxiu.com/66688.html

4.安装Grafana文章源自JAVA秀-https://www.javaxiu.com/66688.html

本文用到的是Windows版本grafana-6.3.3.windows-amd64.zip。文章源自JAVA秀-https://www.javaxiu.com/66688.html

下载地址:https://grafana.com/grafana/download文章源自JAVA秀-https://www.javaxiu.com/66688.html

解压后运行bin目录下的grafana-server.exe启动,游览器访问http://localhost:3000即可看到登录页面,默认账号密码是admin/admin文章源自JAVA秀-https://www.javaxiu.com/66688.html

现在开始创建自己的可视化监控面板。文章源自JAVA秀-https://www.javaxiu.com/66688.html

文章源自JAVA秀-https://www.javaxiu.com/66688.html

1.设置数据源文章源自JAVA秀-https://www.javaxiu.com/66688.html

文章源自JAVA秀-https://www.javaxiu.com/66688.html

SpringBoot + Prometheus + Grafana 打造可视化监控一条龙!真的太顶了!文章源自JAVA秀-https://www.javaxiu.com/66688.html

2. 创建一个Dashboard文章源自JAVA秀-https://www.javaxiu.com/66688.html

文章源自JAVA秀-https://www.javaxiu.com/66688.html

SpringBoot + Prometheus + Grafana 打造可视化监控一条龙!真的太顶了!文章源自JAVA秀-https://www.javaxiu.com/66688.html

SpringBoot + Prometheus + Grafana 打造可视化监控一条龙!真的太顶了!文章源自JAVA秀-https://www.javaxiu.com/66688.html

3. 填写采集的指标点文章源自JAVA秀-https://www.javaxiu.com/66688.html

文章源自JAVA秀-https://www.javaxiu.com/66688.html

SpringBoot + Prometheus + Grafana 打造可视化监控一条龙!真的太顶了!文章源自JAVA秀-https://www.javaxiu.com/66688.html

注意:这里的指标点不能随便填,必须是已有的可以在 Prometheus看到。文章源自JAVA秀-https://www.javaxiu.com/66688.html

SpringBoot + Prometheus + Grafana 打造可视化监控一条龙!真的太顶了!文章源自JAVA秀-https://www.javaxiu.com/66688.html

4.选择图表样式文章源自JAVA秀-https://www.javaxiu.com/66688.html

文章源自JAVA秀-https://www.javaxiu.com/66688.html

SpringBoot + Prometheus + Grafana 打造可视化监控一条龙!真的太顶了!文章源自JAVA秀-https://www.javaxiu.com/66688.html

5.填写标题描述文章源自JAVA秀-https://www.javaxiu.com/66688.html

SpringBoot + Prometheus + Grafana 打造可视化监控一条龙!真的太顶了!文章源自JAVA秀-https://www.javaxiu.com/66688.html

最后点击右上角的保存,输入Dashboad的名称即可。文章源自JAVA秀-https://www.javaxiu.com/66688.html

SpringBoot + Prometheus + Grafana 打造可视化监控一条龙!真的太顶了!文章源自JAVA秀-https://www.javaxiu.com/66688.html

Tips: 这里的图表布局是可以用鼠标拖动的文章源自JAVA秀-https://www.javaxiu.com/66688.html

文章源自JAVA秀-https://www.javaxiu.com/66688.html

5.添加邮件报警文章源自JAVA秀-https://www.javaxiu.com/66688.html

在实际项目中当监控的某的个指标超过阈值(比如CPU使用率过高),希望监控系统自动通过短信、钉钉和邮件等方式报警及时通知运维人员,Grafana就支持该功能。文章源自JAVA秀-https://www.javaxiu.com/66688.html

第一步:点击[Alerting]——>[Notification channels]添加通知通道文章源自JAVA秀-https://www.javaxiu.com/66688.html

SpringBoot + Prometheus + Grafana 打造可视化监控一条龙!真的太顶了!文章源自JAVA秀-https://www.javaxiu.com/66688.html

SpringBoot + Prometheus + Grafana 打造可视化监控一条龙!真的太顶了!文章源自JAVA秀-https://www.javaxiu.com/66688.html

这里的Type有很多选项,包括webhook、钉钉等,这里以邮件为例。文章源自JAVA秀-https://www.javaxiu.com/66688.html

文章源自JAVA秀-https://www.javaxiu.com/66688.html

第二步:邮箱配置文章源自JAVA秀-https://www.javaxiu.com/66688.html

文章源自JAVA秀-https://www.javaxiu.com/66688.html

Grafana默认使用conf目录下defaults.ini作为配置文件运行,根据官方的建议我们不要更改defaults.ini而是在同级目录下新建一个配置文件custom.ini。文章源自JAVA秀-https://www.javaxiu.com/66688.html

文章源自JAVA秀-https://www.javaxiu.com/66688.html

以腾讯企业邮箱为例,配置如下:文章源自JAVA秀-https://www.javaxiu.com/66688.html

文章源自JAVA秀-https://www.javaxiu.com/66688.html

################### SMTP / Emailing ###################[smtp]enabled = truehost = smtp.exmail.qq.com:465user = xxxx@ininin.com# If the password contains # or ; you have to wrap it with triple quotes. Ex """#password;"""password = XXXcert_file =key_file =skip_verify = truefrom_address = xxxx@ininin.comfrom_name = Grafanaehlo_identity = ininin.com
文章源自JAVA秀-https://www.javaxiu.com/66688.html

然后需要重启Grafana,命令文章源自JAVA秀-https://www.javaxiu.com/66688.html

文章源自JAVA秀-https://www.javaxiu.com/66688.html

grafana-server.exe -config=E:\file\grafana-6.3.3\conf\custom.ini
文章源自JAVA秀-https://www.javaxiu.com/66688.html

第三步:为指标添加alert文章源自JAVA秀-https://www.javaxiu.com/66688.html

文章源自JAVA秀-https://www.javaxiu.com/66688.html

SpringBoot + Prometheus + Grafana 打造可视化监控一条龙!真的太顶了!文章源自JAVA秀-https://www.javaxiu.com/66688.html

SpringBoot + Prometheus + Grafana 打造可视化监控一条龙!真的太顶了!文章源自JAVA秀-https://www.javaxiu.com/66688.html

功能说明:文章源自JAVA秀-https://www.javaxiu.com/66688.html

  • Evaluate every文章源自JAVA秀-https://www.javaxiu.com/66688.html

表示检测评率,这里为了测试效果,改为1秒文章源自JAVA秀-https://www.javaxiu.com/66688.html

  • For文章源自JAVA秀-https://www.javaxiu.com/66688.html

如果警报规则配置了For,并且查询违反了配置的阈值,那么它将首先从OK变为Pending。从OK到Pending Grafana不会发送任何通知。一旦警报规则的触发时间超过持续时间,它将更改为Alerting并发送警报通知。文章源自JAVA秀-https://www.javaxiu.com/66688.html

  • Conditions文章源自JAVA秀-https://www.javaxiu.com/66688.html

when 表示什么时间,of 表示条件,is above 表示触发值文章源自JAVA秀-https://www.javaxiu.com/66688.html

同时,设置了is above后会有一条红线。文章源自JAVA秀-https://www.javaxiu.com/66688.html

  • If no data or all values are null文章源自JAVA秀-https://www.javaxiu.com/66688.html

如果没有数据或所有值都为空,这里选择触发报警文章源自JAVA秀-https://www.javaxiu.com/66688.html

  • If execution error or timeout文章源自JAVA秀-https://www.javaxiu.com/66688.html

如果执行错误或超时,这里选择触发报警文章源自JAVA秀-https://www.javaxiu.com/66688.html

注意:下一次触发,比如10秒后,它不会再次触发,防止报警风暴产生!文章源自JAVA秀-https://www.javaxiu.com/66688.html

第四步:测试文章源自JAVA秀-https://www.javaxiu.com/66688.html

请求http://localhost:8848/heap/test接口后,内存升高大于设置的阈值,然后就收到报警邮件。文章源自JAVA秀-https://www.javaxiu.com/66688.html

SpringBoot + Prometheus + Grafana 打造可视化监控一条龙!真的太顶了!文章源自JAVA秀-https://www.javaxiu.com/66688.html

报警邮件文章源自JAVA秀-https://www.javaxiu.com/66688.html

这里图片没有显示出来,搞不懂为什么。文章源自JAVA秀-https://www.javaxiu.com/66688.html

文章源自JAVA秀-https://www.javaxiu.com/66688.html

6.总结文章源自JAVA秀-https://www.javaxiu.com/66688.html

这套监控功能还是挺强大的,就是Prometheus的表达式有点多。文章源自JAVA秀-https://www.javaxiu.com/66688.html

文档:文章源自JAVA秀-https://www.javaxiu.com/66688.html

https://prometheus.io/docs/introduction/first_steps/文章源自JAVA秀-https://www.javaxiu.com/66688.html

https://grafana.com/docs/grafana/latest/文章源自JAVA秀-https://www.javaxiu.com/66688.html

https://github.com/2YSP/monitor-demo文章源自JAVA秀-https://www.javaxiu.com/66688.html

来源:https://www.cnblogs.com/2YSP/p/12827487.html文章源自JAVA秀-https://www.javaxiu.com/66688.html

精彩推荐1.我们公司使用了 5 年的系统限流方案!从实现到部署实战详解,稳的一批!2.40个SpringBoot常用注解:让生产力爆表!3.SpringBoot+CAS这套单点登录通用方案,打通我司几十个系统,稳的一批!大Pass平台4.MyBatisPlus又在搞事了!发布神器,一个依赖轻松搞定权限问题!5.SpringBoot+WebSocket实时监控异常,真的太顶了!6求求你以后别再乱用@Validated 和 @Valid 了,要不然把你同事都的搞疯!!7.求求你别再手动部署jar包了,太low了!动态上传热部署真的太爽了!8.10G大文件上传最全方案:秒传、断点续传、分片上传,包教会!9.新技能 MyBatis 千万数据表,快速分页!10.最新 955 不加班的公司名单
文章源自JAVA秀-https://www.javaxiu.com/66688.html

SpringBoot + Prometheus + Grafana 打造可视化监控一条龙!真的太顶了!文章源自JAVA秀-https://www.javaxiu.com/66688.html

继续阅读
文章末尾固定信息...
weinxin
资源分享QQ群
本站是JAVA秀团队的技术分享社区, 会经常分享资源和教程; 分享的时代, 请别再沉默!
沙海
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定