|
|
|
|
@ -38,6 +38,7 @@ import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
@@ -38,6 +38,7 @@ import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
|
|
|
|
import org.springframework.data.redis.core.RedisOperations; |
|
|
|
|
import org.springframework.data.redis.core.RedisTemplate; |
|
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate; |
|
|
|
|
import org.springframework.util.Assert; |
|
|
|
|
import org.springframework.util.StringUtils; |
|
|
|
|
|
|
|
|
|
import redis.clients.jedis.Jedis; |
|
|
|
|
@ -50,6 +51,7 @@ import redis.clients.jedis.JedisPoolConfig;
@@ -50,6 +51,7 @@ import redis.clients.jedis.JedisPoolConfig;
|
|
|
|
|
* @author Andy Wilkinson |
|
|
|
|
* @author Christian Dupuis |
|
|
|
|
* @author Christoph Strobl |
|
|
|
|
* @author Phillip Webb |
|
|
|
|
*/ |
|
|
|
|
@Configuration |
|
|
|
|
@ConditionalOnClass({ JedisConnection.class, RedisOperations.class, Jedis.class }) |
|
|
|
|
@ -62,7 +64,10 @@ public class RedisAutoConfiguration {
@@ -62,7 +64,10 @@ public class RedisAutoConfiguration {
|
|
|
|
|
return new RedisProperties(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected abstract static class RedisHAConnectionConfiguration { |
|
|
|
|
/** |
|
|
|
|
* Base class for Redis configurations. |
|
|
|
|
*/ |
|
|
|
|
protected static abstract class AbstractRedisConfiguration { |
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
protected RedisProperties properties; |
|
|
|
|
@ -70,78 +75,87 @@ public class RedisAutoConfiguration {
@@ -70,78 +75,87 @@ public class RedisAutoConfiguration {
|
|
|
|
|
@Autowired(required = false) |
|
|
|
|
private RedisSentinelConfiguration sentinelConfiguration; |
|
|
|
|
|
|
|
|
|
protected RedisSentinelConfiguration potentiallyGetSentinelConfig() { |
|
|
|
|
|
|
|
|
|
if (this.sentinelConfiguration == null |
|
|
|
|
&& this.properties.getSentinel() == null) { |
|
|
|
|
return null; |
|
|
|
|
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; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
RedisSentinelConfiguration sentinelConfig = this.sentinelConfiguration; |
|
|
|
|
if (sentinelConfig == null && this.properties.getSentinel() != null) { |
|
|
|
|
sentinelConfig = new RedisSentinelConfiguration().master(this.properties |
|
|
|
|
.getSentinel().getMaster()); |
|
|
|
|
sentinelConfig.setSentinels(createRedisNodesForSentinel(this.properties |
|
|
|
|
.getSentinel())); |
|
|
|
|
protected final RedisSentinelConfiguration getSentinelConfig() { |
|
|
|
|
if (this.sentinelConfiguration != null) { |
|
|
|
|
return this.sentinelConfiguration; |
|
|
|
|
} |
|
|
|
|
return sentinelConfig; |
|
|
|
|
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> createRedisNodesForSentinel(Sentinel sentinel) { |
|
|
|
|
|
|
|
|
|
String[] nodeStrings = StringUtils.commaDelimitedListToStringArray(sentinel |
|
|
|
|
.getNodes()); |
|
|
|
|
|
|
|
|
|
List<RedisNode> nodes = new ArrayList<RedisNode>(nodeStrings.length); |
|
|
|
|
|
|
|
|
|
for (String hostAndPort : nodeStrings) { |
|
|
|
|
String[] args = StringUtils.split(hostAndPort, ":"); |
|
|
|
|
nodes.add(new RedisNode(args[0], Integer.valueOf(args[1]))); |
|
|
|
|
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 nodes; |
|
|
|
|
return sentinels; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Redis connection configuration. |
|
|
|
|
*/ |
|
|
|
|
@Configuration |
|
|
|
|
@ConditionalOnMissingClass(name = "org.apache.commons.pool2.impl.GenericObjectPool") |
|
|
|
|
protected static class RedisConnectionConfiguration extends |
|
|
|
|
RedisHAConnectionConfiguration { |
|
|
|
|
AbstractRedisConfiguration { |
|
|
|
|
|
|
|
|
|
@Bean |
|
|
|
|
@ConditionalOnMissingBean |
|
|
|
|
RedisConnectionFactory redisConnectionFactory() throws UnknownHostException { |
|
|
|
|
JedisConnectionFactory factory = new JedisConnectionFactory( |
|
|
|
|
potentiallyGetSentinelConfig()); |
|
|
|
|
applyConnectionFactoryProperties(factory, this.properties); |
|
|
|
|
return factory; |
|
|
|
|
public RedisConnectionFactory redisConnectionFactory() |
|
|
|
|
throws UnknownHostException { |
|
|
|
|
return applyProperties(new JedisConnectionFactory(getSentinelConfig())); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Redis pooled connection configuration. |
|
|
|
|
*/ |
|
|
|
|
@Configuration |
|
|
|
|
@ConditionalOnClass(GenericObjectPool.class) |
|
|
|
|
protected static class RedisPooledConnectionConfiguration extends |
|
|
|
|
RedisHAConnectionConfiguration { |
|
|
|
|
AbstractRedisConfiguration { |
|
|
|
|
|
|
|
|
|
@Bean |
|
|
|
|
@ConditionalOnMissingBean |
|
|
|
|
RedisConnectionFactory redisConnectionFactory() throws UnknownHostException { |
|
|
|
|
JedisConnectionFactory factory = createJedisConnectionFactory(); |
|
|
|
|
applyConnectionFactoryProperties(factory, this.properties); |
|
|
|
|
return factory; |
|
|
|
|
public RedisConnectionFactory redisConnectionFactory() |
|
|
|
|
throws UnknownHostException { |
|
|
|
|
return applyProperties(createJedisConnectionFactory()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private JedisConnectionFactory createJedisConnectionFactory() { |
|
|
|
|
|
|
|
|
|
if (this.properties.getPool() != null) { |
|
|
|
|
return new JedisConnectionFactory(potentiallyGetSentinelConfig(), |
|
|
|
|
jedisPoolConfig()); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
return new JedisConnectionFactory(potentiallyGetSentinelConfig()); |
|
|
|
|
return new JedisConnectionFactory(getSentinelConfig(), jedisPoolConfig()); |
|
|
|
|
} |
|
|
|
|
return new JedisConnectionFactory(getSentinelConfig()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private JedisPoolConfig jedisPoolConfig() { |
|
|
|
|
@ -156,12 +170,12 @@ public class RedisAutoConfiguration {
@@ -156,12 +170,12 @@ public class RedisAutoConfiguration {
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Standard Redis configuration. |
|
|
|
|
*/ |
|
|
|
|
@Configuration |
|
|
|
|
protected static class RedisConfiguration { |
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
private RedisProperties properties; |
|
|
|
|
|
|
|
|
|
@Bean |
|
|
|
|
@ConditionalOnMissingBean(name = "redisTemplate") |
|
|
|
|
RedisOperations<Object, Object> redisTemplate( |
|
|
|
|
@ -184,14 +198,4 @@ public class RedisAutoConfiguration {
@@ -184,14 +198,4 @@ public class RedisAutoConfiguration {
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void applyConnectionFactoryProperties(JedisConnectionFactory factory, |
|
|
|
|
RedisProperties properties) { |
|
|
|
|
factory.setHostName(properties.getHost()); |
|
|
|
|
factory.setPort(properties.getPort()); |
|
|
|
|
if (properties.getPassword() != null) { |
|
|
|
|
factory.setPassword(properties.getPassword()); |
|
|
|
|
} |
|
|
|
|
factory.setDatabase(properties.getDatabase()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|