Actuator+Prometheus+Grafana打造SpringBoot监控平台

随着业务的越发复杂,对软件系统的要求越来越高,这意味着我们需要随时掌控系统的运行情况。因此,对系统的实时监控以及可视化展示,就成了基础架构的必须能力。

本文介绍Actuator+Prometheus + Grafana的方法监控Springboot 2.X,实现美观漂亮的数据可视化。

环境

  • SpringBoot 2.1.0.RELEASE
  • Actuator
  • Grafana 5.4.2
  • Prometheus 2.6.0
  • JDK 1.8

SpringBoot 项目配置

配置 Actuator

Actuator是Spring自带的监控神器,集成非常简单,使用起来更是简单到爽歪歪,就是简单地rest接口调用查看,在要求不高的监控环境可以使用。

引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <version>2.1.3.RELEASE</version>
    <optional>true</optional>
</dependency>

配置参数

在项目配置文件中配置相关参数

properties 配置方法

#设置监控访问的应用根路径,默认是/actuator
management.endpoints.web.base-path=/monitor
#暴露监控访问接口,默认是/health和/info
management.endpoints.web.exposure.include=*
#显式屏蔽监控访问接口
management.endpoints.web.exposure.exclude=env,metrics
#开放关闭应用程序端点,不建议开启
management.endpoint.shutdown.enabled=true

yml 配置方法

management:
  endpoints:
    enabled: true
    web:
      base-path: /actuator  # 访问根路径
      exposure:
        include: "*"

验证

启动项目,可以在启动日志里面查看到actuator的启动路径

2020-08-03 17:08:26.626  INFO 332 --- [  restartedMain] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 16 endpoint(s) beneath base path '/actuator'

浏览器访问地址 localhost:8081/actuator 看到如下监控数据,说明Springboot能正常提供监控数据。

接口描述

具体每个接口的描述请查看 https://www.jianshu.com/p/d59f06724f1b

屏蔽检查

Actuator的健康检查是具有默认机制的,如检测程序中redis的依赖,则会检测redis的连接情况,如果想屏蔽掉redis的检测,则需要重写相关的检测类来实现,以屏蔽redis为例。

创建一个类来继承AbstractHealthIndicator,实现自己的redis健康检查,如:

/**
* 自定义的redis健康检查类型
* 忽略redis的健康检查
* */

@Slf4j
@Component("redisHealthIndicator")
public class OverrideRedisHealthIndicator extends AbstractHealthIndicator {
    @Override
    protected void doHealthCheck(Health.Builder builder)throws Exception {
        log.warn("actuator ignore redis health check");
    }
}

自定义类的名称必须是 redisHealthIndicator ,不然无法覆盖原有的RedisHealthIndicator。

配置 Prometheus

引入依赖

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

配置参数

properties 配置

spring.application.name=springboot_prometheus
management.endpoints.web.exposure.include=*
management.metrics.tags.application=${spring.application.name}

启动类配置

@SpringBootApplication
public class Springboot2PrometheusApplication {

    public static void main(String[] args) {
        SpringApplication.run(Springboot2PrometheusApplication.class, args);
    }
    
    @Bean
    MeterRegistryCustomizer<MeterRegistry> configurer(
            @Value("${spring.application.name}") String applicationName) {
        return (registry) -> registry.config().commonTags("application", applicationName);
    }
}

验证

启动项目,访问 http://localhost:8081/actuator/prometheus ,如图所示,可以看到一些度量指标。

Prometheus 安装

下载

下载你想安装的prometheus版本,地址为 download prometheus

我下载的是 prometheus-2.6.0.linux-amd64.tar.gz

解压

tar xvfz prometheus-*.tar.gz
cd prometheus-*

配置

在目录创建 prometheus.yml 文件内容如下

# 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:
  - 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:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['127.0.0.1:9090']
###以下内容为SpringBoot应用配置
  - job_name: 'springboot_prometheus'
    scrape_interval: 5s
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ['127.0.0.1:8080']

注意:metrics_path:和targets的配置。

启动

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

在Status -> Targets页面下,我们可以看到我们配置的Target,它们的State为UP 。

至此,Prometheus 和 Springboot 已经连接成功。

Grafana 安装

Grafana 是一款采用 Go 语言编写的开源应用,主要用于大规模指标数据的可视化展现,是网络架构和应用分析中最流行的时序数据展示工具,目前已经支持绝大部分常用的时序数据库。

安装

使用的是ubuntu 16.04TLS,所以找到官网相对应的Ubuntu方式,这是官网的链接地址:https://grafana.com/grafana/download?platform=linux

wget https://dl.grafana.com/oss/release/grafana_5.4.2_amd64.deb 
sudo dpkg -i grafana_5.4.2_amd64.deb 

启动

方式一、Start Grafana by running:

sudo service grafana-server start
sudo update-rc.d grafana-server defaults //设置开机启动(可选)

方式二、To start the service using systemd:

systemctl daemon-reload
systemctl start grafana-server
systemctl status grafana-server
sudo systemctl enable grafana-server.service //设置开机启动

配置

grafana支持很多种看板,你可以根据不同的指标生成图表,

因为图表的配置比较复杂,这里没有深入的研究,而是选用了大神 们做好的模板,对接数据源进行展示

https://grafana.com/dashboards 在这里可以搜索不同的模板。

选择一个你想要的点击去,然后复制id,打开 grafana 并将id粘贴进去,光标离开输入框,会自动加载模板配置。

接着选datasource,然后选取数据源,点击import按钮,完成模板添加。

多个应用的配置,如下配置两个微服务应用:

# 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: 'service-productor'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    metrics_path: /prometheus
    static_configs:
    - targets: ['10.200.110.100:8082']
  - job_name: 'service-consumer'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    metrics_path: /prometheus
    static_configs:
    - targets: ['10.200.110.100:8081']

参考

https://www.cnblogs.com/duanxz/p/10179512.html
https://www.jianshu.com/p/829f91982a0f
https://www.jianshu.com/p/d59f06724f1b
https://blog.csdn.net/qq_33257527/article/details/88294016

本文链接: https://jianz.xyz/index.php/archives/3/

1 + 3 =
快来做第一个评论的人吧~