Skip to content

Commit 761115a

Browse files
committed
Polishing.
Deprecate RedisSentinelConfiguration and RedisClusterConfiguration constructors taking PropertySource in favor of a factory method. Reformat code. Update documentation. See #2860 Original pull request: #2861
1 parent f9a763e commit 761115a

File tree

5 files changed

+72
-36
lines changed

5 files changed

+72
-36
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public RedisConnectionFactory jedisConnectionFactory() {
9696

9797
[TIP]
9898
====
99-
`RedisSentinelConfiguration` can also be defined with a `PropertySource`, which lets you set the following properties:
99+
`RedisSentinelConfiguration` can also be defined through `RedisSentinelConfiguration.of(PropertySource)`, which lets you pick up the following properties:
100100
101101
.Configuration Properties
102102
* `spring.redis.sentinel.master`: name of the master node.
@@ -164,7 +164,7 @@ public class AppConfig {
164164

165165
[TIP]
166166
====
167-
`RedisClusterConfiguration` can also be defined through `PropertySource` and has the following properties:
167+
`RedisClusterConfiguration` can also be defined through `RedisClusterConfiguration.of(PropertySource)`, which lets you pick up the following properties:
168168
169169
.Configuration Properties
170170
- `spring.redis.cluster.nodes`: Comma-delimited list of host:port pairs.

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

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,14 @@ public class RedisClusterConfiguration implements RedisConfiguration, ClusterCon
5151

5252
private RedisPassword password = RedisPassword.none();
5353

54-
private final Set<RedisNode> clusterNodes;
54+
private final Set<RedisNode> clusterNodes = new LinkedHashSet<>();
5555

5656
private @Nullable String username = null;
5757

5858
/**
5959
* Creates a new, default {@link RedisClusterConfiguration}.
6060
*/
61-
public RedisClusterConfiguration() {
62-
this(new MapPropertySource("RedisClusterConfiguration", Collections.emptyMap()));
63-
}
61+
public RedisClusterConfiguration() {}
6462

6563
/**
6664
* Creates a new {@link RedisClusterConfiguration} for given {@link String hostPort} combinations.
@@ -75,27 +73,30 @@ public RedisClusterConfiguration() {
7573
* @param clusterNodes must not be {@literal null}.
7674
*/
7775
public RedisClusterConfiguration(Collection<String> clusterNodes) {
78-
this(new MapPropertySource("RedisClusterConfiguration", asMap(clusterNodes, -1)));
76+
initialize(new MapPropertySource("RedisClusterConfiguration", asMap(clusterNodes, -1)));
7977
}
8078

8179
/**
8280
* Creates a new {@link RedisClusterConfiguration} looking up configuration values from the given
8381
* {@link PropertySource}.
8482
*
85-
* <pre>
86-
* <code>
83+
* <pre class="code">
8784
* spring.redis.cluster.nodes=127.0.0.1:23679,127.0.0.1:23680,127.0.0.1:23681
8885
* spring.redis.cluster.max-redirects=3
89-
* </code>
9086
* </pre>
9187
*
9288
* @param propertySource must not be {@literal null}.
89+
* @deprecated since 3.3, use {@link RedisSentinelConfiguration#of(PropertySource)} instead. This constructor will be
90+
* made private in the next major release.
9391
*/
92+
@Deprecated(since = "3.3")
9493
public RedisClusterConfiguration(PropertySource<?> propertySource) {
94+
initialize(propertySource);
95+
}
9596

96-
Assert.notNull(propertySource, "PropertySource must not be null");
97+
private void initialize(PropertySource<?> propertySource) {
9798

98-
this.clusterNodes = new LinkedHashSet<>();
99+
Assert.notNull(propertySource, "PropertySource must not be null");
99100

100101
if (propertySource.containsProperty(REDIS_CLUSTER_NODES_CONFIG_PROPERTY)) {
101102

@@ -109,6 +110,23 @@ public RedisClusterConfiguration(PropertySource<?> propertySource) {
109110
}
110111
}
111112

113+
/**
114+
* Creates a new {@link RedisClusterConfiguration} looking up configuration values from the given
115+
* {@link PropertySource}.
116+
*
117+
* <pre class="code">
118+
* spring.redis.cluster.nodes=127.0.0.1:23679,127.0.0.1:23680,127.0.0.1:23681
119+
* spring.redis.cluster.max-redirects=3
120+
* </pre>
121+
*
122+
* @param propertySource must not be {@literal null}.
123+
* @return a new {@link RedisClusterConfiguration} configured from the given {@link PropertySource}.
124+
* @since 3.3
125+
*/
126+
public static RedisClusterConfiguration of(PropertySource<?> propertySource) {
127+
return new RedisClusterConfiguration(propertySource);
128+
}
129+
112130
private void appendClusterNodes(Set<String> hostAndPorts) {
113131

114132
for (String hostAndPort : hostAndPorts) {

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.springframework.data.redis.connection;
1717

18-
import static org.springframework.util.StringUtils.commaDelimitedListToSet;
18+
import static org.springframework.util.StringUtils.*;
1919

2020
import java.util.Collections;
2121
import java.util.HashMap;
@@ -32,8 +32,8 @@
3232
import org.springframework.util.StringUtils;
3333

3434
/**
35-
* {@link RedisConfiguration Configuration} class used to set up a {@link RedisConnection}
36-
* with {@link RedisConnectionFactory} for connecting to <a href="https://redis.io/topics/sentinel">Redis Sentinel(s)</a>.
35+
* {@link RedisConfiguration Configuration} class used to set up a {@link RedisConnection} with
36+
* {@link RedisConnectionFactory} for connecting to <a href="https://redis.io/topics/sentinel">Redis Sentinel(s)</a>.
3737
* Useful when setting up a highly available Redis environment.
3838
*
3939
* @author Christoph Strobl
@@ -101,7 +101,10 @@ public RedisSentinelConfiguration(String master, Set<String> sentinelHostAndPort
101101
*
102102
* @param propertySource must not be {@literal null}.
103103
* @since 1.5
104+
* @deprecated since 3.3, use {@link RedisSentinelConfiguration#of(PropertySource)} instead. This constructor will be
105+
* made private in the next major release.
104106
*/
107+
@Deprecated(since = "3.3")
105108
public RedisSentinelConfiguration(PropertySource<?> propertySource) {
106109

107110
Assert.notNull(propertySource, "PropertySource must not be null");
@@ -153,6 +156,17 @@ public RedisSentinelConfiguration(PropertySource<?> propertySource) {
153156
}
154157
}
155158

159+
/**
160+
* Construct a new {@link RedisSentinelConfiguration} from the given {@link PropertySource}.
161+
*
162+
* @param propertySource must not be {@literal null}.
163+
* @return a new {@link RedisSentinelConfiguration} configured from the given {@link PropertySource}.
164+
* @since 3.3
165+
*/
166+
public static RedisSentinelConfiguration of(PropertySource<?> propertySource) {
167+
return new RedisSentinelConfiguration(propertySource);
168+
}
169+
156170
/**
157171
* Set {@literal Sentinels} to connect to.
158172
*
@@ -315,8 +329,7 @@ public boolean equals(@Nullable Object obj) {
315329
return false;
316330
}
317331

318-
return this.database == that.database
319-
&& ObjectUtils.nullSafeEquals(this.master, that.master)
332+
return this.database == that.database && ObjectUtils.nullSafeEquals(this.master, that.master)
320333
&& ObjectUtils.nullSafeEquals(this.sentinels, that.sentinels)
321334
&& ObjectUtils.nullSafeEquals(this.dataNodeUsername, that.dataNodeUsername)
322335
&& ObjectUtils.nullSafeEquals(this.dataNodePassword, that.dataNodePassword)

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ void shouldThrowExecptionOnInvalidHostAndPortString() {
8383

8484
@Test // DATAREDIS-315
8585
void shouldThrowExceptionWhenListOfHostAndPortIsNull() {
86-
assertThatIllegalArgumentException()
87-
.isThrownBy(() -> new RedisClusterConfiguration(Collections.singleton(null)));
86+
assertThatIllegalArgumentException().isThrownBy(() -> new RedisClusterConfiguration(Collections.singleton(null)));
8887
}
8988

9089
@Test // DATAREDIS-315
@@ -97,13 +96,13 @@ void shouldNotFailWhenListOfHostAndPortIsEmpty() {
9796

9897
@Test // DATAREDIS-315
9998
void shouldThrowExceptionGivenNullPropertySource() {
100-
assertThatIllegalArgumentException().isThrownBy(() -> new RedisClusterConfiguration((PropertySource<?>) null));
99+
assertThatIllegalArgumentException().isThrownBy(() -> RedisClusterConfiguration.of((PropertySource<?>) null));
101100
}
102101

103102
@Test // DATAREDIS-315
104103
void shouldNotFailWhenGivenPropertySourceNotContainingRelevantProperties() {
105104

106-
RedisClusterConfiguration config = new RedisClusterConfiguration(new MockPropertySource());
105+
RedisClusterConfiguration config = RedisClusterConfiguration.of(new MockPropertySource());
107106

108107
assertThat(config.getMaxRedirects()).isNull();
109108
assertThat(config.getClusterNodes().size()).isEqualTo(0);
@@ -116,7 +115,7 @@ void shouldBeCreatedCorrectlyGivenValidPropertySourceWithSingleHostPort() {
116115
propertySource.setProperty("spring.redis.cluster.nodes", HOST_AND_PORT_1);
117116
propertySource.setProperty("spring.redis.cluster.max-redirects", "5");
118117

119-
RedisClusterConfiguration config = new RedisClusterConfiguration(propertySource);
118+
RedisClusterConfiguration config = RedisClusterConfiguration.of(propertySource);
120119

121120
assertThat(config.getMaxRedirects()).isEqualTo(5);
122121
assertThat(config.getClusterNodes()).contains(new RedisNode("127.0.0.1", 123));
@@ -130,23 +129,23 @@ void shouldBeCreatedCorrectlyGivenValidPropertySourceWithMultipleHostPort() {
130129
StringUtils.collectionToCommaDelimitedString(Arrays.asList(HOST_AND_PORT_1, HOST_AND_PORT_2, HOST_AND_PORT_3)));
131130
propertySource.setProperty("spring.redis.cluster.max-redirects", "5");
132131

133-
RedisClusterConfiguration config = new RedisClusterConfiguration(propertySource);
132+
RedisClusterConfiguration config = RedisClusterConfiguration.of(propertySource);
134133

135134
assertThat(config.getMaxRedirects()).isEqualTo(5);
136135
assertThat(config.getClusterNodes()).contains(new RedisNode("127.0.0.1", 123), new RedisNode("localhost", 456),
137136
new RedisNode("localhost", 789));
138137
}
139138

140139
@Test // GH-2360
141-
public void shouldBeCreatedCorrectlyGivenValidPropertySourceWithMultipleIPv6AddressesAndPorts() {
140+
void shouldBeCreatedCorrectlyGivenValidPropertySourceWithMultipleIPv6AddressesAndPorts() {
142141

143142
MockPropertySource propertySource = new MockPropertySource();
144143

145144
propertySource.setProperty("spring.redis.cluster.nodes",
146145
StringUtils.collectionToCommaDelimitedString(Arrays.asList(HOST_AND_PORT_4, HOST_AND_PORT_5)));
147146
propertySource.setProperty("spring.redis.cluster.max-redirects", 2);
148147

149-
RedisClusterConfiguration configuration = new RedisClusterConfiguration(propertySource);
148+
RedisClusterConfiguration configuration = RedisClusterConfiguration.of(propertySource);
150149

151150
assertThat(configuration.getMaxRedirects()).isEqualTo(2);
152151
assertThat(configuration.getClusterNodes()).contains(new RedisNode("fe80::a00:27ff:fe4b:ee48", 6379),

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

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ void shouldNotFailWhenListOfHostAndPortIsEmpty() {
9696

9797
@Test // DATAREDIS-372
9898
void shouldThrowExceptionGivenNullPropertySource() {
99-
assertThatIllegalArgumentException().isThrownBy(() -> new RedisSentinelConfiguration(null));
99+
assertThatIllegalArgumentException().isThrownBy(() -> RedisSentinelConfiguration.of(null));
100100
}
101101

102102
@Test // DATAREDIS-372
103103
void shouldNotFailWhenGivenPropertySourceNotContainingRelevantProperties() {
104104

105-
RedisSentinelConfiguration config = new RedisSentinelConfiguration(new MockPropertySource());
105+
RedisSentinelConfiguration config = RedisSentinelConfiguration.of(new MockPropertySource());
106106

107107
assertThat(config.getMaster()).isNull();
108108
assertThat(config.getSentinels()).isEmpty();
@@ -115,7 +115,7 @@ void shouldBeCreatedCorrectlyGivenValidPropertySourceWithMasterAndSingleHostPort
115115
propertySource.setProperty("spring.redis.sentinel.master", "myMaster");
116116
propertySource.setProperty("spring.redis.sentinel.nodes", HOST_AND_PORT_1);
117117

118-
RedisSentinelConfiguration config = new RedisSentinelConfiguration(propertySource);
118+
RedisSentinelConfiguration config = RedisSentinelConfiguration.of(propertySource);
119119

120120
assertThat(config.getMaster()).isNotNull();
121121
assertThat(config.getMaster().getName()).isEqualTo("myMaster");
@@ -131,7 +131,7 @@ void shouldBeCreatedCorrectlyGivenValidPropertySourceWithMasterAndMultipleHostPo
131131
propertySource.setProperty("spring.redis.sentinel.nodes",
132132
StringUtils.collectionToCommaDelimitedString(Arrays.asList(HOST_AND_PORT_1, HOST_AND_PORT_2, HOST_AND_PORT_3)));
133133

134-
RedisSentinelConfiguration config = new RedisSentinelConfiguration(propertySource);
134+
RedisSentinelConfiguration config = RedisSentinelConfiguration.of(propertySource);
135135

136136
assertThat(config.getSentinels()).hasSize(3);
137137
assertThat(config.getSentinels()).contains(new RedisNode("127.0.0.1", 123), new RedisNode("localhost", 456),
@@ -151,6 +151,7 @@ void dataNodePasswordDoesNotAffectSentinelPassword() {
151151

152152
@Test // GH-2218
153153
void dataNodeUsernameDoesNotAffectSentinelUsername() {
154+
154155
RedisSentinelConfiguration configuration = new RedisSentinelConfiguration("myMaster",
155156
Collections.singleton(HOST_AND_PORT_1));
156157
configuration.setUsername("data-admin");
@@ -168,7 +169,7 @@ void readSentinelPasswordFromConfigProperty() {
168169
propertySource.setProperty("spring.redis.sentinel.nodes", HOST_AND_PORT_1);
169170
propertySource.setProperty("spring.redis.sentinel.password", "computer-says-no");
170171

171-
RedisSentinelConfiguration config = new RedisSentinelConfiguration(propertySource);
172+
RedisSentinelConfiguration config = RedisSentinelConfiguration.of(propertySource);
172173

173174
assertThat(config.getSentinelPassword()).isEqualTo(RedisPassword.of("computer-says-no"));
174175
assertThat(config.getSentinels()).hasSize(1).contains(new RedisNode("127.0.0.1", 123));
@@ -183,7 +184,7 @@ void readSentinelUsernameFromConfigProperty() {
183184
propertySource.setProperty("spring.redis.sentinel.username", "sentinel-admin");
184185
propertySource.setProperty("spring.redis.sentinel.password", "foo");
185186

186-
RedisSentinelConfiguration config = new RedisSentinelConfiguration(propertySource);
187+
RedisSentinelConfiguration config = RedisSentinelConfiguration.of(propertySource);
187188

188189
assertThat(config.getSentinelUsername()).isEqualTo("sentinel-admin");
189190
assertThat(config.getSentinelPassword()).isEqualTo(RedisPassword.of("foo"));
@@ -192,51 +193,56 @@ void readSentinelUsernameFromConfigProperty() {
192193

193194
@Test // GH-2860
194195
void readSentinelDataNodeUsernameFromConfigProperty() {
196+
195197
MockPropertySource propertySource = new MockPropertySource();
196198
propertySource.setProperty("spring.redis.sentinel.dataNode.username", "datanode-user");
197199

198-
RedisSentinelConfiguration config = new RedisSentinelConfiguration(propertySource);
200+
RedisSentinelConfiguration config = RedisSentinelConfiguration.of(propertySource);
199201

200202
assertThat(config.getDataNodeUsername()).isEqualTo("datanode-user");
201203
}
202204

203205
@Test // GH-2860
204206
void readSentinelDataNodePasswordFromConfigProperty() {
207+
205208
MockPropertySource propertySource = new MockPropertySource();
206209
propertySource.setProperty("spring.redis.sentinel.dataNode.password", "datanode-password");
207210

208-
RedisSentinelConfiguration config = new RedisSentinelConfiguration(propertySource);
211+
RedisSentinelConfiguration config = RedisSentinelConfiguration.of(propertySource);
209212

210213
assertThat(config.getDataNodePassword()).isEqualTo(RedisPassword.of("datanode-password"));
211214
}
212215

213216
@Test // GH-2860
214217
void readSentinelDataNodeDatabaseFromConfigProperty() {
218+
215219
MockPropertySource propertySource = new MockPropertySource();
216220
propertySource.setProperty("spring.redis.sentinel.dataNode.database", "5");
217221

218-
RedisSentinelConfiguration config = new RedisSentinelConfiguration(propertySource);
222+
RedisSentinelConfiguration config = RedisSentinelConfiguration.of(propertySource);
219223

220224
assertThat(config.getDatabase()).isEqualTo(5);
221225
}
222226

223227
@Test // GH-2860
224228
void shouldThrowErrorWhen() {
229+
225230
MockPropertySource propertySource = new MockPropertySource();
226231
propertySource.setProperty("spring.redis.sentinel.dataNode.database", "thisIsNotAnInteger");
227232

228-
ThrowableAssert.ThrowingCallable call = () -> new RedisSentinelConfiguration(propertySource);
233+
ThrowableAssert.ThrowingCallable call = () -> RedisSentinelConfiguration.of(propertySource);
229234

230235
assertThatThrownBy(call).isInstanceOf(IllegalArgumentException.class)
231236
.hasMessage("Invalid DB index '%s'; integer required", "thisIsNotAnInteger");
232237
}
233238

234239
@Test // GH-2860
235240
void shouldThrowErrorWhen2() {
241+
236242
MockPropertySource propertySource = new MockPropertySource();
237243
propertySource.setProperty("spring.redis.sentinel.dataNode.database", "null");
238244

239-
ThrowableAssert.ThrowingCallable call = () -> new RedisSentinelConfiguration(propertySource);
245+
ThrowableAssert.ThrowingCallable call = () -> RedisSentinelConfiguration.of(propertySource);
240246

241247
assertThatThrownBy(call).isInstanceOf(IllegalArgumentException.class)
242248
.hasMessage("Invalid DB index '%s'; integer required", "null");

0 commit comments

Comments
 (0)