遇一山,过一山,处处有风景;只要勇敢向前,一路尽是繁花盛开。 | (点击查看→)【测试干货】python/java自动化、持续集成、性能、测开、简历、笔试面试等

prometheus监控java项目(jvm等):k8s外、k8s内

前言

虽然可以使用jvisualvm之类的工具监控java项目,但是集群环境下,还是捉襟见肘,下面介绍如何用主流的prometheus来监控java项目。

 

java项目配置

在pom.xml中添加依赖 

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--暴露histogram-->
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
            <version>1.5.1</version>
            <!--<version>1.1.3</version>-->
        </dependency>
        <!-- 可选, 用于进程内存使用图表 -->
        <dependency>
            <groupId>io.github.mweirauch</groupId>
            <artifactId>micrometer-jvm-extras</artifactId>
            <version>0.2.0</version>
        </dependency>

 

修改spring boot配置(application.yml)

(下面management默认内容也是,可以不配)

server:
  port: 8096

spring:
  application:
    name: gift

management:
  endpoint:
    health:
      show-details: always
  endpoints:
    web:
      exposure:
        include: 'prometheus'  # 暴露/actuator/prometheus
  metrics:
    tags:
      application: ${spring.application.name}  # 暴露的数据中添加application label

 

非k8s下java项目监控

打包:mvn clean package

启动项目:java -jar xxx.jar

请求:http://xxx:8096/qzcsbj/actuator/prometheus,查看暴露的数据

 

在Prometheus中添加配置:vim prometheus.yml

  - job_name: "gift"
    metrics_path: "/qzcsbj/actuator/prometheus"
    static_configs:
    - targets: ["qzcsbj:8096"]

 

重启prometheus:systemctl restart prometheus

 

访问prometheus:http://ip:9090/targets

 

targets下可以看到这个端点

 

grafana导入模板:4701

 

效果如下:

 

 

其它参考:非k8s下java项目监控

https://www.cnblogs.com/uncleyong/p/16335583.html

 

k8s下java项目监控

打包:mvn clean package -Dmaven.test.skip=true

打镜像:docker build -t 192.168.117.160/gifts/gift:v1 .

把镜像推送到镜像仓库:docker push 192.168.117.160/gifts/gift:v1

编写资源yaml文件:deploy和svc

vim gift.yaml

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: gift-java-deploy
  namespace: gift
spec:
  replicas: 1
  selector:
    matchLabels:
      k8s-app: gift
  template:
    metadata:
      labels:
        k8s-app: gift
    spec:
      imagePullSecrets:
      - name: gifts-registry

      containers:
      - name: gift
        image: 192.168.117.160/gifts/gift:v1215
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8096

---
apiVersion: v1
kind: Service
metadata:
  name: gift-java-deploy-svc
  namespace: gift
  labels:
    k8s-app: gift-java-deploy-svc
spec:
  type: ClusterIP
  selector:
    k8s-app: gift
  ports:
  - name: gift-java-deploy-svc-api
    port: 8096
    targetPort: 8096
    protocol: TCP

 

创建资源:kubectl apply -f gift.yaml

 

查看资源:kubectl get all -n gift

 

编写servicemonitor资源yaml文件:

vim gift-sm.yaml

---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: java-servicemonitor
  namespace: monitoring
  labels:
    k8s-app: gift
spec:
  endpoints:
  - port: gift-java-deploy-svc-api
    interval: 30s
    scheme: http
    path: '/qzcsbj/actuator/prometheus'
  selector:
    matchLabels:
      k8s-app: gift-java-deploy-svc
  namespaceSelector:
    matchNames:
    - gift

 

创建资源:kubectl apply -f gift-sm.yaml

 

查看资源:kubectl get servicemonitor -n monitoring

 

查看prometheus端口:kubectl get svc -n monitoring

 

访问prometheus:http://192.168.117.162:30576/

 

config已经自动生成配置:搜索gift

 

自动发现

 

targets

 

查询数据:jvm_threads_live_threads

 

grafana导入模板:12856

 

效果:

 

 

思考:看到这个监控数据,你的思路是?

 

 

【bak】

 

 

 

 

posted @ 2021-12-15 17:02  全栈测试笔记  阅读(4335)  评论(0编辑  收藏  举报
浏览器标题切换
浏览器标题切换end