SpringBoot集成Prometheus实现Alert Manager告警上报(二)

摘要

监控服务桥接Prometheus

一、引入prometheus和micrometer

添加健康检查及桥接Prometheus

        <!--健康检查-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<!-- 桥街prometheus -->
		<dependency>
			<groupId>io.micrometer</groupId>
			<artifactId>micrometer-registry-prometheus</artifactId>
			<version>1.9.12</version>
		</dependency>
		<!-- -micrometer 核心包-->
		<dependency>
			<groupId>io.micrometer</groupId>
			<artifactId>micrometer-core</artifactId>
			<version>1.9.12</version>
		</dependency>

上面是我们对prometheus和micrometer的jar的引用

此处简单说下micrometer的功能,为什么要引用他
Micrometer(千分尺)是Pivotal为最流行的监控系统提供的一个简单的仪表客户端门面模式,允许仪表化JVM应用,而无须关心是哪个供应商提供的指标
同时Spring Boot 2.x在spring-boot-actuator中引入了Micrometer

Micrometer中最重要的两个概念是Meter和MeterRegistry
Meter
:用于收集应用的一系列指标的接口。Micrometer提供一系列原生的Meter,包括Timer、Counter、Gauge、DistributionSummary、LongTaskTimer、FunctionCounter、FunctionTimer、TimeGauge。不同的Meter类型有不同的时间序列指标值。例如,增量计数用Counter表示,单个指标值用Gauge表示,计时事件的次数和总时间用Timer表示。每一项指标都有一个唯一标识的指标名称(Metric name)和标签(tag)。
MeterRegistry:Meter是由MeterRegistry创建的。每个监控系统都必须支持Meter-Registry。

二、spring集成Prometheus

编辑application.yml

spring:
  application:
    name: demo_micro
server:
  port: 18080
# Prometheus springboot
management:
  endpoints:
    web:
      exposure:
        #开放页面中的所有端口(endpoint)
        include: '*'
  metrics:
    export:
      prometheus:
        enabled: true
    tags:
      application: ${spring.application.name} 

启动类添加


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

启动springboot,看清楚端口号(后面配置Prometheus配置会用到)

访问:http://127.0.0.1:18080/actuator

{
    "_links": {
        "self": {
            "href": "http://127.0.0.1:18080/actuator",
            "templated": false
        },
        "beans": {
            "href": "http://127.0.0.1:18080/actuator/beans",
            "templated": false
        },
        "caches-cache": {
            "href": "http://127.0.0.1:18080/actuator/caches/{cache}",
            "templated": true
        },
        "caches": {
            "href": "http://127.0.0.1:18080/actuator/caches",
            "templated": false
        },
        "health": {
            "href": "http://127.0.0.1:18080/actuator/health",
            "templated": false
        },
        "health-path": {
            "href": "http://127.0.0.1:18080/actuator/health/{*path}",
            "templated": true
        },
        "info": {
            "href": "http://127.0.0.1:18080/actuator/info",
            "templated": false
        },
        "conditions": {
            "href": "http://127.0.0.1:18080/actuator/conditions",
            "templated": false
        },
        "configprops": {
            "href": "http://127.0.0.1:18080/actuator/configprops",
            "templated": false
        },
        "configprops-prefix": {
            "href": "http://127.0.0.1:18080/actuator/configprops/{prefix}",
            "templated": true
        },
        "env": {
            "href": "http://127.0.0.1:18080/actuator/env",
            "templated": false
        },
        "env-toMatch": {
            "href": "http://127.0.0.1:18080/actuator/env/{toMatch}",
            "templated": true
        },
        "loggers": {
            "href": "http://127.0.0.1:18080/actuator/loggers",
            "templated": false
        },
        "loggers-name": {
            "href": "http://127.0.0.1:18080/actuator/loggers/{name}",
            "templated": true
        },
        "heapdump": {
            "href": "http://127.0.0.1:18080/actuator/heapdump",
            "templated": false
        },
        "threaddump": {
            "href": "http://127.0.0.1:18080/actuator/threaddump",
            "templated": false
        },
        "prometheus": {
            "href": "http://127.0.0.1:18080/actuator/prometheus",
            "templated": false
        },
        "metrics-requiredMetricName": {
            "href": "http://127.0.0.1:18080/actuator/metrics/{requiredMetricName}",
            "templated": true
        },
        "metrics": {
            "href": "http://127.0.0.1:18080/actuator/metrics",
            "templated": false
        },
        "scheduledtasks": {
            "href": "http://127.0.0.1:18080/actuator/scheduledtasks",
            "templated": false
        },
        "mappings": {
            "href": "http://127.0.0.1:18080/actuator/mappings",
            "templated": false
        }
    }
}

