|
@@ -1,61 +0,0 @@
|
|
|
-package com.haha.miniapp.config;
|
|
|
|
|
-
|
|
|
|
|
-import io.lettuce.core.ClientOptions;
|
|
|
|
|
-import io.lettuce.core.SocketOptions;
|
|
|
|
|
-import io.lettuce.core.protocol.ProtocolVersion;
|
|
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
-import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
|
|
|
|
|
-import org.springframework.context.annotation.Bean;
|
|
|
|
|
-import org.springframework.context.annotation.Configuration;
|
|
|
|
|
-import org.springframework.data.redis.connection.RedisConnectionFactory;
|
|
|
|
|
-import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
|
|
|
|
|
-import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
|
|
|
|
-import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
|
|
|
|
|
-
|
|
|
|
|
-import java.time.Duration;
|
|
|
|
|
-
|
|
|
|
|
-/**
|
|
|
|
|
- * Redis 配置类
|
|
|
|
|
- * Sa-Token 使用 Redis 作为持久化存储,通过 sa-token-redis-jackson 自动配置
|
|
|
|
|
- * Redis 连接信息通过 application.yml 中的 spring.redis 配置
|
|
|
|
|
- */
|
|
|
|
|
-@Configuration
|
|
|
|
|
-@Slf4j
|
|
|
|
|
-public class RedisConfig {
|
|
|
|
|
-
|
|
|
|
|
- @Bean
|
|
|
|
|
- public RedisConnectionFactory redisConnectionFactory(RedisProperties redisProperties) {
|
|
|
|
|
- // 配置 Socket 选项 - 连接超时和保持连接
|
|
|
|
|
- SocketOptions socketOptions = SocketOptions.builder()
|
|
|
|
|
- .connectTimeout(Duration.ofMillis(redisProperties.getTimeout().toMillis()))
|
|
|
|
|
- .keepAlive(true)
|
|
|
|
|
- .build();
|
|
|
|
|
-
|
|
|
|
|
- // 配置客户端选项 - 自动重连,使用RESP2协议避免HELLO认证问题
|
|
|
|
|
- ClientOptions clientOptions = ClientOptions.builder()
|
|
|
|
|
- .socketOptions(socketOptions)
|
|
|
|
|
- .autoReconnect(true)
|
|
|
|
|
- .protocolVersion(ProtocolVersion.RESP2)
|
|
|
|
|
- .disconnectedBehavior(ClientOptions.DisconnectedBehavior.REJECT_COMMANDS)
|
|
|
|
|
- .build();
|
|
|
|
|
-
|
|
|
|
|
- // 配置连接池
|
|
|
|
|
- LettucePoolingClientConfiguration poolConfig = LettucePoolingClientConfiguration.builder()
|
|
|
|
|
- .commandTimeout(redisProperties.getTimeout())
|
|
|
|
|
- .clientOptions(clientOptions)
|
|
|
|
|
- .build();
|
|
|
|
|
-
|
|
|
|
|
- // 创建Redis单机配置,设置host、port、password和database
|
|
|
|
|
- RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration();
|
|
|
|
|
- redisConfig.setHostName(redisProperties.getHost());
|
|
|
|
|
- redisConfig.setPort(redisProperties.getPort());
|
|
|
|
|
- redisConfig.setPassword(redisProperties.getPassword());
|
|
|
|
|
- redisConfig.setDatabase(redisProperties.getDatabase());
|
|
|
|
|
-
|
|
|
|
|
- log.info("Redis 增强配置已加载: host={}, port={}, database={}, 连接超时={}ms, 协议版本=RESP2",
|
|
|
|
|
- redisProperties.getHost(), redisProperties.getPort(),
|
|
|
|
|
- redisProperties.getDatabase(), redisProperties.getTimeout().toMillis());
|
|
|
|
|
-
|
|
|
|
|
- return new LettuceConnectionFactory(redisConfig, poolConfig);
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|