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,29 +94,7 @@ public class RedisAutoConfiguration {
|
||||||
|
|
||||||
protected final JedisConnectionFactory applyProperties(
|
protected final JedisConnectionFactory applyProperties(
|
||||||
JedisConnectionFactory factory) {
|
JedisConnectionFactory factory) {
|
||||||
if (StringUtils.hasText(this.properties.getUrl())) {
|
configureConnection(factory);
|
||||||
if (this.properties.getUrl().startsWith("rediss://")) {
|
|
||||||
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 {
|
|
||||||
factory.setHostName(this.properties.getHost());
|
|
||||||
factory.setPort(this.properties.getPort());
|
|
||||||
if (this.properties.getPassword() != null) {
|
|
||||||
factory.setPassword(this.properties.getPassword());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (this.properties.isSsl()) {
|
if (this.properties.isSsl()) {
|
||||||
factory.setUseSsl(true);
|
factory.setUseSsl(true);
|
||||||
}
|
}
|
||||||
|
|
@ -131,6 +105,43 @@ public class RedisAutoConfiguration {
|
||||||
return factory;
|
return factory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void configureConnection(JedisConnectionFactory factory) {
|
||||||
|
if (StringUtils.hasText(this.properties.getUrl())) {
|
||||||
|
configureConnectionFromUrl(factory);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
factory.setHostName(this.properties.getHost());
|
||||||
|
factory.setPort(this.properties.getPort());
|
||||||
|
if (this.properties.getPassword() != null) {
|
||||||
|
factory.setPassword(this.properties.getPassword());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void configureConnectionFromUrl(JedisConnectionFactory factory) {
|
||||||
|
String url = this.properties.getUrl();
|
||||||
|
if (url.startsWith("rediss://")) {
|
||||||
|
factory.setUseSsl(true);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
URI uri = new URI(url);
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected final RedisSentinelConfiguration getSentinelConfig() {
|
protected final RedisSentinelConfiguration getSentinelConfig() {
|
||||||
if (this.sentinelConfiguration != null) {
|
if (this.sentinelConfiguration != null) {
|
||||||
return this.sentinelConfiguration;
|
return this.sentinelConfiguration;
|
||||||
|
|
|
||||||
|
|
@ -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…
Reference in New Issue