Parse Redis database from url if present
See gh-43813 Signed-off-by: Yanming Zhou <zhouyanming@gmail.com>
This commit is contained in:
parent
ebf110d3ab
commit
fcc6655c06
|
|
@ -27,6 +27,7 @@ import org.springframework.boot.autoconfigure.data.redis.RedisConnectionConfigur
|
|||
* @author Andy Wilkinson
|
||||
* @author Phillip Webb
|
||||
* @author Scott Frederick
|
||||
* @author Yanming Zhou
|
||||
*/
|
||||
class PropertiesRedisConnectionDetails implements RedisConnectionDetails {
|
||||
|
||||
|
|
@ -59,7 +60,7 @@ class PropertiesRedisConnectionDetails implements RedisConnectionDetails {
|
|||
if (this.properties.getUrl() != null) {
|
||||
ConnectionInfo connectionInfo = ConnectionInfo.of(this.properties.getUrl());
|
||||
return Standalone.of(connectionInfo.getUri().getHost(), connectionInfo.getUri().getPort(),
|
||||
this.properties.getDatabase());
|
||||
connectionInfo.getDatabase());
|
||||
}
|
||||
return Standalone.of(this.properties.getHost(), this.properties.getPort(), this.properties.getDatabase());
|
||||
}
|
||||
|
|
@ -75,7 +76,7 @@ class PropertiesRedisConnectionDetails implements RedisConnectionDetails {
|
|||
|
||||
@Override
|
||||
public int getDatabase() {
|
||||
return PropertiesRedisConnectionDetails.this.properties.getDatabase();
|
||||
return getStandalone().getDatabase();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@ import org.springframework.data.redis.connection.RedisPassword;
|
|||
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
|
||||
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Base Redis connection configuration.
|
||||
|
|
@ -45,6 +46,7 @@ import org.springframework.util.ClassUtils;
|
|||
* @author Moritz Halbritter
|
||||
* @author Andy Wilkinson
|
||||
* @author Phillip Webb
|
||||
* @author Yanming Zhou
|
||||
*/
|
||||
abstract class RedisConnectionConfiguration {
|
||||
|
||||
|
|
@ -189,11 +191,14 @@ abstract class RedisConnectionConfiguration {
|
|||
|
||||
private final String password;
|
||||
|
||||
private ConnectionInfo(URI uri, boolean useSsl, String username, String password) {
|
||||
private final int database;
|
||||
|
||||
private ConnectionInfo(URI uri, boolean useSsl, String username, String password, int database) {
|
||||
this.uri = uri;
|
||||
this.useSsl = useSsl;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
URI getUri() {
|
||||
|
|
@ -212,6 +217,10 @@ abstract class RedisConnectionConfiguration {
|
|||
return this.password;
|
||||
}
|
||||
|
||||
int getDatabase() {
|
||||
return this.database;
|
||||
}
|
||||
|
||||
static ConnectionInfo of(String url) {
|
||||
try {
|
||||
URI uri = new URI(url);
|
||||
|
|
@ -233,7 +242,14 @@ abstract class RedisConnectionConfiguration {
|
|||
password = candidate;
|
||||
}
|
||||
}
|
||||
return new ConnectionInfo(uri, useSsl, username, password);
|
||||
int database = 0;
|
||||
if (StringUtils.hasText(uri.getPath())) {
|
||||
String[] pathSplit = uri.getPath().split("/", 2);
|
||||
if (pathSplit.length > 1 && !pathSplit[1].isEmpty()) {
|
||||
database = Integer.parseInt(pathSplit[1]);
|
||||
}
|
||||
}
|
||||
return new ConnectionInfo(uri, useSsl, username, password, database);
|
||||
}
|
||||
catch (URISyntaxException ex) {
|
||||
throw new RedisUrlSyntaxException(url, ex);
|
||||
|
|
|
|||
|
|
@ -31,6 +31,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
|
|||
* @author Mark Paluch
|
||||
* @author Stephane Nicoll
|
||||
* @author Scott Frederick
|
||||
* @author Yanming Zhou
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "spring.data.redis")
|
||||
|
|
@ -42,8 +43,8 @@ public class RedisProperties {
|
|||
private int database = 0;
|
||||
|
||||
/**
|
||||
* Connection URL. Overrides host, port, username, and password. Example:
|
||||
* redis://user:password@example.com:6379
|
||||
* Connection URL. Overrides host, port, username, password, and database. Example:
|
||||
* redis://user:password@example.com:6379/8
|
||||
*/
|
||||
private String url;
|
||||
|
||||
|
|
|
|||
|
|
@ -103,7 +103,18 @@ class PropertiesRedisConnectionDetailsTests {
|
|||
RedisConnectionDetails.Standalone standalone = connectionDetails.getStandalone();
|
||||
assertThat(standalone.getHost()).isEqualTo("example.com");
|
||||
assertThat(standalone.getPort()).isEqualTo(1234);
|
||||
assertThat(standalone.getDatabase()).isEqualTo(5);
|
||||
assertThat(standalone.getDatabase()).isEqualTo(9999);
|
||||
}
|
||||
|
||||
@Test
|
||||
void standaloneIsConfiguredFromUrlWithoutDatabase() {
|
||||
this.properties.setUrl("redis://example.com:1234");
|
||||
this.properties.setDatabase(5);
|
||||
PropertiesRedisConnectionDetails connectionDetails = new PropertiesRedisConnectionDetails(this.properties);
|
||||
RedisConnectionDetails.Standalone standalone = connectionDetails.getStandalone();
|
||||
assertThat(standalone.getHost()).isEqualTo("example.com");
|
||||
assertThat(standalone.getPort()).isEqualTo(1234);
|
||||
assertThat(standalone.getDatabase()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -133,9 +144,22 @@ class PropertiesRedisConnectionDetailsTests {
|
|||
RedisProperties.Sentinel sentinel = new RedisProperties.Sentinel();
|
||||
sentinel.setNodes(List.of("localhost:1111", "127.0.0.1:2222", "[::1]:3333"));
|
||||
this.properties.setSentinel(sentinel);
|
||||
this.properties.setDatabase(5);
|
||||
PropertiesRedisConnectionDetails connectionDetails = new PropertiesRedisConnectionDetails(this.properties);
|
||||
assertThat(connectionDetails.getSentinel().getNodes()).containsExactly(new Node("localhost", 1111),
|
||||
new Node("127.0.0.1", 2222), new Node("[::1]", 3333));
|
||||
assertThat(connectionDetails.getSentinel().getDatabase()).isEqualTo(5);
|
||||
}
|
||||
|
||||
@Test
|
||||
void sentinelDatabaseIsConfiguredFromUrl() {
|
||||
RedisProperties.Sentinel sentinel = new RedisProperties.Sentinel();
|
||||
sentinel.setNodes(List.of("localhost:1111", "127.0.0.1:2222", "[::1]:3333"));
|
||||
this.properties.setSentinel(sentinel);
|
||||
this.properties.setUrl("redis://example.com:1234/9999");
|
||||
this.properties.setDatabase(5);
|
||||
PropertiesRedisConnectionDetails connectionDetails = new PropertiesRedisConnectionDetails(this.properties);
|
||||
assertThat(connectionDetails.getSentinel().getDatabase()).isEqualTo(9999);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue