Fix handling of Redis nodes with IPv6 addresses
See gh-39819
This commit is contained in:
committed by
Andy Wilkinson
parent
ca4d64ed16
commit
9b326d59fe
@@ -113,8 +113,10 @@ class PropertiesRedisConnectionDetails implements RedisConnectionDetails {
|
||||
}
|
||||
|
||||
private Node asNode(String node) {
|
||||
String[] components = node.split(":");
|
||||
return new Node(components[0], Integer.parseInt(components[1]));
|
||||
int portSeparatorIndex = node.lastIndexOf(':');
|
||||
String host = node.substring(0, portSeparatorIndex);
|
||||
int port = Integer.parseInt(node.substring(portSeparatorIndex + 1));
|
||||
return new Node(host, port);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -139,7 +139,7 @@ abstract class RedisConnectionConfiguration {
|
||||
}
|
||||
|
||||
private List<String> getNodes(Cluster cluster) {
|
||||
return cluster.getNodes().stream().map((node) -> "%s:%d".formatted(node.host(), node.port())).toList();
|
||||
return cluster.getNodes().stream().map(Node::asString).toList();
|
||||
}
|
||||
|
||||
protected final RedisProperties getProperties() {
|
||||
@@ -162,7 +162,7 @@ abstract class RedisConnectionConfiguration {
|
||||
private List<RedisNode> createSentinels(Sentinel sentinel) {
|
||||
List<RedisNode> nodes = new ArrayList<>();
|
||||
for (Node node : sentinel.getNodes()) {
|
||||
nodes.add(new RedisNode(node.host(), node.port()));
|
||||
nodes.add(RedisNode.fromString(node.asString()));
|
||||
}
|
||||
return nodes;
|
||||
}
|
||||
|
||||
@@ -185,6 +185,9 @@ public interface RedisConnectionDetails extends ConnectionDetails {
|
||||
*/
|
||||
record Node(String host, int port) {
|
||||
|
||||
String asString() {
|
||||
return this.host + ":" + this.port;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -119,12 +119,23 @@ class PropertiesRedisConnectionDetailsTests {
|
||||
@Test
|
||||
void clusterIsConfigured() {
|
||||
RedisProperties.Cluster cluster = new RedisProperties.Cluster();
|
||||
cluster.setNodes(List.of("first:1111", "second:2222", "third:3333"));
|
||||
cluster.setNodes(List.of("localhost:1111", "127.0.0.1:2222", "[::1]:3333"));
|
||||
this.properties.setCluster(cluster);
|
||||
PropertiesRedisConnectionDetails connectionDetails = new PropertiesRedisConnectionDetails(this.properties);
|
||||
assertThat(connectionDetails.getCluster().getNodes()).containsExactly(
|
||||
new RedisConnectionDetails.Node("first", 1111), new RedisConnectionDetails.Node("second", 2222),
|
||||
new RedisConnectionDetails.Node("third", 3333));
|
||||
new RedisConnectionDetails.Node("localhost", 1111), new RedisConnectionDetails.Node("127.0.0.1", 2222),
|
||||
new RedisConnectionDetails.Node("[::1]", 3333));
|
||||
}
|
||||
|
||||
@Test
|
||||
void sentinelIsConfigured() {
|
||||
RedisProperties.Sentinel sentinel = new RedisProperties.Sentinel();
|
||||
sentinel.setNodes(List.of("localhost:1111", "127.0.0.1:2222", "[::1]:3333"));
|
||||
this.properties.setSentinel(sentinel);
|
||||
PropertiesRedisConnectionDetails connectionDetails = new PropertiesRedisConnectionDetails(this.properties);
|
||||
assertThat(connectionDetails.getSentinel().getNodes()).containsExactly(
|
||||
new RedisConnectionDetails.Node("localhost", 1111), new RedisConnectionDetails.Node("127.0.0.1", 2222),
|
||||
new RedisConnectionDetails.Node("[::1]", 3333));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -308,8 +308,14 @@ class RedisAutoConfigurationTests {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.data.redis.sentinel.master:mymaster",
|
||||
"spring.data.redis.sentinel.nodes:" + StringUtils.collectionToCommaDelimitedString(sentinels))
|
||||
.run((context) -> assertThat(context.getBean(LettuceConnectionFactory.class).isRedisSentinelAware())
|
||||
.isTrue());
|
||||
.run((context) -> {
|
||||
assertThat(context.getBean(LettuceConnectionFactory.class).isRedisSentinelAware()).isTrue();
|
||||
assertThat(context.getBean(LettuceConnectionFactory.class).getSentinelConfiguration()).isNotNull();
|
||||
assertThat(context.getBean(LettuceConnectionFactory.class).getSentinelConfiguration().getSentinels())
|
||||
.isNotNull()
|
||||
.extracting(RedisNode::toString)
|
||||
.containsExactlyInAnyOrder("[0:0:0:0:0:0:0:1]:26379", "[0:0:0:0:0:0:0:1]:26380");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -394,17 +400,17 @@ class RedisAutoConfigurationTests {
|
||||
|
||||
@Test
|
||||
void testRedisConfigurationWithCluster() {
|
||||
List<String> clusterNodes = Arrays.asList("127.0.0.1:27379", "127.0.0.1:27380");
|
||||
List<String> clusterNodes = Arrays.asList("127.0.0.1:27379", "127.0.0.1:27380", "[::1]:27381");
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.data.redis.cluster.nodes[0]:" + clusterNodes.get(0),
|
||||
"spring.data.redis.cluster.nodes[1]:" + clusterNodes.get(1))
|
||||
"spring.data.redis.cluster.nodes[1]:" + clusterNodes.get(1),
|
||||
"spring.data.redis.cluster.nodes[2]:" + clusterNodes.get(2))
|
||||
.run((context) -> {
|
||||
RedisClusterConfiguration clusterConfiguration = context.getBean(LettuceConnectionFactory.class)
|
||||
.getClusterConfiguration();
|
||||
assertThat(clusterConfiguration.getClusterNodes()).hasSize(2);
|
||||
assertThat(clusterConfiguration.getClusterNodes())
|
||||
.extracting((node) -> node.getHost() + ":" + node.getPort())
|
||||
.containsExactlyInAnyOrder("127.0.0.1:27379", "127.0.0.1:27380");
|
||||
assertThat(clusterConfiguration.getClusterNodes()).hasSize(3);
|
||||
assertThat(clusterConfiguration.getClusterNodes()).extracting(RedisNode::asString)
|
||||
.containsExactlyInAnyOrder("127.0.0.1:27379", "127.0.0.1:27380", "[::1]:27381");
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user