1. 首页
  2. 程序员

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

快来试试 Spring Boot 应用可视化监控,一目了然!
程序员的成长之路
互联网/程序员/技术/资料共享 
关注
 

阅读本文大概需要 2 分钟。

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

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

首先,添加依赖如下依赖:
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"><span class="code-snippet__tag" style="max-width: 1000%;"><<span class="code-snippet__name" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;">dependency</span>></span> </span><span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <span class="code-snippet__tag" style="max-width: 1000%;"><<span class="code-snippet__name" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;">groupId</span>></span>org.springframework.boot<span class="code-snippet__tag" style="max-width: 1000%;"></<span class="code-snippet__name" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;">groupId</span>></span> </span><span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <span class="code-snippet__tag" style="max-width: 1000%;"><<span class="code-snippet__name" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;">artifactId</span>></span>spring-boot-starter-actuator<span class="code-snippet__tag" style="max-width: 1000%;"></<span class="code-snippet__name" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;">artifactId</span>></span> </span><span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"><span class="code-snippet__tag" style="max-width: 1000%;"></<span class="code-snippet__name" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;">dependency</span>></span> </span>
采集应用的指标信息,我们使用的是prometheus,相应的我们引入包:
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"><span class="code-snippet__tag" style="max-width: 1000%;"><<span class="code-snippet__name" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;">dependency</span>></span> </span><span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <span class="code-snippet__tag" style="max-width: 1000%;"><<span class="code-snippet__name" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;">groupId</span>></span>io.prometheus<span class="code-snippet__tag" style="max-width: 1000%;"></<span class="code-snippet__name" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;">groupId</span>></span> </span><span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <span class="code-snippet__tag" style="max-width: 1000%;"><<span class="code-snippet__name" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;">artifactId</span>></span>simpleclient_spring_boot<span class="code-snippet__tag" style="max-width: 1000%;"></<span class="code-snippet__name" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;">artifactId</span>></span> <span class="code-snippet__tag" style="max-width: 1000%;"><<span class="code-snippet__name" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;">version</span>></span>0.0.26<span class="code-snippet__tag" style="max-width: 1000%;"></<span class="code-snippet__name" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;">version</span>></span> </span><span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"></<span class="code-snippet__name" style="max-width: 1000%;">dependency</span>></span>
然后,在启动类 Application.java 添加如下注解:
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;">@SpringBootApplication</span><span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;">@EnablePrometheusEndpoint</span><span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;">@EnableSpringBootMetricsCollector</span><span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"><span class="code-snippet__keyword" style="max-width: 1000%;">public</span> <span class="code-snippet__class" style="max-width: 1000%;"><span class="code-snippet__keyword" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;">class</span> <span class="code-snippet__title" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;">Application</span> </span>{ </span><span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <span class="code-snippet__function" style="max-width: 1000%;"><span class="code-snippet__keyword" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;">public</span> <span class="code-snippet__keyword" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;">static</span> <span class="code-snippet__keyword" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;">void</span> <span class="code-snippet__title" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;">main(String[] args)</span> </span>{ SpringApplication.run(Application.class, args); </span><span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> }</span><span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;">}</span>
最后,配置默认的登录账号和密码,在 application.yml 中:
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"><span class="code-snippet__attr" style="max-width: 1000%;">security</span>:</span><span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <span class="code-snippet__attr" style="max-width: 1000%;">user</span>:</span><span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <span class="code-snippet__attr" style="max-width: 1000%;">name</span>:<span class="code-snippet__string" style="max-width: 1000%;">user</span></span><span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <span class="code-snippet__attr" style="max-width: 1000%;">password</span>: <span class="code-snippet__string" style="max-width: 1000%;">pwd</span></span>
启动应用程序后,会看到如下一系列的 Mappings
快来试试 Spring Boot 应用可视化监控,一目了然!
img
利用账号密码访问 http://localhost:8080/application/prometheus ,可以看到 Prometheus 格式的指标数据 

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

2、Prometheus 采集 Spring Boot 指标数据

首先,获取 Prometheus 的 Docker 镜像:
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"><span class="code-snippet__meta" style="max-width: 1000%;">$</span> docker pull prom/prometheus</span>
然后,编写配置文件 prometheus.yml :
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"><span class="code-snippet__attr" style="max-width: 1000%;">global</span>:</span><span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <span class="code-snippet__attr" style="max-width: 1000%;">scrape_interval</span>: <span class="code-snippet__string" style="max-width: 1000%;">10s</span></span><span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <span class="code-snippet__attr" style="max-width: 1000%;">scrape_timeout</span>: <span class="code-snippet__string" style="max-width: 1000%;">10s</span></span><span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <span class="code-snippet__attr" style="max-width: 1000%;">evaluation_interval</span>: <span class="code-snippet__string" style="max-width: 1000%;">10m</span></span><span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"><span class="code-snippet__attr" style="max-width: 1000%;">scrape_configs</span>:</span><span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <span class="code-snippet__meta" style="max-width: 1000%;">-</span> <span class="code-snippet__string" style="max-width: 1000%;">job_name: spring-boot</span></span><span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <span class="code-snippet__attr" style="max-width: 1000%;">scrape_interval</span>: <span class="code-snippet__string" style="max-width: 1000%;">5s</span></span><span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <span class="code-snippet__attr" style="max-width: 1000%;">scrape_timeout</span>: <span class="code-snippet__string" style="max-width: 1000%;">5s</span></span><span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <span class="code-snippet__attr" style="max-width: 1000%;">metrics_path</span>: <span class="code-snippet__string" style="max-width: 1000%;">/application/prometheus</span></span><span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <span class="code-snippet__attr" style="max-width: 1000%;">scheme</span>: <span class="code-snippet__string" style="max-width: 1000%;">http</span></span><span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <span class="code-snippet__attr" style="max-width: 1000%;">basic_auth</span>:</span><span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <span class="code-snippet__attr" style="max-width: 1000%;">username</span>: <span class="code-snippet__string" style="max-width: 1000%;">admin</span></span><span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <span class="code-snippet__attr" style="max-width: 1000%;">password</span>: <span class="code-snippet__string" style="max-width: 1000%;">123456</span></span><span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <span class="code-snippet__attr" style="max-width: 1000%;">static_configs</span>:</span><span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <span class="code-snippet__meta" style="max-width: 1000%;">-</span> <span class="code-snippet__string" style="max-width: 1000%;">targets:</span></span><span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <span class="code-snippet__meta" style="max-width: 1000%;">-</span> <span class="code-snippet__string" style="max-width: 1000%;">192.168.11.54:8099 #此处填写 Spring Boot 应用的 IP + 端口号</span></span>
接着,启动 Prometheus :
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;">docker run -d --name prometheus -p <span class="code-snippet__number" style="max-width: 1000%;">9090</span>:<span class="code-snippet__number" style="max-width: 1000%;">9090</span></span><span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;">-v D:testactuatorprometheusprometheus.yml:<span class="code-snippet__regexp" style="max-width: 1000%;">/etc/</span>prometheus/prometheus.yml prom/prometheus</span>
请注意,D:testactuatorprometheusprometheus.yml ,是我的配置文件存放地址,我们需要将它放到容器里面去,所以用了-v来做文件映射。/etc/prometheus/prometheus.yml这个是容器启动的时候去取的默认配置,这里我是直接覆盖掉了它。prom/prometheus这是镜像,如果本地没有,就回去你设置好的镜像仓库去取。
启动完成后用docker ps看下是否已经启动成功,之后打开浏览器输入:
http://localhost:9090/targets,检查 Spring Boot 采集状态是否正常,如果看到下图就是成功了。
快来试试 Spring Boot 应用可视化监控,一目了然!
img

3、Grafana 可视化监控数据

首先,获取 Grafana 的 Docker 镜像:
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"><span class="code-snippet__meta" style="max-width: 1000%;">$</span> docker pull grafana/grafana1</span>
然后,启动 Grafana:
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"><span class="code-snippet__meta" style="max-width: 1000%;">$</span> docker run --name grafana -d -p 3000:3000 grafana/grafana1</span>
接着,访问 http://localhost:3000/ 配置 Prometheus 数据源:
Grafana 登录账号 admin 密码 admin
  1. 先配置数据源.
快来试试 Spring Boot 应用可视化监控,一目了然!
img
2.配置单个指标的可视化监控面板:
快来试试 Spring Boot 应用可视化监控,一目了然!
img
快来试试 Spring Boot 应用可视化监控,一目了然!
img
快来试试 Spring Boot 应用可视化监控,一目了然!
img
prometh采集的数据
快来试试 Spring Boot 应用可视化监控,一目了然!
img
快来试试 Spring Boot 应用可视化监控,一目了然!
img
<END>

推荐阅读:

挑战微信失败!这个APP停止下载

如何保障消息100%投递成功、消息幂等性?

最近面试BAT,整理一份面试资料《Java面试BATJ通关手册》,覆盖了Java核心技术、JVM、Java并发、SSM、微服务、数据库、数据结构等等。


获取方式:点个「在看」,点击上方小卡片,进入公众号后回复「面试题」领取,更多内容陆续奉上。

朕已阅 快来试试 Spring Boot 应用可视化监控,一目了然!

文章收集整理于网络,请勿商用,仅供个人学习使用,如有侵权,请联系作者删除,如若转载,请注明出处:http://www.cxyroad.com/2377.html

QR code