来源:性能与架构
整体结构
SpringBoot 的 actuator 提供了监控端点。
Prometheus 是监控系统,可以从 Springboot 获取监控数据,以时序数据的形式存储,并提供了监控数据的查询服务。
Grafana 是专业的 UI 仪表盘系统,支持非常多的数据源,其中就包括 Prometheus,可以便利的从中获取数据,使用仪表盘展示出来。
springboot 2 中引入了 micrometer,它可以更方便的对接各种监控系统,包括 Prometheus。
![2d981f55bff8836b63d8cb5b33575db0.png](https://img-blog.csdnimg.cn/img_convert/2d981f55bff8836b63d8cb5b33575db0.png)
所以整体的结构就是:
- 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"?> 4.0.0org.springframework.bootspring-boot-starter-parent2.2.4.RELEASEcom.examplespringboot2demo0.0.1-SNAPSHOTspringboot2demoDemo project for Spring Boot1.8org.springframework.bootspring-boot-starter-actuatororg.springframework.bootspring-boot-starter-webio.micrometermicrometer-registry-prometheus1.1.4org.springframework.bootspring-boot-starter-testtestorg.junit.vintagejunit-vintage-engineorg.springframework.bootspring-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); }}
启动服务。
查看监控端点信息:
![beb23687ce9df0e1335e74e707d45ac3.png](https://img-blog.csdnimg.cn/img_convert/beb23687ce9df0e1335e74e707d45ac3.png)
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 是监控目标列表页:
![1538666ab5d912ef31f540c876a69216.png](https://img-blog.csdnimg.cn/img_convert/1538666ab5d912ef31f540c876a69216.png)
http://localhost:9090/graph 是查询控制台,也有简单的图表展示:
![d47aeba84894e622c2f4a4dce7ffb111.png](https://img-blog.csdnimg.cn/img_convert/d47aeba84894e622c2f4a4dce7ffb111.png)
现在还没对接应用,后面对接之后可以看到详细的内容。
3. Prometheus + Springboot应用
监控应用,需要在 Prometheus 配置文件中添加应用的相关信息。
配置文件在容器中的路径:/etc/prometheus。
查看一下配置文件的默认内容:
$ docker exec -it [容器ID] cat /etc/prometheus/prometheus.yml
![4370f79bcc4ad640a694c58e6d1cdd71.png](https://img-blog.csdnimg.cn/img_convert/4370f79bcc4ad640a694c58e6d1cdd71.png)
红框内是我们要关注的部分,按照这个形式添加我们的应用即可。
需要添加的内容为:
- 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 就可以看到我们的应用了:
![0eac133b322dbba3e81d357c899d2bfc.png](https://img-blog.csdnimg.cn/img_convert/0eac133b322dbba3e81d357c899d2bfc.png)
点击端点链接,可以看到监控数据,例如:
![acee4b9d83d601daffbe8a4a30ec8880.png](https://img-blog.csdnimg.cn/img_convert/acee4b9d83d601daffbe8a4a30ec8880.png)
进入查询控制台页面 http://localhost:9090/graph,可以查询一个指标,例如 http_server_requests_seconds_sum,效果:
![36a1cc91e7d1075a29abe6fce4173c03.png](https://img-blog.csdnimg.cn/img_convert/36a1cc91e7d1075a29abe6fce4173c03.png)
4. 部署 Grafana
docker方式运行:
$ docker run -d -p 3000:3000 --name=grafana grafana/grafana
启动后,访问:http://localhost:3000,默认用户名密码 admin/admin。
![3211038652eb93e7e0dbe8535f43fbb0.png](https://img-blog.csdnimg.cn/img_convert/3211038652eb93e7e0dbe8535f43fbb0.png)
![e2dd983edd1f47aa8078d2a1bc4a9885.png](https://img-blog.csdnimg.cn/img_convert/e2dd983edd1f47aa8078d2a1bc4a9885.png)
5. 添加 Prometheus 数据源
![05ef66f76ac636e56c4d6b7725cb721a.png](https://img-blog.csdnimg.cn/img_convert/05ef66f76ac636e56c4d6b7725cb721a.png)
![ef0a648e4dc8a99bc125f7e96664e31c.png](https://img-blog.csdnimg.cn/img_convert/ef0a648e4dc8a99bc125f7e96664e31c.png)
![354a509ee0f4ce8e8b6746321c0053b0.png](https://img-blog.csdnimg.cn/img_convert/354a509ee0f4ce8e8b6746321c0053b0.png)
6. 展示应用的 JVM 信息
Grafana 中已经有现成的 JVM 仪表盘,我们直接导入使用即可。
这个仪表盘的编号为 4701。
![9202b95630baa79b604399b32bf0b562.png](https://img-blog.csdnimg.cn/img_convert/9202b95630baa79b604399b32bf0b562.png)
![239d646bc6013d0630692fc7a5242542.png](https://img-blog.csdnimg.cn/img_convert/239d646bc6013d0630692fc7a5242542.png)
![9c392cadcb0503a0ab41e0119e74124a.png](https://img-blog.csdnimg.cn/img_convert/9c392cadcb0503a0ab41e0119e74124a.png)
![75ebb2e5b0eded8dd6004872288d3949.png](https://img-blog.csdnimg.cn/img_convert/75ebb2e5b0eded8dd6004872288d3949.png)
至此,Prometheus + Grafana + Springboot 的整体流程已经跑通了。
但是,这些指标都是底层通用指标,在业务层面一定会有个性需求,下面我们自己定义一些监控指标。
7. 自定义监控指标
需求:监控所有接口的请求次数。
应用中添加依赖:
org.aspectjaspectjrt1.9.4org.aspectjaspectjweaver1.9.4cglibcglib3.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 中的应用监控端点页面,就可以看到监控结果:
![7ffe4a011550a36d711357abbd601d09.png](https://img-blog.csdnimg.cn/img_convert/7ffe4a011550a36d711357abbd601d09.png)
然后,我们把这个指标在 Grafana 中显示出来。
![8b287689586c8775e9504488b93c1107.png](https://img-blog.csdnimg.cn/img_convert/8b287689586c8775e9504488b93c1107.png)
![aa86fe3475f38e34ec80a7da55bbc014.png](https://img-blog.csdnimg.cn/img_convert/aa86fe3475f38e34ec80a7da55bbc014.png)
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