From 51de8a63e1f668b3f3597bc78242e6d5a3012986 Mon Sep 17 00:00:00 2001 From: Ziin Date: Wed, 9 Jul 2025 15:57:23 +0800 Subject: [PATCH] =?UTF-8?q?1.=E5=BB=B6=E8=BF=9F=E6=8E=A5=E6=94=B6=E6=B6=88?= =?UTF-8?q?=E6=81=AF=EF=BC=8C=E9=98=B2=E6=AD=A2=E5=87=BA=E7=8E=B0=E7=BA=BF?= =?UTF-8?q?=E7=A8=8B=E5=90=83=E6=BB=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 +- .../common/DelayedRabbitMQStarter.java | 27 +++++++++++++++++++ .../controller/BigBrotherController.java | 2 -- .../springbootinit/rabbitMQ/MQReceiver.java | 5 ++-- src/main/resources/application-dev.yml | 3 +++ src/main/resources/application-prod.yml | 8 +++--- src/main/resources/application.yml | 4 +-- src/main/resources/logback-spring.xml | 12 +++++---- 8 files changed, 47 insertions(+), 16 deletions(-) create mode 100644 src/main/java/com/yupi/springbootinit/common/DelayedRabbitMQStarter.java diff --git a/.gitignore b/.gitignore index ce59517..af02781 100644 --- a/.gitignore +++ b/.gitignore @@ -146,4 +146,4 @@ fabric.properties .idea/caches/build_file_checksums.ser !/.xcodemap/ -!/tk-data-save.log +/tk-data-save.log diff --git a/src/main/java/com/yupi/springbootinit/common/DelayedRabbitMQStarter.java b/src/main/java/com/yupi/springbootinit/common/DelayedRabbitMQStarter.java new file mode 100644 index 0000000..06ca336 --- /dev/null +++ b/src/main/java/com/yupi/springbootinit/common/DelayedRabbitMQStarter.java @@ -0,0 +1,27 @@ +package com.yupi.springbootinit.common; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; + +@Component +@Slf4j +public class DelayedRabbitMQStarter implements ApplicationRunner { + + @Resource + private RabbitListenerEndpointRegistry registry; // Spring 提供的监听器注册表 + + @Override + public void run(ApplicationArguments args) throws Exception { + // 延迟 5 秒后启动监听器 + log.info("延迟5秒启动 rabbitMQ 监听"); + Thread.sleep(5000); + registry.getListenerContainer("hosts").start(); + registry.getListenerContainer("bigbrother").start(); + } +} \ No newline at end of file diff --git a/src/main/java/com/yupi/springbootinit/controller/BigBrotherController.java b/src/main/java/com/yupi/springbootinit/controller/BigBrotherController.java index 6733310..ec9b199 100644 --- a/src/main/java/com/yupi/springbootinit/controller/BigBrotherController.java +++ b/src/main/java/com/yupi/springbootinit/controller/BigBrotherController.java @@ -2,10 +2,8 @@ package com.yupi.springbootinit.controller; import com.yupi.springbootinit.common.BaseResponse; import com.yupi.springbootinit.common.ResultUtils; -import com.yupi.springbootinit.model.entity.NewHosts; import com.yupi.springbootinit.model.entity.ServerBigBrother; import com.yupi.springbootinit.rabbitMQ.MQSender; -import com.yupi.springbootinit.service.HostInfoService; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.*; diff --git a/src/main/java/com/yupi/springbootinit/rabbitMQ/MQReceiver.java b/src/main/java/com/yupi/springbootinit/rabbitMQ/MQReceiver.java index 3e71582..6dbb9f7 100644 --- a/src/main/java/com/yupi/springbootinit/rabbitMQ/MQReceiver.java +++ b/src/main/java/com/yupi/springbootinit/rabbitMQ/MQReceiver.java @@ -12,6 +12,7 @@ import com.yupi.springbootinit.service.ServerBigBrotherService; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.core.Message; import org.springframework.amqp.rabbit.annotation.RabbitListener; +import org.springframework.context.annotation.Lazy; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; @@ -44,7 +45,7 @@ public class MQReceiver { // } // } - @RabbitListener(queues = "HOST_INFO_QUEUE") + @RabbitListener(queues = "HOST_INFO_QUEUE",id = "hosts", autoStartup = "false") @Async("taskExecutor") public void receive(List hosts, Channel channel, Message message) throws IOException { long deliveryTag = message.getMessageProperties().getDeliveryTag(); @@ -61,7 +62,7 @@ public class MQReceiver { } } - @RabbitListener(queues = "BIG_BROTHER_QUEUE") + @RabbitListener(queues = "BIG_BROTHER_QUEUE",id = "bigbrother", autoStartup = "false") @Async("taskExecutor") public void bigBrotherReceive(ServerBigBrother bigBrotherList, Channel channel, Message message) throws IOException { long deliveryTag = message.getMessageProperties().getDeliveryTag(); diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index e190c6b..7f89605 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -26,4 +26,7 @@ spring: listener: simple: acknowledge-mode: manual + concurrency: 10 + max-concurrency: 20 + prefetch: 5 diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml index 9d33b56..06faa8c 100644 --- a/src/main/resources/application-prod.yml +++ b/src/main/resources/application-prod.yml @@ -27,10 +27,10 @@ spring: listener: simple: acknowledge-mode: manual -mybatis-plus: - configuration: - # 生产环境关闭日志 - log-impl: '' + concurrency: 10 + max-concurrency: 20 + prefetch: 5 + # 接口文档配置 knife4j: basic: diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index cbea0fe..ebe3572 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -19,7 +19,7 @@ spring: name: springboot-init # 默认 dev 环境 profiles: - active: dev + active: prod # 支持 swagger3 mvc: pathmatch: @@ -60,7 +60,7 @@ server: mybatis-plus: configuration: map-underscore-to-camel-case: false - log-impl: org.apache.ibatis.logging.nologging.NoLoggingImpl + log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl default-executor-type: batch global-config: banner: false diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml index c75a95e..d6dc25a 100644 --- a/src/main/resources/logback-spring.xml +++ b/src/main/resources/logback-spring.xml @@ -34,10 +34,11 @@ - + + + - @@ -47,11 +48,12 @@ - - - + + + + \ No newline at end of file