利用 prometheus 监控 spring boot

spring-boot-monitor

这里采用spring-boot-actuator和 micrometer-registry-prometheus做监控

1.spring-boot-starter-actuator

Spring Boot Actuator可以帮助开发者在生产环境中监视和管理应用程序,一旦在pom中添加spring-boot-starter-actuator依赖,Spring Boot将自动集成审计、健康和指标收集等功能到应用程序中。
首先需要在pom文件中引入依赖


  org.springframework.boot
  spring-boot-starter-actuator

默认无需配置,访问接口,经测试默认只有以下三个接口可用,如果只是用作探针,无需担心安全问题

curl http://127.0.0.1:8080/actuator/health
{"status":"UP"}

curl http://127.0.0.1:8080/actuator/
{"_links":{"self":{"href":"http://127.0.0.1:8080/actuator","templated":false},"health":{"href":"http://127.0.0.1:8080/actuator/health","templated":false},"info":{"href":"http://127.0.0.1:8080/actuator/info","templated":false}}}

curl http://127.0.0.1:8080/actuator/info
{}

修改配置文件application.properties 开放其他功能

management.endpoints.web.exposure.include:*
management.endpoints.enabled-by-default=true
management.endpoint.metrics.enabled=true

这里对所有的功能进行说明

序号 路径(默认/actuator以下) 说明 是否默认开启
1 auditevents 公开审计事件信息 Yes
2 beans 列出所有Spring管理的的Beans Yes
3 caches 公开可用缓存 Yes
4 conditions 展示所有自动配置的条件,并显示匹配或不匹配的原因 Yes
5 configprops 展示@ConfigurationProperties列表 Yes
6 env 公开ConfigurableEnvironment下的属性 Yes
7 flyway 展示由Flyway管理的数据库脚本 Yes
8 health 展示程序的健康信息 Yes
9 httptrace 展示HTTP轨迹信息,默认显示最后100次 Yes
10 info 展示应用程序信息 Yes
11 integrationgraph 显示Spring集成视图 Yes
12 loggers 显示和修改日志配置文件 Yes
13 liquibase 显示由Liquibase管理的数据库脚本 Yes
14 metrics 显示指标信息 Yes
15 mapping 显示所有被@RequestMapping拦截的路径 Yes
16 scheduledtasks 显示定时任务 Yes
17 sessions 可以检索或删除会话(Spring管理的会话 Yes
18 shutdown 发送优雅关闭指令 No
19 threaddump 下载线程dump文件 Yes
20 heapdump 下载堆dump文件(只在Web应用下有效) Yes
21 jolokia 通过HTTP公开JMX Beans(只在Web应用下有效) Yes
22 logfile 返回日志文件内容(只在Web应用下有效) Yes
23 prometheus 以Prometheus服务格式公开指标(只在Web应用下有效) Yes

2.micrometer-registry-prometheus

micrometer-registry-prometheus底层也是spring-boot-starter-actuator实现,只是以open metric的标准提供了一个可用prometheus的监控接口

首先需要在pom文件中引入依赖

        
            io.micrometer
            micrometer-registry-prometheus
        

        
            org.springframework.boot
            spring-boot-starter-actuator
        

配置文件中配置

management.endpoints.web.exposure.include:*
management.endpoints.enabled-by-default=true
management.endpoint.metrics.enabled=true
management.endpoint.prometheus.enabled=true
management.metrics.export.prometheus.enabled=true

打开链接查看指标
http://127.0.0.1:8080/actuator/prometheus

# HELP tomcat_global_request_max_seconds  
# TYPE tomcat_global_request_max_seconds gauge
tomcat_global_request_max_seconds{name="http-nio-8080",} 0.116
# HELP tomcat_sessions_active_max_sessions  
# TYPE tomcat_sessions_active_max_sessions gauge
tomcat_sessions_active_max_sessions 0.0
# HELP jvm_memory_used_bytes The amount of used memory
# TYPE jvm_memory_used_bytes gauge
jvm_memory_used_bytes{area="heap",id="PS Survivor Space",} 0.0
jvm_memory_used_bytes{area="heap",id="PS Old Gen",} 2.329168E7
jvm_memory_used_bytes{area="heap",id="PS Eden Space",} 9.1122472E7
jvm_memory_used_bytes{area="nonheap",id="Metaspace",} 3.8637376E7
jvm_memory_used_bytes{area="nonheap",id="Code Cache",} 8776384.0
jvm_memory_used_bytes{area="nonheap",id="Compressed Class Space",} 5247024.0
# HELP system_cpu_usage The "recent cpu usage" for the whole system
....

添加到prometheus,以下有2种方式让prometheus进行监控,pod和svc的方式,因为应用有可能有多个副本,这里把pod作为性能分析,svc作为状态检查,当然也可以用ingress。
prometheus支持K8s服务发现机制,默认将扫描所有的资源,这里增加过滤器,表示只有注释prometheus_io_scrape_springboot=true才进行采集

- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape_springboot]
  action: keep
  regex: true

以下是完整的job配置文件

      # 获取springboot pod的监控指标
      - job_name: springboot
        metrics_path: /actuator/prometheus
        kubernetes_sd_configs:
        - role: pod
        relabel_configs:
        - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape_springboot]
          action: keep
          regex: true
        - source_labels: [__meta_kubernetes_pod_name]
          action: replace
          target_label: kubernetes_pod_name
        - source_labels: [__meta_kubernetes_namespace]
          action: replace
          target_label: kubernetes_namespace
        - source_labels: [__meta_kubernetes_pod_container_name]
          action: replace
          target_label: kubernetes_pod_container_name
      # 获取springboot svc的监控指标
      - job_name: springboot
        metrics_path: /actuator/prometheus
        kubernetes_sd_configs:
        - role: pod
        relabel_configs:
        - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scrape_springboot]
          action: keep
          regex: true
        - source_labels: [__meta_kubernetes_namespace]
          action: replace
          target_label: kubernetes_namespace
        - source_labels: [__meta_kubernetes_service_name]
          action: replace
          target_label: kubernetes_service_name

这是prometheus启动后的效果


利用 prometheus 监控 spring boot_第1张图片
image

应用一:自动后端服务的可用性检测


利用 prometheus 监控 spring boot_第2张图片
image

应用二:性能监控
grafana官方有很多dashbad
https://grafana.com/grafana/dashboards?search=java

利用 prometheus 监控 spring boot_第3张图片
image

你可能感兴趣的:(利用 prometheus 监控 spring boot)