添加监控依赖
<!-- prometheus -->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<!--actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
添加如下配置
#开启Prometheus监控
management.endpoints.web.exposure.include=*
management.metrics.tags.application= ${spring.application.name}
server.tomcat.mbeanregistry.enabled=true
添加如下代码
package com.wsmg.collection.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import io.micrometer.core.instrument.MeterRegistry;
@Configuration
public class MeterRegistrConfig {
@Bean
MeterRegistryCustomizer<MeterRegistry> configurer(
@Value("${spring.application.name}") String applicationName) {
return (registry) -> registry.config().commonTags("application", applicationName);
}
}
prometheus配置
- 配置prometheus.yml,示例如下:
- job_name: '微网能源'
#scrape_interval: 2s
static_configs:
- targets: ['ip:port']
labels:
__metrics_path__: "/actuator/prometheus" #网关
group: gateway
- 创建应用告警规则 application.yml,重启prometheus。
groups:
- name: 服务监控
rules:
- alert: 应用下线
expr: up{job="xxx"} == 0
for: 1s
labels:
severity: 严重告警
annotations:
summary: "{{$labels.group}} 下线, 请尽快处理!"
description: "{{$labels.group}}-{{$labels.instance}} 下线,当前状态{{ $value }}. "
Grafana配置仪表盘
- 导入模板:11378
- 显示应用监控图表信息如下