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

1、Spring Boot 应用暴露监控指标【版本 1.5.7.RELEASE】

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

<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

img

利用账号密码访问 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

请注意,D:\test\actuator\prometheus\prometheus.yml ,是我的配置文件存放地址,我们需要将它放到容器里面去,所以用了-v来做文件映射。/etc/prometheus/prometheus.yml这个是容器启动的时候去取的默认配置,这里我是直接覆盖掉了它。prom/prometheus这是镜像,如果本地没有,就回去你设置好的镜像仓库去取。

启动完成后用docker ps看下是否已经启动成功,之后打开浏览器输入:
http://localhost:9090/targets,检查 Spring Boot 采集状态是否正常,如果看到下图就是成功了。

img
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. 先配置数据源.

img

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

img

img

img

prometh采集的数据

img

img

PS:如果觉得我的分享不错,欢迎大家随手点赞、在看。

END

点击下方卡片关注,订阅更多精彩内容

推荐阅读:

华为正式宣布养猪,网友沸腾:支持华为自救!

API 面试四连杀:接口如何设计?安全如何保证?签名如何实现?防重如何实现?

Spring Boot 中引入 MyBatisPlus 的常规流程

垃圾代码和优质代码的区别?

入职腾讯第九年,我辞职了

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值