Przeglądaj źródła

仅当配置了 spring.redis.sentinel.master 时生效;未配 Sentinel 时仍走自动配置。

dujian 1 miesiąc temu
rodzic
commit
31da74e7c9

+ 8 - 1
alien-store/src/main/java/shop/alien/store/config/RedisLettuceConfig.java

@@ -3,6 +3,8 @@ package shop.alien.store.config;
 import io.lettuce.core.RedisURI;
 import io.lettuce.core.sentinel.api.sync.RedisSentinelCommands;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.boot.autoconfigure.AutoConfigureBefore;
+import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
 import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.beans.factory.annotation.Value;
@@ -25,14 +27,19 @@ import java.util.Map;
  */
 @Slf4j
 @Configuration
+@AutoConfigureBefore(RedisAutoConfiguration.class)
 public class RedisLettuceConfig {
 
     @Bean
     @Primary
-    @ConditionalOnProperty(prefix = "spring.redis.sentinel", name = "master")
+    @ConditionalOnProperty(
+            prefix = "spring.redis.sentinel",
+            name = "master",
+            matchIfMissing = false)
     public LettuceConnectionFactory redisConnectionFactory(
             RedisProperties properties,
             @Value("${spring.redis.master-port:#{null}}") Integer masterPortOverride) {
+        log.info("RedisLettuceConfig: 使用 Sentinel 解析 Master 并创建仅连 Master 的 Redis 连接(避免 READONLY)");
         RedisProperties.Sentinel sentinel = properties.getSentinel();
         String masterName = sentinel.getMaster();
         String[] firstSentinel = parseFirstSentinelNode(sentinel.getNodes());