Skip to content

Commit

Permalink
调整使用了类似 ip.indexOf(":") 查找是否有端口的地方的逻辑, 一些 ":" 替换为常量
Browse files Browse the repository at this point in the history
  • Loading branch information
KeRan213539 committed Sep 8, 2020
1 parent bb4e177 commit 601f0b4
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -85,14 +86,11 @@ public List<Instance> 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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public ServerListManager(List<String> 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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -359,7 +360,7 @@ private List<String> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -588,8 +589,8 @@ public String callServer(String api, Map<String, String> params, Map<String, Str
if (curServer.startsWith(UtilAndComs.HTTPS) || curServer.startsWith(UtilAndComs.HTTP)) {
url = curServer + api;
} else {
if (!curServer.contains(UtilAndComs.SERVER_ADDR_IP_SPLITER)) {
curServer = curServer + UtilAndComs.SERVER_ADDR_IP_SPLITER + serverPort;
if (!IpUtil.containsPort(curServer)) {
curServer = curServer + IpUtil.IP_PORT_SPLITER + serverPort;
}
url = NamingHttpClientManager.getInstance().getPrefix() + curServer + api;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ public class UtilAndComs {

public static final String NACOS_NAMING_LOG_LEVEL = "com.alibaba.nacos.naming.log.level";

public static final String SERVER_ADDR_IP_SPLITER = ":";

public static final int DEFAULT_CLIENT_BEAT_THREAD_COUNT =
Runtime.getRuntime().availableProcessors() > 1 ? Runtime.getRuntime().availableProcessors() / 2 : 1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,39 @@
import org.junit.Assert;
import org.junit.Test;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

/**
* test IpUtil.
* @ClassName: IpUtilTest
* @date 2020/9/3 10:31
*/
public class IpUtilTest {

@Test
public void test111(){
try {
Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
while (en.hasMoreElements()) {
NetworkInterface ni = en.nextElement();
Enumeration<InetAddress> 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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class RaftProxy {
public void proxyGet(String server, String api, Map<String, String> 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;

Expand All @@ -67,7 +67,7 @@ public void proxyGet(String server, String api, Map<String, String> params) thro
public void proxy(String server, String api, Map<String, String> 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;
Expand Down Expand Up @@ -103,7 +103,7 @@ public void proxyPostLarge(String server, String api, String content, Map<String
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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public static String reqApi(String api, Map<String, String> 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);
Expand Down Expand Up @@ -242,7 +242,7 @@ public static String reqApi(String api, Map<String, String> 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) {
Expand Down Expand Up @@ -292,7 +292,7 @@ public static String reqCommon(String path, Map<String, String> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]+";
Expand Down

0 comments on commit 601f0b4

Please sign in to comment.