Skip to content

Commit 6175d90

Browse files
committed
Add static master-replica mode
Signed-off-by: 용현 <dydguskim@gripcorp.co>
1 parent ac41e3f commit 6175d90

File tree

4 files changed

+30
-32
lines changed

4 files changed

+30
-32
lines changed

module/spring-boot-data-redis/src/main/java/org/springframework/boot/data/redis/autoconfigure/JedisConnectionConfiguration.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ private JedisConnectionFactory createJedisConnectionFactory(
103103
Assert.state(sentinelConfig != null, "'sentinelConfig' must not be null");
104104
yield new JedisConnectionFactory(sentinelConfig, clientConfiguration);
105105
}
106+
case STATIC_MASTER_REPLICA -> throw new IllegalStateException("Static master replica is not supported for Jedis");
106107
};
107108
}
108109

module/spring-boot-data-redis/src/main/java/org/springframework/boot/data/redis/autoconfigure/LettuceConnectionConfiguration.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
4141
import org.springframework.boot.autoconfigure.condition.ConditionalOnThreading;
4242
import org.springframework.boot.data.redis.autoconfigure.RedisConnectionDetails.Node;
43+
import org.springframework.boot.data.redis.autoconfigure.RedisProperties.Lettuce;
4344
import org.springframework.boot.data.redis.autoconfigure.RedisProperties.Lettuce.Cluster.Refresh;
44-
import org.springframework.boot.data.redis.autoconfigure.RedisProperties.Lettuce.StaticMasterReplica;
4545
import org.springframework.boot.data.redis.autoconfigure.RedisProperties.Pool;
4646
import org.springframework.boot.ssl.SslBundle;
4747
import org.springframework.boot.ssl.SslOptions;
@@ -145,14 +145,19 @@ private LettuceConnectionFactory createConnectionFactory(
145145
Assert.state(sentinelConfig != null, "'sentinelConfig' must not be null");
146146
yield new LettuceConnectionFactory(sentinelConfig, clientConfiguration);
147147
}
148+
case STATIC_MASTER_REPLICA -> {
149+
RedisStaticMasterReplicaConfiguration configuration = getStaticMasterReplicaConfiguration();
150+
Assert.state(configuration != null, "'staticMasterReplicaConfiguration' must not be null");
151+
yield new LettuceConnectionFactory(configuration, clientConfiguration);
152+
}
148153
};
149154
}
150155

151156
private @Nullable RedisStaticMasterReplicaConfiguration getStaticMasterReplicaConfiguration() {
152-
StaticMasterReplica staticMasterReplica = getProperties().getLettuce().getStaticMasterReplica();
157+
RedisProperties.Lettuce lettuce = getProperties().getLettuce();
153158

154-
if (!CollectionUtils.isEmpty(staticMasterReplica.getNodes())) {
155-
List<Node> nodes = asNodes(staticMasterReplica.getNodes());
159+
if (!CollectionUtils.isEmpty(lettuce.getNodes())) {
160+
List<Node> nodes = asNodes(lettuce.getNodes());
156161
RedisStaticMasterReplicaConfiguration configuration = new RedisStaticMasterReplicaConfiguration(
157162
nodes.get(0).host(), nodes.get(0).port());
158163
configuration.setUsername(getProperties().getUsername());

module/spring-boot-data-redis/src/main/java/org/springframework/boot/data/redis/autoconfigure/RedisConnectionConfiguration.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
3636
import org.springframework.util.Assert;
3737
import org.springframework.util.ClassUtils;
38+
import org.springframework.util.CollectionUtils;
3839

3940
/**
4041
* Base Redis connection configuration.
@@ -48,6 +49,7 @@
4849
* @author Andy Wilkinson
4950
* @author Phillip Webb
5051
* @author Yanming Zhou
52+
* @author Yong-Hyun Kim
5153
*/
5254
abstract class RedisConnectionConfiguration {
5355

@@ -156,7 +158,7 @@ protected final RedisProperties getProperties() {
156158

157159
protected @Nullable SslBundle getSslBundle() {
158160
return switch (this.mode) {
159-
case STANDALONE -> (this.connectionDetails.getStandalone() != null)
161+
case STANDALONE, STATIC_MASTER_REPLICA -> (this.connectionDetails.getStandalone() != null)
160162
? this.connectionDetails.getStandalone().getSslBundle() : null;
161163
case CLUSTER -> (this.connectionDetails.getCluster() != null)
162164
? this.connectionDetails.getCluster().getSslBundle() : null;
@@ -197,12 +199,15 @@ private Mode determineMode() {
197199
if (getClusterConfiguration() != null) {
198200
return Mode.CLUSTER;
199201
}
202+
if (!CollectionUtils.isEmpty(this.properties.getLettuce().getNodes())) {
203+
return Mode.STATIC_MASTER_REPLICA;
204+
}
200205
return Mode.STANDALONE;
201206
}
202207

203208
enum Mode {
204209

205-
STANDALONE, CLUSTER, SENTINEL
210+
STANDALONE, CLUSTER, SENTINEL, STATIC_MASTER_REPLICA
206211

207212
}
208213

module/spring-boot-data-redis/src/main/java/org/springframework/boot/data/redis/autoconfigure/RedisProperties.java

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,19 @@ public static class Lettuce {
483483

484484
private final Cluster cluster = new Cluster();
485485

486-
private final StaticMasterReplica staticMasterReplica = new StaticMasterReplica();
486+
/**
487+
* List of static master-replica "host:port" pairs regardless of role
488+
* as the actual roles are determined by querying each node's ROLE command.
489+
*/
490+
private @Nullable List<String> nodes;
491+
492+
public @Nullable List<String> getNodes() {
493+
return this.nodes;
494+
}
495+
496+
public void setNodes(@Nullable List<String> nodes) {
497+
this.nodes = nodes;
498+
}
487499

488500
public Duration getShutdownTimeout() {
489501
return this.shutdownTimeout;
@@ -509,10 +521,6 @@ public Cluster getCluster() {
509521
return this.cluster;
510522
}
511523

512-
public StaticMasterReplica getStaticMasterReplica() {
513-
return this.staticMasterReplica;
514-
}
515-
516524
public static class Cluster {
517525

518526
private final Refresh refresh = new Refresh();
@@ -569,27 +577,6 @@ public void setAdaptive(boolean adaptive) {
569577

570578
}
571579

572-
/**
573-
* Lettuce static master-replica properties.
574-
*/
575-
public static class StaticMasterReplica {
576-
577-
/**
578-
* List of "host:port" pairs regardless of role as the actual roles are
579-
* determined by querying each node's ROLE command.
580-
*/
581-
private @Nullable List<String> nodes;
582-
583-
public @Nullable List<String> getNodes() {
584-
return this.nodes;
585-
}
586-
587-
public void setNodes(@Nullable List<String> nodes) {
588-
this.nodes = nodes;
589-
}
590-
591-
}
592-
593580
}
594581

595582
}

0 commit comments

Comments
 (0)