如何在Spring Boot中集成Elastic APM进行应用性能监控

本文涉及的产品
应用实时监控服务-应用监控,每月50GB免费额度
简介: 如何在Spring Boot中集成Elastic APM进行应用性能监控

如何在Spring Boot中集成Elastic APM进行应用性能监控

在现代应用程序中,性能监控和故障排除是确保应用程序稳定性和性能的重要方面。Elastic APM(应用性能监控)是一种开源解决方案,能够实时收集和分析应用程序性能数据。本文将介绍如何在Spring Boot项目中集成Elastic APM,实现对应用程序的全面性能监控。

什么是Elastic APM

Elastic APM是Elastic Stack(也称为ELK Stack)的一部分,用于监控软件应用程序的性能和可用性。它可以捕获应用程序的响应时间、错误、数据库查询、外部HTTP请求等详细信息,帮助开发者快速定位和解决性能瓶颈和错误。

在Spring Boot中集成Elastic APM

为了在Spring Boot项目中使用Elastic APM,我们需要以下几个步骤:

  1. 添加Elastic APM依赖
  2. 配置Elastic APM
  3. 启动Elastic APM Server
  4. 验证集成效果

1. 添加Elastic APM依赖

首先,我们需要在pom.xml中添加Elastic APM的依赖:

<dependencies>
    <dependency>
        <groupId>co.elastic.apm</groupId>
        <artifactId>elastic-apm-agent</artifactId>
        <version>1.25.0</version>
    </dependency>
</dependencies>
AI 代码解读

2. 配置Elastic APM

接下来,我们需要配置Elastic APM。在Spring Boot项目的src/main/resources目录下创建一个elasticapm.properties文件,并添加以下配置:

# Elastic APM Server URL
elastic.apm.server_urls=http://localhost:8200

# Application name
elastic.apm.service_name=my-spring-boot-app

# Application environment
elastic.apm.environment=production

# Secret token for APM Server
elastic.apm.secret_token=

# Enable distributed tracing
elastic.apm.distributed_tracing=true
AI 代码解读

然后,在Spring Boot的启动类中添加Elastic APM的Java Agent。修改src/main/java/cn/juwatech/Application.java文件:

package cn.juwatech;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
   

    public static void main(String[] args) {
   
        // Add Elastic APM agent
        ElasticApmAttacher.attach();

        SpringApplication.run(Application.class, args);
    }
}
AI 代码解读

3. 启动Elastic APM Server

为了接收和处理来自应用程序的性能数据,我们需要启动Elastic APM Server。可以使用Docker来快速启动APM Server:

docker run -d --name=apm-server -p 8200:8200 -e ELASTIC_APM_SECRET_TOKEN= -e ELASTIC_APM_SERVER_URLS=http://localhost:8200 docker.elastic.co/apm/apm-server:7.12.0
AI 代码解读

4. 验证集成效果

现在,我们可以启动Spring Boot应用程序并验证Elastic APM的集成效果。在应用程序启动并运行一段时间后,访问Elastic APM UI查看性能数据。默认情况下,Elastic APM UI在Kibana中可用,您可以通过以下URL访问:

http://localhost:5601/app/apm
AI 代码解读

使用Spring Boot Controller演示

为了展示Elastic APM的监控效果,我们可以创建一个简单的Spring Boot控制器。创建一个名为HelloController的控制器类:

package cn.juwatech.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
   

    @GetMapping("/hello")
    public String sayHello() {
   
        return "Hello, Elastic APM!";
    }
}
AI 代码解读

启动Spring Boot应用程序并访问http://localhost:8080/hello,然后在Elastic APM UI中查看监控数据。

5. 自定义监控

Elastic APM还允许我们添加自定义的监控数据。可以通过APM Agent的API来实现这一点。比如,我们可以在控制器中添加自定义事务和跨度:

package cn.juwatech.controller;

import co.elastic.apm.api.ElasticApm;
import co.elastic.apm.api.Span;
import co.elastic.apm.api.Transaction;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
   

    @GetMapping("/hello")
    public String sayHello() {
   
        Transaction transaction = ElasticApm.startTransaction();
        transaction.setName("Custom Transaction");
        transaction.setType(Transaction.TYPE_REQUEST);

        Span span = transaction.startSpan();
        span.setName("Custom Span");
        span.setType("custom");

        try {
   
            // Simulate some work
            Thread.sleep(100);
            return "Hello, Elastic APM!";
        } catch (InterruptedException e) {
   
            e.printStackTrace();
            return "Error!";
        } finally {
   
            span.end();
            transaction.end();
        }
    }
}
AI 代码解读

总结

通过以上步骤,我们成功地在Spring Boot项目中集成了Elastic APM,实现了对应用程序的性能监控。Elastic APM不仅能够捕获应用程序的基本性能指标,还可以通过自定义监控来满足更多的需求。在实际项目中,可以根据需求进一步优化和扩展Elastic APM的使用,例如配置更多的监控项、设置告警等。

