Skip to content

Add support for configuring the Redis database using spring.data.redis.url #43813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* @author Andy Wilkinson
* @author Phillip Webb
* @author Scott Frederick
* @author Yanming Zhou
*/
class PropertiesRedisConnectionDetails implements RedisConnectionDetails {

Expand Down Expand Up @@ -59,7 +60,7 @@ public Standalone getStandalone() {
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());
}
Expand All @@ -75,7 +76,7 @@ public Sentinel getSentinel() {

@Override
public int getDatabase() {
return PropertiesRedisConnectionDetails.this.properties.getDatabase();
return getStandalone().getDatabase();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
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.
Expand All @@ -45,6 +46,7 @@
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @author Yanming Zhou
*/
abstract class RedisConnectionConfiguration {

Expand Down Expand Up @@ -189,11 +191,14 @@ static final class ConnectionInfo {

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() {
Expand All @@ -212,6 +217,10 @@ String getPassword() {
return this.password;
}

int getDatabase() {
return this.database;
}

static ConnectionInfo of(String url) {
try {
URI uri = new URI(url);
Expand All @@ -233,7 +242,14 @@ static ConnectionInfo of(String url) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* @author Mark Paluch
* @author Stephane Nicoll
* @author Scott Frederick
* @author Yanming Zhou
* @since 1.0.0
*/
@ConfigurationProperties(prefix = "spring.data.redis")
Expand All @@ -42,8 +43,8 @@ public class RedisProperties {
private int database = 0;

/**
* Connection URL. Overrides host, port, username, and password. Example:
* redis://user:[email protected]:6379
* Connection URL. Overrides host, port, username, password, and database. Example:
* redis://user:[email protected]:6379/8
*/
private String url;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,18 @@ void standaloneIsConfiguredFromUrl() {
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
Expand Down Expand Up @@ -133,9 +144,22 @@ void sentinelIsConfigured() {
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