From 601f0b4609747c750b0ef70582aadf46e235c8e4 Mon Sep 17 00:00:00 2001 From: qq213539 <213539@qq.com> Date: Tue, 8 Sep 2020 10:07:42 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=BD=BF=E7=94=A8=E4=BA=86?= =?UTF-8?q?=E7=B1=BB=E4=BC=BC=20ip.indexOf(":")=20=E6=9F=A5=E6=89=BE?= =?UTF-8?q?=E6=98=AF=E5=90=A6=E6=9C=89=E7=AB=AF=E5=8F=A3=E7=9A=84=E5=9C=B0?= =?UTF-8?q?=E6=96=B9=E7=9A=84=E9=80=BB=E8=BE=91,=20=E4=B8=80=E4=BA=9B=20":?= =?UTF-8?q?"=20=E6=9B=BF=E6=8D=A2=E4=B8=BA=E5=B8=B8=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AddressServerGeneratorManager.java | 12 ++++----- .../constant/AddressServerConstants.java | 5 ---- .../client/config/impl/ServerListManager.java | 7 ++--- .../nacos/client/naming/net/NamingProxy.java | 5 ++-- .../client/naming/utils/UtilAndComs.java | 2 -- .../nacos/common/utils/IpUtilTest.java | 26 +++++++++++++++++++ .../consistency/persistent/raft/RaftCore.java | 2 +- .../persistent/raft/RaftProxy.java | 6 ++--- .../nacos/naming/misc/NamingProxy.java | 6 ++--- .../alibaba/nacos/naming/misc/NetUtils.java | 3 ++- .../nacos/naming/misc/UtilsAndCommons.java | 2 -- 11 files changed, 47 insertions(+), 29 deletions(-) diff --git a/address/src/main/java/com/alibaba/nacos/address/component/AddressServerGeneratorManager.java b/address/src/main/java/com/alibaba/nacos/address/component/AddressServerGeneratorManager.java index 32fb0b88fed..fbec81b8f81 100644 --- a/address/src/main/java/com/alibaba/nacos/address/component/AddressServerGeneratorManager.java +++ b/address/src/main/java/com/alibaba/nacos/address/component/AddressServerGeneratorManager.java @@ -18,6 +18,7 @@ import com.alibaba.nacos.address.constant.AddressServerConstants; import com.alibaba.nacos.api.common.Constants; +import com.alibaba.nacos.common.utils.IpUtil; import com.alibaba.nacos.naming.core.Instance; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; @@ -85,14 +86,11 @@ public List generateInstancesByIps(String serviceName, String rawProdu } private String[] generateIpAndPort(String ip) { - - int index = ip.indexOf(AddressServerConstants.IP_PORT_SEPARATOR); - if (index != -1) { - - return new String[] {ip.substring(0, index), ip.substring(index + 1)}; + String[] result = IpUtil.splitIpPortStr(ip); + if (result.length != IpUtil.SPLIT_IP_PORT_RESULT_LENGTH) { + return new String[] {result[0], String.valueOf(AddressServerConstants.DEFAULT_SERVER_PORT)}; } - - return new String[] {ip, String.valueOf(AddressServerConstants.DEFAULT_SERVER_PORT)}; + return result; } /** diff --git a/address/src/main/java/com/alibaba/nacos/address/constant/AddressServerConstants.java b/address/src/main/java/com/alibaba/nacos/address/constant/AddressServerConstants.java index fc565dd806f..31741f56b5f 100644 --- a/address/src/main/java/com/alibaba/nacos/address/constant/AddressServerConstants.java +++ b/address/src/main/java/com/alibaba/nacos/address/constant/AddressServerConstants.java @@ -38,11 +38,6 @@ public interface AddressServerConstants { */ String DEFAULT_PRODUCT = "nacos"; - /** - * the separator between ip and port. - */ - String IP_PORT_SEPARATOR = ":"; - /** * the separator for service name between raw service name and group. */ diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java index 0e088931fce..befe1ce3744 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java @@ -90,7 +90,7 @@ public ServerListManager(List fixed, String namespace) { for (String serverAddr : fixed) { String[] serverAddrArr = IpUtil.splitIpPortStr(serverAddr); if (serverAddrArr.length == 1) { - serverAddrs.add(serverAddrArr[0] + ":" + ParamUtil.getDefaultServerPort()); + serverAddrs.add(serverAddrArr[0] + IpUtil.IP_PORT_SPLITER + ParamUtil.getDefaultServerPort()); } else { serverAddrs.add(serverAddr); } @@ -159,7 +159,8 @@ public ServerListManager(Properties properties) throws NacosException { } else { String[] serverAddrArr = IpUtil.splitIpPortStr(serverAddr); if (serverAddrArr.length == 1) { - serverAddrs.add(HTTP + serverAddrArr[0] + ":" + ParamUtil.getDefaultServerPort()); + serverAddrs.add(HTTP + serverAddrArr[0] + IpUtil.IP_PORT_SPLITER + ParamUtil + .getDefaultServerPort()); } else { serverAddrs.add(HTTP + serverAddr); } @@ -359,7 +360,7 @@ private List getApacheServerList(String url, String name) { String[] ipPort = IpUtil.splitIpPortStr(serverAddr.trim()); String ip = ipPort[0].trim(); if (ipPort.length == 1) { - result.add(ip + ":" + ParamUtil.getDefaultServerPort()); + result.add(ip + IpUtil.IP_PORT_SPLITER + ParamUtil.getDefaultServerPort()); } else { result.add(serverAddr); } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java b/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java index 12488ff44c8..52c1c09000a 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java @@ -45,6 +45,7 @@ import com.alibaba.nacos.common.lifecycle.Closeable; import com.alibaba.nacos.common.utils.HttpMethod; import com.alibaba.nacos.common.utils.IoUtils; +import com.alibaba.nacos.common.utils.IpUtil; import com.alibaba.nacos.common.utils.JacksonUtils; import com.alibaba.nacos.common.utils.StringUtils; import com.alibaba.nacos.common.utils.ThreadUtils; @@ -588,8 +589,8 @@ public String callServer(String api, Map params, Map 1 ? Runtime.getRuntime().availableProcessors() / 2 : 1; diff --git a/common/src/test/java/com/alibaba/nacos/common/utils/IpUtilTest.java b/common/src/test/java/com/alibaba/nacos/common/utils/IpUtilTest.java index 5b87db843bf..a86e842e4ae 100644 --- a/common/src/test/java/com/alibaba/nacos/common/utils/IpUtilTest.java +++ b/common/src/test/java/com/alibaba/nacos/common/utils/IpUtilTest.java @@ -19,6 +19,10 @@ import org.junit.Assert; import org.junit.Test; +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.util.Enumeration; + /** * test IpUtil. * @ClassName: IpUtilTest @@ -26,6 +30,28 @@ */ public class IpUtilTest { + @Test + public void test111(){ + try { + Enumeration en = NetworkInterface.getNetworkInterfaces(); + while (en.hasMoreElements()) { + NetworkInterface ni = en.nextElement(); + Enumeration ads = ni.getInetAddresses(); + while (ads.hasMoreElements()) { + InetAddress ip = ads.nextElement(); + System.out.println(ip.getHostAddress()); + // Compatible group does not regulate 11 network segments + if (!ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1 + /* && ip.isSiteLocalAddress() */) { +// System.out.println(ip.getHostAddress()); + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } + } + @Test public void testIsIpv4() { Assert.assertTrue(IpUtil.isIpv4("127.0.0.1")); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftCore.java b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftCore.java index 2d855b8402b..da17e28bfaa 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftCore.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftCore.java @@ -939,7 +939,7 @@ public boolean isLeader() { */ public static String buildUrl(String ip, String api) { if (!IpUtil.containsPort(ip)) { - ip = ip + UtilsAndCommons.IP_PORT_SPLITER + ApplicationUtils.getPort(); + ip = ip + IpUtil.IP_PORT_SPLITER + ApplicationUtils.getPort(); } return "http://" + ip + ApplicationUtils.getContextPath() + api; } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftProxy.java b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftProxy.java index 3958270a781..8c8cb43cb93 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftProxy.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftProxy.java @@ -45,7 +45,7 @@ public class RaftProxy { public void proxyGet(String server, String api, Map params) throws Exception { // do proxy if (!IpUtil.containsPort(server)) { - server = server + UtilsAndCommons.IP_PORT_SPLITER + ApplicationUtils.getPort(); + server = server + IpUtil.IP_PORT_SPLITER + ApplicationUtils.getPort(); } String url = "http://" + server + ApplicationUtils.getContextPath() + api; @@ -67,7 +67,7 @@ public void proxyGet(String server, String api, Map params) thro public void proxy(String server, String api, Map params, HttpMethod method) throws Exception { // do proxy if (!IpUtil.containsPort(server)) { - server = server + UtilsAndCommons.IP_PORT_SPLITER + ApplicationUtils.getPort(); + server = server + IpUtil.IP_PORT_SPLITER + ApplicationUtils.getPort(); } String url = "http://" + server + ApplicationUtils.getContextPath() + api; HttpClient.HttpResult result; @@ -103,7 +103,7 @@ public void proxyPostLarge(String server, String api, String content, Map params, String curSe HttpClient.HttpResult result; if (!IpUtil.containsPort(curServer)) { - curServer = curServer + UtilsAndCommons.IP_PORT_SPLITER + ApplicationUtils.getPort(); + curServer = curServer + IpUtil.IP_PORT_SPLITER + ApplicationUtils.getPort(); } result = HttpClient.httpGet("http://" + curServer + api, headers, params); @@ -242,7 +242,7 @@ public static String reqApi(String api, Map params, String curSe HttpClient.HttpResult result; if (!IpUtil.containsPort(curServer)) { - curServer = curServer + UtilsAndCommons.IP_PORT_SPLITER + ApplicationUtils.getPort(); + curServer = curServer + IpUtil.IP_PORT_SPLITER + ApplicationUtils.getPort(); } if (isPost) { @@ -292,7 +292,7 @@ public static String reqCommon(String path, Map params, String c HttpClient.HttpResult result; if (!IpUtil.containsPort(curServer)) { - curServer = curServer + UtilsAndCommons.IP_PORT_SPLITER + ApplicationUtils.getPort(); + curServer = curServer + IpUtil.IP_PORT_SPLITER + ApplicationUtils.getPort(); } if (isPost) { diff --git a/naming/src/main/java/com/alibaba/nacos/naming/misc/NetUtils.java b/naming/src/main/java/com/alibaba/nacos/naming/misc/NetUtils.java index bcdf3f568bc..d224bb7e2c2 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/misc/NetUtils.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/misc/NetUtils.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.naming.misc; +import com.alibaba.nacos.common.utils.IpUtil; import com.alibaba.nacos.core.utils.ApplicationUtils; import com.alibaba.nacos.core.utils.InetUtils; @@ -32,7 +33,7 @@ public class NetUtils { * @return local server address */ public static String localServer() { - return InetUtils.getSelfIp() + UtilsAndCommons.IP_PORT_SPLITER + ApplicationUtils.getPort(); + return InetUtils.getSelfIp() + IpUtil.IP_PORT_SPLITER + ApplicationUtils.getPort(); } /** diff --git a/naming/src/main/java/com/alibaba/nacos/naming/misc/UtilsAndCommons.java b/naming/src/main/java/com/alibaba/nacos/naming/misc/UtilsAndCommons.java index 7d6e7edc6f4..38289f0c9fa 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/misc/UtilsAndCommons.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/misc/UtilsAndCommons.java @@ -95,8 +95,6 @@ public class UtilsAndCommons { public static final String CACHE_KEY_SPLITER = "@@@@"; - public static final String IP_PORT_SPLITER = ":"; - public static final int MAX_PUBLISH_WAIT_TIME_MILLIS = 5000; public static final String VERSION_STRING_SYNTAX = "[0-9]+\\.[0-9]+\\.[0-9]+";