added URIBuilder

pull/894/head
Rui Gu 8 years ago
parent e63fe6fd6c
commit 8477f15640

@ -37,6 +37,7 @@ import org.redisson.connection.RedisClientEntry;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.FutureListener;
import org.redisson.misc.URIBuilder;
/**
*
@ -55,7 +56,7 @@ public class RedisNodes<N extends Node> implements NodesGroup<N> {
@Override
public N getNode(String address) {
Collection<N> clients = (Collection<N>) connectionManager.getClients();
URI uri = URI.create(address);
URI uri = URIBuilder.create(address);
InetSocketAddress addr = new InetSocketAddress(uri.getHost(), uri.getPort());
for (N node : clients) {
if (node.getAddr().equals(addr)) {

@ -44,6 +44,7 @@ import io.netty.util.Timer;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.FutureListener;
import io.netty.util.concurrent.GlobalEventExecutor;
import org.redisson.misc.URIBuilder;
/**
* Low-level Redis client
@ -101,7 +102,7 @@ public class RedisClient {
*/
@Deprecated
public RedisClient(String address) {
this(URI.create(address));
this(URIBuilder.create(address));
}
/*

@ -26,6 +26,7 @@ import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.Timer;
import org.redisson.misc.URIBuilder;
/**
*
@ -56,11 +57,11 @@ public class RedisClientConfig {
private String sslKeystorePassword;
public RedisClientConfig setAddress(String host, int port) {
this.address = URI.create("redis://" + host + ":" + port);
this.address = URIBuilder.create("redis://" + host + ":" + port);
return this;
}
public RedisClientConfig setAddress(String address) {
this.address = URI.create(address);
this.address = URIBuilder.create(address);
return this;
}
public RedisClientConfig setAddress(URI address) {

@ -17,6 +17,7 @@ package org.redisson.client;
import java.net.InetSocketAddress;
import java.net.URI;
import org.redisson.misc.URIBuilder;
/**
*
@ -27,12 +28,12 @@ public class RedisRedirectException extends RedisException {
private static final long serialVersionUID = 181505625075250011L;
private int slot;
private URI url;
private final int slot;
private final URI url;
public RedisRedirectException(int slot, String url) {
this.slot = slot;
this.url = URI.create("//" + url);
this.url = URIBuilder.create("//" + url);
}
public int getSlot() {

@ -18,6 +18,7 @@ package org.redisson.cluster;
import java.net.URI;
import java.util.HashSet;
import java.util.Set;
import org.redisson.misc.URIBuilder;
/**
*
@ -52,7 +53,7 @@ public class ClusterNodeInfo {
return address;
}
public void setAddress(String address) {
this.address = URI.create(address);
this.address = URIBuilder.create(address);
}
public void addSlotRange(ClusterSlotRange range) {

@ -18,6 +18,7 @@ package org.redisson.config;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import org.redisson.misc.URIBuilder;
/**
*
@ -53,7 +54,7 @@ public class ClusterServersConfig extends BaseMasterSlaveServersConfig<ClusterSe
*/
public ClusterServersConfig addNodeAddress(String ... addresses) {
for (String address : addresses) {
nodeAddresses.add(URI.create(address));
nodeAddresses.add(URIBuilder.create(address));
}
return this;
}

@ -18,6 +18,7 @@ package org.redisson.config;
import java.net.URI;
import java.util.HashSet;
import java.util.Set;
import org.redisson.misc.URIBuilder;
/**
*
@ -60,7 +61,7 @@ public class MasterSlaveServersConfig extends BaseMasterSlaveServersConfig<Maste
*/
public MasterSlaveServersConfig setMasterAddress(String masterAddress) {
if (masterAddress != null) {
this.masterAddress = URI.create(masterAddress);
this.masterAddress = URIBuilder.create(masterAddress);
}
return this;
}
@ -85,7 +86,7 @@ public class MasterSlaveServersConfig extends BaseMasterSlaveServersConfig<Maste
*/
public MasterSlaveServersConfig addSlaveAddress(String ... addresses) {
for (String address : addresses) {
slaveAddresses.add(URI.create(address));
slaveAddresses.add(URIBuilder.create(address));
}
return this;
}

@ -18,6 +18,7 @@ package org.redisson.config;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import org.redisson.misc.URIBuilder;
/**
* Configuration for an Azure Redis Cache or AWS ElastiCache servers.
@ -61,7 +62,7 @@ public class ReplicatedServersConfig extends BaseMasterSlaveServersConfig<Replic
*/
public ReplicatedServersConfig addNodeAddress(String ... addresses) {
for (String address : addresses) {
nodeAddresses.add(URI.create(address));
nodeAddresses.add(URIBuilder.create(address));
}
return this;
}

