@ -17,6 +17,8 @@
package org.springframework.boot.autoconfigure.redis ;
package org.springframework.boot.autoconfigure.redis ;
import java.net.UnknownHostException ;
import java.net.UnknownHostException ;
import java.util.ArrayList ;
import java.util.List ;
import org.apache.commons.pool2.impl.GenericObjectPool ;
import org.apache.commons.pool2.impl.GenericObjectPool ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.beans.factory.annotation.Autowired ;
@ -24,15 +26,19 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass ;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass ;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean ;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean ;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass ;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass ;
import org.springframework.boot.autoconfigure.redis.RedisProperties.Sentinel ;
import org.springframework.boot.context.properties.EnableConfigurationProperties ;
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.RedisNode ;
import org.springframework.data.redis.connection.RedisSentinelConfiguration ;
import org.springframework.data.redis.connection.jedis.JedisConnection ;
import org.springframework.data.redis.connection.jedis.JedisConnection ;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory ;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory ;
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 ;
import org.springframework.data.redis.core.StringRedisTemplate ;
import org.springframework.data.redis.core.StringRedisTemplate ;
import org.springframework.util.StringUtils ;
import redis.clients.jedis.Jedis ;
import redis.clients.jedis.Jedis ;
import redis.clients.jedis.JedisPoolConfig ;
import redis.clients.jedis.JedisPoolConfig ;
@ -43,6 +49,7 @@ import redis.clients.jedis.JedisPoolConfig;
* @author Dave Syer
* @author Dave Syer
* @author Andy Wilkinson
* @author Andy Wilkinson
* @author Christian Dupuis
* @author Christian Dupuis
* @author Christoph Strobl
* /
* /
@Configuration
@Configuration
@ConditionalOnClass ( { JedisConnection . class , RedisOperations . class , Jedis . class } )
@ConditionalOnClass ( { JedisConnection . class , RedisOperations . class , Jedis . class } )
@ -55,17 +62,58 @@ public class RedisAutoConfiguration {
return new RedisProperties ( ) ;
return new RedisProperties ( ) ;
}
}
@Configuration
protected abstract static class RedisHAConnectionConfiguration {
@ConditionalOnMissingClass ( name = "org.apache.commons.pool2.impl.GenericObjectPool" )
protected static class RedisConnectionConfiguration {
@Autowired
@Autowired
private RedisProperties properties ;
protected RedisProperties properties ;
@Autowired ( required = false )
private RedisSentinelConfiguration sentinelConfiguration ;
protected RedisSentinelConfiguration potentiallyGetSentinelConfig ( ) {
if ( this . sentinelConfiguration = = null
& & this . properties . getSentinel ( ) = = null ) {
return null ;
}
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 ( ) ) ) ;
}
return sentinelConfig ;
}
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 ] ) ) ) ;
}
return nodes ;
}
}
@Configuration
@ConditionalOnMissingClass ( name = "org.apache.commons.pool2.impl.GenericObjectPool" )
protected static class RedisConnectionConfiguration extends
RedisHAConnectionConfiguration {
@Bean
@Bean
@ConditionalOnMissingBean
@ConditionalOnMissingBean
RedisConnectionFactory redisConnectionFactory ( ) throws UnknownHostException {
RedisConnectionFactory redisConnectionFactory ( ) throws UnknownHostException {
JedisConnectionFactory factory = new JedisConnectionFactory ( ) ;
JedisConnectionFactory factory = new JedisConnectionFactory (
potentiallyGetSentinelConfig ( ) ) ;
applyConnectionFactoryProperties ( factory , this . properties ) ;
applyConnectionFactoryProperties ( factory , this . properties ) ;
return factory ;
return factory ;
}
}
@ -74,10 +122,8 @@ public class RedisAutoConfiguration {
@Configuration
@Configuration
@ConditionalOnClass ( GenericObjectPool . class )
@ConditionalOnClass ( GenericObjectPool . class )
protected static class RedisPooledConnectionConfiguration {
protected static class RedisPooledConnectionConfiguration extends
RedisHAConnectionConfiguration {
@Autowired
private RedisProperties properties ;
@Bean
@Bean
@ConditionalOnMissingBean
@ConditionalOnMissingBean
@ -88,10 +134,14 @@ public class RedisAutoConfiguration {
}
}
private JedisConnectionFactory createJedisConnectionFactory ( ) {
private JedisConnectionFactory createJedisConnectionFactory ( ) {
if ( this . properties . getPool ( ) ! = null ) {
if ( this . properties . getPool ( ) ! = null ) {
return new JedisConnectionFactory ( jedisPoolConfig ( ) ) ;
return new JedisConnectionFactory ( potentiallyGetSentinelConfig ( ) ,
jedisPoolConfig ( ) ) ;
}
else {
return new JedisConnectionFactory ( potentiallyGetSentinelConfig ( ) ) ;
}
}
return new JedisConnectionFactory ( ) ;
}
}
private JedisPoolConfig jedisPoolConfig ( ) {
private JedisPoolConfig jedisPoolConfig ( ) {