-
Notifications
You must be signed in to change notification settings - Fork 947
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extend RedisClusterClient and RedisClusterFactoryBean to take multipl…
…e hosts #401 RedisClusterClient and RedisClusterFactoryBean can now be initialized by taking multiple hosts within one URI. RedisClusterClient.create("redis+tls://password@host1:6379,host2:6380")
- Loading branch information
Showing
7 changed files
with
353 additions
and
74 deletions.
There are no files selected for viewing
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
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
81 changes: 81 additions & 0 deletions
81
src/main/java/com/lambdaworks/redis/cluster/RedisClusterURIUtil.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,81 @@ | ||
/* | ||
* Copyright 2016 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 com.lambdaworks.redis.cluster; | ||
|
||
import java.net.URI; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.lambdaworks.redis.RedisURI; | ||
import com.lambdaworks.redis.internal.HostAndPort; | ||
|
||
/** | ||
* {@link RedisClusterURIUtil} is a collection of {@link RedisURI}-based utility methods for {@link RedisClusterClient} use. | ||
* | ||
* @author Mark Paluch | ||
* @since 4.4 | ||
*/ | ||
public class RedisClusterURIUtil { | ||
|
||
/** | ||
* Parse a Redis Cluster URI with potentially multiple hosts into a {@link List} of {@link RedisURI}. | ||
* | ||
* An URI follows the syntax: {@code redis://[password@]host[:port][,host2[:port2]]} | ||
* | ||
* @param uri must not be empty or {@literal null}. | ||
* @return {@link List} of {@link RedisURI}. | ||
*/ | ||
public static List<RedisURI> toRedisURIs(URI uri) { | ||
|
||
RedisURI redisURI = RedisURI.create(uri); | ||
|
||
String[] parts = redisURI.getHost().split("\\,"); | ||
|
||
List<RedisURI> redisURIs = new ArrayList<>(parts.length); | ||
|
||
for (String part : parts) { | ||
HostAndPort hostAndPort = HostAndPort.parse(part); | ||
|
||
RedisURI nodeUri = RedisURI.create(hostAndPort.getHostText(), | ||
hostAndPort.hasPort() ? hostAndPort.getPort() : redisURI.getPort()); | ||
|
||
applyUriConnectionSettings(redisURI, nodeUri); | ||
|
||
redisURIs.add(nodeUri); | ||
} | ||
|
||
return redisURIs; | ||
} | ||
|
||
/** | ||
* Apply {@link RedisURI} settings such as SSL/Timeout/password. | ||
* | ||
* @param from from {@link RedisURI}. | ||
* @param to from {@link RedisURI}. | ||
*/ | ||
static void applyUriConnectionSettings(RedisURI from, RedisURI to) { | ||
|
||
if (from.getPassword() != null && from.getPassword().length != 0) { | ||
to.setPassword(new String(from.getPassword())); | ||
} | ||
|
||
to.setTimeout(from.getTimeout()); | ||
to.setUnit(from.getUnit()); | ||
to.setSsl(from.isSsl()); | ||
to.setStartTls(from.isStartTls()); | ||
to.setVerifyPeer(from.isVerifyPeer()); | ||
} | ||
} |
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.