@ -18,6 +18,7 @@ package org.redisson.config;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import org.redisson.misc.URIBuilder;
/**
*
@ -67,7 +68,7 @@ public class SentinelServersConfig extends BaseMasterSlaveServersConfig<Sentinel
*/
public SentinelServersConfig addSentinelAddress(String ... addresses) {
for (String address : addresses) {
sentinelAddresses.add(URI.create(address));
sentinelAddresses.add(URIBuilder.create(address));
}
return this;
}

@ -16,6 +16,7 @@
package org.redisson.config;
import java.net.URI;
import org.redisson.misc.URIBuilder;
/**
*
@ -125,7 +126,7 @@ public class SingleServerConfig extends BaseConfig<SingleServerConfig> {
*/
public SingleServerConfig setAddress(String address) {
if (address != null) {
this.address = URI.create(address);
this.address = URIBuilder.create(address);
}
return this;
}

@ -48,6 +48,7 @@ import org.slf4j.LoggerFactory;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.FutureListener;
import io.netty.util.internal.PlatformDependent;
import org.redisson.misc.URIBuilder;
/**
*
@ -104,7 +105,7 @@ public class SentinelConnectionManager extends MasterSlaveConnectionManager {
log.info("slave: {} added", host);
if (flags.contains("s_down") || flags.contains("disconnected")) {
URI uri = URI.create(host);
URI uri = URIBuilder.create(host);
disconnectedSlaves.add(uri);
log.warn("slave: {} is down", host);
}
@ -208,7 +209,7 @@ public class SentinelConnectionManager extends MasterSlaveConnectionManager {
String port = parts[3];
String addr = "redis://" + ip + ":" + port;
URI uri = URI.create(addr);
URI uri = URIBuilder.create(addr);
registerSentinel(cfg, uri, c);
}
}
@ -230,7 +231,7 @@ public class SentinelConnectionManager extends MasterSlaveConnectionManager {
// to avoid addition twice
if (slaves.putIfAbsent(slaveAddr, true) == null) {
final MasterSlaveEntry entry = getEntry(singleSlotRange.getStartSlot());
RFuture<Void> future = entry.addSlave(URI.create(slaveAddr));
RFuture<Void> future = entry.addSlave(URIBuilder.create(slaveAddr));
future.addListener(new FutureListener<Void>() {
@Override
public void operationComplete(Future<Void> future) throws Exception {
@ -372,7 +373,7 @@ public class SentinelConnectionManager extends MasterSlaveConnectionManager {
String newMaster = "redis://" + ip + ":" + port;
if (!newMaster.equals(current)
&& currentMaster.compareAndSet(current, newMaster)) {
changeMaster(singleSlotRange.getStartSlot(), URI.create(newMaster));
changeMaster(singleSlotRange.getStartSlot(), URIBuilder.create(newMaster));
log.info("master {} changed to {}", current, newMaster);
}
}

@ -30,6 +30,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import org.redisson.connection.ClientConnectionsEntry;
import io.netty.util.internal.PlatformDependent;
import org.redisson.misc.URIBuilder;
/**
* Weighted Round Robin balancer.
@ -77,7 +78,7 @@ public class WeightedRoundRobinBalancer implements LoadBalancer {
*/
public WeightedRoundRobinBalancer(Map<String, Integer> weights, int defaultWeight) {
for (Entry<String, Integer> entry : weights.entrySet()) {
URI uri = URI.create(entry.getKey());
URI uri = URIBuilder.create(entry.getKey());
InetSocketAddress addr = new InetSocketAddress(uri.getHost(), uri.getPort());
if (entry.getValue() <= 0) {
throw new IllegalArgumentException("Weight can't be less than or equal zero");

@ -0,0 +1,40 @@
/**
* Copyright 2016 Nikita Koksharov
*
* 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.redisson.misc;
import java.net.URI;
/**
*
* @author Rui Gu (https://github.com/jackygurui)
*/
public class URIBuilder {
public static URI create(String uri) {
URI u = URI.create(uri);
//Let's assuming most of the time it is OK.
if (u.getHost() != null) {
return u;
}
String s = uri.substring(0, uri.lastIndexOf(":"))
.replaceFirst("redis://", "")
.replaceFirst("rediss://", "");
//Assuming this is an IPv6 format, other situations will be handled by
//Netty at a later stage.
return URI.create(uri.replace(s, "[" + s + "]"));
}
}
Loading…
Cancel
Save