diff --git a/redisson/src/main/java/org/redisson/config/ConfigSupport.java b/redisson/src/main/java/org/redisson/config/ConfigSupport.java index 05a8cddbe..8a7cf8677 100644 --- a/redisson/src/main/java/org/redisson/config/ConfigSupport.java +++ b/redisson/src/main/java/org/redisson/config/ConfigSupport.java @@ -19,6 +19,9 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.Reader; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; import java.net.URI; import java.net.URL; import java.util.List; @@ -121,62 +124,101 @@ public class ConfigSupport { private ObjectMapper jsonMapper = createMapper(null, null); private ObjectMapper yamlMapper = createMapper(new YAMLFactory(), null); + private void patchUriObject() throws IOException { + patchUriField("lowMask", "L_DASH"); + patchUriField("highMask", "H_DASH"); + } + + private void patchUriField(String methodName, String fieldName) + throws IOException { + try { + Method lowMask = URI.class.getDeclaredMethod(methodName, String.class); + lowMask.setAccessible(true); + long lowMaskValue = (long) lowMask.invoke(null, "-_"); + + Field lowDash = URI.class.getDeclaredField(fieldName); + + Field modifiers = Field.class.getDeclaredField("modifiers"); + modifiers.setAccessible(true); + modifiers.setInt(lowDash, lowDash.getModifiers() & ~Modifier.FINAL); + + lowDash.setAccessible(true); + lowDash.setLong(null, lowMaskValue); + } catch (Exception e) { + throw new IOException(e); + } + } + public T fromJSON(String content, Class configType) throws IOException { + patchUriObject(); return jsonMapper.readValue(content, configType); } public T fromJSON(File file, Class configType) throws IOException { + patchUriObject(); return fromJSON(file, configType, null); } public T fromJSON(File file, Class configType, ClassLoader classLoader) throws IOException { + patchUriObject(); jsonMapper = createMapper(null, classLoader); return jsonMapper.readValue(file, configType); } public T fromJSON(URL url, Class configType) throws IOException { + patchUriObject(); return jsonMapper.readValue(url, configType); } public T fromJSON(Reader reader, Class configType) throws IOException { + patchUriObject(); return jsonMapper.readValue(reader, configType); } public T fromJSON(InputStream inputStream, Class configType) throws IOException { + patchUriObject(); return jsonMapper.readValue(inputStream, configType); } public String toJSON(Config config) throws IOException { + patchUriObject(); return jsonMapper.writeValueAsString(config); } public T fromYAML(String content, Class configType) throws IOException { + patchUriObject(); return yamlMapper.readValue(content, configType); } public T fromYAML(File file, Class configType) throws IOException { + patchUriObject(); return yamlMapper.readValue(file, configType); } public T fromYAML(File file, Class configType, ClassLoader classLoader) throws IOException { + patchUriObject(); yamlMapper = createMapper(new YAMLFactory(), classLoader); return yamlMapper.readValue(file, configType); } public T fromYAML(URL url, Class configType) throws IOException { + patchUriObject(); return yamlMapper.readValue(url, configType); } public T fromYAML(Reader reader, Class configType) throws IOException { + patchUriObject(); return yamlMapper.readValue(reader, configType); } public T fromYAML(InputStream inputStream, Class configType) throws IOException { + patchUriObject(); return yamlMapper.readValue(inputStream, configType); } public String toYAML(Config config) throws IOException { + patchUriObject(); return yamlMapper.writeValueAsString(config); } diff --git a/redisson/src/main/java/org/redisson/connection/SentinelConnectionManager.java b/redisson/src/main/java/org/redisson/connection/SentinelConnectionManager.java index 261041b9c..2cbb7e64e 100755 --- a/redisson/src/main/java/org/redisson/connection/SentinelConnectionManager.java +++ b/redisson/src/main/java/org/redisson/connection/SentinelConnectionManager.java @@ -80,7 +80,7 @@ public class SentinelConnectionManager extends MasterSlaveConnectionManager { // TODO async List master = connection.sync(RedisCommands.SENTINEL_GET_MASTER_ADDR_BY_NAME, cfg.getMasterName()); - String masterHost = "redis://" + master.get(0) + ":" + master.get(1); + String masterHost = createAddress(master.get(0), master.get(1)); this.config.setMasterAddress(masterHost); currentMaster.set(masterHost); log.info("master: {} added", masterHost); @@ -96,7 +96,7 @@ public class SentinelConnectionManager extends MasterSlaveConnectionManager { String port = map.get("port"); String flags = map.get("flags"); - String host = "redis://" + ip + ":" + port; + String host = createAddress(ip, port); this.config.addSlaveAddress(host); slaves.put(host, true); @@ -132,6 +132,13 @@ public class SentinelConnectionManager extends MasterSlaveConnectionManager { future.awaitUninterruptibly(); } } + + private String createAddress(String host, Object port) { + if (host.contains(":")) { + host = "[" + host + "]"; + } + return "redis://" + host + ":" + port; + } @Override protected MasterSlaveEntry createMasterSlaveEntry(MasterSlaveServersConfig config, @@ -207,7 +214,7 @@ public class SentinelConnectionManager extends MasterSlaveConnectionManager { String ip = parts[2]; String port = parts[3]; - String addr = "redis://" + ip + ":" + port; + String addr = createAddress(ip, port); URI uri = URI.create(addr); registerSentinel(cfg, uri, c); } @@ -221,7 +228,7 @@ public class SentinelConnectionManager extends MasterSlaveConnectionManager { final String ip = parts[2]; final String port = parts[3]; - final String slaveAddr = "redis://" + ip + ":" + port; + final String slaveAddr = createAddress(ip, port); if (!isUseSameMaster(parts)) { return; @@ -309,7 +316,7 @@ public class SentinelConnectionManager extends MasterSlaveConnectionManager { String slaveAddr = ip + ":" + port; String master = currentMaster.get(); - String slaveMaster = "redis://" + parts[6] + ":" + parts[7]; + String slaveMaster = createAddress(parts[6], parts[7]); if (!master.equals(slaveMaster)) { log.warn("Skipped slave up {} for master {} differs from current {}", slaveAddr, slaveMaster, master); return false; @@ -369,7 +376,7 @@ public class SentinelConnectionManager extends MasterSlaveConnectionManager { String port = parts[4]; String current = currentMaster.get(); - String newMaster = "redis://" + ip + ":" + port; + String newMaster = createAddress(ip, port); if (!newMaster.equals(current) && currentMaster.compareAndSet(current, newMaster)) { changeMaster(singleSlotRange.getStartSlot(), URI.create(newMaster));