SpringBoot+Prometheus+Grafana实现微服务自动化监控

1、简介

springboot想必大家都很熟悉了,接下来简单介绍下Prometheus&Granfana。

1.1 Prometheus

Prometheus 是由 SoundCloud 开源监控告警解决方案,使用Go语言开发。

官网:https://prometheus.io/

下载地址:https://prometheus.io/download/

架构:

Prometheus architecture

基本原理:

Prometheus的基本原理是通过HTTP协议周期性抓取被监控组件的状态,任意组件只要提供对应的HTTP接口就可以接入监控。输出被监控组件信息的HTTP接口被叫做exporter 。

1.2 Granfana

Grafana是一个跨平台的开源的度量分析和可视化工具,可以通过将采集的数据查询然后可视化的展示,并及时通知。

官网:https://grafana.com/

下载地址:https://grafana.com/grafana/download?pg=get&plcmt=selfmanaged-box1-cta1

2、安装使用

本次统一使用windows版

2.1 Prometheus安装

下载windows版: https://github.com/prometheus/prometheus/releases/download/v2.27.1/prometheus-2.27.1.windows-amd64.zip

解压后,运行prometheus.exe,浏览器访问127.0.0.1:9090

prometheus本地环境安装成功~

2.2 Granfana安装

下载windows版本:https://dl.grafana.com/oss/release/grafana-7.5.7.windows-amd64.zip

下载后解压,在bin目录下,打开grafana-server.exe,浏览器访问http://127.0.0.1:3000/

我的测试版本已内置Prometheus数据源

如果没有,可以手动添加

3、与SpringBoot集成

3.1 引入依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>

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

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

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

<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<scope>runtime</scope>
</dependency>

</dependencies>

3.2 新增配置文件application.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
server:
port: 8888

spring:
application:
name: springboot-prometheus

management:
metrics:
export:
prometheus:
enabled: true
step: 1m
descriptions: true
tags:
application: ${spring.application.name}
web:
server:
auto-time-requests: true
endpoints:
prometheus:
id: springmetrics
web:
exposure:
include: health,info,env,prometheus,metrics,httptrace,threaddump,heapdump,springmetrics

3.3 启动类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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 PrometheusApplication {

public static void main(String[] args) {
SpringApplication.run(PrometheusApplication.class, args);
}

@Bean
MeterRegistryCustomizer<MeterRegistry> metricsCommonTags(
@Value("${spring.application.name}") String applicationName) {
return registry -> registry.config().commonTags("application", applicationName);
}
}

启动PrometheusApplication,访问 http://127.0.0.1:8888/actuator/prometheus,可以获取到采集信息。

4、Prometheus&Granfana配置

4.1 Prometheus配置

修改prometheus.yml,添加如下内容:

1
2
3
4
5
- job_name: 'springboot_prometheus'
scrape_interval: 5s
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['127.0.0.1:8888']

重启prometheus, 查看status->Targets:

prometheus Graph中已可以看到监控数据。

4.2 Granfana配置

导入JVM (Micrometer) dashboard: https://grafana.com/grafana/dashboards/4701

点击导入,监控大屏就出来了

是不是很酷炫!!!


SpringBoot+Prometheus+Grafana实现微服务自动化监控
https://river106.cn/posts/ab8172ae.html
作者
river106
发布于
2021年6月4日
许可协议