Skip to content

Commit 34f53d4

Browse files
committed
Polish "Fix handling of Redis nodes with IPv6 addresses"
See gh-39819
1 parent 9b326d5 commit 34f53d4

File tree

5 files changed

+27
-27
lines changed

5 files changed

+27
-27
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/PropertiesRedisConnectionDetails.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/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::asString).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(RedisNode.fromString(node.asString()));
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-4
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.
@@ -185,9 +185,6 @@ interface Cluster {
185185
*/
186186
record Node(String host, int port) {
187187

188-
String asString() {
189-
return this.host + ":" + this.port;
190-
}
191188
}
192189

193190
}

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

+7-7
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
/**
@@ -122,9 +124,8 @@ void clusterIsConfigured() {
122124
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("localhost", 1111), new RedisConnectionDetails.Node("127.0.0.1", 2222),
127-
new RedisConnectionDetails.Node("[::1]", 3333));
127+
assertThat(connectionDetails.getCluster().getNodes()).containsExactly(new Node("localhost", 1111),
128+
new Node("127.0.0.1", 2222), new Node("[::1]", 3333));
128129
}
129130

130131
@Test
@@ -133,9 +134,8 @@ void sentinelIsConfigured() {
133134
sentinel.setNodes(List.of("localhost:1111", "127.0.0.1:2222", "[::1]:3333"));
134135
this.properties.setSentinel(sentinel);
135136
PropertiesRedisConnectionDetails connectionDetails = new PropertiesRedisConnectionDetails(this.properties);
136-
assertThat(connectionDetails.getSentinel().getNodes()).containsExactly(
137-
new RedisConnectionDetails.Node("localhost", 1111), new RedisConnectionDetails.Node("127.0.0.1", 2222),
138-
new RedisConnectionDetails.Node("[::1]", 3333));
137+
assertThat(connectionDetails.getSentinel().getNodes()).containsExactly(new Node("localhost", 1111),
138+
new Node("127.0.0.1", 2222), new Node("[::1]", 3333));
139139
}
140140

141141
}

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

+9-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.
@@ -309,12 +309,11 @@ void testRedisConfigurationWithIpv6Sentinel() {
309309
.withPropertyValues("spring.data.redis.sentinel.master:mymaster",
310310
"spring.data.redis.sentinel.nodes:" + StringUtils.collectionToCommaDelimitedString(sentinels))
311311
.run((context) -> {
312-
assertThat(context.getBean(LettuceConnectionFactory.class).isRedisSentinelAware()).isTrue();
313-
assertThat(context.getBean(LettuceConnectionFactory.class).getSentinelConfiguration()).isNotNull();
314-
assertThat(context.getBean(LettuceConnectionFactory.class).getSentinelConfiguration().getSentinels())
315-
.isNotNull()
316-
.extracting(RedisNode::toString)
317-
.containsExactlyInAnyOrder("[0:0:0:0:0:0:0:1]:26379", "[0:0:0:0:0:0:0:1]:26380");
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));
318317
});
319318
}
320319

@@ -409,10 +408,10 @@ void testRedisConfigurationWithCluster() {
409408
RedisClusterConfiguration clusterConfiguration = context.getBean(LettuceConnectionFactory.class)
410409
.getClusterConfiguration();
411410
assertThat(clusterConfiguration.getClusterNodes()).hasSize(3);
412-
assertThat(clusterConfiguration.getClusterNodes()).extracting(RedisNode::asString)
413-
.containsExactlyInAnyOrder("127.0.0.1:27379", "127.0.0.1:27380", "[::1]:27381");
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));
414414
});
415-
416415
}
417416

418417
@Test

0 commit comments

Comments
 (0)