SpringBoot微服务 运用 Micrometer 集成 Prometheus 监控 Java 应用性能
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SpringBoot微服务 <span style="color: black;">运用</span> Micrometer 集成 Prometheus 监控 Java 应用性能</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">1、Micrometer 介绍</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">2、环境、软件准备</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">3、Spring Boot 工程集成 Micrometer</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">4、配置 Prometheus 监控应用指标</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">5、配置 Grafana Dashboard 展示监控项</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">6、自定义监控指标并展示到 Grafana</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"> 6.1、监控某几个 API 请求次数</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"> 6.2、监控所有 API 请求次数</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"> 6.3、监控实时在线人数</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1、Micrometer 介绍</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Micrometer 为 Java 平台上的性能数据收集<span style="color: black;">供给</span>了一个通用的 API,它<span style="color: black;">供给</span>了多种度量指标类型(Timers、Guauges、Counters等),<span style="color: black;">同期</span>支持接入<span style="color: black;">区别</span>的监控系统,例如 Influxdb、Graphite、Prometheus 等。<span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">经过</span> Micrometer 收集 Java 性能数据,<span style="color: black;">协同</span> Prometheus 监控系统实时获取数据,并<span style="color: black;">最后</span>在 Grafana 上展示出来,从而很容易实现应用的监控。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Micrometer 中有两个最核心的概念,分别是计量器(Meter)和计量器注册表(MeterRegistry)。计量器用来收集<span style="color: black;">区别</span>类型的性能指标信息,Micrometer <span style="color: black;">供给</span>了如下几种<span style="color: black;">区别</span>类型的计量器:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">计数器(Counter): <span style="color: black;">暗示</span>收集的数据是<span style="color: black;">根据</span>某个趋势(<span style="color: black;">增多</span>/减少)<span style="color: black;">始终</span>变化的,<span style="color: black;">亦</span>是最常用的一种计量器,例如接口请求总数、请求错误总数、队列数量变化等。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">计量仪(Gauge): <span style="color: black;">暗示</span>搜集的瞬时的数据,<span style="color: black;">能够</span>任意变化的,例如常用的 CPU Load、Mem <span style="color: black;">运用</span>量、Network <span style="color: black;">运用</span>量、实时在线人数统计等,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">计时器(Timer): 用来记录事件的<span style="color: black;">连续</span>时间,这个用的比较少。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">分布概要(Distribution summary): 用来记录事件的分布<span style="color: black;">状况</span>,<span style="color: black;">暗示</span>一段时间范围内对数据进行采样,<span style="color: black;">能够</span>用于统计网络请求平均延迟、请求延迟占比等。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2、环境、软件准备</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这次</span>演示环境,我是在本机 MAC OS 上操作,以下是安装的软件及版本:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Docker: 18.06.3-ce</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Oracle VirtualBox: 6.0.8 r130520 (Qt5.6.3)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Linux: 7.6.1810</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Prometheus: v2.11.1</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Grafana: v6.3.4</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">重视</span>:<span style="color: black;">这儿</span>为了快速方便<span style="color: black;">起步</span> Prometheus、Grafana 服务,我<span style="color: black;">运用</span> Docker 方式<span style="color: black;">起步</span>,<span style="color: black;">因此</span>本机需要安装好 Docker 环境,<span style="color: black;">这儿</span>忽略 Docker 的安装过程,着重介绍一下 Spring Boot 项目<span style="color: black;">怎样</span><span style="color: black;">运用</span> Micrometer 来监控 Java 应用性能,并集成到 Prometheus <span style="color: black;">最后</span><span style="color: black;">运用</span> Grafana Dashboard 展示出来。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3、Spring Boot 工程集成 Micrometer</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span><span style="color: black;">通常</span>说 Spring Boot 集成 Micrometer 值得时 Spring 2.x 版本,<span style="color: black;">由于</span>在该版本 spring-boot-actuator <span style="color: black;">运用</span>了 Micrometer 来实现监控,而在 Spring Boot 1.5x 中<span style="color: black;">能够</span><span style="color: black;">经过</span>micrometer-spring-legacy 来<span style="color: black;">运用</span> micrometer,显然在 2.x 版本有更高的集成度,<span style="color: black;">运用</span>起来<span style="color: black;">亦</span>非常方便了。<span style="color: black;">那样</span>创建一个 Spring Boot 2.1.4.RELEASE 版本的工程 springboot2-promethues,<span style="color: black;">首要</span>添加依赖如下:</p><dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<version>1.1.4</version>
</dependency><p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这儿</span>引入了 io.micrometer 的 micrometer-registry-prometheus 依赖以及 spring-boot-starter-actuator 依赖,<span style="color: black;">由于</span>该包对 Prometheus 进行了封装,<span style="color: black;">能够</span>很方便的集成到 Spring Boot 工程中。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">其次在 application.properties 中配置如下:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">server.port=8088</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">spring.application.name=springboot2-prometheus</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">management.endpoints.web.exposure.include=*</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">management.metrics.tags.application=${spring.application.name}</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这儿</span> management.endpoints.web.exposure.include=* 配置为开启 Actuator 服务,<span style="color: black;">由于</span>Spring Boot Actuator 会自动配置一个 URL 为 /actuator/Prometheus 的 HTTP 服务来供 Prometheus 抓取数据,<span style="color: black;">不外</span>默认该服务是关闭的,该配置将打开所有的 Actuator 服务。management.metrics.tags.application 配置会将该工程应用名<span style="color: black;">叫作</span>添加到计量器注册表的 tag 中去,方便后边 Prometheus <span style="color: black;">按照</span>应用名<span style="color: black;">叫作</span>来区分<span style="color: black;">区别</span>的服务。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">而后</span>在工程<span style="color: black;">起步</span>主类中添加 Bean 如下来监控 JVM 性能指标信息:</p>@SpringBootApplication
public class Springboot2PrometheusApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot2PrometheusApplication.class, args);
}
@Bean
MeterRegistryCustomizer<MeterRegistry> configurer(@Value("${spring.application.name}") String applicationName){
return registry -> registry.config().commonTags("application", applicationName);
}
}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">最后,<span style="color: black;">起步</span>服务,浏览器<span style="color: black;">拜访</span> http://127.0.0.1:8088/actuator/prometheus 就<span style="color: black;">能够</span>看到应用的 一系列<span style="color: black;">区别</span>类型 metrics 信息,例如 http_server_requests_seconds summary、jvm_memory_used_bytes gauge、jvm_gc_memory_promoted_bytes_total counter 等等。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">4、配置 Prometheus 监控应用指标</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Prometheus 的安装配置可参考之前<span style="color: black;">文案</span> Prometheus 监控报警系统 AlertManager 之邮件告警,写的很<span style="color: black;">仔细</span>,<span style="color: black;">这儿</span>就不在<span style="color: black;">仔细</span>说明了,修改 prometheus.yml 配置,在上篇<span style="color: black;">文案</span>配置示例<span style="color: black;">基本</span>上,添加上边<span style="color: black;">起步</span>的服务<span style="color: black;">位置</span>来执行监控。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$ vim prommetheus.yml</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">......</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">- job_name: application</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">scrape_interval: 5s</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">metrics_path: /actuator/prometheus</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">file_sd_configs:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">- files: </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这儿</span>依然采用 file_sd_configs 方式动态服务<span style="color: black;">发掘</span>,新建 <local_dir>/groups/applicationgroups/application.json 文件如下:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$ vim groups/applicationgroups/application.json</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">[</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">"targets": [</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">"192.168.1.124:8088"</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">],</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">"labels": {</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">"instance": "springboot2-prometheus",</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">"service": "springboot2-prometheus-service"</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">]</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这儿</span> 192.168.1.124:8088 <span style="color: black;">便是</span>上边本地<span style="color: black;">起步</span>的服务<span style="color: black;">位置</span>,<span style="color: black;">亦</span><span style="color: black;">便是</span> Prometheus 要监控的服务<span style="color: black;">位置</span>,<span style="color: black;">同期</span>添加<span style="color: black;">有些</span>与应用<span style="color: black;">关联</span>的标签,方便后期执行 PromSQL <span style="color: black;">查找</span>语句区分。最后重启 Prometheus 服务,查看 Prometheus UI 界面确认 Target <span style="color: black;">是不是</span>添加成功。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在 Graph 页面执行一个简单的<span style="color: black;">查找</span>,<span style="color: black;">亦</span>是获取 springboot2-prometheus 服务的 JVM 性能指标值的。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">5、配置 Grafana Dashboard 展示监控项</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Prometheus <span style="color: black;">此刻</span><span style="color: black;">已然</span><span style="color: black;">能够</span>正常监控到应用 JVM 信息了,<span style="color: black;">那样</span><span style="color: black;">咱们</span>可以配置 Grafana Dashboard 来优雅直观的展示出来这些监控值了。<span style="color: black;">首要</span>创建 Grafana 服务,<span style="color: black;">这儿</span>为了方便,依旧采用 Docker 的方式<span style="color: black;">起步</span>,<span style="color: black;">起步</span>命令如下:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$ docker run -d -p 3000:3000 --name=grafana grafana/grafana</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">起步</span>完毕后,浏览器<span style="color: black;">拜访</span> http://192.168.1.121:3000 <span style="color: black;">就可</span>,首次登录<span style="color: black;">运用</span> admin:admin 默认账户<span style="color: black;">秘码</span>登录并修改<span style="color: black;">秘码</span>。登录完毕,需要添加数据源,<span style="color: black;">这儿</span><span style="color: black;">咱们</span>要添加的<span style="color: black;">便是</span>上边 Prometheus 数据源,配置如下图:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/40fb95ecebab490ab6fadc0a98848de6~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728815687&x-signature=VNyR%2BrCONyJ2uS7%2BWmYFs9PXsjM%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">配置完毕,接下来需要导入对应的监控 JVM 的 Dashboard 模板,模板编号为 4701。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">导入完毕后,就<span style="color: black;">能够</span>看到 JVM (Micrometer) <span style="color: black;">各样</span>类型指标监控图形化以后的页面。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/8466179e9e534a6aaf35498882d099d4~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728815687&x-signature=3SIJOq%2BNYIqemFLQuE6waWSfLMw%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">6、自定义监控指标并展示到 Grafana</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">上边是 spring-boot-actuator 集<span style="color: black;">成为了</span> Micrometer 来<span style="color: black;">供给</span>的默认监控项,覆盖 JVM 各个层间的监控,<span style="color: black;">协同</span> Grafana Dashboard 模板基本<span style="color: black;">能够</span>满足<span style="color: black;">咱们</span><span style="color: black;">平常</span>对 Java 应用的监控。当然,它<span style="color: black;">亦</span>支持自定义监控指标,实现各个方面的监控,例如统计<span style="color: black;">拜访</span>某一个 API 接口的请求数,统计实时在线人数、统计实时接口响应时间等功能,而这些都<span style="color: black;">能够</span><span style="color: black;">经过</span><span style="color: black;">运用</span>上边的四种计量器来实现。接下来,来演示下<span style="color: black;">怎样</span>自定义监控指标并展示到 Grafana 上。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">6.1、监控某几个 API 请求次数</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>继续在 springboot2-promethues 工程上添加 IndexController.java,来实现分别统计<span style="color: black;">拜访</span> index 及 core 接口请求次数,代码如下:</p>@RestController
@RequestMapping("/v1")
public class IndexController {
@Autowired
MeterRegistry registry;
private Counter counter_core;
private Counter counter_index;
@PostConstruct
private void init(){
counter_core = registry.counter("app_requests_method_count", "method", "IndexController.core");
counter_index = registry.counter("app_requests_method_count", "method", "IndexController.index");
}
@RequestMapping(value = "/index")
public Object index(){
try{
counter_index.increment();
} catch (Exception e) {
return e;
}
return counter_index.count() + " index of springboot2-prometheus.";
}
@RequestMapping(value = "/core")
public Object coreUrl(){
try{
counter_core.increment();
} catch (Exception e) {
return e;
}
return counter_core.count() + " coreUrl Monitor by Prometheus.";
}
}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">说明一下,<span style="color: black;">这儿</span>是一个简单的 RestController 接口,<span style="color: black;">运用</span>了 Counter 计量器来统计<span style="color: black;">拜访</span> /v1/index 及 /v1/core 接口<span style="color: black;">拜访</span>量。<span style="color: black;">由于</span><span style="color: black;">拜访</span>数会<span style="color: black;">连续</span>的<span style="color: black;">增多</span>,<span style="color: black;">因此</span><span style="color: black;">这儿</span><span style="color: black;">运用</span> Counter 比较合适。<span style="color: black;">起步</span>服务,<span style="color: black;">咱们</span>来分别<span style="color: black;">拜访</span>一下这两个接口,为了更好的<span style="color: black;">协同</span>下边演示,<span style="color: black;">能够</span>多<span style="color: black;">拜访</span>几次。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">服务<span style="color: black;">能够</span>正常<span style="color: black;">拜访</span>,并且<span style="color: black;">拜访</span>了 6 次 /v1/index,<span style="color: black;">拜访</span>了 10 次 /v1/core。接下来,<span style="color: black;">咱们</span><span style="color: black;">能够</span>到 Prometheus UI 界面上<span style="color: black;">运用</span> PromSQL <span style="color: black;">查找</span>自定义的监控信息了。分别添加 Graph 并执行如下<span style="color: black;">查找</span>语句,<span style="color: black;">查找</span>结果如下:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">能够</span>看到正确统计出来这两个接口请求的<span style="color: black;">拜访</span>数,<span style="color: black;">这儿</span>解释一下<span style="color: black;">查找</span>语句:app_requests_method_count_total{application="springboot2-prometheus", instance="springboot2-prometheus", method="IndexController.core"} <span style="color: black;">这儿</span>的</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">app_requests_method_count_total 为上边代码中设置的 Counter 名<span style="color: black;">叫作</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">application 为初始化 registry 时设置的通用标签,标注应用名<span style="color: black;">叫作</span>,<span style="color: black;">这般</span>做好处<span style="color: black;">便是</span><span style="color: black;">能够</span><span style="color: black;">按照</span>应用名<span style="color: black;">叫作</span>区分<span style="color: black;">区别</span>的应用。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">instance 为 <local_dir>/groups/applicationgroups/application.json 中配置的 instance 实例名<span style="color: black;">叫作</span>,用来区分应用实例。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">method 为上边代码中设置的 Counter 标签名<span style="color: black;">叫作</span>,<span style="color: black;">能够</span>用来区分<span style="color: black;">区别</span>的<span style="color: black;">办法</span>,<span style="color: black;">这般</span>就<span style="color: black;">不消</span>为每一个<span style="color: black;">办法</span>设置一个 Counter 了。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">接下来,<span style="color: black;">咱们</span>在 Grafana Dashboard 上添加一个新的 Panel 并添加 Query <span style="color: black;">查找</span>,最后图形化展示出来。<span style="color: black;">首要</span>添加一个 Panel 并命名为 自定义监控指标,<span style="color: black;">而后</span>点击 Add Query <span style="color: black;">增多</span>一个新的 Query <span style="color: black;">查找</span>,<span style="color: black;">查找</span>语句为上边的 PromSQL 语句,<span style="color: black;">不外</span><span style="color: black;">这儿</span>为了更好的扩展性,<span style="color: black;">咱们</span><span style="color: black;">能够</span>将 application 及 instance 两个参数赋值为变量,而这些变量<span style="color: black;">能够</span>直接从 Prometheus 上传递过来,<span style="color: black;">最后</span>的<span style="color: black;">查找</span>语句为 app_requests_method_count_total{application="$application", instance="$instance", method="IndexController.core"},最后修改 Title 为 实时<span style="color: black;">拜访</span>量 /v1/core,<span style="color: black;">保留</span>一下,返回首页就<span style="color: black;">能够</span>看到刚添加的 Dashboard 了,是不是很直观。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">6.2、监控所有 API 请求次数</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">上边针对某个或某几个接口请求次数做了监控,<span style="color: black;">倘若</span><span style="color: black;">咱们</span>想针对<span style="color: black;">全部</span>应用监控所有接口请求总次数,这个该<span style="color: black;">怎样</span>实现呢?监控请求次数<span style="color: black;">能够</span>继续<span style="color: black;">运用</span> Counter 计数器,<span style="color: black;">全部</span>应用所有请求,<span style="color: black;">咱们</span>自然而然的想到了 Spring AOP,<span style="color: black;">经过</span>切面注入<span style="color: black;">能够</span>做到统计所有请求记录,添加依赖如下:</p><dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.2.12</version>
</dependency><p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">添加 AspectAop.java 代码到 Springboot2PrometheusApplication.java 同级目录, 代码如下:</p>@Component
@Aspect
public class AspectAop {
@Autowired
MeterRegistry registry;
private Counter counter_total;
ThreadLocal<Long> startTime = new ThreadLocal<>();
@Pointcut("execution(public * com.promethues.demo.controller.*.*(..))")
private void pointCut(){}
@PostConstruct
public void init(){
counter_total = registry.counter("app_requests_count", "v1", "core");
}
@Before("pointCut()")
public void doBefore(JoinPoint joinPoint)throws Throwable {
startTime.set(System.currentTimeMillis());
counter_total.increment();
}
@AfterReturning(returning = "returnVal", pointcut = "pointCut()")
public void doAftereReturning(Object returnVal){
System.out.println("请求执行时间:" + (System.currentTimeMillis() - startTime.get()));
}
}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这儿</span> Spring AOP 操作代码就不在说了,<span style="color: black;">咱们</span>创建了一个名<span style="color: black;">叫作</span>为 app_requests_count 的 Counter,所有请求过来都会执行 counter_total.increment(); 操作,从而实现统计所有请求总数。重启服务,<span style="color: black;">拜访</span>多次<span style="color: black;">区别</span>的接口,<span style="color: black;">而后</span>在 Prometheus UI 界面执行 PromSQL <span style="color: black;">查找</span>,<span style="color: black;">查找</span>语句为 app_requests_count_total{application="springboot2-prometheus", instance="springboot2-prometheus", v1="core"} <span style="color: black;">查找</span>结果如下:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">能够</span>看到,能够正确统计出来所有的请求数量,<span style="color: black;">此刻</span>,<span style="color: black;">咱们</span><span style="color: black;">能够</span>在 Grafana 上之前的面板上<span style="color: black;">增多</span>一个新的 Query 并图形化<span style="color: black;">表示</span>出来了,Query 语句为: app_requests_count_total{application="$application", instance="$instance",v1="core"}, 添加完成后,展示效果如下:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">6.3、监控实时在线人数</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">接下来,来演示下<span style="color: black;">怎样</span>监控瞬时数据变化,例如实时交易总金额,实时网络请求响应时间,实时在线人数等,<span style="color: black;">这儿</span><span style="color: black;">咱们</span>简单模拟一下实时在线人数监控,<span style="color: black;">这儿</span>采用 Gauge 计量仪来做为指标统计类型,在 IndexController.java 中添加<span style="color: black;">关联</span>代码如下:</p>@RestController
@RequestMapping("/v1")
public class IndexController {
@Autowired
MeterRegistry registry;
private Counter counter_core;
private Counter counter_index;
private AtomicInteger app_online_count;
@PostConstruct
private void init(){
counter_core = registry.counter("app_requests_method_count", "method", "IndexController.core");
counter_index = registry.counter("app_requests_method_count", "method", "IndexController.index");
app_online_count = registry.gauge("app_online_count", new AtomicInteger(0));
}
@RequestMapping(value = "/index")
public Object index(){
try{
counter_index.increment();
} catch (Exception e) {
return e;
}
return counter_index.count() + " index of springboot2-prometheus.";
}
@RequestMapping(value = "/core")
public Object coreUrl(){
try{
counter_core.increment();
} catch (Exception e) {
return e;
}
return counter_core.count() + " coreUrl Monitor by Prometheus.";
}
@RequestMapping(value = "/online")
public Object onlineCount(){
int people = 0;
try {
people = new Random().nextInt(2000);
app_online_count.set(people);
} catch (Exception e){
return e;
}
return "current online people: " + people;
}
}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">重启服务,<span style="color: black;">拜访</span>一下 /v1/online 接口,得到一个 2000 以内的随机数<span style="color: black;">做为</span>实时在线人数,浏览器<span style="color: black;">拜访</span>一下,得到结果如下:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/0ce858ea5b874ed1b18cb7da9859de59~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728815687&x-signature=NaTYI%2FTRbd1dq3MhcQp5IRsG95c%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>在 Prometheus UI 界面执行一下 PromeSQL <span style="color: black;">查找</span>语句 app_online_count{application="springboot2-prometheus", instance="springboot2-prometheus"},<span style="color: black;">一样</span>能够对应获取到实时数据。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/cf6494e23966456b944f2a38b36aac82~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728815687&x-signature=eoYujjyLHX8y4ArYBa4KZoE6ySM%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">继续在 Grafana 上之前的面板上<span style="color: black;">增多</span>一个新的 Query 并图形化<span style="color: black;">表示</span>出来,Query 语句为: app_online_count{application="$application", instance="$instance"}, 添加完成后,展示效果如下:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">重视</span>:<span style="color: black;">这儿</span>我采用了 Grafana 中 Gauge 图形来展示的,<span style="color: black;">能够</span><span style="color: black;">按照</span><span style="color: black;">实质</span><span style="color: black;">需求</span>来展示对应的数据。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/a1c84ec55c704341990b9f3851321975~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728815687&x-signature=hQi8i2cqpIRv%2FoEzh8z42peYBAA%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">参考资料</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Micrometer Documentation</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">prometheus docs</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运用</span> Micrometer 记录 Java 应用性能指标</p>
百度seo优化论坛 http://www.fok120.com/ 期待你更多的精彩评论,一起交流学习。
页:
[1]