<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>io.micrometergroupId>
<artifactId>micrometer-registry-prometheusartifactId>
dependency>
# Actuator prometheus
management.endpoints.web.exposure.include=*
# 将应用名称添加到计量器的 tag 中去,以便 Prometheus 根据应用名区分不同服务
management.metrics.tags.application=${spring.application.name}
package com.car.life.service.wallet.config
import io.micrometer.core.instrument.MeterRegistry
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
/**
* @program: car-life-project
*
* @description: MircoMeterConfig
*
* @author: loulvlin
*
* @create: 2021-02-22 14:01
*/
@Configuration
class MircoMeterConfig {
@Bean
fun configurer(
@Value("\${spring.application.name}") applicationName: String): MeterRegistryCustomizer<MeterRegistry> {
return MeterRegistryCustomizer { registry: MeterRegistry -> registry.config().commonTags("application", applicationName) }
}
}
if(request.requestURI=="/actuator/prometheus" || request.requestURI=="/status" || request.requestURI=="/favicon.ico"){
return super.preHandle(request, response, handler)
}
http://192.168.40.234:11000/actuator/prometheus
http://localhost:11000/actuator/prometheus
mkdir -p /home/prometheus/
mkdir -p /home/prometheus/rules
cd /home/prometheus/
docker run --name prometheus -d -p 127.0.0.1:9090:9090 prom/prometheus
docker cp /etc/prometheus/prometheus.yml .
docker rm -f prometheus
添加一个job_name 为wallet-service的job
# 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: 'wallet-service'
scrape_interval: 15s
scrape_timeout: 10s
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['192.168.40.234:11000']
"labels": {
"instance": "wallet-service",
"service": "car-life-project"
}
如果要修改时区,需要自己构建image,参数配置不起作用
docker pull prom/prometheus
docker run --name prometheus -d --restart always \
--hostname prometheus \
-p 9090:9090 \
-v /home/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
-v /home/prometheus/rules:/home/prometheus/rules \
-u root prom/prometheus
http://192.168.0.35:9090/targets #监控目标列表页
#查询控制台 可以查询一个指标,例如 http_server_requests_seconds_sum
http://192.168.0.35:9090/graph
docker pull grafana/grafana
docker run -d --restart always \
-p 3000:3000 \
--name=grafana \
grafana/grafana
# 启动后,访问:http://192.168.0.35:3000,默认用户名密码 admin/admin 新密码修改成123456
1. 配置中添加 Prometheus 数据源[url:http://192.168.0.35:9090/,HTTP Method
:GET]最后Save&Test
1. +-->import 输入4701 或 12271[推荐]
2. 保存dashboard
3. 页面背景色--->页面左下角-->admin-->Preference
注意:如果机器人配置了多个关键字,推送到钉钉中的消息内容必须包含至少一个关键词,才能推送成功
mkdir -p /home/alertManager && cd /home/alertManager
docker run -d --restart always --name alertmanager -p 9093:9093 prom/alertmanager
docker cp alertmanager:/etc/alertmanager/alertmanager.yml .
docker rm -f alertmanager
192.168.0.35:8060为后面prometheus-webhook-dingtalk部署的ip和端口
global:
resolve_timeout: 5m
route:
receiver: webhook
group_wait: 30s
group_interval: 5m
repeat_interval: 5m
group_by: [alertname]
routes:
- receiver: webhook
group_wait: 10s
match:
team: mobile-dev
receivers:
- name: webhook
webhook_configs:
- url: http://192.168.0.35:8060/dingtalk/webhook1/send
send_resolved: true
cd /home/prometheus/rules
vim wallet-service.yml
groups:
- name: wallet-service
rules:
- alert: wallet-service
expr: up{job="wallet-service"} == 0
for: 15s
labels:
severity: critical
team: mobile-dev
annotations:
summary: "服务监控,恭喜您,您的wallet-service服务已经挂掉啦"
description: "{{$labels.instance}} of job {{$labels.job}} 已经停止."
cd /home/prometheus
vim prometheus.yml
192.168.0.35:9093为alertmanager部署的ip和端口
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets: ['192.168.0.35:9093']
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
- /home/prometheus/rules/*.yml
# - "first_rules.yml"
# - "second_rules.yml"
删除prometheus容器并重启[由于前面已经做过rules映射配置,故重启容器即可]
docker run --name prometheus -d --restart always \
--hostname prometheus \
-p 9090:9090 \
-v /home/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
-v /home/prometheus/rules:/home/prometheus/rules \
-u root prom/prometheus
docker pull prom/alertmanager
docker run -d \
-p 9093:9093 \
--name alertmanager \
--restart=always \
-v /etc/localtime:/etc/localtime \
-v /home/alertManager/alertmanager.yml:/etc/alertmanager/alertmanager.yml \
prom/alertmanager
https://oapi.dingtalk.com/robot/send?access_token=XXXXXX
docker pull timonwong/prometheus-webhook-dingtalk
docker run -d --restart always -p 8060:8060 --name webhook timonwong/prometheus-webhook-dingtalk --ding.profile="webhook1=https://oapi.dingtalk.com/robot/send?access_token=XXXXXX"
http://192.168.0.35:9090/rules
http://192.168.0.35:9090/targets
http://192.168.0.35:9090/alerts
http://192.168.0.35:9090/targets
http://192.168.0.35:9090/alerts