作者:吴容,腾讯云大数据Elasticsearch高级开发工程师

在《腾讯云ES:图文详解!你想了解的ElasticAPM这里全都有》这篇文章中,我们对Elastic APM的组件架构、数据模型和工作原理有了比较清晰的认识,本篇文章将从实践的角度出发,演示如何使用Elastic APM来实时监控我们的SpringBoot服务。

一、创建ES集群

首先在演示的第一步,需要先创建出一个ES集群环境,这里我们到腾讯云ES控制台创建一个7.14.2白金版的ES集群。

图1. 腾讯云ES控制台创建ES集群

二、部署APM Server

创建好ES集群后,开始部署APM Sever。这里有两种部署模式,第一种是通过Kibana上的Fleet Intergration方式进行安装部署,第二种则是手动进行安装。本文演示手动部署模式。

1、下载APM Server下载APM Server安装包可通过如下链接获取:https://www.elastic.co/cn/downloads/past-releases#apm-server由于我们刚创建的腾讯云ES集群版本为7.14.2,因此我们这里选择的APM Server版本也选择为7.14.2。

2、修改配置文件下载完成后,解压到对应的目录,然后配置apm-server.yml文件如下:

apm-server:host: "localhost:8200"#-------------------------- Elasticsearch output --------------------------output.elasticsearch:hosts: ["ES-VIP:9200"]enabled: trueusername: "elastic"password: "changeme"
#================================= Logging =================================logging.level: infologging.metrics.enabled: truelogging.metrics.period: 30slogging.to_files: truelogging.files:path: /usr/local/service/APMServer/apm-server-7.14.2/logsname: apm-server.log
# Defines if the HTTP endpoint is enabled.http.enabled: truehttp.host: localhost# Port on which the HTTP endpoint will bind. Default is 5066.http.port: 5066#============================= X-pack Monitoring =============================# Set to true to enable the monitoring reporter.monitoring.enabled: truemonitoring.elasticsearch:hosts: ["ES-VIP:9200"]username: "elastic"password: "changeme"

其中,上面的配置中output.elasticsearch为APM agent采集的性能数据输出的es集群信息,monitoring.elasticsearch则表示将APM Server的监控采集信息输出到对应的es集群中。

3、启动APM Server我们通过如下命令来启动APM Server。如果是生产环境通常会通过以启动脚本文件方式进行启动。./apm-server -e启动完成后,我们便可以在ES集群中看到新创建的apm相关索引:

图2. APM Server启动后默认创建的索引

并且在Kibana的监控模块,可以看到APM Server实例的监控信息:

图3. Kibana监控页展示APM Server实例信息

三、集成APM Java agent

本文演示的Java Application是基于SpringBoot框架开发的一个Java服务,该服务里提供与ES集群交互的Rest API接口。如果希望通过APM来监控我们的Java服务,则必须要在Java服务中引入APM Agent。因此,该服务中集成的Java Agent版本为1.33.0。

1、下载apm agent下载地址:Maven Central Repository Search

图4. 下载apm agent

注意:这里的Java Agent,不是通过Maven仓库依赖的方式集成,而是通过第三方lib方式。2、启动应用服务下载完成apm agent后,将agent的jar文件放置到Java项目的lib包中,然后在服务的启动命令里通过-javaagent方式进行集成。本demo的集成命令为:

java \-javaagent:lib/elastic-apm-agent-1.33.0.jar \-Delastic.apm.service_name=spring-boot-application \-Delastic.apm.application_packages=org.tencentyun.ces \-Delastic.apm.server_urls=http://localhost:8200 \-jar wr-ces.jar &

‍其中:-javaagent:lib/elastic-apm-agent-1.33.0.jar 为该Java Agent的相对路径;-Delastic.apm.application_packages=com.tencentyun.ces 为APM堆栈跟踪的应用包名称,多个包之间通过逗号分隔 https://www.elastic.co/guide/en/apm/agent/java/1.x/config-stacktrace.html#config-application-packages。-Delastic.apm.service_name=spring-boot-application 为APM的service名称,该名称最终会在APM的Services和ServiceMap中进行直观展示。-Delastic.apm.server_urls=http://localhost:8200 为APM Server的服务地址,由于我们这里的Java应用和APM Server是部署在同一台服务器上,因此地址为localhost:8200。-jar wr-ces.jar 表示java服务的jar包名称。通过上面的命来启动Java服务,可以看到日志中打印了如下信息,表示Java Agent和APM Server连接成功。

