refactoring

pull/6384/head
Nikita Koksharov 4 weeks ago
parent 621efdd3b1
commit fd131ce1a3

@ -20,6 +20,7 @@ import org.redisson.api.RedissonClient;
import org.redisson.api.RedissonReactiveClient; import org.redisson.api.RedissonReactiveClient;
import org.redisson.api.RedissonRxClient; import org.redisson.api.RedissonRxClient;
import org.redisson.config.*; import org.redisson.config.*;
import org.redisson.misc.RedisURI;
import org.redisson.spring.data.connection.RedissonConnectionFactory; import org.redisson.spring.data.connection.RedissonConnectionFactory;
import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -69,9 +70,6 @@ import java.util.List;
@EnableConfigurationProperties({RedissonProperties.class, RedisProperties.class}) @EnableConfigurationProperties({RedissonProperties.class, RedisProperties.class})
public class RedissonAutoConfiguration { public class RedissonAutoConfiguration {
private static final String REDIS_PROTOCOL_PREFIX = "redis://";
private static final String REDISS_PROTOCOL_PREFIX = "rediss://";
@Autowired(required = false) @Autowired(required = false)
private List<RedissonAutoConfigurationCustomizer> redissonAutoConfigurationCustomizers; private List<RedissonAutoConfigurationCustomizer> redissonAutoConfigurationCustomizers;
@ -358,12 +356,12 @@ public class RedissonAutoConfiguration {
} }
private String getPrefix() { private String getPrefix() {
String prefix = REDIS_PROTOCOL_PREFIX; String prefix = RedisURI.REDIS_PROTOCOL;
Method isSSLMethod = ReflectionUtils.findMethod(RedisProperties.class, "isSsl"); Method isSSLMethod = ReflectionUtils.findMethod(RedisProperties.class, "isSsl");
Method getSSLMethod = ReflectionUtils.findMethod(RedisProperties.class, "getSsl"); Method getSSLMethod = ReflectionUtils.findMethod(RedisProperties.class, "getSsl");
if (isSSLMethod != null) { if (isSSLMethod != null) {
if ((Boolean) ReflectionUtils.invokeMethod(isSSLMethod, redisProperties)) { if ((Boolean) ReflectionUtils.invokeMethod(isSSLMethod, redisProperties)) {
prefix = REDISS_PROTOCOL_PREFIX; prefix = RedisURI.REDIS_SSL_PROTOCOL;
} }
} else if (getSSLMethod != null) { } else if (getSSLMethod != null) {
Object ss = ReflectionUtils.invokeMethod(getSSLMethod, redisProperties); Object ss = ReflectionUtils.invokeMethod(getSSLMethod, redisProperties);
@ -371,7 +369,7 @@ public class RedissonAutoConfiguration {
Method isEnabledMethod = ReflectionUtils.findMethod(ss.getClass(), "isEnabled"); Method isEnabledMethod = ReflectionUtils.findMethod(ss.getClass(), "isEnabled");
Boolean enabled = (Boolean) ReflectionUtils.invokeMethod(isEnabledMethod, ss); Boolean enabled = (Boolean) ReflectionUtils.invokeMethod(isEnabledMethod, ss);
if (enabled) { if (enabled) {
prefix = REDISS_PROTOCOL_PREFIX; prefix = RedisURI.REDIS_SSL_PROTOCOL;
} }
} }
} }
@ -395,7 +393,7 @@ public class RedissonAutoConfiguration {
private String[] convert(String prefix, List<String> nodesObject) { private String[] convert(String prefix, List<String> nodesObject) {
List<String> nodes = new ArrayList<>(nodesObject.size()); List<String> nodes = new ArrayList<>(nodesObject.size());
for (String node : nodesObject) { for (String node : nodesObject) {
if (!node.startsWith(REDIS_PROTOCOL_PREFIX) && !node.startsWith(REDISS_PROTOCOL_PREFIX)) { if (!RedisURI.isValid(node)) {
nodes.add(prefix + node); nodes.add(prefix + node);
} else { } else {
nodes.add(node); nodes.add(node);

@ -62,7 +62,7 @@ public class RedisClusterNodeDecoder implements Decoder<List<RedisClusterNode>>
RedisURI address = null; RedisURI address = null;
if (!flags.contains(Flag.NOADDR)) { if (!flags.contains(Flag.NOADDR)) {
String addr = params[1].split("@")[0]; String addr = params[1].split("@")[0];
address = new RedisURI("redis://" + addr); address = new RedisURI(RedisURI.REDIS_PROTOCOL + addr);
} }
String masterId = params[3]; String masterId = params[3];

@ -62,7 +62,7 @@ public class RedisClusterNodeDecoder implements Decoder<List<RedisClusterNode>>
RedisURI address = null; RedisURI address = null;
if (!flags.contains(Flag.NOADDR)) { if (!flags.contains(Flag.NOADDR)) {
String addr = params[1].split("@")[0]; String addr = params[1].split("@")[0];
address = new RedisURI("redis://" + addr); address = new RedisURI(RedisURI.REDIS_PROTOCOL + addr);
} }
String masterId = params[3]; String masterId = params[3];

@ -62,7 +62,7 @@ public class RedisClusterNodeDecoder implements Decoder<List<RedisClusterNode>>
RedisURI address = null; RedisURI address = null;
if (!flags.contains(Flag.NOADDR)) { if (!flags.contains(Flag.NOADDR)) {
String addr = params[1].split("@")[0]; String addr = params[1].split("@")[0];
address = new RedisURI("redis://" + addr); address = new RedisURI(RedisURI.REDIS_PROTOCOL + addr);
} }
String masterId = params[3]; String masterId = params[3];

@ -62,7 +62,7 @@ public class RedisClusterNodeDecoder implements Decoder<List<RedisClusterNode>>
RedisURI address = null; RedisURI address = null;
if (!flags.contains(Flag.NOADDR)) { if (!flags.contains(Flag.NOADDR)) {
String addr = params[1].split("@")[0]; String addr = params[1].split("@")[0];
address = new RedisURI("redis://" + addr); address = new RedisURI(RedisURI.REDIS_PROTOCOL + addr);
} }
String masterId = params[3]; String masterId = params[3];

@ -62,7 +62,7 @@ public class RedisClusterNodeDecoder implements Decoder<List<RedisClusterNode>>
RedisURI address = null; RedisURI address = null;
if (!flags.contains(Flag.NOADDR)) { if (!flags.contains(Flag.NOADDR)) {
String addr = params[1].split("@")[0]; String addr = params[1].split("@")[0];
address = new RedisURI("redis://" + addr); address = new RedisURI(RedisURI.REDIS_PROTOCOL + addr);
} }
String masterId = params[3]; String masterId = params[3];

@ -62,7 +62,7 @@ public class RedisClusterNodeDecoder implements Decoder<List<RedisClusterNode>>
RedisURI address = null; RedisURI address = null;
if (!flags.contains(Flag.NOADDR)) { if (!flags.contains(Flag.NOADDR)) {
String addr = params[1].split("@")[0]; String addr = params[1].split("@")[0];
address = new RedisURI("redis://" + addr); address = new RedisURI(RedisURI.REDIS_PROTOCOL + addr);
} }
String masterId = params[3]; String masterId = params[3];

@ -62,7 +62,7 @@ public class RedisClusterNodeDecoder implements Decoder<List<RedisClusterNode>>
RedisURI address = null; RedisURI address = null;
if (!flags.contains(Flag.NOADDR)) { if (!flags.contains(Flag.NOADDR)) {
String addr = params[1].split("@")[0]; String addr = params[1].split("@")[0];
address = new RedisURI("redis://" + addr); address = new RedisURI(RedisURI.REDIS_PROTOCOL + addr);
} }
String masterId = params[3]; String masterId = params[3];

@ -62,7 +62,7 @@ public class RedisClusterNodeDecoder implements Decoder<List<RedisClusterNode>>
RedisURI address = null; RedisURI address = null;
if (!flags.contains(Flag.NOADDR)) { if (!flags.contains(Flag.NOADDR)) {
String addr = params[1].split("@")[0]; String addr = params[1].split("@")[0];
address = new RedisURI("redis://" + addr); address = new RedisURI(RedisURI.REDIS_PROTOCOL + addr);
} }
String masterId = params[3]; String masterId = params[3];

@ -62,7 +62,7 @@ public class RedisClusterNodeDecoder implements Decoder<List<RedisClusterNode>>
RedisURI address = null; RedisURI address = null;
if (!flags.contains(Flag.NOADDR)) { if (!flags.contains(Flag.NOADDR)) {
String addr = params[1].split("@")[0]; String addr = params[1].split("@")[0];
address = new RedisURI("redis://" + addr); address = new RedisURI(RedisURI.REDIS_PROTOCOL + addr);
} }
String masterId = params[3]; String masterId = params[3];

@ -62,7 +62,7 @@ public class RedisClusterNodeDecoder implements Decoder<List<RedisClusterNode>>
RedisURI address = null; RedisURI address = null;
if (!flags.contains(Flag.NOADDR)) { if (!flags.contains(Flag.NOADDR)) {
String addr = params[1].split("@")[0]; String addr = params[1].split("@")[0];
address = new RedisURI("redis://" + addr); address = new RedisURI(RedisURI.REDIS_PROTOCOL + addr);
} }
String masterId = params[3]; String masterId = params[3];

@ -63,7 +63,7 @@ public class RedisClusterNodeDecoder implements Decoder<List<RedisClusterNode>>
RedisURI address = null; RedisURI address = null;
if (!flags.contains(Flag.NOADDR)) { if (!flags.contains(Flag.NOADDR)) {
String addr = params[1].split("@")[0]; String addr = params[1].split("@")[0];
address = new RedisURI("redis://" + addr); address = new RedisURI(RedisURI.REDIS_PROTOCOL + addr);
} }
String masterId = params[3]; String masterId = params[3];

@ -63,7 +63,7 @@ public class RedisClusterNodeDecoder implements Decoder<List<RedisClusterNode>>
RedisURI address = null; RedisURI address = null;
if (!flags.contains(Flag.NOADDR)) { if (!flags.contains(Flag.NOADDR)) {
String addr = params[1].split("@")[0]; String addr = params[1].split("@")[0];
address = new RedisURI("redis://" + addr); address = new RedisURI(RedisURI.REDIS_PROTOCOL + addr);
} }
String masterId = params[3]; String masterId = params[3];

@ -71,7 +71,7 @@ public class RedisClusterNodeDecoder implements Decoder<List<RedisClusterNode>>
continue; continue;
} }
address = new RedisURI("redis://" + addr); address = new RedisURI(RedisURI.REDIS_PROTOCOL + addr);
address = serviceManager.toURI("redis", address.getHost(), String.valueOf(address.getPort())); address = serviceManager.toURI("redis", address.getHost(), String.valueOf(address.getPort()));
} }

@ -71,7 +71,7 @@ public class RedisClusterNodeDecoder implements Decoder<List<RedisClusterNode>>
continue; continue;
} }
address = new RedisURI("redis://" + addr); address = new RedisURI(RedisURI.REDIS_PROTOCOL + addr);
address = serviceManager.toURI("redis", address.getHost(), String.valueOf(address.getPort())); address = serviceManager.toURI("redis", address.getHost(), String.valueOf(address.getPort()));
} }

@ -71,7 +71,7 @@ public class RedisClusterNodeDecoder implements Decoder<List<RedisClusterNode>>
continue; continue;
} }
address = new RedisURI("redis://" + addr); address = new RedisURI(RedisURI.REDIS_PROTOCOL + addr);
address = serviceManager.toURI("redis", address.getHost(), String.valueOf(address.getPort())); address = serviceManager.toURI("redis", address.getHost(), String.valueOf(address.getPort()));
} }

