Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save fastnsilver/72979829b8e1e7181754 to your computer and use it in GitHub Desktop.

Select an option

Save fastnsilver/72979829b8e1e7181754 to your computer and use it in GitHub Desktop.
Custom JedisPoolConfig
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.redis.RedisProperties;
import org.springframework.boot.autoconfigure.redis.RedisProperties.Sentinel;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import redis.clients.jedis.JedisPoolConfig;
@Configuration
@EnableConfigurationProperties
public class CustomRedisPooledConfiguration {
@Bean(name = "org.springframework.autoconfigure.redis.RedisProperties")
public RedisProperties redisProperties() {
return new RedisProperties();
}
protected static abstract class AbstractRedisConfiguration {
@Autowired
protected RedisProperties properties;
@Autowired(required = false)
private RedisSentinelConfiguration sentinelConfiguration;
protected final JedisConnectionFactory applyProperties(
JedisConnectionFactory factory) {
factory.setHostName(this.properties.getHost());
factory.setPort(this.properties.getPort());
if (this.properties.getPassword() != null) {
factory.setPassword(this.properties.getPassword());
}
factory.setDatabase(this.properties.getDatabase());
return factory;
}
protected final RedisSentinelConfiguration getSentinelConfig() {
if (this.sentinelConfiguration != null) {
return this.sentinelConfiguration;
}
Sentinel sentinelProperties = this.properties.getSentinel();
if (sentinelProperties != null) {
RedisSentinelConfiguration config = new RedisSentinelConfiguration();
config.master(sentinelProperties.getMaster());
config.setSentinels(createSentinels(sentinelProperties));
return config;
}
return null;
}
private List<RedisNode> createSentinels(Sentinel sentinel) {
List<RedisNode> sentinels = new ArrayList<RedisNode>();
String nodes = sentinel.getNodes();
for (String node : StringUtils.commaDelimitedListToStringArray(nodes)) {
try {
String[] parts = StringUtils.split(node, ":");
Assert.state(parts.length == 2, "Must be defined as 'host:port'");
sentinels.add(new RedisNode(parts[0], Integer.valueOf(parts[1])));
}
catch (RuntimeException ex) {
throw new IllegalStateException("Invalid redis sentinel "
+ "property '" + node + "'", ex);
}
}
return sentinels;
}
}
@Configuration
@ConditionalOnClass(GenericObjectPool.class)
protected static class RedisPooledConnectionConfiguration extends
AbstractRedisConfiguration {
@Autowired
private Environment env;
@Bean
public RedisConnectionFactory redisConnectionFactory()
throws UnknownHostException {
return applyProperties(createJedisConnectionFactory());
}
private JedisConnectionFactory createJedisConnectionFactory() {
if (this.properties.getPool() != null) {
return new JedisConnectionFactory(getSentinelConfig(), jedisPoolConfig());
}
return new JedisConnectionFactory(getSentinelConfig());
}
private JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig config = new JedisPoolConfig();
RedisProperties.Pool props = this.properties.getPool();
config.setMaxTotal(props.getMaxActive());
config.setMaxIdle(props.getMaxIdle());
config.setMinIdle(props.getMinIdle());
config.setMaxWaitMillis(props.getMaxWait());
config.setMinEvictableIdleTimeMillis(env.getProperty("spring.redis.pool.minEvictableIdleTimeMillis", Long.class, config.getMinEvictableIdleTimeMillis()));
config.setTimeBetweenEvictionRunsMillis(env.getProperty("spring.redis.pool.timeBetweenEvictionRunsMillis", Long.class, config.getTimeBetweenEvictionRunsMillis()));
return config;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment