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