Skip to content

Commit f9a763e

Browse files
mustaphazorgatisamuel-klose
authored andcommitted
Extend properties for RedisSentinelConfiguration.
Closes #2860 Original pull request: #2861 Co-authored-by: Samuel Klose <39386136+samKl99@users.noreply.github.com>
1 parent 9543076 commit f9a763e

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

src/main/antora/modules/ROOT/pages/redis/connection-modes.adoc

+3
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ public RedisConnectionFactory jedisConnectionFactory() {
103103
* `spring.redis.sentinel.nodes`: Comma delimited list of host:port pairs.
104104
* `spring.redis.sentinel.username`: The username to apply when authenticating with Redis Sentinel (requires Redis 6)
105105
* `spring.redis.sentinel.password`: The password to apply when authenticating with Redis Sentinel
106+
* `spring.redis.sentinel.dataNode.username`: The username to apply when authenticating with Redis Data Node
107+
* `spring.redis.sentinel.dataNode.password`: The password to apply when authenticating with Redis Data Node
108+
* `spring.redis.sentinel.dataNode.database`: The database index to apply when authenticating with Redis Data Node
106109
====
107110

108111
Sometimes, direct interaction with one of the Sentinels is required. Using `RedisConnectionFactory.getSentinelConnection()` or `RedisConnection.getSentinelCommands()` gives you access to the first active Sentinel configured.

src/main/java/org/springframework/data/redis/connection/RedisSentinelConfiguration.java

+29
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
* @author Mark Paluch
4242
* @author Vikas Garg
4343
* @author John Blum
44+
* @author Samuel Klose
45+
* @author Mustapha Zorgati
4446
* @since 1.4
4547
*/
4648
public class RedisSentinelConfiguration implements RedisConfiguration, SentinelConfiguration {
@@ -49,6 +51,9 @@ public class RedisSentinelConfiguration implements RedisConfiguration, SentinelC
4951
private static final String REDIS_SENTINEL_NODES_CONFIG_PROPERTY = "spring.redis.sentinel.nodes";
5052
private static final String REDIS_SENTINEL_USERNAME_CONFIG_PROPERTY = "spring.redis.sentinel.username";
5153
private static final String REDIS_SENTINEL_PASSWORD_CONFIG_PROPERTY = "spring.redis.sentinel.password";
54+
private static final String REDIS_SENTINEL_DATA_NODE_USERNAME_CONFIG_PROPERTY = "spring.redis.sentinel.dataNode.username";
55+
private static final String REDIS_SENTINEL_DATA_NODE_PASSWORD_CONFIG_PROPERTY = "spring.redis.sentinel.dataNode.password";
56+
private static final String REDIS_SENTINEL_DATA_NODE_DATABASE_CONFIG_PROPERTY = "spring.redis.sentinel.dataNode.database";
5257

5358
private int database;
5459

@@ -122,6 +127,30 @@ public RedisSentinelConfiguration(PropertySource<?> propertySource) {
122127
String sentinelUsername = String.valueOf(propertySource.getProperty(REDIS_SENTINEL_USERNAME_CONFIG_PROPERTY));
123128
this.setSentinelUsername(sentinelUsername);
124129
}
130+
131+
if (propertySource.containsProperty(REDIS_SENTINEL_DATA_NODE_USERNAME_CONFIG_PROPERTY)) {
132+
String dataNodeUsername = String
133+
.valueOf(propertySource.getProperty(REDIS_SENTINEL_DATA_NODE_USERNAME_CONFIG_PROPERTY));
134+
this.setUsername(dataNodeUsername);
135+
}
136+
137+
if (propertySource.containsProperty(REDIS_SENTINEL_DATA_NODE_PASSWORD_CONFIG_PROPERTY)) {
138+
String dataNodePassword = String
139+
.valueOf(propertySource.getProperty(REDIS_SENTINEL_DATA_NODE_PASSWORD_CONFIG_PROPERTY));
140+
this.setPassword(dataNodePassword);
141+
}
142+
143+
if (propertySource.containsProperty(REDIS_SENTINEL_DATA_NODE_DATABASE_CONFIG_PROPERTY)) {
144+
String databaseSource = String
145+
.valueOf(propertySource.getProperty(REDIS_SENTINEL_DATA_NODE_DATABASE_CONFIG_PROPERTY));
146+
int database;
147+
try {
148+
database = Integer.parseInt(databaseSource);
149+
} catch (NumberFormatException ex) {
150+
throw new IllegalArgumentException(String.format("Invalid DB index '%s'; integer required", databaseSource));
151+
}
152+
this.setDatabase(database);
153+
}
125154
}
126155

127156
/**

src/test/java/org/springframework/data/redis/connection/RedisSentinelConfigurationUnitTests.java

+55
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Collections;
2222
import java.util.HashSet;
2323

24+
import org.assertj.core.api.ThrowableAssert;
2425
import org.junit.jupiter.api.Test;
2526

2627
import org.springframework.mock.env.MockPropertySource;
@@ -32,6 +33,8 @@
3233
* @author Christoph Strobl
3334
* @author Mark Paluch
3435
* @author Vikas Garg
36+
* @author Samuel Klose
37+
* @author Mustapha Zorgati
3538
*/
3639
class RedisSentinelConfigurationUnitTests {
3740

@@ -186,4 +189,56 @@ void readSentinelUsernameFromConfigProperty() {
186189
assertThat(config.getSentinelPassword()).isEqualTo(RedisPassword.of("foo"));
187190
assertThat(config.getSentinels()).hasSize(1).contains(new RedisNode("127.0.0.1", 123));
188191
}
192+
193+
@Test // GH-2860
194+
void readSentinelDataNodeUsernameFromConfigProperty() {
195+
MockPropertySource propertySource = new MockPropertySource();
196+
propertySource.setProperty("spring.redis.sentinel.dataNode.username", "datanode-user");
197+
198+
RedisSentinelConfiguration config = new RedisSentinelConfiguration(propertySource);
199+
200+
assertThat(config.getDataNodeUsername()).isEqualTo("datanode-user");
201+
}
202+
203+
@Test // GH-2860
204+
void readSentinelDataNodePasswordFromConfigProperty() {
205+
MockPropertySource propertySource = new MockPropertySource();
206+
propertySource.setProperty("spring.redis.sentinel.dataNode.password", "datanode-password");
207+
208+
RedisSentinelConfiguration config = new RedisSentinelConfiguration(propertySource);
209+
210+
assertThat(config.getDataNodePassword()).isEqualTo(RedisPassword.of("datanode-password"));
211+
}
212+
213+
@Test // GH-2860
214+
void readSentinelDataNodeDatabaseFromConfigProperty() {
215+
MockPropertySource propertySource = new MockPropertySource();
216+
propertySource.setProperty("spring.redis.sentinel.dataNode.database", "5");
217+
218+
RedisSentinelConfiguration config = new RedisSentinelConfiguration(propertySource);
219+
220+
assertThat(config.getDatabase()).isEqualTo(5);
221+
}
222+
223+
@Test // GH-2860
224+
void shouldThrowErrorWhen() {
225+
MockPropertySource propertySource = new MockPropertySource();
226+
propertySource.setProperty("spring.redis.sentinel.dataNode.database", "thisIsNotAnInteger");
227+
228+
ThrowableAssert.ThrowingCallable call = () -> new RedisSentinelConfiguration(propertySource);
229+
230+
assertThatThrownBy(call).isInstanceOf(IllegalArgumentException.class)
231+
.hasMessage("Invalid DB index '%s'; integer required", "thisIsNotAnInteger");
232+
}
233+
234+
@Test // GH-2860
235+
void shouldThrowErrorWhen2() {
236+
MockPropertySource propertySource = new MockPropertySource();
237+
propertySource.setProperty("spring.redis.sentinel.dataNode.database", "null");
238+
239+
ThrowableAssert.ThrowingCallable call = () -> new RedisSentinelConfiguration(propertySource);
240+
241+
assertThatThrownBy(call).isInstanceOf(IllegalArgumentException.class)
242+
.hasMessage("Invalid DB index '%s'; integer required", "null");
243+
}
189244
}

0 commit comments

Comments
 (0)