阅读本文大概需要 2 分钟。
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>
<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
2、Prometheus 采集 Spring Boot 指标数据
<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>
<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 采集状态是否正常,如果看到下图就是成功了。3、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 pull grafana/grafana1</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> docker run --name grafana -d -p 3000:3000 grafana/grafana1</span>
Grafana 登录账号 admin 密码 admin
先配置数据源.
prometh
采集的数据推荐阅读:
最近面试BAT,整理一份面试资料《Java面试BATJ通关手册》,覆盖了Java核心技术、JVM、Java并发、SSM、微服务、数据库、数据结构等等。
朕已阅
文章收集整理于网络,请勿商用,仅供个人学习使用,如有侵权,请联系作者删除,如若转载,请注明出处:http://www.cxyroad.com/2377.html