优化Redis连接配置,支持集群、哨兵和单机模式,同时移除不必要的配置项
This commit is contained in:
@ -15,8 +15,10 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
|
|||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||||
|
import org.springframework.data.redis.connection.RedisClusterConfiguration;
|
||||||
import org.springframework.data.redis.connection.RedisNode;
|
import org.springframework.data.redis.connection.RedisNode;
|
||||||
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
|
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
|
||||||
|
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
|
||||||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
||||||
import org.springframework.data.redis.core.RedisOperations;
|
import org.springframework.data.redis.core.RedisOperations;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
@ -69,10 +71,26 @@ public class RedisLettuceConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private LettuceConnectionFactory getLettuceConnectionFactory(Integer db) {
|
private LettuceConnectionFactory getLettuceConnectionFactory(Integer db) {
|
||||||
LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(this.sentinelConfig());
|
LettuceConnectionFactory factory;
|
||||||
lettuceConnectionFactory.setDatabase(db);
|
if (redisProperties.getCluster() != null && redisProperties.getCluster().getNodes() != null && !redisProperties.getCluster().getNodes().isEmpty()) {
|
||||||
lettuceConnectionFactory.afterPropertiesSet();
|
// 集群模式
|
||||||
return lettuceConnectionFactory;
|
RedisClusterConfiguration clusterConfig = new RedisClusterConfiguration(redisProperties.getCluster().getNodes());
|
||||||
|
clusterConfig.setPassword(redisProperties.getPassword());
|
||||||
|
factory = new LettuceConnectionFactory(clusterConfig);
|
||||||
|
} else if (redisProperties.getSentinel() != null && redisProperties.getSentinel().getNodes() != null && !redisProperties.getSentinel().getNodes().isEmpty()) {
|
||||||
|
// 哨兵模式
|
||||||
|
factory = new LettuceConnectionFactory(this.sentinelConfig());
|
||||||
|
} else {
|
||||||
|
// 单机模式
|
||||||
|
RedisStandaloneConfiguration standaloneConfig = new RedisStandaloneConfiguration();
|
||||||
|
standaloneConfig.setHostName(redisProperties.getHost());
|
||||||
|
standaloneConfig.setPort(redisProperties.getPort());
|
||||||
|
standaloneConfig.setPassword(redisProperties.getPassword());
|
||||||
|
factory = new LettuceConnectionFactory(standaloneConfig);
|
||||||
|
}
|
||||||
|
factory.setDatabase(db);
|
||||||
|
factory.afterPropertiesSet();
|
||||||
|
return factory;
|
||||||
}
|
}
|
||||||
|
|
||||||
private RedisSentinelConfiguration sentinelConfig() {
|
private RedisSentinelConfiguration sentinelConfig() {
|
||||||
|
@ -75,24 +75,6 @@ spring:
|
|||||||
sasl.mechanism: SCRAM-SHA-256
|
sasl.mechanism: SCRAM-SHA-256
|
||||||
sasl.jaas.config: org.apache.kafka.common.security.scram.ScramLoginModule required username="jltest" password="Unicom#123";
|
sasl.jaas.config: org.apache.kafka.common.security.scram.ScramLoginModule required username="jltest" password="Unicom#123";
|
||||||
|
|
||||||
# 天宫 redis 需要使用哨兵进行访问
|
|
||||||
redis:
|
|
||||||
sentinel:
|
|
||||||
master: eshop-redis
|
|
||||||
nodes: 10.125.164.124:32718, 10.125.164.118:32716, 10.125.164.121:32716
|
|
||||||
password: Unicom#135
|
|
||||||
|
|
||||||
# 天宫Eureka配置
|
|
||||||
eureka:
|
|
||||||
client:
|
|
||||||
service-url:
|
|
||||||
defaultZone: http://10.242.37.148:5001/eureka
|
|
||||||
instance:
|
|
||||||
prefer-ip-address: true
|
|
||||||
ip-address: 125.32.114.204
|
|
||||||
hostname: 125.32.114.204
|
|
||||||
instance-ip: 125.32.114.204:${server.port}
|
|
||||||
|
|
||||||
mybatis-plus:
|
mybatis-plus:
|
||||||
configuration:
|
configuration:
|
||||||
# 是否开启自动驼峰命名规则映射:从数据库列名到Java属性驼峰命名的类似映射
|
# 是否开启自动驼峰命名规则映射:从数据库列名到Java属性驼峰命名的类似映射
|
||||||
@ -123,6 +105,8 @@ hystrix:
|
|||||||
forceClosed: true
|
forceClosed: true
|
||||||
|
|
||||||
ribbon:
|
ribbon:
|
||||||
|
eureka:
|
||||||
|
enabled: false
|
||||||
ReadTimeout: 20000 #请求处理的超时时间
|
ReadTimeout: 20000 #请求处理的超时时间
|
||||||
ConnectTimeout: 20000 #请求连接超时时间
|
ConnectTimeout: 20000 #请求连接超时时间
|
||||||
MaxAutoRetries: 0 #对当前实例的重试次数
|
MaxAutoRetries: 0 #对当前实例的重试次数
|
||||||
|
Reference in New Issue
Block a user