@ -150,7 +150,7 @@ public class RedisClientConfig {
} }
public RedisClientConfig setAddress(String host, int port) { public RedisClientConfig setAddress(String host, int port) {
this.address = new RedisURI("redis://" + host + ":" + port); this.address = new RedisURI(RedisURI.REDIS_PROTOCOL + host + ":" + port);
return this; return this;
} }
public RedisClientConfig setAddress(String address) { public RedisClientConfig setAddress(String address) {

@ -741,9 +741,9 @@ public interface RedisCommands {
Arrays.asList(HSCAN.getName(), SCAN.getName(), ZSCAN.getName(), SSCAN.getName())); Arrays.asList(HSCAN.getName(), SCAN.getName(), ZSCAN.getName(), SSCAN.getName()));
RedisStrictCommand<List<ClusterNodeInfo>> CLUSTER_NODES = new RedisStrictCommand<List<ClusterNodeInfo>>("CLUSTER", "NODES", RedisStrictCommand<List<ClusterNodeInfo>> CLUSTER_NODES = new RedisStrictCommand<List<ClusterNodeInfo>>("CLUSTER", "NODES",
new ObjectDecoder(new ClusterNodesDecoder(false))); new ObjectDecoder(new ClusterNodesDecoder(RedisURI.REDIS_PROTOCOL)));
RedisStrictCommand<List<ClusterNodeInfo>> CLUSTER_NODES_SSL = new RedisStrictCommand<List<ClusterNodeInfo>>("CLUSTER", "NODES", RedisStrictCommand<List<ClusterNodeInfo>> CLUSTER_NODES_SSL = new RedisStrictCommand<List<ClusterNodeInfo>>("CLUSTER", "NODES",
new ObjectDecoder(new ClusterNodesDecoder(true))); new ObjectDecoder(new ClusterNodesDecoder(RedisURI.REDIS_SSL_PROTOCOL)));
RedisStrictCommand<Long> TIME_LONG = new RedisStrictCommand<Long>("TIME", new TimeLongObjectDecoder()); RedisStrictCommand<Long> TIME_LONG = new RedisStrictCommand<Long>("TIME", new TimeLongObjectDecoder());
RedisStrictCommand<Time> TIME = new RedisStrictCommand<Time>("TIME", new TimeObjectDecoder()); RedisStrictCommand<Time> TIME = new RedisStrictCommand<Time>("TIME", new TimeObjectDecoder());
RedisStrictCommand<Map<String, String>> CLUSTER_INFO = new RedisStrictCommand<Map<String, String>>("CLUSTER", "INFO", new StringMapDataDecoder()); RedisStrictCommand<Map<String, String>> CLUSTER_INFO = new RedisStrictCommand<Map<String, String>>("CLUSTER", "INFO", new StringMapDataDecoder());

@ -34,11 +34,11 @@ import java.util.List;
*/ */
public class ClusterNodesDecoder implements Decoder<List<ClusterNodeInfo>> { public class ClusterNodesDecoder implements Decoder<List<ClusterNodeInfo>> {
private final boolean ssl; private final String protocol;
public ClusterNodesDecoder(boolean ssl) { public ClusterNodesDecoder(String protocol) {
super(); super();
this.ssl = ssl; this.protocol = protocol;
} }
@Override @Override
@ -97,11 +97,6 @@ public class ClusterNodesDecoder implements Decoder<List<ClusterNodeInfo>> {
} }
private String createUri(String[] params) { private String createUri(String[] params) {
String protocol = "redis://";
if (ssl) {
protocol = "rediss://";
}
String[] parts = params[1].split(","); String[] parts = params[1].split(",");
String addr = parts[0].split("@")[0]; String addr = parts[0].split("@")[0];
String name = addr.substring(0, addr.lastIndexOf(":")); String name = addr.substring(0, addr.lastIndexOf(":"));

@ -32,6 +32,9 @@ import java.util.Objects;
*/ */
public final class RedisURI { public final class RedisURI {
public static final String REDIS_PROTOCOL= "redis://";
public static final String REDIS_SSL_PROTOCOL = "rediss://";
private final boolean ssl; private final boolean ssl;
private final String host; private final String host;
private final int port; private final int port;
@ -39,6 +42,10 @@ public final class RedisURI {
private String password; private String password;
private int hashCode; private int hashCode;
public static boolean isValid(String url) {
return url.startsWith(REDIS_PROTOCOL) || url.startsWith(REDIS_SSL_PROTOCOL);
}
public RedisURI(String scheme, String host, int port) { public RedisURI(String scheme, String host, int port) {
this.ssl = "rediss".equals(scheme); this.ssl = "rediss".equals(scheme);
this.host = host; this.host = host;
@ -47,7 +54,7 @@ public final class RedisURI {
} }
public RedisURI(String uri) { public RedisURI(String uri) {
if (!uri.startsWith("redis://") && !uri.startsWith("rediss://")) { if (!isValid(uri)) {
throw new IllegalArgumentException("Redis url should start with redis:// or rediss:// (for SSL connection)"); throw new IllegalArgumentException("Redis url should start with redis:// or rediss:// (for SSL connection)");
} }

@ -9,12 +9,13 @@ import org.redisson.cluster.ClusterNodeInfo;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import org.redisson.misc.RedisURI;
public class ClusterNodesDecoderTest { public class ClusterNodesDecoderTest {
@Test @Test
public void testIPs() throws IOException { public void testIPs() throws IOException {
ClusterNodesDecoder decoder = new ClusterNodesDecoder(false); ClusterNodesDecoder decoder = new ClusterNodesDecoder(RedisURI.REDIS_PROTOCOL);
ByteBuf buf = Unpooled.buffer(); ByteBuf buf = Unpooled.buffer();
String info = "7af253f8c20a3b3fbd481801bd361ec6643c6f0b 192.168.234.129:7001@17001 master - 0 1478865073260 8 connected 5461-10922\n" + String info = "7af253f8c20a3b3fbd481801bd361ec6643c6f0b 192.168.234.129:7001@17001 master - 0 1478865073260 8 connected 5461-10922\n" +
@ -34,7 +35,7 @@ public class ClusterNodesDecoderTest {
@Test @Test
public void testHostnames() throws IOException { public void testHostnames() throws IOException {
ClusterNodesDecoder decoder = new ClusterNodesDecoder(false); ClusterNodesDecoder decoder = new ClusterNodesDecoder(RedisURI.REDIS_PROTOCOL);
ByteBuf buf = Unpooled.buffer(); ByteBuf buf = Unpooled.buffer();
String info = "7af253f8c20a3b3fbd481801bd361ec6643c6f0b 192.168.234.129:7001@17001,hostname1 master - 0 1478865073260 8 connected 5461-10922\n" + String info = "7af253f8c20a3b3fbd481801bd361ec6643c6f0b 192.168.234.129:7001@17001,hostname1 master - 0 1478865073260 8 connected 5461-10922\n" +

Loading…
Cancel
Save