Spring Boot 2.0 应用监控可视化(Spring Boot 2.0+Prometheus+Grafana)

SpringBoot工程搭建

1、新建Spring boot工程

2、pom.xml的内容如下:

<dependencies>

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

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

        <!-- Spring boot actuator to expose metrics endpoint -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- Micormeter core dependecy  -->
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-core</artifactId>
        </dependency>
        <!-- Micrometer Prometheus registry  -->
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
        </dependency>
        
    </dependencies>

3、application.properties的内容如下:

#Metrics related configurations
management.endpoint.metrics.enabled=true
management.endpoints.web.exposure.include=*
management.endpoint.prometheus.enabled=true
management.metrics.export.prometheus.enabled=true

4、启动应用

访问 http://localhost:8080/actuator ,如下图所示

访问 http://localhost:8080/actuator/prometheus ,如下图所示

至此,Spring boot工程搭建完毕。

Prometheus 采集 Spring Boot 指标数据

1、获取 Prometheus 镜像

docker pull prom/prometheus

2、在本地创建 prometheus.yml 文件,内容如下

global:
  scrape_interval: 10s
  scrape_timeout: 10s
  evaluation_interval: 10m
scrape_configs:
  - job_name: spring-boot
    scrape_interval: 5s
    scrape_timeout: 5s
    metrics_path: /actuator/prometheus
    scheme: http
    static_configs:
      - targets:
        -  127.0.0.1:8080

注意:要把最后的IP地址改为你本地的IP地址,在Windows环境下,执行 ipconfig 命令,即可看到

3、启动容器

docker run -d --name prometheus -p 9090:9090 -v C:\Users\Administrator\config\prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus

注意:如果无法正常启动容器,请检查下面的两点是否符合要求:

  • 在Windows环境下使用容器,一定要把新建的prometheus.yml文件放在C盘的Users(即用户)目录下;
  • 容器需要将C盘开启共享

4、访问 http://localhost:9090/graph ,如下图所示

点击上面菜单栏的 Status -> Targets

即可得到下图:

至此,实现了利用 Prometheus 采集 Spring Boot 指标数据

 

Grafana 可视化监控数据

1、获取 Grafana 镜像

docker pull grafana/grafana

2、运行容器 

docker run --name grafana -d -p 3000:3000 grafana/grafana

3、访问 http://localhost:3000 ,并用账号/密码:admin/admin登录,如下图所示

4、配置 Prometheus 数据源

注意:本地ip地址要写具体的地址,不能写localhost:9090或127.0.0.1:9090

5、配置面板

 

实验效果

 

参考文献

https://dzone.com/users/3228111/satish-sharma.html

http://chenzhijun.me/2017/11/02/spring-boot-actuator-prometheus-grafana/

https://www.jianshu.com/p/7ecb57a3f326

https://cloud.tencent.com/developer/news/121874

https://www.jianshu.com/p/d0a70e2dfeba

Prometheus官方文档

Grafana 官方文档

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值