马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
接上篇文章, 拿SpringBoot举个例
1.1 默认线程池的隐患
Spring Boot的@Async默认利用SimpleAsyncTaskExecutor(无复用线程),频繁创建/销毁线程易引发性能标题。
1.2 自定义线程池设置
- @Configuration
- @EnableAsync
- public class AsyncConfig implements AsyncConfigurer {
- @Override
- public Executor getAsyncExecutor() {
- ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
- executor.setCorePoolSize(10); // 核心线程数=CPU核心数×2
- executor.setMaxPoolSize(20); // 突发流量缓冲
- executor.setQueueCapacity(100); // 根据业务容忍延迟调整
- executor.setThreadNamePrefix("Async-");
- executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
- executor.initialize();
- return executor;
- }
- }
- // 使用示例
- @Service
- public class ReportService {
- @Async // 指定使用自定义线程池
- public CompletableFuture<Report> generateReportAsync(Long id) {
- // 模拟耗时操作
- Thread.sleep(2000);
- return CompletableFuture.completedFuture(new Report(id, "Done"));
- }
- }
复制代码 1.3 线程池监控(Micrometer + Prometheus)
- # application.yml
- management:
- endpoints:
- web:
- exposure:
- include: "metrics,prometheus"
- metrics:
- tags:
- application: ${spring.application.name}
复制代码- @Bean
- public MeterBinder threadPoolMetrics(ThreadPoolTaskExecutor executor) {
- return registry -> {
- Gauge.builder("thread.pool.active", executor, ThreadPoolTaskExecutor::getActiveCount)
- .description("当前活跃线程数")
- .register(registry);
- Gauge.builder("thread.pool.queue.size", executor, e -> e.getThreadPoolExecutor().getQueue().size())
- .description("任务队列长度")
- .register(registry);
- };
- }
复制代码 通过http://localhost:8080/actuator/prometheus可获取实时指标。
二、Spring Boot内存泄漏排查:一个真实OOM案例
2.1 故障征象
- 应用运行24小时后出现java.lang.OutOfMemoryError: Java heap space
- GC日志表现老年代占用连续增长
2.2 诊断步骤
步骤1:天生堆转储文件
- # 在应用启动命令中添加
- -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp/heapdump.hprof
复制代码 步骤2:利用MAT分析
- 打开heapdump.hprof,选择Dominator Tree
- 发现ConcurrentHashMap$Node[]占用80%内存
- 查察引用链,定位到缓存工具类未清算逾期数据
步骤3:代码修复
- // 错误代码:静态Map无限增长
- public class CacheManager {
- private static Map<String, Object> cache = new ConcurrentHashMap<>();
-
- public static void put(String key, Object value) {
- cache.put(key, value);
- }
- }
- // 修复:引入Guava Cache自动过期
- public class CacheManager {
- private static LoadingCache<String, Object> cache = CacheBuilder.newBuilder()
- .maximumSize(1000)
- .expireAfterWrite(10, TimeUnit.MINUTES)
- .build(new CacheLoader<>() {
- @Override
- public Object load(String key) {
- return loadFromDB(key);
- }
- });
- }
复制代码 三、Spring Data JPA连接池优化(HikariCP实战)
3.1 默认设置风险
Spring Boot默认利用HikariCP,但以下参数需针对性调解:
- spring:
- datasource:
- hikari:
- maximum-pool-size: 20 # 默认10,根据DB并发能力调整
- connection-timeout: 3000 # 获取连接超时时间(ms)
- idle-timeout: 600000 # 空闲连接存活时间(默认10分钟)
- max-lifetime: 1800000 # 连接最大生命周期(默认30分钟)
- leak-detection-threshold: 5000 # 连接泄漏检测阈值(生产环境建议开启)
复制代码 3.2 监控集成
- @Bean
- public MeterBinder hikariMetrics(HikariDataSource dataSource) {
- return registry -> {
- HikariPoolMXBean pool = dataSource.getHikariPoolMXBean();
- Gauge.builder("db.pool.active", pool::getActiveConnections)
- .register(registry);
- Gauge.builder("db.pool.idle", pool::getIdleConnections)
- .register(registry);
- Gauge.builder("db.pool.total", pool::getTotalConnections)
- .register(registry);
- };
- }
复制代码 四、生产级Spring Boot JVM参数模板
4.1 基础参数(JDK11+)
- java -jar your-app.jar \
- -Xms2g -Xmx2g # 堆内存固定,避免动态调整开销 \
- -XX:MaxMetaspaceSize=256m # 防止元空间膨胀 \
- -XX:+UseG1GC # 低延迟垃圾回收器 \
- -XX:MaxGCPauseMillis=200 # 目标最大停顿时间 \
- -Xlog:gc*,gc+heap=debug:file=gc.log:time,uptime:filecount=5,filesize=100m \
- -Dspring.profiles.active=prod
复制代码 4.2 容器环境适配(Dfile.encoding警告修复)
- FROM eclipse-temurin:17-jdk
- ENV LANG C.UTF-8
- ENV JAVA_OPTS="-Dfile.encoding=UTF-8"
复制代码 五、实战:利用Arthas在线诊断Spring Boot应用
5.1 安装与附加进程
- curl -O https://arthas.aliyun.com/arthas-boot.jar
- java -jar arthas-boot.jar # 选择目标进程
复制代码 5.2 常用命令
- # 1. 查看实时线程状态
- thread -n 3 # 显示CPU占用最高的3个线程
- # 2. 监控方法调用耗时
- watch com.example.service.*Service * '{params, returnObj}' -x 3
- # 3. 动态修改日志级别(无需重启)
- logger --name ROOT --level debug
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |