Skip to content

Commit 6ce1853

Browse files
committed
Redis sentinel should respect database in url also
Database property should be ignored if url is used. Signed-off-by: Yanming Zhou <[email protected]>
1 parent 5f71576 commit 6ce1853

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ public Standalone getStandalone() {
6060
if (this.properties.getUrl() != null) {
6161
ConnectionInfo connectionInfo = ConnectionInfo.of(this.properties.getUrl());
6262
return Standalone.of(connectionInfo.getUri().getHost(), connectionInfo.getUri().getPort(),
63-
(connectionInfo.getDatabase() != null) ? connectionInfo.getDatabase()
64-
: this.properties.getDatabase());
63+
(connectionInfo.getDatabase() != null) ? connectionInfo.getDatabase() : 0);
6564
}
6665
return Standalone.of(this.properties.getHost(), this.properties.getPort(), this.properties.getDatabase());
6766
}
@@ -77,7 +76,7 @@ public Sentinel getSentinel() {
7776

7877
@Override
7978
public int getDatabase() {
80-
return PropertiesRedisConnectionDetails.this.properties.getDatabase();
79+
return getStandalone().getDatabase();
8180
}
8281

8382
@Override

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

+14-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ void standaloneIsConfiguredFromUrlWithoutDatabase() {
114114
RedisConnectionDetails.Standalone standalone = connectionDetails.getStandalone();
115115
assertThat(standalone.getHost()).isEqualTo("example.com");
116116
assertThat(standalone.getPort()).isEqualTo(1234);
117-
assertThat(standalone.getDatabase()).isEqualTo(5);
117+
assertThat(standalone.getDatabase()).isEqualTo(0);
118118
}
119119

120120
@Test
@@ -144,9 +144,22 @@ void sentinelIsConfigured() {
144144
RedisProperties.Sentinel sentinel = new RedisProperties.Sentinel();
145145
sentinel.setNodes(List.of("localhost:1111", "127.0.0.1:2222", "[::1]:3333"));
146146
this.properties.setSentinel(sentinel);
147+
this.properties.setDatabase(5);
147148
PropertiesRedisConnectionDetails connectionDetails = new PropertiesRedisConnectionDetails(this.properties);
148149
assertThat(connectionDetails.getSentinel().getNodes()).containsExactly(new Node("localhost", 1111),
149150
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);
150163
}
151164

152165
}

0 commit comments

Comments
 (0)