Polish `spring.redis.url` support

See gh-7395
pull/7718/merge
Phillip Webb 8 years ago
parent 90eb58252e
commit 8f7efbe12a

@ -22,8 +22,6 @@ import java.net.UnknownHostException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.pool2.impl.GenericObjectPool; import org.apache.commons.pool2.impl.GenericObjectPool;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig; import redis.clients.jedis.JedisPoolConfig;
@ -66,8 +64,6 @@ import org.springframework.util.StringUtils;
@EnableConfigurationProperties(RedisProperties.class) @EnableConfigurationProperties(RedisProperties.class)
public class RedisAutoConfiguration { public class RedisAutoConfiguration {
private static final Log logger = LogFactory.getLog(RedisAutoConfiguration.class);
/** /**
* Redis connection configuration. * Redis connection configuration.
*/ */
@ -98,21 +94,20 @@ public class RedisAutoConfiguration {
protected final JedisConnectionFactory applyProperties( protected final JedisConnectionFactory applyProperties(
JedisConnectionFactory factory) { JedisConnectionFactory factory) {
configureConnection(factory);
if (this.properties.isSsl()) {
factory.setUseSsl(true);
}
factory.setDatabase(this.properties.getDatabase());
if (this.properties.getTimeout() > 0) {
factory.setTimeout(this.properties.getTimeout());
}
return factory;
}
private void configureConnection(JedisConnectionFactory factory) {
if (StringUtils.hasText(this.properties.getUrl())) { if (StringUtils.hasText(this.properties.getUrl())) {
if (this.properties.getUrl().startsWith("rediss://")) { configureConnectionFromUrl(factory);
factory.setUseSsl(true);
}
try {
URI redisURI = new URI(this.properties.getUrl());
factory.setHostName(redisURI.getHost());
factory.setPort(redisURI.getPort());
if (redisURI.getUserInfo() != null) {
factory.setPassword(redisURI.getUserInfo().split(":", 2)[1]);
}
}
catch (URISyntaxException e) {
logger.error("Incorrect spring.redis.url", e);
}
} }
else { else {
factory.setHostName(this.properties.getHost()); factory.setHostName(this.properties.getHost());
@ -121,14 +116,30 @@ public class RedisAutoConfiguration {
factory.setPassword(this.properties.getPassword()); factory.setPassword(this.properties.getPassword());
} }
} }
if (this.properties.isSsl()) { }
private void configureConnectionFromUrl(JedisConnectionFactory factory) {
String url = this.properties.getUrl();
if (url.startsWith("rediss://")) {
factory.setUseSsl(true); factory.setUseSsl(true);
} }
factory.setDatabase(this.properties.getDatabase()); try {
if (this.properties.getTimeout() > 0) { URI uri = new URI(url);
factory.setTimeout(this.properties.getTimeout()); factory.setHostName(uri.getHost());
factory.setPort(uri.getPort());
if (uri.getUserInfo() != null) {
String password = uri.getUserInfo();
int index = password.lastIndexOf(":");
if (index >= 0) {
password = password.substring(index + 1);
}
factory.setPassword(password);
}
}
catch (URISyntaxException ex) {
throw new IllegalArgumentException("Malformed 'spring.redis.url' " + url,
ex);
} }
return factory;
} }
protected final RedisSentinelConfiguration getSentinelConfig() { protected final RedisSentinelConfiguration getSentinelConfig() {

@ -41,6 +41,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Christian Dupuis * @author Christian Dupuis
* @author Christoph Strobl * @author Christoph Strobl
* @author Eddú Meléndez * @author Eddú Meléndez
* @author Marco Aust
*/ */
public class RedisAutoConfigurationTests { public class RedisAutoConfigurationTests {
@ -76,10 +77,9 @@ public class RedisAutoConfigurationTests {
} }
@Test @Test
public void testOverrideURLRedisConfiguration() throws Exception { public void testOverrideUrlRedisConfiguration() throws Exception {
load("spring.redis.host:foo", "spring.redis.password:xyz", load("spring.redis.host:foo", "spring.redis.password:xyz",
"spring.redis.port:1000", "spring.redis.port:1000", "spring.redis.ssl:true",
"spring.redis.ssl:true",
"spring.redis.url:redis://user:password@example:33"); "spring.redis.url:redis://user:password@example:33");
assertThat(this.context.getBean(JedisConnectionFactory.class).getHostName()) assertThat(this.context.getBean(JedisConnectionFactory.class).getHostName())
.isEqualTo("example"); .isEqualTo("example");

Loading…
Cancel
Save