前言:
1. Springboot的 spring-boot-starter-actuator 提供了监控断点,micrometer-registry-prometheus 将Springboot产生的端点数据转换成Prometheus允许的格式
2. Prometheus 是监控服务,可以从 Springboot 获取监控数据,以时序数据的形式存储,并提供了监控数据的查询服务
3. Grafana 是专业的 UI 仪表盘系统,支持非常多的数据源,其中就包括 Prometheus,可以便利的从中获取数据,使用仪表盘展示出来
一、创建Springboot应用
1. 添加actuator,micrometer依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.swn</groupId>
<artifactId>prometheus</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>prometheus</name>
<description>prometheus</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2. application.yml
server:
port: 8088
spring:
application:
name: springboot-prometheus
management:
endpoints:
web:
exposure:
# 打开所有 Actuator 服务
include: "*"
endpoint:
prometheus:
enabled: true
health:
show-details: always
metrics:
export:
prometheus:
# 指标允许被导出
enabled: true
3. 启动类添加Bean,用于监控JVM性能指标
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);
}
/**
* 将springboot注册到prometheus
* 用于监控JVM性能指标
*
* @param applicationName
* @return
*/
@Bean
MeterRegistryCustomizer<MeterRegistry> configurer(
@Value("${spring.application.name}") String applicationName) {
return (registry) -> registry.config().commonTags("application", applicationName);
}
}
二、部署Prometheus
1. 添加 /etc/prometheus/prometheus.yml 文件
# my global config
global:
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 configuration
alerting:
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=<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']
# springboot 配置
- job_name: 'springboot_prometheus'
scrape_interval: 5s
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['192.168.3.136:8088']
job_name:自定义一个应用名称
scrape_interval:每隔5秒获取一次springboot端点数据
metrics_path:指定端点路径
targets:springboot服务地址端口
2. docker方式启动
docker run --name prometheus -d -p 9090:9090 -v /etc/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
访问地址:http://虚拟机IP:9090/
三、部署Grafana
1. docker方式启动
docker run -d -p 3000:3000 --name=grafana grafana/grafana
访问地址:http://虚拟机IP:3000/, admin/admin
2. 配置数据源
选择普罗米修斯
配置普罗米修斯地址
保存测试
3. 配置仪表盘