目录

Spring Boot 首笔交易慢问题排查与优化实践

Spring Boot 首笔交易慢问题排查与优化实践

在我们的微服务项目中,遇到这样的问题:应用启动后,第一笔交易响应耗时高达4、5秒,而后续请求均能在毫秒级完成。这不仅触发监控告警,也极大影响了用户体验。本文将结合日志排查、性能工具分析以及预热优化手段,总结出一套完整的排查思路和解决方案。


问题背景

在我们的微服务系统中,首笔交易响应明显偏慢,经过初步排查发现:

  • Flowable 流程部署、Redis 连接建立、PageHelper 代理生成和 Hibernate Validator 校验等操作均集中在首笔交易时进行;
  • 后续交易响应迅速,说明业务逻辑本身并无性能瓶颈,而主要问题出在各类资源的首次初始化上。

这种"懒加载"机制虽然能够延迟资源加载,但在首笔交易时往往会导致严重延时,影响整体体验。实际项目中需平衡启动速度与首次响应效率,主动预热关键组件。


排查步骤

1. 日志分析

首先,将日志级别调为 DEBUG,详细观察首笔交易与后续交易之间的差异。

在 Flowable 工作流启动时,日志中会出现如下部署信息:

log 复制代码
2025-03-31-15:24:25:326 [thread1] DEBUG o.f.e.i.bpmn.deployer.BpmnDeployer.deploy.72 -- Processing deployment SpringBootAutoDeployment
2025-03-31-15:24:25:340 [thread1] DEBUG o.f.e.i.b.d.ParsedDeploymentBuilder.build.54 -- Processing BPMN resource E:\gitProjects\flowableProject\target\classes\processes\eib.bpmn20.xml

同样,Redis 连接在首次调用时会看到大量lettuce包日志,如:

log 复制代码
2025-03-31-15:24:23:587 [XNIO-1 task-1] DEBUG io.lettuce.core.RedisClient.initializeChannelAsync0.304 -- Connecting to Redis at 10.240.75.250:7379

这些信息表明,在首次调用时,系统才开始部署流程、建立 Redis 连接以及加载其它第三方组件,从而导致延迟。

2. 性能工具定位

由于单纯依赖日志排查比较繁琐,我们还使用了 Java VisualVM(JDK 自带工具,也可选择其它工具)进行采样分析。

在 VisualVM 中选择目标进程后通过 CPU 取样,示意图如下(也可配置JMX远程连接)。

观察结果如下:

发现首笔交易相比后续交易多出以下方法的调用(省略的部分二方包慢代码):

  • com.github.pagehelper.dialect.auto.DataSourceAutoDialect.<init>
  • org.hibernate.validator.internal.engine.ValidatorImpl.validate()

这些方法的初始化也成为首笔交易慢的原因之一。


优化方案:提前预热各种资源

针对上述问题,我们的优化思路很简单:提前初始化各项资源,确保首笔交易时不再触发大量懒加载。为此,我们将所有预热操作改写成基于 ApplicationRunner 的实现,保证在 Spring Boot 启动后就自动执行。

1. Flowable 流程部署预热

在应用启动时,通过扫描 BPMN 文件提前部署流程,避免在交易中首次部署导致延迟。

java 复制代码
import org.flowable.engine.RepositoryService;
import org.flowable.engine.repository.DeploymentBuilder;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.stereotype.Component;

@Component
public class ProcessDeploymentRunner implements ApplicationRunner {

    private final RepositoryService repositoryService;

    public ProcessDeploymentRunner(RepositoryService repositoryService) {
        this.repositoryService = repositoryService;
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 扫描 processes 目录下的所有 BPMN 文件
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource[] resources = resolver.getResources("classpath:/processes/*.bpmn20.xml");

        if (resources.length == 0) {
            System.out.println("未在 processes 目录下找到 BPMN 文件");
            return;
        }

        DeploymentBuilder deploymentBuilder = repositoryService.createDeployment()
                .name("自动部署流程");

        for (Resource resource : resources) {
            deploymentBuilder.addInputStream(resource.getFilename(), resource.getInputStream());
        }

        deploymentBuilder.deploy();
        System.out.println("流程定义已部署,数量:" + resources.length);
    }
}

2. Redis 连接预热

利用 ApplicationRunner 发送一次 PING 请求,提前建立 Redis 连接,避免首笔交易时因连接建立而耗时。

