diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/PropertiesRedisConnectionDetails.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/PropertiesRedisConnectionDetails.java index 72e2c9efe46..458baa93819 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/PropertiesRedisConnectionDetails.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/PropertiesRedisConnectionDetails.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -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); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisConnectionConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisConnectionConfiguration.java index e3d59342d8e..b64347ab22d 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisConnectionConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisConnectionConfiguration.java @@ -123,8 +123,8 @@ abstract class RedisConnectionConfiguration { } RedisProperties.Cluster clusterProperties = this.properties.getCluster(); if (this.connectionDetails.getCluster() != null) { - RedisClusterConfiguration config = new RedisClusterConfiguration( - getNodes(this.connectionDetails.getCluster())); + RedisClusterConfiguration config = new RedisClusterConfiguration(); + config.setClusterNodes(getNodes(this.connectionDetails.getCluster())); if (clusterProperties != null && clusterProperties.getMaxRedirects() != null) { config.setMaxRedirects(clusterProperties.getMaxRedirects()); } @@ -138,8 +138,12 @@ abstract class RedisConnectionConfiguration { return null; } - private List getNodes(Cluster cluster) { - return cluster.getNodes().stream().map((node) -> "%s:%d".formatted(node.host(), node.port())).toList(); + private List getNodes(Cluster cluster) { + return cluster.getNodes().stream().map(this::asRedisNode).toList(); + } + + private RedisNode asRedisNode(Node node) { + return new RedisNode(node.host(), node.port()); } protected final RedisProperties getProperties() { @@ -162,7 +166,7 @@ abstract class RedisConnectionConfiguration { private List createSentinels(Sentinel sentinel) { List nodes = new ArrayList<>(); for (Node node : sentinel.getNodes()) { - nodes.add(new RedisNode(node.host(), node.port())); + nodes.add(asRedisNode(node)); } return nodes; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisConnectionDetails.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisConnectionDetails.java index 7aed831582c..b423e61ab2e 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisConnectionDetails.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisConnectionDetails.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/PropertiesRedisConnectionDetailsTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/PropertiesRedisConnectionDetailsTests.java index e4c325e5241..4d3aa55ab84 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/PropertiesRedisConnectionDetailsTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/PropertiesRedisConnectionDetailsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,8 @@ import java.util.List; import org.junit.jupiter.api.Test; +import org.springframework.boot.autoconfigure.data.redis.RedisConnectionDetails.Node; + import static org.assertj.core.api.Assertions.assertThat; /** @@ -119,12 +121,21 @@ 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)); + assertThat(connectionDetails.getCluster().getNodes()).containsExactly(new Node("localhost", 1111), + new Node("127.0.0.1", 2222), new 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 Node("localhost", 1111), + new Node("127.0.0.1", 2222), new Node("[::1]", 3333)); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationTests.java index 79d33e99f30..8e6dc52f51f 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -312,8 +312,13 @@ 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) -> { + LettuceConnectionFactory connectionFactory = context.getBean(LettuceConnectionFactory.class); + assertThat(connectionFactory.isRedisSentinelAware()).isTrue(); + assertThat(connectionFactory.getSentinelConfiguration().getSentinels()).isNotNull() + .containsExactlyInAnyOrder(new RedisNode("[0:0:0:0:0:0:0:1]", 26379), + new RedisNode("[0:0:0:0:0:0:0:1]", 26380)); + }); } @Test @@ -398,19 +403,19 @@ class RedisAutoConfigurationTests { @Test void testRedisConfigurationWithCluster() { - List clusterNodes = Arrays.asList("127.0.0.1:27379", "127.0.0.1:27380"); + List 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()).containsExactlyInAnyOrder( + new RedisNode("127.0.0.1", 27379), new RedisNode("127.0.0.1", 27380), + new RedisNode("[::1]", 27381)); }); - } @Test