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

沙海 2021年3月21日01:45:11杂谈 Java评论29字数 2330阅读7分46秒阅读模式
摘要

Spring Boot 应用可视化监控,一目了然! Java后端编程

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

Java后端编程 文章源自JAVA秀-https://www.javaxiu.com/5926.html

点击上方 java后端编程 ,选择 星标 公众号重磅资讯,干货,第一时间送达
文章源自JAVA秀-https://www.javaxiu.com/5926.html

1、Spring Boot 应用暴露监控指标【版本 1.5.7.RELEASE】文章源自JAVA秀-https://www.javaxiu.com/5926.html

首先,添加依赖如下依赖:文章源自JAVA秀-https://www.javaxiu.com/5926.html

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

采集应用的指标信息,我们使用的是prometheus,相应的我们引入包:文章源自JAVA秀-https://www.javaxiu.com/5926.html

<dependency>            <groupId>io.prometheus</groupId>      <artifactId>simpleclient_spring_boot</artifactId>                     <version>0.0.26</version>    </dependency>

然后,在启动类 Application.java 添加如下注解:文章源自JAVA秀-https://www.javaxiu.com/5926.html

@SpringBootApplication@EnablePrometheusEndpoint@EnableSpringBootMetricsCollectorpublic class Application {      public static void main(String[] args) {        SpringApplication.run(Application.class, args);  }}

最后,配置默认的登录账号和密码,在 application.yml 中:文章源自JAVA秀-https://www.javaxiu.com/5926.html

security:  user:    name:user    password: pwd

启动应用程序后,会看到如下一系列的 Mappings文章源自JAVA秀-https://www.javaxiu.com/5926.html

Spring Boot 应用可视化监控,一目了然!文章源自JAVA秀-https://www.javaxiu.com/5926.html

img文章源自JAVA秀-https://www.javaxiu.com/5926.html

利用账号密码访问 http://localhost:8080/application/prometheus ,可以看到 Prometheus 格式的指标数据 文章源自JAVA秀-https://www.javaxiu.com/5926.html

Spring Boot 应用可视化监控,一目了然!文章源自JAVA秀-https://www.javaxiu.com/5926.html

首先,获取 Prometheus 的 Docker 镜像:文章源自JAVA秀-https://www.javaxiu.com/5926.html

$ docker pull prom/prometheus

然后,编写配置文件 prometheus.yml :文章源自JAVA秀-https://www.javaxiu.com/5926.html

global:  scrape_interval: 10s  scrape_timeout: 10s  evaluation_interval: 10mscrape_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 :文章源自JAVA秀-https://www.javaxiu.com/5926.html

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这是镜像,如果本地没有,就回去你设置好的镜像仓库去取。文章源自JAVA秀-https://www.javaxiu.com/5926.html

文章源自JAVA秀-https://www.javaxiu.com/5926.html

启动完成后用docker ps看下是否已经启动成功,之后打开浏览器输入:http://localhost:9090/targets,检查 Spring Boot 采集状态是否正常,如果看到下图就是成功了。文章源自JAVA秀-https://www.javaxiu.com/5926.html

Spring Boot 应用可视化监控,一目了然!文章源自JAVA秀-https://www.javaxiu.com/5926.html

img文章源自JAVA秀-https://www.javaxiu.com/5926.html

首先,获取 Grafana 的 Docker 镜像:文章源自JAVA秀-https://www.javaxiu.com/5926.html

$ docker pull grafana/grafana1

然后,启动 Grafana:文章源自JAVA秀-https://www.javaxiu.com/5926.html

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

接着,访问 http://localhost:3000/ 配置 Prometheus 数据源:文章源自JAVA秀-https://www.javaxiu.com/5926.html

Grafana 登录账号 admin 密码 admin文章源自JAVA秀-https://www.javaxiu.com/5926.html

  1. 先配置数据源.文章源自JAVA秀-https://www.javaxiu.com/5926.html

Spring Boot 应用可视化监控,一目了然!文章源自JAVA秀-https://www.javaxiu.com/5926.html

img文章源自JAVA秀-https://www.javaxiu.com/5926.html

2.配置单个指标的可视化监控面板:文章源自JAVA秀-https://www.javaxiu.com/5926.html

Spring Boot 应用可视化监控,一目了然!文章源自JAVA秀-https://www.javaxiu.com/5926.html

img文章源自JAVA秀-https://www.javaxiu.com/5926.html

Spring Boot 应用可视化监控,一目了然!文章源自JAVA秀-https://www.javaxiu.com/5926.html

img文章源自JAVA秀-https://www.javaxiu.com/5926.html

Spring Boot 应用可视化监控,一目了然!文章源自JAVA秀-https://www.javaxiu.com/5926.html

img文章源自JAVA秀-https://www.javaxiu.com/5926.html

prometh采集的数据文章源自JAVA秀-https://www.javaxiu.com/5926.html

Spring Boot 应用可视化监控,一目了然!文章源自JAVA秀-https://www.javaxiu.com/5926.html

img文章源自JAVA秀-https://www.javaxiu.com/5926.html

Spring Boot 应用可视化监控,一目了然!文章源自JAVA秀-https://www.javaxiu.com/5926.html

img文章源自JAVA秀-https://www.javaxiu.com/5926.html

推荐文章文章源自JAVA秀-https://www.javaxiu.com/5926.html

基于SpringBoot库存管理系统,附源码!一文全面解析postman工具新来的妹纸问我, AJAX 请求为什么不安全?一款高颜值的 SpringBoot 项目,值得收藏!Docker常用命令,这些都要会,记得收藏!社群福利:程序员技术交流群这可能是你离头部大佬最近的一次,为提高群成员质量,表哥特意邀请了华为、腾讯、阿里的朋友进群,方便大家学习交流,一起进步。有兴趣入群的同学,可长按扫描下方二维码,一定要备注:城市+昵称+技术方向,根据格式备注,可更快被通过且邀请进群▲长按添加好友进交流群
文章源自JAVA秀-https://www.javaxiu.com/5926.html
继续阅读
速蛙云 - 极致体验,强烈推荐!!!购买套餐就免费送各大视频网站会员!快速稳定、独家福利社、流媒体稳定解锁!速度快,全球上网、视频、游戏加速、独立IP均支持!基础套餐性价比很高!这里不多说,我一直正在使用,推荐购买:https://www.javaxiu.com/59919.html
weinxin
资源分享QQ群
本站是JAVA秀团队的技术分享社区, 会经常分享资源和教程; 分享的时代, 请别再沉默!
沙海
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定