使用prometheus和grafana监控springboot应用

springboot 应用配置

Gradle

//        monitoring
implementation 'org.springframework.boot:spring-boot-starter-actuator:2.3.4.RELEASE'
compile 'io.micrometer:micrometer-registry-prometheus'
compile 'io.micrometer:micrometer-core'

Maven

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

application.yml

#monitoring
management:
  endpoint:
    metrics:
      enabled: true
    #启用prometheus
    prometheus:
      enabled: true
  endpoints:
    web:
      exposure:
      	# '*' 表示所有,也可以指定如:health,info等
        include: '*'
        exclude: env,beans
  metrics:
    export:
      prometheus:
        enabled: true
  #监控中以使用独立的端口
  server:
    port: 8081 

运行检查

查看springboot健康指标

http://localhost:8081/actuator/health

在这里插入图片描述

up正常,down异常

查看prometheus指标

http://localhost:8081/actuator/prometheus

在这里插入图片描述

配置prometheus

prometheus的配置文件
prometheus.yml

scrape_configs:
  # 可随意指定
  - job_name: 'spring-boot'
    # 多久采集一次数据
    scrape_interval: 15s
    # 采集时的超时时间
    scrape_timeout: 10s
    # 采集的路径
    metrics_path: '/actuator/prometheus'
    # 采集服务的地址,设置成Springboot应用所在服务器的具体地址
    static_configs:
      - targets: ['192.168.100.12:8081']

其中192.168.100.12为本机的ip,注意使用docker运行时,ip不要使用localhost,docker的网卡是独立的。

使用docker运行prometheus

docker run -d -p 9090:9090 -v ~/spring-boot-on-kubernetes/docker/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus --config.file=/etc/prometheus/prometheus.yml

-v 参数,挂载本地~/spring-boot-on-kubernetes/docker/prometheus.yml

grafana配置

配置prometheus
在这里插入图片描述

配置模板
下载grafana模板:https://grafana.com/grafana/dashboards/10280
导入
在这里插入图片描述
展示
在这里插入图片描述
** 使用docker安装 **

docker run -d -p 3001:3000 --link vibrant_meninsky:prometheus grafana/grafana

其中vibrant_meninsky为prometheus 的docker名字

配置prometheus时使用

http://prometheus:9090

在这里插入图片描述

参考
https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html#production-ready-enabling

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值