forked from spring-projects/spring-data-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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: spring-projects#306
- Loading branch information
1 parent
c156c82
commit bb990b1
Showing
5 changed files
with
440 additions
and
3 deletions.
There are no files selected for viewing
149 changes: 149 additions & 0 deletions
149
src/main/java/org/springframework/data/redis/connection/RedisElastiCacheConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/* | ||
* 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 java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* Configuration class used for setting up {@link RedisConnection} via {@link RedisConnectionFactory} using connecting | ||
* to <a href="https://aws.amazon.com/documentation/elasticache/">AWS ElastiCache with Read Replicas</a> . | ||
* | ||
* @author Mark Paluch | ||
* @since 2.1 | ||
*/ | ||
public class RedisElastiCacheConfiguration { | ||
|
||
private static final int DEFAULT_PORT = 6379; | ||
|
||
private List<RedisStandaloneConfiguration> nodes = new ArrayList<>(); | ||
private int database; | ||
private RedisPassword password = RedisPassword.none(); | ||
|
||
/** | ||
* Create a new {@link RedisElastiCacheConfiguration} given {@code hostName}. | ||
* | ||
* @param hostName must not be {@literal null} or empty. | ||
*/ | ||
public RedisElastiCacheConfiguration(String hostName) { | ||
this(hostName, DEFAULT_PORT); | ||
} | ||
|
||
/** | ||
* Create a new {@link RedisElastiCacheConfiguration} given {@code hostName} and {@code port}. | ||
* | ||
* @param hostName must not be {@literal null} or empty. | ||
* @param port a valid TCP port (1-65535). | ||
*/ | ||
public RedisElastiCacheConfiguration(String hostName, int port) { | ||
addNode(hostName, port); | ||
} | ||
|
||
/** | ||
* Add a {@link RedisStandaloneConfiguration node} to the list of nodes given {@code hostName}. | ||
* | ||
* @param hostName must not be {@literal null} or empty. | ||
* @param port a valid TCP port (1-65535). | ||
*/ | ||
public void addNode(String hostName, int port) { | ||
addNode(new RedisStandaloneConfiguration(hostName, port)); | ||
} | ||
|
||
/** | ||
* Add a {@link RedisStandaloneConfiguration node} to the list of nodes. | ||
* | ||
* @param node must not be {@literal null}. | ||
*/ | ||
private void addNode(RedisStandaloneConfiguration node) { | ||
|
||
Assert.notNull(node, "RedisStandaloneConfiguration must not be null!"); | ||
|
||
node.setPassword(password); | ||
node.setDatabase(database); | ||
nodes.add(node); | ||
} | ||
|
||
/** | ||
* Add a {@link RedisStandaloneConfiguration node} to the list of nodes given {@code hostName}. | ||
* | ||
* @param hostName must not be {@literal null} or empty. | ||
* @return {@code this} {@link RedisElastiCacheConfiguration}. | ||
*/ | ||
public RedisElastiCacheConfiguration node(String hostName) { | ||
return node(hostName, DEFAULT_PORT); | ||
} | ||
|
||
/** | ||
* Add a {@link RedisStandaloneConfiguration node} to the list of nodes given {@code hostName} and {@code port}. | ||
* | ||
* @param hostName must not be {@literal null} or empty. | ||
* @param port a valid TCP port (1-65535). | ||
* @return {@code this} {@link RedisElastiCacheConfiguration}. | ||
*/ | ||
public RedisElastiCacheConfiguration node(String hostName, int port) { | ||
|
||
addNode(hostName, port); | ||
return this; | ||
} | ||
|
||
/** | ||
* @return the db index. | ||
*/ | ||
public int getDatabase() { | ||
return database; | ||
} | ||
|
||
/** | ||
* Sets the index of the database used by this connection factory. Default is 0. | ||
* | ||
* @param index database index. | ||
*/ | ||
public void setDatabase(int index) { | ||
|
||
Assert.isTrue(index >= 0, () -> String.format("Invalid DB index '%s' (a positive index required)", index)); | ||
|
||
this.database = index; | ||
this.nodes.forEach(it -> it.setDatabase(database)); | ||
} | ||
|
||
/** | ||
* @return never {@literal null}. | ||
*/ | ||
public RedisPassword getPassword() { | ||
return password; | ||
} | ||
|
||
/** | ||
* @param password must not be {@literal null}. | ||
*/ | ||
public void setPassword(RedisPassword password) { | ||
|
||
Assert.notNull(password, "RedisPassword must not be null!"); | ||
|
||
this.password = password; | ||
this.nodes.forEach(it -> it.setPassword(password)); | ||
} | ||
|
||
/** | ||
* @return list of {@link RedisStandaloneConfiguration nodes}. | ||
*/ | ||
public List<RedisStandaloneConfiguration> getNodes() { | ||
return Collections.unmodifiableList(new ArrayList<>(nodes)); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
...java/org/springframework/data/redis/connection/lettuce/ElastiCacheConnectionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* 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.lettuce; | ||
|
||
import io.lettuce.core.ReadFrom; | ||
import io.lettuce.core.RedisClient; | ||
import io.lettuce.core.RedisURI; | ||
import io.lettuce.core.api.StatefulConnection; | ||
import io.lettuce.core.codec.RedisCodec; | ||
import io.lettuce.core.masterslave.MasterSlave; | ||
import io.lettuce.core.masterslave.StatefulRedisMasterSlaveConnection; | ||
|
||
import java.util.Collection; | ||
import java.util.Optional; | ||
|
||
import org.springframework.lang.Nullable; | ||
|
||
/** | ||
* {@link LettuceConnectionProvider} implementation for a AWS ElastiCache with replicas setup. <br/> | ||
* Lettuce auto-discovers node roles from the static {@link RedisURI} collection. | ||
* | ||
* @author Mark Paluch | ||
* @since 2.1 | ||
*/ | ||
class ElastiCacheConnectionProvider implements LettuceConnectionProvider { | ||
|
||
private final RedisClient client; | ||
private final RedisCodec<?, ?> codec; | ||
private final Optional<ReadFrom> readFrom; | ||
private final Collection<RedisURI> nodes; | ||
|
||
/** | ||
* Create new {@link ElastiCacheConnectionProvider}. | ||
* | ||
* @param client must not be {@literal null}. | ||
* @param codec must not be {@literal null}. | ||
* @param nodes must not be {@literal null}. | ||
* @param readFrom can be {@literal null}. | ||
*/ | ||
ElastiCacheConnectionProvider(RedisClient client, RedisCodec<?, ?> codec, Collection<RedisURI> nodes, | ||
@Nullable ReadFrom readFrom) { | ||
|
||
this.client = client; | ||
this.codec = codec; | ||
this.readFrom = Optional.ofNullable(readFrom); | ||
this.nodes = nodes; | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.springframework.data.redis.connection.lettuce.LettuceConnectionProvider#getConnection(java.lang.Class) | ||
*/ | ||
@Override | ||
public <T extends StatefulConnection<?, ?>> T getConnection(Class<T> connectionType) { | ||
|
||
if (StatefulConnection.class.isAssignableFrom(connectionType)) { | ||
|
||
StatefulRedisMasterSlaveConnection<?, ?> connection = MasterSlave.connect(client, codec, nodes); | ||
readFrom.ifPresent(connection::setReadFrom); | ||
|
||
return connectionType.cast(connection); | ||
} | ||
|
||
throw new UnsupportedOperationException("Connection type " + connectionType + " not supported!"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.