相关实践学习
通过轻量消息队列(原MNS)主题HTTP订阅+ARMS实现自定义数据多渠道告警
本场景将自定义告警信息同时分发至多个通知渠道的需求,例如短信、电子邮件及钉钉群组等。通过采用轻量消息队列(原 MNS)的主题模型的HTTP订阅方式,并结合应用实时监控服务提供的自定义集成能力,使得您能够以简便的配置方式实现上述多渠道同步通知的功能。
目录
打赏
0
0
0
0
75
分享
相关文章
第16课:Spring Boot中集成 Shiro
第16课:Spring Boot中集成 Shiro
252 0
第15课: Spring Boot中集成ActiveMQ
第15课: Spring Boot中集成ActiveMQ
163 0
Spring Boot 集成 JUnit 单元测试
本文介绍了在Spring Boot中使用JUnit 5进行单元测试的常用方法与技巧,包括添加依赖、编写测试类、使用@SpringBootTest参数、自动装配测试模块(如JSON、MVC、WebFlux、JDBC等),以及@MockBean和@SpyBean的应用。内容实用,适合Java开发者参考学习。
291 0
|
5月前
|
微服务——SpringBoot使用归纳——Spring Boot中集成 Shiro——Shiro 身份和权限认证
本文介绍了 Apache Shiro 的身份认证与权限认证机制。在身份认证部分,分析了 Shiro 的认证流程,包括应用程序调用 `Subject.login(token)` 方法、SecurityManager 接管认证以及通过 Realm 进行具体的安全验证。权限认证部分阐述了权限(permission)、角色(role)和用户(user)三者的关系,其中用户可拥有多个角色,角色则对应不同的权限组合,例如普通用户仅能查看或添加信息,而管理员可执行所有操作。
207 0
微服务——SpringBoot使用归纳——Spring Boot中集成 Shiro——Shiro 三大核心组件
本课程介绍如何在Spring Boot中集成Shiro框架,主要讲解Shiro的认证与授权功能。Shiro是一个简单易用的Java安全框架,用于认证、授权、加密和会话管理等。其核心组件包括Subject(认证主体)、SecurityManager(安全管理员)和Realm(域)。Subject负责身份认证,包含Principals(身份)和Credentials(凭证);SecurityManager是架构核心,协调内部组件运作;Realm则是连接Shiro与应用数据的桥梁,用于访问用户账户及权限信息。通过学习,您将掌握Shiro的基本原理及其在项目中的应用。
191 0
微服务——SpringBoot使用归纳——Spring Boot 中集成Redis——Redis 介绍
本文介绍在 Spring Boot 中集成 Redis 的方法。Redis 是一种支持多种数据结构的非关系型数据库(NoSQL),具备高并发、高性能和灵活扩展的特点,适用于缓存、实时数据分析等场景。其数据以键值对形式存储,支持字符串、哈希、列表、集合等类型。通过将 Redis 与 Mysql 集群结合使用,可实现数据同步,提升系统稳定性。例如,在网站架构中优先从 Redis 获取数据,故障时回退至 Mysql,确保服务不中断。
182 0
微服务——SpringBoot使用归纳——Spring Boot 中集成Redis——Redis 介绍
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——基于 xml 的整合
本教程介绍了基于XML的MyBatis整合方式。首先在`application.yml`中配置XML路径,如`classpath:mapper/*.xml`,然后创建`UserMapper.xml`文件定义SQL映射,包括`resultMap`和查询语句。通过设置`namespace`关联Mapper接口,实现如`getUserByName`的方法。Controller层调用Service完成测试,访问`/getUserByName/{name}`即可返回用户信息。为简化Mapper扫描,推荐在Spring Boot启动类用`@MapperScan`注解指定包路径避免逐个添加`@Mapper`
163 0
第07课:Spring Boot集成Thymeleaf模板引擎
第07课:Spring Boot集成Thymeleaf模板引擎
195 0
第07课:Spring Boot集成Thymeleaf模板引擎
Java 开发中基于 Spring Boot 3.2 框架集成 MQTT 5.0 协议实现消息推送与订阅功能的技术方案解析
本文介绍基于Spring Boot 3.2集成MQTT 5.0的消息推送与订阅技术方案,涵盖核心技术栈选型(Spring Boot、Eclipse Paho、HiveMQ)、项目搭建与配置、消息发布与订阅服务实现,以及在智能家居控制系统中的应用实例。同时,详细探讨了安全增强(TLS/SSL)、性能优化(异步处理与背压控制)、测试监控及生产环境部署方案,为构建高可用、高性能的消息通信系统提供全面指导。附资源下载链接:[https://pan.quark.cn/s/14fcf913bae6](https://pan.quark.cn/s/14fcf913bae6)。
313 0
【Azure Application Insights】为Spring Boot应用集成Application Insight SDK
本文以Java Spring Boot项目为例,详细说明如何集成Azure Application Insights SDK以收集和展示日志。内容包括三步配置:1) 在`pom.xml`中添加依赖项`applicationinsights-runtime-attach`和`applicationinsights-core`;2) 在main函数中调用`ApplicationInsights.attach()`;3) 配置`applicationinsights.json`文件。同时提供问题排查建议及自定义日志方法示例,帮助用户顺利集成并使用Application Insights服务。
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等

登录插画

登录以查看您的控制台资源

管理云资源
状态一览
快捷访问