prometheus监控spring boot项目

prometheus是一个开源系统。它通过抓取目标的HTTP端点暴露的metrics数据来进行监控和报警。Prometheus主要的功能就是监控,监控的基础上才会有报警。本文通过prometheus监控spring boot项目来对prometheus进行介绍,去掉不常见次要的功能(短生命周期Job的监控、服务发现、报警)。架构图如下,其中Prometheus target为要监控的spring boot项目,Prometheus Server为Prometheus服务,Grafana为另一个系统,通过采集Prometheus的数据做监控的可视化功能。


整体架构图

可以看到Grafana依赖于Prometheus Server,Prometheus Server依赖于Prometheus target。我们从最低层的Prometheus target开始。

1)Prometheus target

如果是一个Spring Boot2.x的项目,可以非常方便的配置好对Prometheus的支持,这里就以一个Spring Boot2.x的Java Web应用为例。
1)pom.xml中添加actuator及prometheus依赖

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
      <groupId>io.micrometer</groupId>
      <artifactId>micrometer-registry-prometheus</artifactId>
    </dependency>

2)application.yml中添加配置

    management:
      endpoints:
        web:
          exposure:
            include: prometheus

3)访问http://localhost:8080/actuator/prometheus会响应prometheus的metrics信息,如下:

    # HELP process_cpu_usage The "recent cpu usage" for the Java Virtual Machine process
    # TYPE process_cpu_usage gauge
    process_cpu_usage 0.26040277851847904
    # HELP tomcat_cache_hit_total  
    # TYPE tomcat_cache_hit_total counter
    tomcat_cache_hit_total 0.0
    # HELP hikaricp_connections_timeout_total Connection timeout total count
    # TYPE hikaricp_connections_timeout_total counter
    hikaricp_connections_timeout_total{pool="HikariPool-1",} 0.0
    # HELP jdbc_connections_max  
    # TYPE jdbc_connections_max gauge
    jdbc_connections_max{name="dataSource",} 10.0
    ...

此时即完成了Prometheus target的工作,显示的metrics信息被Promethus进行采集用于监控。

2)Prometheus Server

Prometheus Server有几种方式,这里使用Docker安装方式。如下:

    docker run -p 9090:9090 \
    -v /tmp/prometheus.yml:/etc/prometheus/prometheus.yml \
        prom/prometheus

其中-v参考指定了将本机中的/tmp/prometheus.yml文件映射到/etc/prometheus/prometheus.yml文件。
也可以使用Dockerfile将文件拷贝到镜像中,Dockerfile如下:

    FROM prom/prometheus
    ADD prometheus_config_address.yml /etc/prometheus/prometheus.yml

该prometheus.yml文件即为promethues的配置文件,Prometheus配置监控哪个Target是通过该配置文件配置的。配置文件内容如下:其中scrape_configs就是配置要监控哪个项目。

    global:
      scrape_interval:     10s # 抓取时间间隔
      evaluation_interval: 10s # 评估规则时间间隔
    scrape_configs:
      - job_name: 'xxx'
    static_configs:
      - targets: ['10.10.10.10:1000'] # 配置EndPoint(IP:端口)
    metrics_path: '/actuator/prometheus'# 配置EndPoint(路径)

3)Grafana

Grafana用于采集Prometheus数据进行可视化。这里也使用Docker环境的来安装Grafana,和Prometheus Server不同的是,Grafana配置依赖的Prometheus Server是通过界面配置的,而不是通过配置文件。配置项主要有两个,要采集的数据源(grafana除了Prometheus还支持其它数据源)和要显示的仪表板(对数据进行什么样的显示)。
3.1 创建数据源(data source)
主要是配置URL,其它使用默认即可,这里的URL要注意,由于是使用Docker,所以localhost代表容器内部的本机,所以并不能获取到prometheus地址。要使用服务名。
3.2 创建仪表板(dashboard)
创建一个面板(panel),编译面板,选择数据源为Prometheus,查询窗口填写prometheus metrics中的key即可实时显示出效果。

至此,即可在Grafana上以图形的方式显示出spring boot项目的监控数据。图形如下:
grafana显示效果
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,421评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,057评论 6 342
  • 欢迎来到Spring Boot Actuator教程系列的第二部分。在第一部分中,你学习到了spring-boot...
    alvin_wang阅读 32,624评论 1 35
  • ▏年之书(节选) * 并不需要成为科学家才能理解树的世界 只要成为一只鸟,或者 四月天。 * 那些小燕子, 那么小...
    夜行不锦衣阅读 3,499评论 0 0
  • 荐语: 暴力?我们现在离暴力很远了吧,暴力是战争、打架、破口大骂……那么是不是就这些呢? 如果是,咱们遵纪守法的普...
    童妈后花园阅读 1,911评论 0 1