Use a String[] so sential nodes are trimmed

Update `RedisProperties` to use an actual String[] for sentinal nodes
rather than a simple String. This allows us to lean on the updated
binder to automatically trim the elements.

Fixes gh-11029
This commit is contained in:
Phillip Webb 2017-11-15 16:19:59 -08:00
parent fd5c43cdc9
commit 3035df59cc
4 changed files with 25 additions and 11 deletions

View File

@ -111,8 +111,7 @@ abstract class RedisConnectionConfiguration {
private List<RedisNode> createSentinels(RedisProperties.Sentinel sentinel) {
List<RedisNode> nodes = new ArrayList<>();
for (String node : StringUtils
.commaDelimitedListToStringArray(sentinel.getNodes())) {
for (String node : sentinel.getNodes()) {
try {
String[] parts = StringUtils.split(node, ":");
Assert.state(parts.length == 2, "Must be defined as 'host:port'");

View File

@ -266,9 +266,9 @@ public class RedisProperties {
private String master;
/**
* Comma-separated list of host:port pairs.
* Nodes as host:port pairs.
*/
private String nodes;
private String[] nodes;
public String getMaster() {
return this.master;
@ -278,11 +278,11 @@ public class RedisProperties {
this.master = master;
}
public String getNodes() {
public String[] getNodes() {
return this.nodes;
}
public void setNodes(String nodes) {
public void setNodes(String[] nodes) {
this.nodes = nodes;
}

View File

@ -18,6 +18,8 @@ package org.springframework.boot.autoconfigure.data.redis;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.junit.After;
import org.junit.Test;
@ -26,6 +28,7 @@ import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration.LettuceClientConfigurationBuilder;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
@ -153,12 +156,15 @@ public class RedisAutoConfigurationTests {
@Test
public void testRedisConfigurationWithSentinelAndPassword() throws Exception {
List<String> sentinels = Arrays.asList("127.0.0.1:26379", "127.0.0.1:26380");
load("spring.redis.password=password", "spring.redis.sentinel.master:mymaster",
"spring.redis.sentinel.nodes:"
+ StringUtils.collectionToCommaDelimitedString(sentinels));
assertThat(this.context.getBean(LettuceConnectionFactory.class).getPassword())
.isEqualTo("password");
"spring.redis.sentinel.nodes:127.0.0.1:26379, 127.0.0.1:26380");
LettuceConnectionFactory connectionFactory = this.context
.getBean(LettuceConnectionFactory.class);
assertThat(connectionFactory.getPassword()).isEqualTo("password");
Set<RedisNode> sentinels = connectionFactory.getSentinelConfiguration()
.getSentinels();
assertThat(sentinels.stream().map(Object::toString).collect(Collectors.toSet()))
.contains("127.0.0.1:26379", "127.0.0.1:26380");
}
@Test

View File

@ -270,4 +270,13 @@ public class ArrayBinderTests {
assertThat(result).isNotNull().isEmpty();
}
@Test
public void bindToArrayWhenHasSpacesShouldTrim() throws Exception {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo", "1, 2,3");
this.sources.add(source);
String[] result = this.binder.bind("foo", Bindable.of(String[].class)).get();
assertThat(result).containsExactly("1", "2", "3");
}
}