项目实战--Spring Boot大数据量报表Excel优化

一、项目场景

项目中要实现交易报表,处理大规模数据导出时,出现单个Excel文件过大导致性能下降的问题,需求是导出大概四千万条数据到Excel文件,不影响正式环境的其他查询。

二、方案
1.使用读写分离,查询操作由从库处理
2.数据分批查询
3.异步导出数据
4.生成和拆分多个Excel文件
三、实现

1.pom.xml中添加以下依赖:


<dependencies>
    <!-- Spring Boot Starter Data JPA -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <!-- Spring Boot Starter Async -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- Apache POI for Excel -->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
    </dependency>
</dependencies>

包括SpringBoot、Spring Data JPA、异步处理相关的依赖,以及用于生成Excel文件的Apache POI库。

2.application.properties中加入数据库配置,以及异步任务执行器的配置:

# Database configuration
spring.datasource.url=jdbc:mysql://localhost:3306/yourdatabase
spring.datasource.username=yourusername
spring.datasource.password=yourpassword
# Async configuration
spring.task.execution.pool.core-size=10
spring.task.execution.pool.max-size=20
spring.task.execution.pool.queue-capacity=500
spring.task.execution.thread-name-prefix=Async-thread

3.使用从库进行查询
减轻主库的查询压力,建议在架构上使用读写分离,查询操作由从库处理。这样可以确保主库的操作性能和其他接口查询不受影响。

@Service
public class DataService {
    @Autowired
    private DataRepository dataRepository;
    public List<Data> fetchData(int offset, int limit) {
        return dataRepository.findAll(PageRequest.of(offset, limit)).getContent();
    }
}

4.数据分批查询策略
防止一次性查询大量数据导致内存溢出,采用分页查询的方式,每次查询部分数据进行处理。

@Service
public class DataExportService {
    @Autowired
    private DataService dataService;
    @Async
    public void exportData() {
        int pageSize = 10000;
        int pageNumber = 0;
        List<Data> dataBatch;
        do {
            dataBatch = dataService.fetchData(pageNumber, pageSize);
            if (!dataBatch.isEmpty()) {
                // 导出数据到Excel
                exportToExcel(dataBatch, pageNumber);
            }
            pageNumber++;
        } while (!dataBatch.isEmpty());
    }
}

5.异步任务配置
通过@EnableAsync注解启用异步任务,并配置一个任务执行线程来单独执行导出任务。

@Configuration
@EnableAsync
public class AsyncConfig implements AsyncConfigurer {
    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(20);
        executor.setQueueCapacity(500);
        executor.setThreadNamePrefix("Async-");
        executor.initialize();
        return executor;
    }
}

6.导出任务接口实现
使用@Async注解将导出任务的方法标记为异步执行。

@Service
public class DataExportService {
    @Autowired
    private DataService dataService;
    @Async
    public void exportData() {
        // 数据查询和导出的逻辑
    }
}

7.生成和拆分Excel文件
使用Apache POI处理Excel,查询到的数据批次,将数据分成多个Excel文件,避免单个文件过大。

public void exportToExcel(List<Data> dataBatch, int batchNumber) {
    Workbook workbook = new XSSFWorkbook();
    Sheet sheet = workbook.createSheet("Data");
    int rowNum = 0;
    for (Data data : dataBatch) {
        Row row = sheet.createRow(rowNum++);
        row.createCell(0).setCellValue(data.getId());
        row.createCell(1).setCellValue(data.getName());
        // 其他数据列
    }
    try (FileOutputStream fos = new FileOutputStream("data_batch_" + batchNumber + ".xlsx")) {
        workbook.write(fos);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
dynamic-datasource-spring-boot-starter 是一个用于在 Spring Boot 应用中实现多数据源的开源项目。它提供了简单易用的配置方式和强大的功能,让我们能够轻松地管理和切换多个数据源。 使用 dynamic-datasource-spring-boot-starter 实现多数据源的步骤如下: 1. 引入 dynamic-datasource-spring-boot-starter 依赖。在 Maven 或 Gradle 中添加对该项目的依赖,即可将其引入到项目中。 2. 配置多个数据源。在应用的配置文件中,添加多个数据源的配置信息。可以为每个数据源指定不同的 URL、用户名、密码等参数。 3. 配置数据源路由策略。使用 dynamic-datasource-spring-boot-starter 提供的数据源路由策略,将数据源动态地路由到对应的方法或服务上。 4. 使用注解标识数据源。在需要访问不同数据源的方法或服务上,使用相应的注解标识数据源。dynamic-datasource-spring-boot-starter 提供了一系列的注解,如 @DataSource、@Master、@Slave 等,用于标识不同的数据源。 5. 运行应用。运行应用后,dynamic-datasource-spring-boot-starter 会根据配置和注解的设置,将请求路由到正确的数据源上。 通过以上步骤,我们就能够轻松地实现多数据源的管理和切换。dynamic-datasource-spring-boot-starter 的灵活性和高性能使得在实际项目中使用多数据源变得非常简单和高效。同时,该项目还提供了丰富的监控和调试功能,方便我们对多数据源的运行情况进行监控和优化。总之,dynamic-datasource-spring-boot-starter 是一个强大的工具,为我们实现多数据源提供了便捷和可靠的解决方案。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

容若只如初见

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值