Polishing.

Deprecate RedisSentinelConfiguration and RedisClusterConfiguration constructors taking PropertySource in favor of a factory method.

Reformat code. Update documentation.

See #2860
Original pull request: #2861
This commit is contained in:
Mark Paluch
2024-03-01 14:10:18 +01:00
parent f9a763e55a
commit 761115ab37
5 changed files with 72 additions and 36 deletions

View File

@@ -51,16 +51,14 @@ public class RedisClusterConfiguration implements RedisConfiguration, ClusterCon
private RedisPassword password = RedisPassword.none();
private final Set<RedisNode> clusterNodes;
private final Set<RedisNode> clusterNodes = new LinkedHashSet<>();
private @Nullable String username = null;
/**
* Creates a new, default {@link RedisClusterConfiguration}.
*/
public RedisClusterConfiguration() {
this(new MapPropertySource("RedisClusterConfiguration", Collections.emptyMap()));
}
public RedisClusterConfiguration() {}
/**
* Creates a new {@link RedisClusterConfiguration} for given {@link String hostPort} combinations.
@@ -75,28 +73,31 @@ public class RedisClusterConfiguration implements RedisConfiguration, ClusterCon
* @param clusterNodes must not be {@literal null}.
*/
public RedisClusterConfiguration(Collection<String> clusterNodes) {
this(new MapPropertySource("RedisClusterConfiguration", asMap(clusterNodes, -1)));
initialize(new MapPropertySource("RedisClusterConfiguration", asMap(clusterNodes, -1)));
}
/**
* Creates a new {@link RedisClusterConfiguration} looking up configuration values from the given
* {@link PropertySource}.
*
* <pre>
* <code>
* <pre class="code">
* spring.redis.cluster.nodes=127.0.0.1:23679,127.0.0.1:23680,127.0.0.1:23681
* spring.redis.cluster.max-redirects=3
* </code>
* </pre>
*
* @param propertySource must not be {@literal null}.
* @deprecated since 3.3, use {@link RedisSentinelConfiguration#of(PropertySource)} instead. This constructor will be
* made private in the next major release.
*/
@Deprecated(since = "3.3")
public RedisClusterConfiguration(PropertySource<?> propertySource) {
initialize(propertySource);
}
private void initialize(PropertySource<?> propertySource) {
Assert.notNull(propertySource, "PropertySource must not be null");
this.clusterNodes = new LinkedHashSet<>();
if (propertySource.containsProperty(REDIS_CLUSTER_NODES_CONFIG_PROPERTY)) {
Object redisClusterNodes = propertySource.getProperty(REDIS_CLUSTER_NODES_CONFIG_PROPERTY);
@@ -109,6 +110,23 @@ public class RedisClusterConfiguration implements RedisConfiguration, ClusterCon
}
}
/**
* Creates a new {@link RedisClusterConfiguration} looking up configuration values from the given
* {@link PropertySource}.
*
* <pre class="code">
* spring.redis.cluster.nodes=127.0.0.1:23679,127.0.0.1:23680,127.0.0.1:23681
* spring.redis.cluster.max-redirects=3
* </pre>
*
* @param propertySource must not be {@literal null}.
* @return a new {@link RedisClusterConfiguration} configured from the given {@link PropertySource}.
* @since 3.3
*/
public static RedisClusterConfiguration of(PropertySource<?> propertySource) {
return new RedisClusterConfiguration(propertySource);
}
private void appendClusterNodes(Set<String> hostAndPorts) {
for (String hostAndPort : hostAndPorts) {

View File

@@ -15,7 +15,7 @@
*/
package org.springframework.data.redis.connection;
import static org.springframework.util.StringUtils.commaDelimitedListToSet;
import static org.springframework.util.StringUtils.*;
import java.util.Collections;
import java.util.HashMap;
@@ -32,8 +32,8 @@ import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/**
* {@link RedisConfiguration Configuration} class used to set up a {@link RedisConnection}
* with {@link RedisConnectionFactory} for connecting to <a href="https://redis.io/topics/sentinel">Redis Sentinel(s)</a>.
* {@link RedisConfiguration Configuration} class used to set up a {@link RedisConnection} with
* {@link RedisConnectionFactory} for connecting to <a href="https://redis.io/topics/sentinel">Redis Sentinel(s)</a>.
* Useful when setting up a highly available Redis environment.
*
* @author Christoph Strobl
@@ -101,7 +101,10 @@ public class RedisSentinelConfiguration implements RedisConfiguration, SentinelC
*
* @param propertySource must not be {@literal null}.
* @since 1.5
* @deprecated since 3.3, use {@link RedisSentinelConfiguration#of(PropertySource)} instead. This constructor will be
* made private in the next major release.
*/
@Deprecated(since = "3.3")
public RedisSentinelConfiguration(PropertySource<?> propertySource) {
Assert.notNull(propertySource, "PropertySource must not be null");
@@ -153,6 +156,17 @@ public class RedisSentinelConfiguration implements RedisConfiguration, SentinelC
}
}
/**
* Construct a new {@link RedisSentinelConfiguration} from the given {@link PropertySource}.
*
* @param propertySource must not be {@literal null}.
* @return a new {@link RedisSentinelConfiguration} configured from the given {@link PropertySource}.
* @since 3.3
*/
public static RedisSentinelConfiguration of(PropertySource<?> propertySource) {
return new RedisSentinelConfiguration(propertySource);
}
/**
* Set {@literal Sentinels} to connect to.
*
@@ -315,8 +329,7 @@ public class RedisSentinelConfiguration implements RedisConfiguration, SentinelC
return false;
}
return this.database == that.database
&& ObjectUtils.nullSafeEquals(this.master, that.master)
return this.database == that.database && ObjectUtils.nullSafeEquals(this.master, that.master)
&& ObjectUtils.nullSafeEquals(this.sentinels, that.sentinels)
&& ObjectUtils.nullSafeEquals(this.dataNodeUsername, that.dataNodeUsername)
&& ObjectUtils.nullSafeEquals(this.dataNodePassword, that.dataNodePassword)