关于Springboot 集成 Actuator,Prometheus,Grafana

简介

  • Actuator是 SpringBoot自带的监控功能,可以帮助实现对程序内部运行情况监控,比如监控状况、Bean加载情况、环境变量、日志信息、线程信息等
  • Prometheus是由SoundCloud开发的开源监控报警系统和时序列数据库(TSDB)。Prometheus使用Go语言开发,是Google BorgMon监控系统的开源版本
  • Grafana是一个跨平台的开源的度量分析和可视化工具,可以通过将采集的数据查询然后可视化的展示,并及时通知。这里我们使用Grafana是由于Prometheus的数据展示页面不够优雅

actuator

官方文档地址:https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html#production-ready-endpoints
一般actuator有两种形式的监控,web和jmx。这里我们主要使用的web形式的监控

maven 引入
<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
endpoint开启

endpoint默认打开的(但是大部分都不是暴露的),只有shutdown是默认关闭的,可以在application.properties中配置来打开

management.endpoint.shutdown.enabled=true

endpoint 暴露

大部分endpoint是不暴露的,默认暴露的endpoint只有下面几个

  • http://localhost:8080/actuator
  • http://localhost:8080/actuator/info
  • http://localhost:8080/actuator/health

常用一般使用http和jms来暴露,这里我们只介绍http的方式,也是在application.properties中配置

# Use "*" to expose all endpoints, or a comma-separated list to expose selected ones
management.endpoints.web.exposure.include=health,info 
management.endpoints.web.exposure.exclude=

显示详细的健康信息

management.endpoint.health.show-details=always

spring boot actuator有几个预定义的健康指标比如DataSourceHealthIndicator, DiskSpaceHealthIndicator, MongoHealthIndicator, RedisHealthIndicator, CassandraHealthIndicator等。当使用了这些模块的时候时,这些健康指标作为健康检查的一部分。

在这里插入图片描述
继承AbstractHealthIndicator

/metrics

提供了一部分应用的指标

  • jvm运行指标
  • tomcat的运行指标
  • 系统运行指标
  • 线程池运行指标等等
  • 给prometheus提供数据

/metrics
-
具体指标的查询是 /metrics/{name}
/metrics/jvm.memory.max
在这里插入图片描述

/loggers

可以查看和临时更改日志的等级
post方法用来提交修改
在这里插入图片描述

自定义endpoint
@Endpoint(id = "customer")
public class CustomerMetricsIndicator {

    @ReadOperation
    public Map<String,Object> time(){
        Map<String,Object> res = new HashMap<>();
        res.put("当前时间:",new Date().toString());
        return res;
    }

}

这个endpoint需要自己注入到ioc中

@Configuration
public class EndPointConfiguration {

    @Bean
    public CustomerMetricsIndicator customerMetricsIndicator(){
        return new CustomerMetricsIndicator();
    }

}

prometheus

Prometheus 是由 SoundCloud 开源监控告警解决方案。
prometheus存储的是时序数据,即按相同时序(相同名称和标签),以时间维度存储连续的数据的集合。

<dependency>
		<groupId>io.micrometer</groupId>
		<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

访问链接:http://localhost:8080/actuator/prometheus

下载

配置 prometheus.yml,新增一个job

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']
  - job_name: 'spring-boot-actuator'
    metrics_path: '/actuator/prometheus'
    scrape_interval: 5s
    static_configs:
    - targets: ['192.9.100.166:8080']  

启动

	./prometheus --config.file=prometheus.yml

访问9090端口
在这里插入图片描述

Grafana

  • 下载 Grafana:https://grafana.com/grafana/download
wget https://dl.grafana.com/oss/release/grafana-7.0.3-1.x86_64.rpm
sudo yum install grafana-7.0.3-1.x86_64.rpm
  • 启动Grafana, service grafana-server start

  • 访问Grafana , http://localhost:3000 , 默认的帐号密码 admin/admin

  • 登录进去后先配置数据源,这里我们的数据源是prometheus
    在这里插入图片描述

  • 接下来配置dashboard,配置完后就可以看到一个比较直观的图表统计
    在这里插入图片描述

  • tips ,grafana官网提供了很多现成的数据模板,可以直接拿来使用
    https://grafana.com/grafana/dashboards

在这里插入图片描述

将选中的id导入到我们的grafana中
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值