From 7aceb760a2f4d841f9ef4607392014188fc7cf37 Mon Sep 17 00:00:00 2001 From: Ziin Date: Tue, 8 Jul 2025 15:19:59 +0800 Subject: [PATCH] =?UTF-8?q?1.=E4=BF=AE=E6=94=B9=E5=A4=9A=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=EF=BC=8C=E4=BF=AE=E5=A4=8D=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E4=B8=8D=E5=A4=8D=E7=94=A8=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../springbootinit/config/AsyncConfig.java | 23 +++++++++++++++++++ .../model/entity/ServerBigBrother.java | 4 ++-- .../springbootinit/rabbitMQ/MQReceiver.java | 5 ++-- .../service/impl/HostInfoServiceImpl.java | 1 - .../impl/ServerBigBrotherServiceImpl.java | 9 ++++---- src/main/resources/application.yml | 2 +- src/main/resources/logback-spring.xml | 15 ++++++++---- 7 files changed, 44 insertions(+), 15 deletions(-) create mode 100644 src/main/java/com/yupi/springbootinit/config/AsyncConfig.java diff --git a/src/main/java/com/yupi/springbootinit/config/AsyncConfig.java b/src/main/java/com/yupi/springbootinit/config/AsyncConfig.java new file mode 100644 index 0000000..f111e92 --- /dev/null +++ b/src/main/java/com/yupi/springbootinit/config/AsyncConfig.java @@ -0,0 +1,23 @@ +package com.yupi.springbootinit.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +import java.util.concurrent.Executor; + +@Configuration +@EnableAsync +public class AsyncConfig { + @Bean + public Executor taskExecutor() { + ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); + executor.setCorePoolSize(10); // 与 yaml 保持一致 + executor.setMaxPoolSize(20); + executor.setQueueCapacity(200); + executor.setThreadNamePrefix("save-task-"); + executor.initialize(); + return executor; + } +} \ No newline at end of file diff --git a/src/main/java/com/yupi/springbootinit/model/entity/ServerBigBrother.java b/src/main/java/com/yupi/springbootinit/model/entity/ServerBigBrother.java index cb84303..7687af1 100644 --- a/src/main/java/com/yupi/springbootinit/model/entity/ServerBigBrother.java +++ b/src/main/java/com/yupi/springbootinit/model/entity/ServerBigBrother.java @@ -99,8 +99,8 @@ public class ServerBigBrother { /** * 该数据所属的账号id */ - @TableField(value = "owner_id") - private String ownerId; + @TableField(value = "user_id") + private Long userId; /** * 租户 Id diff --git a/src/main/java/com/yupi/springbootinit/rabbitMQ/MQReceiver.java b/src/main/java/com/yupi/springbootinit/rabbitMQ/MQReceiver.java index a149856..3e71582 100644 --- a/src/main/java/com/yupi/springbootinit/rabbitMQ/MQReceiver.java +++ b/src/main/java/com/yupi/springbootinit/rabbitMQ/MQReceiver.java @@ -45,7 +45,7 @@ public class MQReceiver { // } @RabbitListener(queues = "HOST_INFO_QUEUE") - @Async + @Async("taskExecutor") public void receive(List hosts, Channel channel, Message message) throws IOException { long deliveryTag = message.getMessageProperties().getDeliveryTag(); try { @@ -62,7 +62,7 @@ public class MQReceiver { } @RabbitListener(queues = "BIG_BROTHER_QUEUE") - @Async + @Async("taskExecutor") public void bigBrotherReceive(ServerBigBrother bigBrotherList, Channel channel, Message message) throws IOException { long deliveryTag = message.getMessageProperties().getDeliveryTag(); try { @@ -74,6 +74,7 @@ public class MQReceiver { } catch (Exception e) { channel.basicNack(deliveryTag, false, false); log.error("消息消费失败"); + log.error(e.getMessage()); throw new BusinessException(ErrorCode.QUEUE_CONSUMPTION_FAILURE); } } diff --git a/src/main/java/com/yupi/springbootinit/service/impl/HostInfoServiceImpl.java b/src/main/java/com/yupi/springbootinit/service/impl/HostInfoServiceImpl.java index c995c19..12943d2 100644 --- a/src/main/java/com/yupi/springbootinit/service/impl/HostInfoServiceImpl.java +++ b/src/main/java/com/yupi/springbootinit/service/impl/HostInfoServiceImpl.java @@ -48,7 +48,6 @@ public class HostInfoServiceImpl extends ServiceImpl i @Override - @Async("taskExecutor") public CompletableFuture saveHostInfo(List newHosts) { try { StopWatch stopWatch = new StopWatch(); diff --git a/src/main/java/com/yupi/springbootinit/service/impl/ServerBigBrotherServiceImpl.java b/src/main/java/com/yupi/springbootinit/service/impl/ServerBigBrotherServiceImpl.java index 2ba0c27..d5c4ae6 100644 --- a/src/main/java/com/yupi/springbootinit/service/impl/ServerBigBrotherServiceImpl.java +++ b/src/main/java/com/yupi/springbootinit/service/impl/ServerBigBrotherServiceImpl.java @@ -1,5 +1,6 @@ package com.yupi.springbootinit.service.impl; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; import lombok.extern.slf4j.Slf4j; @@ -24,13 +25,13 @@ public class ServerBigBrotherServiceImpl extends ServiceImpllambdaQuery() - .eq(ServerBigBrother::getDisplayId, bigBrother.getDisplayId()) - .eq(ServerBigBrother::getTenantId, bigBrother.getTenantId())); + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("display_id", bigBrother.getDisplayId()) + .eq("tenant_id", bigBrother.getTenantId()); + ServerBigBrother serverBigBrother = baseMapper.selectOne(queryWrapper); if(serverBigBrother == null){ save(bigBrother); stopWatch.stop(); diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 4c9c952..cbea0fe 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -5,7 +5,7 @@ spring: task: # Spring 执行器配置,对应 TaskExecutionProperties 配置类。对于 Spring 异步任务,会使用该执行器。 execution: - thread-name-prefix: save-task # 线程池的线程名的前缀。默认为 task- ,建议根据自己应用来设置 + thread-name-prefix: save-task # 线程池的线程名的前缀。默认为 task- ,建议根据自己应用来设置 pool: # 线程池相关 core-size: 10 # 核心线程数,线程池创建时候初始化的线程数。默认为 8 。 max-size: 20 # 最大线程数,线程池最大的线程数,只有在缓冲队列满了之后,才会申请超过核心线程数的线程。默认为 Integer.MAX_VALUE diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml index 5951739..c75a95e 100644 --- a/src/main/resources/logback-spring.xml +++ b/src/main/resources/logback-spring.xml @@ -30,10 +30,15 @@ - + - + + + + + + @@ -45,8 +50,8 @@ - - - + + + \ No newline at end of file