1、前言Prometheus:一个开源系统监控和警报工具包。Grafana:Go语言研发的开源数据可视化工具,能够做数据监控和数据统计,带有告警功能。Micrometer:收集Java性能数据2、SpringBoot集成Micrometer
1、pom.xml中引入依赖<!-- micrometer:收集Java性能数据 -->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>2、application.yml配置server:
port: 9200
spring:
application:
name: demo
management:
endpoints:
web:
exposure:
include: *
metrics:
tags:
application: ${spring.application.name}3、Micrometer配置@Configuration
public class MetricsConfig {
@Bean
MeterRegistryCustomizer<MeterRegistry> configurer(
@Value("${spring.application.name}") String applicationName) {
return (registry) -> registry.config().commonTags("application", applicationName);
}
}起步应用,拜访 http://127.0.0.1:9200/actuator/prometheus
3、安排Prometheus+Grafana
# 准备
git clone https://gitee.com/zhengqingya/docker-compose.git
cd docker-compose/Liunx
# `docker-compose-prometheus.yml` 需修改grafana中配置的mysql连接信息
# `prometheus.yml` 自动配置
# 运行服务
docker-compose -f docker-compose-prometheus.yml -p prometheus up -dgrafana拜访位置 ip位置:3000 默认登录账号秘码:admin/adminprometheus拜访位置: ip位置:9090exporter拜访位置: ip位置:9100/metrics
重视prometheus.yml配置需和springboot应用对应
4、监控SpringBoot
1、新增Prometheus数据源
Configuration -> Data sources -> Add data source -> Prometheus
2、导入Dashboard
Create -> Import
grafana面板资源 https://grafana.com/grafana/dashboards/4701
5、邮件告警
1、配置grafana.ini
#################################### SMTP / Emailing ##########################
[smtp]
# 是不是启用
enabled = true
# 服务位置
host = smtp.qq.com:465
# 邮箱用户名
user = xxx@qq.com
# If the password contains # or ; you have to wrap it with triple quotes. Ex """#password;"""
# 授权秘码
password = xxx
;cert_file =
;key_file =
skip_verify = true
# 邮件From位置
from_address = xxx@qq.com
from_name = Grafana
# EHLO identity in SMTP dialog (defaults to instance_name)
;ehlo_identity = dashboard.example.com
# SMTP startTLS policy (defaults to OpportunisticStartTLS)
;startTLS_policy = NoStartTLS
[emails]
;welcome_email_on_sign_up = false
;templates_pattern = emails/*.html2、Alerting -> Notification channels
测试发送通告
3、Add panel
仿造Heap used配置一个
tips: Metrics browser中不支持模板变量 ( ex: $application ) sum(jvm_memory_used_bytes{application="demo", instance="192.168.101.88:9200", area="heap"})*100/sum(jvm_memory_max_bytes{application="demo",instance="192.168.101.88:9200", area="heap"})
Apply
超标告警通告
6、本文案例demo源码
https://gitee.com/zhengqingya/java-workspace
今日分享语句: 不奋斗,你的才华怎样配得上你的任性。 不奋斗,你的脚步怎样赶得上父母老去的速度。 不奋斗,世界这么大你靠什么去瞧瞧。
|