Spring Boot 应用可视化监控,一目了然!

互联网架构师

共 3385字,需浏览 7分钟

 · 2021-06-07

上一篇:一个90后员工猝死的全过程


1、Spring Boot 应用暴露监控指标

首先,添加依赖如下依赖:

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

采集应用的指标信息,我们使用的是prometheus,相应的我们引入包:

<dependency>          
   <groupId>io.prometheus</groupId>    
   <artifactId>simpleclient_spring_boot</artifactId>                       
   <version>0.0.26</version>    
</dependency>
然后,在启动类 Application.java 添加如下注解:
@SpringBootApplication
@EnablePrometheusEndpoint
@EnableSpringBootMetricsCollector
public class Application {  
    public static void main(String[] args) {
     SpringApplication.run(Application.class, args); 
    }
}
最后,配置默认的登录账号和密码,在 application.yml 中:
security:
  user:
    name:user
    password: pwd

启动应用程序后,会看到如下一系列的 Mappings

利用账号密码访问 http://localhost:8080/application/prometheus ,可以看到 Prometheus 格式的指标数据:

2、Prometheus 采集 Spring Boot 指标数据

首先,获取 Prometheus 的 Docker 镜像:

$ docker pull prom/prometheus

然后,编写配置文件 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: /application/prometheus
    scheme: http
    basic_auth:
      username: admin
      password: 123456
    static_configs:
      - targets:
        - 192.168.11.54:8099 #此处填写 Spring Boot 应用的 IP + 端口号

接着,启动 Prometheus :

docker run -d --name prometheus -p 9090:9090
-v D:\test\actuator\prometheus\prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus

3、Grafana 可视化监控数据

首先,获取 Grafana 的 Docker 镜像:

$ docker pull grafana/grafana1

然后,启动 Grafana:

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

接着,访问 http://localhost:3000/ 配置 Prometheus 数据源:

Grafana 登录账号 admin 密码 admin

  1. 先配置数据源.

2.配置单个指标的可视化监控面板:

prometh采集的数据:

参考:https://www.jianshu.com/p/7ecb57a3f326
原文:https://blog.csdn.net/weixin_44196699/article/details/105010045



最后,关注公众号互联网架构师,在后台回复:2T,可以获取我整理和创作的 Java 系列教程非常齐全。

 推荐阅读 

1、2019 年 9 月全国程序员工资统计,你是什么水平?

2、如何才能成为优秀的架构师?

3、从零开始搭建创业公司后台技术栈

4、程序员一般可以从什么平台接私活?

5、37岁程序员被裁,120天没找到工作,无奈去小公司,结果懵了...

6、滴滴业务中台构建实践,首次曝光

7、不认命,从10年流水线工人,到谷歌上班的程序媛,一位湖南妹子的励志故事

8、15张图看懂瞎忙和高效的区别!


浏览 3
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报