diff --git a/common/src/main/java/com/alibaba/otter/canal/common/utils/CommonUtils.java b/common/src/main/java/com/alibaba/otter/canal/common/utils/CommonUtils.java index 52b9621a7c..0372449dfa 100644 --- a/common/src/main/java/com/alibaba/otter/canal/common/utils/CommonUtils.java +++ b/common/src/main/java/com/alibaba/otter/canal/common/utils/CommonUtils.java @@ -1,5 +1,7 @@ package com.alibaba.otter.canal.common.utils; +import org.apache.commons.lang.ArrayUtils; + import java.io.File; /** @@ -50,4 +52,24 @@ public static boolean deleteDir(File dirFile) { return dirFile.delete(); } + + + /** + * 拆分IP地址和端口号 + * + * @param text ip地址和端口号,ip和端口号以英文冒号(:)分隔; + * ipv4 127.0.0.1:3306 + * ipv6 [::1]:3306 + * @return + */ + public static String[] splitIPAndPort(String text) { + text = text.replace("[", "").replace("]", ""); + int idx = text.lastIndexOf(':'); + if (idx > 0) { + String ip = text.substring(0, idx); + String port = text.substring(idx + 1); + return new String[]{ip, port}; + } + return ArrayUtils.EMPTY_STRING_ARRAY; + } } diff --git a/connector/rabbitmq-connector/src/main/java/com/alibaba/otter/canal/connector/rabbitmq/consumer/CanalRabbitMQConsumer.java b/connector/rabbitmq-connector/src/main/java/com/alibaba/otter/canal/connector/rabbitmq/consumer/CanalRabbitMQConsumer.java index 5b7f774a5f..cc082aacc2 100644 --- a/connector/rabbitmq-connector/src/main/java/com/alibaba/otter/canal/connector/rabbitmq/consumer/CanalRabbitMQConsumer.java +++ b/connector/rabbitmq-connector/src/main/java/com/alibaba/otter/canal/connector/rabbitmq/consumer/CanalRabbitMQConsumer.java @@ -9,6 +9,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import com.alibaba.otter.canal.common.utils.CommonUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -88,7 +89,7 @@ public void connect() { } // 解析出端口 modified by 16075140 if (nameServer != null && nameServer.contains(":")) { - String[] serverHostAndPort = nameServer.split(":"); + String[] serverHostAndPort = CommonUtils.splitIPAndPort(nameServer); factory.setHost(serverHostAndPort[0]); factory.setPort(Integer.parseInt(serverHostAndPort[1])); } else { diff --git a/connector/rabbitmq-connector/src/main/java/com/alibaba/otter/canal/connector/rabbitmq/producer/CanalRabbitMQProducer.java b/connector/rabbitmq-connector/src/main/java/com/alibaba/otter/canal/connector/rabbitmq/producer/CanalRabbitMQProducer.java index a75434d44f..e0e16f2525 100644 --- a/connector/rabbitmq-connector/src/main/java/com/alibaba/otter/canal/connector/rabbitmq/producer/CanalRabbitMQProducer.java +++ b/connector/rabbitmq-connector/src/main/java/com/alibaba/otter/canal/connector/rabbitmq/producer/CanalRabbitMQProducer.java @@ -6,6 +6,7 @@ import java.util.Properties; import java.util.concurrent.TimeoutException; +import com.alibaba.otter.canal.common.utils.CommonUtils; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -52,7 +53,7 @@ public void init(Properties properties) { ConnectionFactory factory = new ConnectionFactory(); String servers = rabbitMQProperties.getHost(); if (servers.contains(":")) { - String[] serverHostAndPort = servers.split(":"); + String[] serverHostAndPort = CommonUtils.splitIPAndPort(servers); factory.setHost(serverHostAndPort[0]); factory.setPort(Integer.parseInt(serverHostAndPort[1])); } else { diff --git a/deployer/src/main/bin/startup.sh b/deployer/src/main/bin/startup.sh index 583c080a1a..df490189f9 100644 --- a/deployer/src/main/bin/startup.sh +++ b/deployer/src/main/bin/startup.sh @@ -102,7 +102,7 @@ else JAVA_OPTS="-server -Xms1024m -Xmx1024m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:MaxPermSize=128m $JAVA_OPTS" fi -JAVA_OPTS=" $JAVA_OPTS -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true -Dfile.encoding=UTF-8" +JAVA_OPTS=" $JAVA_OPTS -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true -Djava.net.preferIPv6Stack=true -Dfile.encoding=UTF-8" CANAL_OPTS="-DappName=otter-canal -Dlogback.configurationFile=$logback_configurationFile -Dcanal.conf=$canal_conf" if [ -e $canal_conf -a -e $logback_configurationFile ] diff --git a/instance/spring/src/main/java/com/alibaba/otter/canal/instance/spring/support/SocketAddressEditor.java b/instance/spring/src/main/java/com/alibaba/otter/canal/instance/spring/support/SocketAddressEditor.java index 878c01a4f8..b5c7bb8235 100644 --- a/instance/spring/src/main/java/com/alibaba/otter/canal/instance/spring/support/SocketAddressEditor.java +++ b/instance/spring/src/main/java/com/alibaba/otter/canal/instance/spring/support/SocketAddressEditor.java @@ -3,6 +3,7 @@ import java.beans.PropertyEditorSupport; import java.net.InetSocketAddress; +import com.alibaba.otter.canal.common.utils.CommonUtils; import org.apache.commons.lang.StringUtils; import org.springframework.beans.PropertyEditorRegistrar; import org.springframework.beans.PropertyEditorRegistry; @@ -14,7 +15,7 @@ public void registerCustomEditors(PropertyEditorRegistry registry) { } public void setAsText(String text) throws IllegalArgumentException { - String[] addresses = StringUtils.split(text, ":"); + String[] addresses = CommonUtils.splitIPAndPort(text); if (addresses.length > 0) { if (addresses.length != 2) { throw new RuntimeException("address[" + text + "] is illegal, eg.127.0.0.1:3306");