redis集群连接
@Configuration
@ConditionalOnProperty(prefix = "redis.connection", value = "type", havingValue = "false")
public class RedisClusterConfiguration extends RedisConfiguration {
private static final String COMMA = ",";
private static final String COLON = ":";
@Value("${redis.cluster.nodes}")
private String clusterNodes;
@Value("${redis.cluster.max-redirects}")
private Integer maxRedirects;
@Value("${redis.cluster.useDefaultPoolConfig}")
private boolean useDefaultPoolConfig;
@Value("${redis.password}")
private String password;
public JedisConnectionFactory loadFactory() {
org.springframework.data.redis.connection.RedisClusterConfiguration clusterConfiguration = new org.springframework.data.redis.connection.RedisClusterConfiguration();
clusterConfiguration.setClusterNodes(getNodes(clusterNodes));
clusterConfiguration.setMaxRedirects(maxRedirects);
clusterConfiguration.setPassword(password);
if (useDefaultPoolConfig) {
return new JedisConnectionFactory(clusterConfiguration);
} else {
return new JedisConnectionFactory(clusterConfiguration, buildPoolConfig());
}
}
private List<RedisNode> getNodes(String nodes) {
List<RedisNode> result;
if (StringUtils.isEmpty(nodes)) {
result = Collections.emptyList();
} else {
result = new ArrayList<>();
for (String hostPort : nodes.split(COMMA)) {
String host = hostPort.split(COLON)[0];
Integer port = Integer.valueOf(hostPort.split(COLON)[1]);
result.add(new RedisNode(host, port));
}
}
return result;
}
}
@Configuration
//@ConditionalOnProperty(prefix = "cache", value = "type", havingValue = "redis", matchIfMissing = false)
@EnableCaching
@Data
public abstract class RedisConfiguration {
@Value("${redis.pool_config.maxTotal}")
private int maxTotal;
@Value("${redis.pool_config.maxIdle}")
private int maxIdle;
@Value("${redis.pool_config.minIdle}")
private int minIdle;
@Value("${redis.pool_config.testOnBorrow}")
private boolean testOnBorrow;
@Value("${redis.pool_config.testOnReturn}")
private boolean testOnReturn;
@Value("${redis.pool_config.testWhileIdle}")
private boolean testWhileIdle;
@Value("${redis.pool_config.minEvictableMs}")
private long minEvictableMs;
@Value("${redis.pool_config.evictionRunsMs}")
private long evictionRunsMs;
@Value("${redis.pool_config.maxWaitMills}")
private long maxWaitMills;
@Value("${redis.pool_config.numberTestsPerEvictionRun}")
private int numberTestsPerEvictionRun;
@Value("${redis.pool_config.blockWhenExhausted}")
private boolean blockWhenExhausted;
@Bean
public RedisConnectionFactory redisConnectionFactory() {
return loadFactory();
}
protected abstract JedisConnectionFactory loadFactory();
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory());
RedisSerializer<?> stringSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringSerializer);
redisTemplate.setValueSerializer(stringSerializer);
redisTemplate.setHashKeySerializer(stringSerializer);
redisTemplate.setHashValueSerializer(stringSerializer);
return redisTemplate;
}
protected JedisPoolConfig buildPoolConfig() {
final JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(maxTotal);
poolConfig.setMaxIdle(maxIdle);
poolConfig.setMinIdle(minIdle);
poolConfig.setTestOnBorrow(testOnBorrow);
poolConfig.setTestOnReturn(testOnReturn);
poolConfig.setTestWhileIdle(testWhileIdle);
poolConfig.setMinEvictableIdleTimeMillis(minEvictableMs);
poolConfig.setTimeBetweenEvictionRunsMillis(evictionRunsMs);
poolConfig.setMaxWaitMillis(maxWaitMills);
poolConfig.setNumTestsPerEvictionRun(numberTestsPerEvictionRun);
poolConfig.setBlockWhenExhausted(blockWhenExhausted);
return poolConfig;
}
}
@Configuration
@ConditionalOnProperty(prefix = "redis.connection", value = "type", havingValue = "true")
public class RedisStandaloneConfiguration extends RedisConfiguration {
@Value("${redis.standalone.host}")
private String host;
@Value("${redis.standalone.port}")
private Integer port;
@Value("${redis.standalone.clientName}")
private String clientName;
@Value("${redis.standalone.connectTimeout}")
private Long connectTimeout;
@Value("${redis.standalone.readTimeout}")
private Long readTimeout;
@Value("${redis.standalone.useDefaultClientConfig}")
private boolean useDefaultClientConfig;
@Value("${redis.standalone.usePoolConfig}")
private boolean usePoolConfig;
@Value("${redis.db}")
private Integer db;
@Value("${redis.password}")
private String password;
public JedisConnectionFactory loadFactory() {
org.springframework.data.redis.connection.RedisStandaloneConfiguration standaloneConfiguration = new org.springframework.data.redis.connection.RedisStandaloneConfiguration();
standaloneConfiguration.setHostName(host);
standaloneConfiguration.setPort(port);
standaloneConfiguration.setDatabase(db);
standaloneConfiguration.setPassword(password);
if (useDefaultClientConfig) {
return new JedisConnectionFactory(standaloneConfiguration);
} else {
return new JedisConnectionFactory(standaloneConfiguration, buildClientConfig());
}
}
private JedisClientConfiguration buildClientConfig() {
if (usePoolConfig) {
return JedisClientConfiguration.builder()
.clientName(clientName)
.connectTimeout(Duration.ofMillis(connectTimeout))
.readTimeout(Duration.ofMillis(readTimeout))
.usePooling().poolConfig(buildPoolConfig())
.build();
} else {
return JedisClientConfiguration.builder()
.clientName(clientName)
.connectTimeout(Duration.ofMillis(connectTimeout))
.readTimeout(Duration.ofMillis(readTimeout)).build();
}
}
}
redis:
# standalone or cluster
connection:
type: "${ENV_PUB_REDIS_SINGLE_FLAG:true}"
standalone:
#host: "${REDIS_HOST:127.0.0.1}"
#port: "${REDIS_PORT:6379}"
host: "${ENV_PUB_REDIS_HOST_HL:127.0.0.1}"
port: "${ENV_PUB_REDIS_PORT_HL:6379}"
useDefaultClientConfig: "${REDIS_USE_DEFAULT_CLIENT_CONFIG:true}"
# this value may be used only if you used not default ClientConfig
clientName: "${REDIS_CLIENT_NAME:standalone}"
# this value may be used only if you used not default ClientConfig
connectTimeout: "${REDIS_CLIENT_CONNECT_TIMEOUT:30000}"
# this value may be used only if you used not default ClientConfig
readTimeout: "${REDIS_CLIENT_READ_TIMEOUT:60000}"
# this value may be used only if you used not default ClientConfig
usePoolConfig: "${REDIS_CLIENT_USE_POOL_CONFIG:false}"
cluster:
# Comma-separated list of "host:port" pairs to bootstrap from.
nodes: "${REDIS_NODES:}"
# Maximum number of redirects to follow when executing commands across the cluster.
max-redirects: "${REDIS_MAX_REDIRECTS:12}"
useDefaultPoolConfig: "${REDIS_USE_DEFAULT_POOL_CONFIG:true}"
# db index
db: "${REDIS_DB:0}"
# db password
password: "${REDIS_PASSWORD:}"
# pool config
pool_config:
maxTotal: "${REDIS_POOL_CONFIG_MAX_TOTAL:128}"
maxIdle: "${REDIS_POOL_CONFIG_MAX_IDLE:128}"
minIdle: "${REDIS_POOL_CONFIG_MIN_IDLE:16}"
testOnBorrow: "${REDIS_POOL_CONFIG_TEST_ON_BORROW:true}"
testOnReturn: "${REDIS_POOL_CONFIG_TEST_ON_RETURN:true}"
testWhileIdle: "${REDIS_POOL_CONFIG_TEST_WHILE_IDLE:true}"
minEvictableMs: "${REDIS_POOL_CONFIG_MIN_EVICTABLE_MS:60000}"
evictionRunsMs: "${REDIS_POOL_CONFIG_EVICTION_RUNS_MS:30000}"
maxWaitMills: "${REDIS_POOL_CONFIG_MAX_WAIT_MS:60000}"
numberTestsPerEvictionRun: "${REDIS_POOL_CONFIG_NUMBER_TESTS_PER_EVICTION_RUN:3}"
blockWhenExhausted: "${REDIS_POOL_CONFIG_BLOCK_WHEN_EXHAUSTED:true}"
|