Commit 1819e0e1d6811e1170c6b5aa76dff598a6760e17
1 parent
c5b547e0
异步化 Redis MQ 的初始化,提升启动速度
Showing
2 changed files
with
9 additions
and
1 deletions
yudao-framework/yudao-spring-boot-starter-mq/src/main/java/cn/iocoder/yudao/framework/mq/config/YudaoMQAutoConfiguration.java
| ... | ... | @@ -25,6 +25,7 @@ import org.springframework.data.redis.listener.ChannelTopic; |
| 25 | 25 | import org.springframework.data.redis.listener.RedisMessageListenerContainer; |
| 26 | 26 | import org.springframework.data.redis.stream.DefaultStreamMessageListenerContainerX; |
| 27 | 27 | import org.springframework.data.redis.stream.StreamMessageListenerContainer; |
| 28 | +import org.springframework.scheduling.annotation.Async; | |
| 28 | 29 | |
| 29 | 30 | import java.util.List; |
| 30 | 31 | import java.util.Properties; |
| ... | ... | @@ -54,6 +55,7 @@ public class YudaoMQAutoConfiguration { |
| 54 | 55 | * 创建 Redis Pub/Sub 广播消费的容器 |
| 55 | 56 | */ |
| 56 | 57 | @Bean |
| 58 | + @Async // 异步化,可降低 2 秒左右的启动时间 | |
| 57 | 59 | public RedisMessageListenerContainer redisMessageListenerContainer( |
| 58 | 60 | RedisMQTemplate redisMQTemplate, List<AbstractChannelMessageListener<?>> listeners) { |
| 59 | 61 | // 创建 RedisMessageListenerContainer 对象 |
| ... | ... | @@ -76,6 +78,7 @@ public class YudaoMQAutoConfiguration { |
| 76 | 78 | * Redis Stream 的 xreadgroup 命令:https://www.geek-book.com/src/docs/redis/redis/redis.io/commands/xreadgroup.html |
| 77 | 79 | */ |
| 78 | 80 | @Bean(initMethod = "start", destroyMethod = "stop") |
| 81 | + @Async // 异步化,可降低 5 秒左右的启动时间 | |
| 79 | 82 | public StreamMessageListenerContainer<String, ObjectRecord<String, String>> redisStreamMessageListenerContainer( |
| 80 | 83 | RedisMQTemplate redisMQTemplate, List<AbstractStreamMessageListener<?>> listeners) { |
| 81 | 84 | RedisTemplate<String, ?> redisTemplate = redisMQTemplate.getRedisTemplate(); |
| ... | ... | @@ -95,6 +98,8 @@ public class YudaoMQAutoConfiguration { |
| 95 | 98 | // 第二步,注册监听器,消费对应的 Stream 主题 |
| 96 | 99 | String consumerName = buildConsumerName(); |
| 97 | 100 | listeners.parallelStream().forEach(listener -> { |
| 101 | + log.info("[redisStreamMessageListenerContainer][开始注册 StreamKey({}) 对应的监听器({})]", | |
| 102 | + listener.getStreamKey(), listener.getClass().getName()); | |
| 98 | 103 | // 创建 listener 对应的消费者分组 |
| 99 | 104 | try { |
| 100 | 105 | redisTemplate.opsForStream().createGroup(listener.getStreamKey(), listener.getGroup()); |
| ... | ... | @@ -111,6 +116,8 @@ public class YudaoMQAutoConfiguration { |
| 111 | 116 | .autoAcknowledge(false) // 不自动 ack |
| 112 | 117 | .cancelOnError(throwable -> false); // 默认配置,发生异常就取消消费,显然不符合预期;因此,我们设置为 false |
| 113 | 118 | container.register(builder.build(), listener); |
| 119 | + log.info("[redisStreamMessageListenerContainer][完成注册 StreamKey({}) 对应的监听器({})]", | |
| 120 | + listener.getStreamKey(), listener.getClass().getName()); | |
| 114 | 121 | }); |
| 115 | 122 | return container; |
| 116 | 123 | } | ... | ... |
yudao-server/src/main/resources/application-local.yaml
| ... | ... | @@ -30,7 +30,7 @@ spring: |
| 30 | 30 | multi-statement-allow: true |
| 31 | 31 | dynamic: # 多数据源配置 |
| 32 | 32 | druid: # Druid 【连接池】相关的全局配置 |
| 33 | - initial-size: 5 # 初始连接数 | |
| 33 | + initial-size: 1 # 初始连接数 | |
| 34 | 34 | min-idle: 10 # 最小连接池数量 |
| 35 | 35 | max-active: 20 # 最大连接池数量 |
| 36 | 36 | max-wait: 600000 # 配置获取连接等待超时的时间,单位:毫秒 |
| ... | ... | @@ -171,6 +171,7 @@ logging: |
| 171 | 171 | cn.iocoder.yudao.module.member.dal.mysql: debug |
| 172 | 172 | cn.iocoder.yudao.module.trade.dal.mysql: debug |
| 173 | 173 | cn.iocoder.yudao.module.promotion.dal.mysql: debug |
| 174 | +debug: false | |
| 174 | 175 | |
| 175 | 176 | --- #################### 微信公众号、小程序相关配置 #################### |
| 176 | 177 | wx: | ... | ... |