java 复制代码
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class RedisWarmupRunner implements ApplicationRunner {

    private final StringRedisTemplate redisTemplate;

    public RedisWarmupRunner(StringRedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Override
    public void run(ApplicationArguments args) {
        try {
            String pingResult = redisTemplate.getConnectionFactory().getConnection().ping();
            System.out.println("✅ Redis connection pre-warmed successfully: " + pingResult);
        } catch (Exception e) {
            System.err.println("❌ Redis warm-up failed: " + e.getMessage());
        }
    }
}

3. PageHelper 预热

通过执行一条简单的查询语句,触发 PageHelper 及相关 MyBatis Mapper 的初始化。

java 复制代码
import com.baomidou.mybatisplus.extension.toolkit.SqlRunner;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class PageHelperWarmupRunner implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) {
        try {
            boolean result = SqlRunner.db().selectObjs("SELECT 1").size() > 0;
            System.out.println("✅ PageHelper & SqlRunner pre-warm completed, result: " + result);
        } catch (Exception e) {
            System.err.println("❌ PageHelper pre-warm failed: " + e.getMessage());
        }
    }
}

(请确保配置文件中已开启 SQL Runner 功能:
mybatis-plus.global-config.enable-sql-runner=true

4. Hibernate Validator 预热

通过一次 dummy 校验操作,提前加载 Hibernate Validator 相关类和反射逻辑。

java 复制代码
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class ValidatorWarmupRunner implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) {
        try {
            Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
            DummyEntity dummy = new DummyEntity();
            validator.validate(dummy);
            System.out.println("✅ Hibernate Validator pre-warm completed!");
        } catch (Exception e) {
            System.err.println("❌ Hibernate Validator pre-warm failed: " + e.getMessage());
        }
    }

    private static class DummyEntity {
        @jakarta.validation.constraints.NotNull
        private String name;
    }
}

5. Undertow 预热(可选)

如果使用 Undertow 作为内嵌服务器,也可以通过主动发送 HTTP 请求预热相关资源。此外,在配置文件中开启过滤器提前初始化也有助于降低延迟。

application.yml 中设置:

yaml 复制代码
server:
  undertow:
    eager-init-filters: true

再通过下面的代码发送一次预热请求:

java 复制代码
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
public class UndertowWarmupRunner implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) {
        try {
            RestTemplate restTemplate = new RestTemplate();
            String response = restTemplate.getForObject("http://localhost:8080/health", String.class);
            System.out.println("✅ Undertow pre-warm completed, response: " + response);
        } catch (Exception e) {
            System.err.println("❌ Undertow pre-warm failed: " + e.getMessage());
        }
    }
}

总结

通过上述方案,我们将 Flowable 流程部署、Redis 连接、PageHelper 初始化、Hibernate Validator 校验和 Undertow 相关组件的预热操作全部迁移到 ApplicationRunner 中,在应用启动后就自动执行。这样,首笔交易时不再需要进行大量初始化工作,各项资源已预先加载,确保后续请求能达到毫秒级响应,大大提升了用户体验并避免了无效的监控告警。

本文是转载文章,点击查看原文
如有侵权,请联系 xyy@jishuzhan.net 删除
相关推荐
一切皆有迹可循2 小时前
IntelliJ IDEA中Spring Boot项目整合MyBatis:从零实现高效数据持久化
java·spring boot·intellij-idea·mybatis
獨枭4 小时前
使用 Spring Boot 快速构建企业微信 JS-SDK 权限签名后端服务
javascript·spring boot·企业微信
QING6185 小时前
【LeakCanary】的实现原理与调优技巧
android·性能优化·app
菜鸟起航ing6 小时前
【Java面试系列】Spring Boot中自动配置原理与自定义Starter开发实践详解 - 3-5年Java开发必备知识
java·spring boot·面试·自动配置·自定义starter
来自星星的坤9 小时前
Spring Boot 邮件发送配置遇到的坑:解决 JavaMailSenderImpl 未找到的错误
java·开发语言·spring boot·后端·spring
梦想实现家_Z9 小时前
Spring Boot整合DeepSeek+MCP实践详解
spring boot·deepseek·mcp
缘友一世10 小时前
解决Spring Boot上传默认限制文件大小和完善超限异常(若依框架)
java·spring boot·后端
小杨40410 小时前
springboot框架项目实践应用十七(springcloud整合nacos)
spring boot·后端·spring cloud
亭台烟雨中10 小时前
【Springboot后端之间使用websocket长连接通信】
spring boot·后端·websocket
斯~内克14 小时前
前端单页应用性能优化全指南:从加载提速到极致体验
前端·性能优化