沪ICP备2021032517号-1

Sprint Boot 项目集成 Prometheus监控

  |   0 评论   |   0 浏览

POM文件添加依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

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

k8s configmap 环境变量应用配置

configmap

apiVersion: v1
kind: ConfigMap
data:
  MANAGEMENT_ENDPOINT_LOGGERS_ENABLED: "true"
  MANAGEMENT_ENDPOINTS_WEB_EXPOSURE_INCLUDE: info,health,prometheus,loggers,instance
  MANAGEMENT_METRICS_DISTRIBUTION_SLA_HTTP_SERVER_REQUESTS: 5ms,10ms,25ms,50ms,100ms,250ms,500ms,1000ms,2500ms,5s,10s
  MANAGEMENT_SERVER_PORT: "9998"
metadata:
  annotations:
  labels:
    cattle.io/creator: norman
  name: java-spring-boot-actuator
  namespace: app-dev

deployment

        envFrom:
        - configMapRef:
            name: java-spring-boot-actuator
        image: harbor.learn.com/mblog-app-service:20210813-v2.8.0-release

非k8s环境使用配置文件

# Actuator 使用独立端口,避免度量采集针对 /info /health 和 /prometheus 的请求
management.server.port=9998
management.endpoints.web.exposure.include=info,health,prometheus,loggers 
management.endpoint.loggers.enabled=true
management.metrics.distribution.sla.http.server.requests=5ms,10ms,25ms,50ms,100ms,250ms,500ms,1000ms,2500ms,5s,10s

验证集成结果

curl 127.0.0.1:9998/actuator/prometheus
curl 127.0.0.1:9998/actuator/health

Prometheus配置

需要提前安装好 prometheus--operator

kubernetes service 添加标签

'''
labels:
   monitor: prometheus
'''

image.png

serviceMonitor配置

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  namespace: monitoring
  name: spring-prometheus
  labels:
    monitor: prometheus
spec:
  endpoints:
  - port: http
    interval: 30s
    scheme: http
    path: '/actuator/prometheus'
    #path: '/actuator'
  selector:
    matchLabels:
      monitor: prometheus
  namespaceSelector:
    any: true
endpoints:

该部分定义了匹配标签服务的 端口和路径 --port 需要和 service 中的 port 下 -name 值对应

如果只匹配部分命名空间

image.png

匹配所有空间需要添加权限认证

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
  name: prometheus
rules:
- apiGroups: [""]
  resources:
  - nodes
  - services
  - endpoints
  - pods
  verbs: ["get", "list", "watch"]
- apiGroups: [""]
  resources:
  - configmaps
  verbs: ["get"]
- nonResourceURLs: ["/metrics"]
  verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: prometheus
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: prometheus
subjects:
- kind: ServiceAccount
  name: prometheus-k8s
  namespace: monitoring

serviceMonitor配置后可以看到获取数据成功

image.png

配置接口耗时监控展示

grafana 配置

sum(rate(http_server_requests_seconds_count{}[1m]))by(uri,method,status)

当有接口请求后,就可以看到每个接口的请求耗时

image.png

Axes Left Y轴 单位是 时间模块中的 seconds(s)

配置 jvm使用情况监控展示



标题:Sprint Boot 项目集成 Prometheus监控
作者:zifuy
地址:https://www.zifuy.cn/articles/2020/09/08/1599580229726.html