Skip to content

Commit 457ed37

Browse files
committed
Merge pull request #43813 from quaff
* gh-43813: Polish "Parse Redis database from url if present" Parse Redis database from url if present Closes gh-43813
2 parents ebf110d + 49597e0 commit 457ed37

File tree

4 files changed

+50
-8
lines changed

4 files changed

+50
-8
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/PropertiesRedisConnectionDetails.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* @author Andy Wilkinson
2828
* @author Phillip Webb
2929
* @author Scott Frederick
30+
* @author Yanming Zhou
3031
*/
3132
class PropertiesRedisConnectionDetails implements RedisConnectionDetails {
3233

@@ -59,7 +60,7 @@ public Standalone getStandalone() {
5960
if (this.properties.getUrl() != null) {
6061
ConnectionInfo connectionInfo = ConnectionInfo.of(this.properties.getUrl());
6162
return Standalone.of(connectionInfo.getUri().getHost(), connectionInfo.getUri().getPort(),
62-
this.properties.getDatabase());
63+
connectionInfo.getDatabase());
6364
}
6465
return Standalone.of(this.properties.getHost(), this.properties.getPort(), this.properties.getDatabase());
6566
}
@@ -75,7 +76,7 @@ public Sentinel getSentinel() {
7576

7677
@Override
7778
public int getDatabase() {
78-
return PropertiesRedisConnectionDetails.this.properties.getDatabase();
79+
return getStandalone().getDatabase();
7980
}
8081

8182
@Override

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisConnectionConfiguration.java

+18-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
3434
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
3535
import org.springframework.util.ClassUtils;
36+
import org.springframework.util.StringUtils;
3637

3738
/**
3839
* Base Redis connection configuration.
@@ -45,6 +46,7 @@
4546
* @author Moritz Halbritter
4647
* @author Andy Wilkinson
4748
* @author Phillip Webb
49+
* @author Yanming Zhou
4850
*/
4951
abstract class RedisConnectionConfiguration {
5052

@@ -189,11 +191,14 @@ static final class ConnectionInfo {
189191

190192
private final String password;
191193

192-
private ConnectionInfo(URI uri, boolean useSsl, String username, String password) {
194+
private final int database;
195+
196+
private ConnectionInfo(URI uri, boolean useSsl, String username, String password, int database) {
193197
this.uri = uri;
194198
this.useSsl = useSsl;
195199
this.username = username;
196200
this.password = password;
201+
this.database = database;
197202
}
198203

199204
URI getUri() {
@@ -212,6 +217,10 @@ String getPassword() {
212217
return this.password;
213218
}
214219

220+
int getDatabase() {
221+
return this.database;
222+
}
223+
215224
static ConnectionInfo of(String url) {
216225
try {
217226
URI uri = new URI(url);
@@ -233,7 +242,14 @@ static ConnectionInfo of(String url) {
233242
password = candidate;
234243
}
235244
}
236-
return new ConnectionInfo(uri, useSsl, username, password);
245+
int database = 0;
246+
if (StringUtils.hasText(uri.getPath())) {
247+
String[] pathSplit = uri.getPath().split("/", 2);
248+
if (pathSplit.length > 1 && !pathSplit[1].isEmpty()) {
249+
database = Integer.parseInt(pathSplit[1]);
250+
}
251+
}
252+
return new ConnectionInfo(uri, useSsl, username, password, database);
237253
}
238254
catch (URISyntaxException ex) {
239255
throw new RedisUrlSyntaxException(url, ex);

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisProperties.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* @author Mark Paluch
3232
* @author Stephane Nicoll
3333
* @author Scott Frederick
34+
* @author Yanming Zhou
3435
* @since 1.0.0
3536
*/
3637
@ConfigurationProperties(prefix = "spring.data.redis")
@@ -42,8 +43,8 @@ public class RedisProperties {
4243
private int database = 0;
4344

4445
/**
45-
* Connection URL. Overrides host, port, username, and password. Example:
46-
* redis://user:password@example.com:6379
46+
* Connection URL. Overrides host, port, username, password, and database. Example:
47+
* redis://user:password@example.com:6379/8
4748
*/
4849
private String url;
4950

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/PropertiesRedisConnectionDetailsTests.java

+26-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -103,7 +103,18 @@ void standaloneIsConfiguredFromUrl() {
103103
RedisConnectionDetails.Standalone standalone = connectionDetails.getStandalone();
104104
assertThat(standalone.getHost()).isEqualTo("example.com");
105105
assertThat(standalone.getPort()).isEqualTo(1234);
106-
assertThat(standalone.getDatabase()).isEqualTo(5);
106+
assertThat(standalone.getDatabase()).isEqualTo(9999);
107+
}
108+
109+
@Test
110+
void standaloneIsConfiguredFromUrlWithoutDatabase() {
111+
this.properties.setUrl("redis://example.com:1234");
112+
this.properties.setDatabase(5);
113+
PropertiesRedisConnectionDetails connectionDetails = new PropertiesRedisConnectionDetails(this.properties);
114+
RedisConnectionDetails.Standalone standalone = connectionDetails.getStandalone();
115+
assertThat(standalone.getHost()).isEqualTo("example.com");
116+
assertThat(standalone.getPort()).isEqualTo(1234);
117+
assertThat(standalone.getDatabase()).isEqualTo(0);
107118
}
108119

109120
@Test
@@ -133,9 +144,22 @@ void sentinelIsConfigured() {
133144
RedisProperties.Sentinel sentinel = new RedisProperties.Sentinel();
134145
sentinel.setNodes(List.of("localhost:1111", "127.0.0.1:2222", "[::1]:3333"));
135146
this.properties.setSentinel(sentinel);
147+
this.properties.setDatabase(5);
136148
PropertiesRedisConnectionDetails connectionDetails = new PropertiesRedisConnectionDetails(this.properties);
137149
assertThat(connectionDetails.getSentinel().getNodes()).containsExactly(new Node("localhost", 1111),
138150
new Node("127.0.0.1", 2222), new Node("[::1]", 3333));
151+
assertThat(connectionDetails.getSentinel().getDatabase()).isEqualTo(5);
152+
}
153+
154+
@Test
155+
void sentinelDatabaseIsConfiguredFromUrl() {
156+
RedisProperties.Sentinel sentinel = new RedisProperties.Sentinel();
157+
sentinel.setNodes(List.of("localhost:1111", "127.0.0.1:2222", "[::1]:3333"));
158+
this.properties.setSentinel(sentinel);
159+
this.properties.setUrl("redis://example.com:1234/9999");
160+
this.properties.setDatabase(5);
161+
PropertiesRedisConnectionDetails connectionDetails = new PropertiesRedisConnectionDetails(this.properties);
162+
assertThat(connectionDetails.getSentinel().getDatabase()).isEqualTo(9999);
139163
}
140164

141165
}

0 commit comments

Comments
 (0)