2022-07-29 19:45:14,764 [main] INFO co.elastic.apm.agent.configuration.StartupInfo - Starting Elastic APM 1.33.0 as apm-server (0.0.1) on Java 1.8.0_312 Runtime version: 1.8.0_312-b07 VM version: 25.312-b07 (Red Hat, Inc.) Linux 4.18.0-305.25.1.el8_4.x86_642022-07-29 19:45:14,764 [main] INFO co.elastic.apm.agent.configuration.StartupInfo - service_name: 'spring-boot-application' (source: Java System Properties)2022-07-29 19:45:14,764 [main] INFO co.elastic.apm.agent.configuration.StartupInfo - server_urls: 'http://localhost:8200' (source: Java System Properties)2022-07-29 19:45:14,764 [main] INFO co.elastic.apm.agent.configuration.StartupInfo - application_packages: 'org.tencentyun.ces' (source: Java System Properties)2022-07-29 19:45:20,336 [main] INFO co.elastic.apm.agent.impl.ElasticApmTracer - Tracer switched to RUNNING state2022-07-29 19:45:20,383 [elastic-apm-server-healthcheck] INFO co.elastic.apm.agent.report.ApmServerHealthChecker - Elastic APM server is available: { "build_date": "2021-09-15T09:23:02Z", "build_sha": "dd1680c3f841ef0392dcec228fb0f65d2ac446e2", "version": "7.14.2"}

图5. 启动应用服务

3、查看APM数据随后通过curl命令向Java服务中添加一条数据,然后保存到ES集群中。再回到Kibana的APM界面可以看到Service列表页里已经有了刚刚启动的Java服务了。

图6. APM Service列表页

点击spring-boot-application这个Service进去之后,可以看到该Service更详细的监控和Transaction信息。如我们刚刚对ES集群做的写入文档save操作。

图7. APM Transactions列表页

选择其中一个Transaction再次点击进去后,可以看到每一次请求的调用链路和耗时情况。非常方便我们定位分析线上问题。

图8. APM 调用链详情信息

另外APM还提供了服务地图的可视化界面,可以非常直观的看出服务与服务之间的拓扑结构,以及每个服务的基本性能指标。如本Java应用中,只与ES进行了交互。

图9. APM Service Map

APM的Errors还详细记录了每个服务的异常日志信息,通过这些信息,可以做异常告警等操作。

图10. APM Errors列表页

到此,我们演示完成了Elastic APM监控SpringBoot服务的完整过程。可以看出整个过程搭建起来非常简单和便捷。也能够看出Elastic生态的强大和繁荣。

四、Elastic APM高性能构架

上面我们演示的demo由于数据量很小,因此直接采用了单机部署模式,并且APM Server从agent接收到的数据是直接写入Elasticsearch集群中的。但是在生产环境,由于服务通常都采用多实例或集群模式部署,且数据量庞大。因此为了提高Elastic APM的整体性能和吞吐量,我们会对APM Server采用多实例部署方式,且将数据先输出到Kakfa中,然后再通过Logstash等管道组件将数据消费到ES中。apm-server.yml中将数据输出到kafka具体配置如下:

output.kafka:enabled: truehosts: ["localhost:9092"]topic: apm-springbootusername: 'changeme'password: 'changeme'

具体部署架构示意图如图11所示:

图11. Elastic APM高性能部署架构

四、小结

本文详细演示了在腾讯云ES集群的基础上,通过Elastic APM来实时监控SpringBoot的应用服务。通过Elastic APM我们能够非常直观地查看每一个请求链路的具体性能指标等信息。同时也能直观的看到服务与服务之间相互交互的拓扑结构。最后我们还介绍了如何在生产环境中部署一个高性能的APM系统架构。如要了解更多,快到腾讯云Elasticsearch Service控制台创建集群进行体验吧。

免费体验活动专区

Elasticsearch 新用户可享 2核4G,0元 体验 30 天!顺畅体验云上集群

添加小助手回复 Elasticsearch 加入 Elasticsearch 技术社区👇

推荐阅读

关注腾讯云大数据公众号

邀您探索数据的无限可能

点击“阅读原文”,了解相关产品最新动态

↓↓↓

文章来源于腾讯云开发者社区,点击查看原文