prometheus下载慢_Prometheus + Grafana 监控 SpringBoot
整体结构
SpringBoot 的 actuator
提供了监控端点。
Prometheus 是监控系统,可以从 Springboot 获取监控数据,以时序数据的形式存储,并提供了监控数据的查询服务。
Grafana 是专业的 UI 仪表盘系统,支持非常多的数据源,其中就包括 Prometheus,可以便利的从中获取数据,使用仪表盘展示出来。
springboot 2 中引入了 micrometer
,它可以更方便的对接各种监控系统,包括 Prometheus。
所以整体的结构就是:
- springboot(micrometer)产生监控数据。
- Prometheus 获取 springboot 应用的监控数据,存储,并提供数据查询服务。
- Grafana 对接 Prometheus 数据源,调用其数据查询服务,用专业的仪表盘 UI 进行展示。
实践步骤
创建应用 -- 作为监控目标,产生监控数据。
集成度量库 micrometer -- 以便对接监控系统 Prometheus。
部署 prometheus
配置 prometheus -- 监控之前创建的 springboot 应用,了解 Prometheus 的查询服务。
部署 Grafana
添加 Prometheus 数据源
添加 JVM 监控仪表盘 -- 展示之前 springboot 应用的 JVM 状态。
自定义监控指标 -- 自有的监控指标都是底层基础数据,业务相关指标需要我们自己写代码。
动态变更监控目标 -- 如果监控目标发生变动就改一次 Prometheus 的配置文件,并重新启动,这是不适合的,需要使用动态配置的方式。
1. 创建应用 集成 micrometer
创建一个最简的 springboot 应用,添加 micrometer 依赖。
pom.xml :
<?xml version="1.0" encoding="UTF-8"?> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.springframework.boot spring-boot-starter-parent 2.2.4.RELEASE com.example springboot2demo 0.0.1-SNAPSHOT springboot2demo Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-starter-web io.micrometer micrometer-registry-prometheus 1.1.4 org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.boot spring-boot-maven-plugin
application.properties
spring.application.name=springboot2demo# 打开所有 Actuator 服务management.endpoints.web.exposure.include=*# 将应用名称添加到计量器的 tag 中去# 以便 Prometheus 根据应用名区分不同服务management.metrics.tags.application=${spring.application.name}
在启动类中添加Bean,用于监控JVM性能指标:
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;@SpringBootApplicationpublic class Springboot2demoApplication { public static void main(String[] args) { SpringApplication.run(Springboot2demoApplication.class, args); } @Bean MeterRegistryCustomizer configurer( @Value("${spring.application.name}") String applicationName) { return (registry) -> registry.config().commonTags("application", applicationName); }}
启动服务。
查看监控端点信息:
2. 部署 Prometheus
官网:
https://prometheus.io/
可以下载安装包来安装,但下载速度极其慢,几乎下载不了。
可以使用 docker 部署,因为国内有docker镜像,所以速度很快。
docker 方式启动:
$ docker run --name prometheus -d -p 127.0.0.1:9090:9090 prom/prometheus
执行完成后就OK了,可以看一下 Prometheus 的界面。
http://localhost:9090/targets 是监控目标列表页:
http://localhost:9090/graph 是查询控制台,也有简单的图表展示:
现在还没对接应用,后面对接之后可以看到详细的内容。
3. Prometheus + Springboot应用
监控应用,需要在 Prometheus 配置文件中添加应用的相关信息。
配置文件在容器中的路径:/etc/prometheus
。
查看一下配置文件的默认内容:
$ docker exec -it [容器ID] cat /etc/prometheus/prometheus.yml
红框内是我们要关注的部分,按照这个形式添加我们的应用即可。
需要添加的内容为:
- job_name: 'springboot_app' scrape_interval: 5s metrics_path: '/actuator/prometheus' static_configs: - targets: ['192.168.31.6:8080'] "labels": { "instance": "springboot2-A", "service": "springboot2-A-service" }
metrics_path
指定监控端点的路径。
targets
指定应用的IP端口,这里使用了IP,没有使用localhost
,因为 Prometheus 是容器运行的,如果使用 localhost
就会访问容器内部。
配置不是直接在容器内部修改,可以把容器内部的配置文件拷贝出来一份,修改后,重启启动容器,挂载本地修改过的配置文件。
拷贝容器中的配置文件:
$ docker cp [容器ID]:/etc/prometheus/prometheus.yml .
修改配置文件,添加配置,最终的内容:
# my global configglobal: 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 configurationalerting: 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: # The job name is added as a label `job=` 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: 'springboot_app' scrape_interval: 5s metrics_path: '/actuator/prometheus' static_configs: - targets: ['192.168.31.6:8080'] "labels": { "instance": "springboot2-A", "service": "springboot2-A-service" }
停掉之前的容器,重新启动:
$ docker run --name prometheus -d \ -p 9090:9090 \ -v [PATH]/prometheus.yml:/etc/prometheus/prometheus.yml \ prom/prometheus
访问监控列表页 http://localhost:9090/targets 就可以看到我们的应用了:
点击端点链接,可以看到监控数据,例如:
进入查询控制台页面 http://localhost:9090/graph,可以查询一个指标,例如 http_server_requests_seconds_sum
,效果:
4. 部署 Grafana
docker方式运行:
$ docker run -d \ -p 3000:3000 \ --name=grafana \ grafana/grafana
启动后,访问:http://localhost:3000,默认用户名密码 admin/admin
。
5. 添加 Prometheus 数据源
6. 展示应用的 JVM 信息
Grafana 中已经有现成的 JVM 仪表盘,我们直接导入使用即可。
这个仪表盘的编号为 4701
。
至此,Prometheus + Grafana + Springboot 的整体流程已经跑通了。
但是,这些指标都是底层通用指标,在业务层面一定会有个性需求,下面我们自己定义一些监控指标。
7. 自定义监控指标
需求:监控所有接口的请求次数。
应用中添加依赖:
org.aspectj aspectjrt 1.9.4 org.aspectj aspectjweaver 1.9.4 cglib cglib 3.2.12
使用AOP方式对接口请求计数:
package com.example.springboot2demo;import io.micrometer.core.instrument.Counter;import io.micrometer.core.instrument.MeterRegistry;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;@Component@Aspectpublic class APICounterAop { @Pointcut("execution(public * com.example.springboot2demo.*.*(..))") public void pointCut() { } ThreadLocal startTime = new ThreadLocal<>(); @Autowired MeterRegistry registry; private Counter counter; @PostConstruct private void init() { counter = registry.counter("requests_total", "status", "success"); } @Before("pointCut()") public void doBefore(JoinPoint joinPoint) throws Throwable { System.out.println("do before"); counter.increment(); //请求计数 } @AfterReturning(returning = "returnVal", pointcut = "pointCut()") public void doAfterReturning(Object returnVal) { System.out.println("do after"); }}
创建一个测试接口:
package com.example.springboot2demo;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class TestController { @RequestMapping("/hello") public String hello() { return "hello"; }}
重启应用,多访问几次测试接口,然后查看 Prometheus 中的应用监控端点页面,就可以看到监控结果:
然后,我们把这个指标在 Grafana 中显示出来。
8. 动态变更监控目标
上面 Prometheus 配置文件中定义的监控目标使用的是静态方式,改配置文件后需要重启。
如果服务变更了,或者增加服务了,经常重启 Prometheus 肯定不合适。
Prometheus 提供了动态加载的方式,把服务信息放到一个单独的文件中,Prometheus 配置中指定这个外部文件,内容变化后,Prometheus 就会自动重新加载。
服务信息配置文件例如:
[ { "targets": [ "192.168.31.6:8080" ], "labels": { "instance": "springboot2-A", "service": "springboot2-A-service" } }]
Prometheus 配置文件中的写法:
... - job_name: 'springboot_app' scrape_interval: 5s metrics_path: '/actuator/prometheus' file_sd_configs: - files: - /home/*.json refresh_interval: 1m
启动 Prometheus 容器时要挂载这个服务信息配置文件的目录:
$ docker run --name prometheus -d -p 9090:9090 \ -v [PATH]/prometheus.yml:/etc/prometheus/prometheus.yml \ -v [PATH]:/home \ prom/prometheus
推荐阅读:
- 异地多活架构
- 使用 Canal 实现数据异构
- MySQL order by 是怎么工作的?
- 阿里开源的分布式事务框架 Seata
- 分布式唯一ID的生成方案
prometheus下载慢_Prometheus + Grafana 监控 SpringBoot相关推荐
- 运维(32) Prometheus+Grafana监控SpringBoot
文章目录 一.前言 二.SpringBoot集成Micrometer 1.`pom.xml`中引入依赖 2.`application.yml`配置 3.Micrometer配置 三.部署Prometh ...
- smartadmin mysql_Prometheus、Alertmanager、Grafana 监控 Linux 与 MySQL
//检查各个端口的放行 //部署各个模块与应用 cd /usr/local/Prometheus_compose vim docker-compose.yml version: "3&quo ...
- SpringBoot应用监控SpringBoot+Prometheus+Grafana
SpringBoot应用监控SpringBoot+Prometheus+Grafana 1. SpringBoot应用监控 1.1 SpringBoot应用监控 1.2 SpringBoot应用搭建 ...
- prometheus + grafana 对 springboot 项目进行监控
1.prometheus接入springboot prometheus安装后,在安装目录有一个默认的配置文件prometheus.yml # my global config global:scrap ...
- 基于 Prometheus、InfluxDB 与 Grafana 打造监控平台
在本文中,我将把几个常用的监控部分给梳理一下.前面我们提到过,在性能监控图谱中,有操作系统.应用服务器.中间件.队列.缓存.数据库.网络.前端.负载均衡.Web 服务器.存储.代码等很多需要监控的点. ...
- 搭建prometheus+grafana监控系统
prometheus简介 Prometheus是最初在SoundCloud上构建的开源系统监视和警报工具包 .自2012年成立以来,许多公司和组织都采用了Prometheus,该项目拥有非常活跃的开发 ...
- 使用Prometheus+Grafana 监控MySQL/MONGODB
使用Prometheus+Grafana 监控MySQL/MONGODB 之前就久仰 Prometheus 大名,因为有用zabbix 进行监控,就没去安装它.现在正好用上监控MONGO+MYSQL. ...
- 基于Prometheus+Grafana监控SQL Server数据库
墨墨导读:本文整理了基于Prometheus+Grafana监控SQL Server数据库的全过程,分享至此,希望对大家有帮助. 搭建SQL Server环境 使用容器建立SQL Server环境非常 ...
- Prometheus+Grafana监控PostgreSQL
Prometheus+Grafana监控PostgreSQL Prometheus:2.32.0 Grafana:8.3.3 PG:13.2 Linux:CentOS7.6 docker:18.06. ...
最新文章
- 本科计算机专业考北大软微,2022北京大学软微学院计算机智能科技考研必看经验指导(含复试解析)...
- xcode5 delete provisioning profile
- Python3——字典
- PostgreSQL 数据类型
- 变化很大!2021年THE世界大学排名出炉,这些中国高校表现出色!
- oracle grid安装看不到config,【图片】【rac11g安装问题】出了问题特来请教各位大侠【oracle吧】_百度贴吧...
- 运维测试工作笔记0003---使用Jmeter测试_http接口_高并发测试
- 仿照小米官网项目具体操作与细节
- 3.10 Spark RDD编程案例
- ios开发--清理缓存
- PRML Chapter01 练习题Exercise
- 2013年度CSDN十大博客之星
- 短信注册验证以及邮箱激活
- qt无边框窗体的移动
- Axure-图片放大缩小
- word中去除页眉中的横线
- 使用思科模拟器设计和配置校园网络
- 【NOIP2017提高A组冲刺11.5】轰炸
- day55 django 模型层,orm连表操作
- 漫画 | 为什么程序员的女朋友或老婆颜值普遍都偏高?