@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
/ *
* Copyright 2012 - 2015 the original author or authors .
* Copyright 2012 - 2016 the original author or authors .
*
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
@ -29,10 +29,12 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@@ -29,10 +29,12 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass ;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean ;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass ;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Cluster ;
import org.springframework.boot.autoconfigure.data.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.data.redis.connection.RedisClusterConfiguration ;
import org.springframework.data.redis.connection.RedisConnectionFactory ;
import org.springframework.data.redis.connection.RedisNode ;
import org.springframework.data.redis.connection.RedisSentinelConfiguration ;
@ -76,6 +78,9 @@ public class RedisAutoConfiguration {
@@ -76,6 +78,9 @@ public class RedisAutoConfiguration {
@Autowired ( required = false )
private RedisSentinelConfiguration sentinelConfiguration ;
@Autowired ( required = false )
private RedisClusterConfiguration clusterConfiguration ;
protected final JedisConnectionFactory applyProperties (
JedisConnectionFactory factory ) {
factory . setHostName ( this . properties . getHost ( ) ) ;
@ -104,21 +109,41 @@ public class RedisAutoConfiguration {
@@ -104,21 +109,41 @@ public class RedisAutoConfiguration {
return null ;
}
/ * *
* Create a { @link RedisClusterConfiguration } if necessary .
* @return { @literal null } if no cluster settings are set .
* /
protected final RedisClusterConfiguration getClusterConfiguration ( ) {
if ( this . clusterConfiguration ! = null ) {
return this . clusterConfiguration ;
}
if ( this . properties . getCluster ( ) = = null ) {
return null ;
}
Cluster clusterProperties = this . properties . getCluster ( ) ;
RedisClusterConfiguration config = new RedisClusterConfiguration (
clusterProperties . getNodes ( ) ) ;
if ( clusterProperties . getMaxRedirects ( ) ! = null ) {
config . setMaxRedirects ( config . getMaxRedirects ( ) ) ;
}
return config ;
}
private List < RedisNode > createSentinels ( Sentinel sentinel ) {
List < RedisNode > sentinels = new ArrayList < RedisNode > ( ) ;
String nodes = sentinel . getNodes ( ) ;
for ( String node : StringUtils . commaDelimitedListToStringArray ( nodes ) ) {
List < RedisNode > nodes = new ArrayList < RedisNode > ( ) ;
for ( String node : StringUtils . commaDelimitedListToStringArray ( sentinel . getNodes ( ) ) ) {
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 ] ) ) ) ;
node s. add ( new RedisNode ( parts [ 0 ] , Integer . valueOf ( parts [ 1 ] ) ) ) ;
}
catch ( RuntimeException ex ) {
throw new IllegalStateException (
"Invalid redis sentinel " + "property '" + node + "'" , ex ) ;
}
}
return sentinels ;
return node s;
}
}
@ -135,9 +160,18 @@ public class RedisAutoConfiguration {
@@ -135,9 +160,18 @@ public class RedisAutoConfiguration {
@ConditionalOnMissingBean ( RedisConnectionFactory . class )
public JedisConnectionFactory redisConnectionFactory ( )
throws UnknownHostException {
return applyProperties ( new JedisConnectionFactory ( getSentinelConfig ( ) ) ) ;
return applyProperties ( createJedisConnectionFactory ( ) ) ;
}
private JedisConnectionFactory createJedisConnectionFactory ( ) {
if ( getSentinelConfig ( ) ! = null ) {
return new JedisConnectionFactory ( getSentinelConfig ( ) ) ;
}
if ( getClusterConfiguration ( ) ! = null ) {
return new JedisConnectionFactory ( getClusterConfiguration ( ) ) ;
}
return new JedisConnectionFactory ( ) ;
}
}
/ * *
@ -156,10 +190,16 @@ public class RedisAutoConfiguration {
@@ -156,10 +190,16 @@ public class RedisAutoConfiguration {
}
private JedisConnectionFactory createJedisConnectionFactory ( ) {
if ( this . properties . getPool ( ) ! = null ) {
return new JedisConnectionFactory ( getSentinelConfig ( ) , jedisPoolConfig ( ) ) ;
JedisPoolConfig poolConfig = this . properties . getPool ( ) ! = null ? jedisPoolConfig ( )
: new JedisPoolConfig ( ) ;
if ( getSentinelConfig ( ) ! = null ) {
return new JedisConnectionFactory ( getSentinelConfig ( ) , poolConfig ) ;
}
if ( getClusterConfiguration ( ) ! = null ) {
return new JedisConnectionFactory ( getClusterConfiguration ( ) , poolConfig ) ;
}
return new JedisConnectionFactory ( getSentinelConfig ( ) ) ;
return new JedisConnectionFactory ( poolConfig ) ;
}
private JedisPoolConfig jedisPoolConfig ( ) {