监控指标:http://127.0.0.1:18080/actuator/metrics 

{
    "names": [
        "application.ready.time",
        "application.started.time",
        "disk.free",
        "disk.total",
        "executor.active",
        "executor.completed",
        "executor.pool.core",
        "executor.pool.max",
        "executor.pool.size",
        "executor.queue.remaining",
        "executor.queued",
        "http.server.requests",
        "jvm.buffer.count",
        "jvm.buffer.memory.used",
        "jvm.buffer.total.capacity",
        "jvm.classes.loaded",
        "jvm.classes.unloaded",
        "jvm.gc.live.data.size",
        "jvm.gc.max.data.size",
        "jvm.gc.memory.allocated",
        "jvm.gc.memory.promoted",
        "jvm.gc.overhead",
        "jvm.gc.pause",
        "jvm.memory.committed",
        "jvm.memory.max",
        "jvm.memory.usage.after.gc",
        "jvm.memory.used",
        "jvm.threads.daemon",
        "jvm.threads.live",
        "jvm.threads.peak",
        "jvm.threads.states",
        "logback.events",
        "process.cpu.usage",
        "process.start.time",
        "process.uptime",
        "system.cpu.count",
        "system.cpu.usage",
        "tomcat.sessions.active.current",
        "tomcat.sessions.active.max",
        "tomcat.sessions.alive.max",
        "tomcat.sessions.created",
        "tomcat.sessions.expired",
        "tomcat.sessions.rejected"
    ]
}

查看相关指标值: http://127.0.0.1:18080/actuator/prometheus (比较多, 就写一部分)

# HELP jvm_buffer_memory_used_bytes An estimate of the memory that the Java virtual machine is using for this buffer pool
# TYPE jvm_buffer_memory_used_bytes gauge
jvm_buffer_memory_used_bytes{application="demo_micro",id="mapped",} 0.0
jvm_buffer_memory_used_bytes{application="demo_micro",id="direct",} 81920.0
# HELP tomcat_sessions_rejected_sessions_total  
# TYPE tomcat_sessions_rejected_sessions_total counter
tomcat_sessions_rejected_sessions_total{application="demo_micro",} 0.0
# HELP process_start_time_seconds Start time of the process since unix epoch.
# TYPE process_start_time_seconds gauge
process_start_time_seconds{application="demo_micro",} 1.696419458554E9
# HELP jvm_gc_max_data_size_bytes Max size of long-lived heap memory pool
# TYPE jvm_gc_max_data_size_bytes gauge
jvm_gc_max_data_size_bytes{application="demo_micro",} 5.661261824E9
# HELP system_cpu_usage The "recent cpu usage" of the system the application is running in
# TYPE system_cpu_usage gauge
system_cpu_usage{application="demo_micro",} 0.031243497706356593

三、配置Prometheus接入监控指标

打开prometheus.yml编辑 alertmanager配置及监控服务配置

# 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:
    - follow_redirects: true
      enable_http2: true
      scheme: http
      timeout: 10s
      static_configs:
      - targets: #修改配置alertmanager的地址
        - 127.0.0.1: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"]
      #配置监控的服务地址及获取指标值的地址
   - job_name: "demo_micro"
     metrics_path: 'actuator/prometheus'
     static_configs:
       - targets: ["192.168.2.62:18080"]

重启Prometheus 

四、验证Prometheus接入被监控服务的数据

进入Prometheus的UI界面->点击Graph->点击地球 能看到指标就算OK了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值