Skip to content

Commit 1476d8d

Browse files
committed
Merge pull request #39819 from tobi-laa
* gh-39819: Polish "Fix handling of Redis nodes with IPv6 addresses" Fix handling of Redis nodes with IPv6 addresses Closes gh-39819
2 parents ca4d64e + 34f53d4 commit 1476d8d

File tree

5 files changed

+46
-24
lines changed

5 files changed

+46
-24
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/PropertiesRedisConnectionDetails.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -113,8 +113,10 @@ public Cluster getCluster() {
113113
}
114114

115115
private Node asNode(String node) {
116-
String[] components = node.split(":");
117-
return new Node(components[0], Integer.parseInt(components[1]));
116+
int portSeparatorIndex = node.lastIndexOf(':');
117+
String host = node.substring(0, portSeparatorIndex);
118+
int port = Integer.parseInt(node.substring(portSeparatorIndex + 1));
119+
return new Node(host, port);
118120
}
119121

120122
}

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

+9-5
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ protected final RedisClusterConfiguration getClusterConfiguration() {
123123
}
124124
RedisProperties.Cluster clusterProperties = this.properties.getCluster();
125125
if (this.connectionDetails.getCluster() != null) {
126-
RedisClusterConfiguration config = new RedisClusterConfiguration(
127-
getNodes(this.connectionDetails.getCluster()));
126+
RedisClusterConfiguration config = new RedisClusterConfiguration();
127+
config.setClusterNodes(getNodes(this.connectionDetails.getCluster()));
128128
if (clusterProperties != null && clusterProperties.getMaxRedirects() != null) {
129129
config.setMaxRedirects(clusterProperties.getMaxRedirects());
130130
}
@@ -138,8 +138,12 @@ protected final RedisClusterConfiguration getClusterConfiguration() {
138138
return null;
139139
}
140140

141-
private List<String> getNodes(Cluster cluster) {
142-
return cluster.getNodes().stream().map((node) -> "%s:%d".formatted(node.host(), node.port())).toList();
141+
private List<RedisNode> getNodes(Cluster cluster) {
142+
return cluster.getNodes().stream().map(this::asRedisNode).toList();
143+
}
144+
145+
private RedisNode asRedisNode(Node node) {
146+
return new RedisNode(node.host(), node.port());
143147
}
144148

145149
protected final RedisProperties getProperties() {
@@ -162,7 +166,7 @@ protected boolean isPoolEnabled(Pool pool) {
162166
private List<RedisNode> createSentinels(Sentinel sentinel) {
163167
List<RedisNode> nodes = new ArrayList<>();
164168
for (Node node : sentinel.getNodes()) {
165-
nodes.add(new RedisNode(node.host(), node.port()));
169+
nodes.add(asRedisNode(node));
166170
}
167171
return nodes;
168172
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisConnectionDetails.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/PropertiesRedisConnectionDetailsTests.java

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,8 @@
2020

2121
import org.junit.jupiter.api.Test;
2222

23+
import org.springframework.boot.autoconfigure.data.redis.RedisConnectionDetails.Node;
24+
2325
import static org.assertj.core.api.Assertions.assertThat;
2426

2527
/**
@@ -119,12 +121,21 @@ void standaloneIsConfiguredFromProperties() {
119121
@Test
120122
void clusterIsConfigured() {
121123
RedisProperties.Cluster cluster = new RedisProperties.Cluster();
122-
cluster.setNodes(List.of("first:1111", "second:2222", "third:3333"));
124+
cluster.setNodes(List.of("localhost:1111", "127.0.0.1:2222", "[::1]:3333"));
123125
this.properties.setCluster(cluster);
124126
PropertiesRedisConnectionDetails connectionDetails = new PropertiesRedisConnectionDetails(this.properties);
125-
assertThat(connectionDetails.getCluster().getNodes()).containsExactly(
126-
new RedisConnectionDetails.Node("first", 1111), new RedisConnectionDetails.Node("second", 2222),
127-
new RedisConnectionDetails.Node("third", 3333));
127+
assertThat(connectionDetails.getCluster().getNodes()).containsExactly(new Node("localhost", 1111),
128+
new Node("127.0.0.1", 2222), new Node("[::1]", 3333));
129+
}
130+
131+
@Test
132+
void sentinelIsConfigured() {
133+
RedisProperties.Sentinel sentinel = new RedisProperties.Sentinel();
134+
sentinel.setNodes(List.of("localhost:1111", "127.0.0.1:2222", "[::1]:3333"));
135+
this.properties.setSentinel(sentinel);
136+
PropertiesRedisConnectionDetails connectionDetails = new PropertiesRedisConnectionDetails(this.properties);
137+
assertThat(connectionDetails.getSentinel().getNodes()).containsExactly(new Node("localhost", 1111),
138+
new Node("127.0.0.1", 2222), new Node("[::1]", 3333));
128139
}
129140

130141
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationTests.java

+15-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -308,8 +308,13 @@ void testRedisConfigurationWithIpv6Sentinel() {
308308
this.contextRunner
309309
.withPropertyValues("spring.data.redis.sentinel.master:mymaster",
310310
"spring.data.redis.sentinel.nodes:" + StringUtils.collectionToCommaDelimitedString(sentinels))
311-
.run((context) -> assertThat(context.getBean(LettuceConnectionFactory.class).isRedisSentinelAware())
312-
.isTrue());
311+
.run((context) -> {
312+
LettuceConnectionFactory connectionFactory = context.getBean(LettuceConnectionFactory.class);
313+
assertThat(connectionFactory.isRedisSentinelAware()).isTrue();
314+
assertThat(connectionFactory.getSentinelConfiguration().getSentinels()).isNotNull()
315+
.containsExactlyInAnyOrder(new RedisNode("[0:0:0:0:0:0:0:1]", 26379),
316+
new RedisNode("[0:0:0:0:0:0:0:1]", 26380));
317+
});
313318
}
314319

315320
@Test
@@ -394,19 +399,19 @@ void testRedisSentinelUrlConfiguration() {
394399

395400
@Test
396401
void testRedisConfigurationWithCluster() {
397-
List<String> clusterNodes = Arrays.asList("127.0.0.1:27379", "127.0.0.1:27380");
402+
List<String> clusterNodes = Arrays.asList("127.0.0.1:27379", "127.0.0.1:27380", "[::1]:27381");
398403
this.contextRunner
399404
.withPropertyValues("spring.data.redis.cluster.nodes[0]:" + clusterNodes.get(0),
400-
"spring.data.redis.cluster.nodes[1]:" + clusterNodes.get(1))
405+
"spring.data.redis.cluster.nodes[1]:" + clusterNodes.get(1),
406+
"spring.data.redis.cluster.nodes[2]:" + clusterNodes.get(2))
401407
.run((context) -> {
402408
RedisClusterConfiguration clusterConfiguration = context.getBean(LettuceConnectionFactory.class)
403409
.getClusterConfiguration();
404-
assertThat(clusterConfiguration.getClusterNodes()).hasSize(2);
405-
assertThat(clusterConfiguration.getClusterNodes())
406-
.extracting((node) -> node.getHost() + ":" + node.getPort())
407-
.containsExactlyInAnyOrder("127.0.0.1:27379", "127.0.0.1:27380");
410+
assertThat(clusterConfiguration.getClusterNodes()).hasSize(3);
411+
assertThat(clusterConfiguration.getClusterNodes()).containsExactlyInAnyOrder(
412+
new RedisNode("127.0.0.1", 27379), new RedisNode("127.0.0.1", 27380),
413+
new RedisNode("[::1]", 27381));
408414
});
409-
410415
}
411416

412417
@Test

0 commit comments

Comments
 (0)