Skip to content

Commit cc2fd42

Browse files
committed
Parse redis database from url if present
Fix GH-43810 Signed-off-by: Yanming Zhou <[email protected]>
1 parent 1cd93cf commit cc2fd42

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

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

+3-1
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,8 @@ 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() != null) ? connectionInfo.getDatabase()
64+
: this.properties.getDatabase());
6365
}
6466
return Standalone.of(this.properties.getHost(), this.properties.getPort(), this.properties.getDatabase());
6567
}

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 Integer database;
195+
196+
private ConnectionInfo(URI uri, boolean useSsl, String username, String password, Integer 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+
Integer 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+
Integer database = null;
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:[email protected]:6379
46+
* Connection URL. Overrides host, port, username, password, and database. Example:
47+
* redis://user:[email protected]: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

+11
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,17 @@ 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(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);
106117
assertThat(standalone.getDatabase()).isEqualTo(5);
107118
}
108119

0 commit comments

Comments
 (0)