DATAREDIS-762 - Add AWS ElastiCache configuration.

We now provide RedisElastiCacheConfiguration to configure connections to AWS ElastiCache for Redis services with optional read replicas.

RedisElastiCacheConfiguration elastiCache = new RedisElastiCacheConfiguration("redis-node-1.elasticache.aws.com")
                                                   .node("redis-node-2.elasticache.aws.com");

LettuceConnectionFactory factory = new LettuceConnectionFactory(elastiCache, configuration);

Original Pull Request: #306
This commit is contained in:
Mark Paluch
2018-01-30 15:31:06 +01:00
committed by Christoph Strobl
parent c156c82cf0
commit bb990b14ae
5 changed files with 440 additions and 3 deletions

View File

@@ -0,0 +1,83 @@
/*
* Copyright 2018 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.connection;
import static org.assertj.core.api.Assertions.*;
import org.junit.Test;
/**
* Unit tests for {@link RedisElastiCacheConfiguration}.
*
* @author Mark Paluch
*/
public class RedisElastiCacheConfigurationUnitTests {
@Test // DATAREDIS-762
public void shouldCreateSingleHostConfiguration() {
RedisElastiCacheConfiguration singleHost = new RedisElastiCacheConfiguration("localhost");
assertThat(singleHost.getNodes()).hasSize(1);
RedisStandaloneConfiguration node = singleHost.getNodes().get(0);
assertThat(node.getHostName()).isEqualToIgnoringCase("localhost");
assertThat(node.getPort()).isEqualTo(6379);
}
@Test // DATAREDIS-762
public void shouldCreateMultiHostConfiguration() {
RedisElastiCacheConfiguration multiHost = new RedisElastiCacheConfiguration("localhost");
multiHost.node("other-host", 6479);
assertThat(multiHost.getNodes()).hasSize(2);
RedisStandaloneConfiguration firstNode = multiHost.getNodes().get(0);
assertThat(firstNode.getHostName()).isEqualToIgnoringCase("localhost");
assertThat(firstNode.getPort()).isEqualTo(6379);
RedisStandaloneConfiguration secondNode = multiHost.getNodes().get(1);
assertThat(secondNode.getHostName()).isEqualToIgnoringCase("other-host");
assertThat(secondNode.getPort()).isEqualTo(6479);
}
@Test // DATAREDIS-762
public void shouldApplyPasswordToNodes() {
RedisElastiCacheConfiguration multiHost = new RedisElastiCacheConfiguration("localhost").node("other-host", 6479);
multiHost.setPassword(RedisPassword.of("foobar"));
multiHost.node("third", 1234);
assertThat(multiHost.getNodes()).extracting("password").containsExactly(RedisPassword.of("foobar"),
RedisPassword.of("foobar"), RedisPassword.of("foobar"));
}
@Test // DATAREDIS-762
public void shouldApplyDatabaseToNodes() {
RedisElastiCacheConfiguration multiHost = new RedisElastiCacheConfiguration("localhost").node("other-host", 6479);
multiHost.setDatabase(4);
multiHost.node("third", 1234);
assertThat(multiHost.getNodes()).extracting("database").containsExactly(4, 4, 4);
}
}

View File

@@ -15,9 +15,7 @@
*/
package org.springframework.data.redis.connection.lettuce;
import static org.hamcrest.core.Is.*;
import static org.hamcrest.core.IsEqual.*;
import static org.hamcrest.core.IsNull.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.junit.Assume.*;
@@ -43,6 +41,7 @@ import org.springframework.data.redis.RedisSystemException;
import org.springframework.data.redis.SettingsUtils;
import org.springframework.data.redis.connection.DefaultStringRedisConnection;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisElastiCacheConfiguration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.StringRedisConnection;
@@ -386,6 +385,65 @@ public class LettuceConnectionFactoryTests {
factory.destroy();
}
@Test // DATAREDIS-762
public void factoryUsesElastiCacheMasterSlaveConnections() {
assumeThat(String.format("No slaves connected to %s:%s.", SettingsUtils.getHost(), SettingsUtils.getPort()),
connection.info("replication").getProperty("connected_slaves", "0").compareTo("0") > 0, is(true));
LettuceClientConfiguration configuration = LettuceTestClientConfiguration.builder().readFrom(ReadFrom.SLAVE)
.build();
RedisElastiCacheConfiguration elastiCache = new RedisElastiCacheConfiguration(SettingsUtils.getHost())
.node(SettingsUtils.getHost(), SettingsUtils.getPort() + 1);
LettuceConnectionFactory factory = new LettuceConnectionFactory(elastiCache,
configuration);
factory.afterPropertiesSet();
RedisConnection connection = factory.getConnection();
try {
assertThat(connection.ping(), is(equalTo("PONG")));
assertThat(connection.info().getProperty("role"), is(equalTo("slave")));
} finally {
connection.close();
}
factory.destroy();
}
@Test // DATAREDIS-762
public void factoryUsesElastiCacheMasterWithoutMaster() {
assumeThat(String.format("No slaves connected to %s:%s.", SettingsUtils.getHost(), SettingsUtils.getPort()),
connection.info("replication").getProperty("connected_slaves", "0").compareTo("0") > 0, is(true));
LettuceClientConfiguration configuration = LettuceTestClientConfiguration.builder().readFrom(ReadFrom.MASTER)
.build();
RedisElastiCacheConfiguration elastiCache = new RedisElastiCacheConfiguration(SettingsUtils.getHost(),
SettingsUtils.getPort() + 1);
LettuceConnectionFactory factory = new LettuceConnectionFactory(elastiCache, configuration);
factory.afterPropertiesSet();
RedisConnection connection = factory.getConnection();
try {
connection.ping();
fail("Expected RedisException: Master is currently unknown");
} catch (RedisSystemException e) {
assertThat(e.getCause(), is(instanceOf(RedisException.class)));
assertThat(e.getCause().getMessage(), containsString("Master is currently unknown"));
} finally {
connection.close();
}
factory.destroy();
}
@Test // DATAREDIS-580
public void factoryUsesMasterSlaveConnections() {