fetch self ip
This commit is contained in:
parent
12432b0eb0
commit
eb87625083
@ -44,7 +44,7 @@ import java.sql.Timestamp;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.LOCAL_IP;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.LOCAL_IP;
|
||||
|
||||
/**
|
||||
* 软负载客户端发布数据专用控制器
|
||||
|
@ -41,7 +41,7 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.STANDALONE_MODE;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE;
|
||||
import static com.alibaba.nacos.config.server.utils.LogUtil.pullLog;
|
||||
|
||||
/**
|
||||
|
@ -27,7 +27,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.LOCAL_IP;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.LOCAL_IP;
|
||||
|
||||
/**
|
||||
* health service
|
||||
|
@ -15,7 +15,7 @@
|
||||
*/
|
||||
package com.alibaba.nacos.config.server.model.app;
|
||||
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.LOCAL_IP;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.LOCAL_IP;
|
||||
|
||||
/**
|
||||
* app info
|
||||
|
@ -40,7 +40,7 @@ import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.STANDALONE_MODE;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE;
|
||||
import static com.alibaba.nacos.config.server.service.PersistService.CONFIG_INFO4BETA_ROW_MAPPER;
|
||||
import static com.alibaba.nacos.config.server.utils.LogUtil.defaultLog;
|
||||
import static com.alibaba.nacos.config.server.utils.LogUtil.fatalLog;
|
||||
|
@ -33,7 +33,7 @@ import java.util.*;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.STANDALONE_MODE;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE;
|
||||
import static com.alibaba.nacos.config.server.utils.LogUtil.*;
|
||||
|
||||
/**
|
||||
|
@ -29,7 +29,7 @@ import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.NACOS_HOME;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.NACOS_HOME;
|
||||
|
||||
/**
|
||||
* 磁盘操作工具类。
|
||||
|
@ -21,7 +21,7 @@ import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.STANDALONE_MODE;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE;
|
||||
|
||||
/**
|
||||
* datasource adapter
|
||||
|
@ -40,9 +40,9 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.NACOS_HOME;
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.NACOS_HOME_KEY;
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.STANDALONE_MODE;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.NACOS_HOME;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.NACOS_HOME_KEY;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE;
|
||||
|
||||
/**
|
||||
* local data source
|
||||
|
@ -48,9 +48,9 @@ import java.util.*;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.LOCAL_IP;
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.STANDALONE_MODE;
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.readClusterConf;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.LOCAL_IP;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.readClusterConf;
|
||||
import static com.alibaba.nacos.config.server.utils.LogUtil.defaultLog;
|
||||
import static com.alibaba.nacos.config.server.utils.LogUtil.fatalLog;
|
||||
|
||||
|
@ -35,7 +35,7 @@ import javax.annotation.PostConstruct;
|
||||
import java.sql.*;
|
||||
import java.util.List;
|
||||
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.STANDALONE_MODE;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE;
|
||||
import static com.alibaba.nacos.config.server.utils.LogUtil.fatalLog;
|
||||
|
||||
/**
|
||||
|
@ -34,7 +34,7 @@ import javax.annotation.PostConstruct;
|
||||
import java.sql.*;
|
||||
import java.util.List;
|
||||
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.STANDALONE_MODE;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE;
|
||||
import static com.alibaba.nacos.config.server.utils.LogUtil.fatalLog;
|
||||
|
||||
/**
|
||||
|
@ -46,8 +46,8 @@ import java.util.Random;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.LOCAL_IP;
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.STANDALONE_MODE;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.LOCAL_IP;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE;
|
||||
import static com.alibaba.nacos.config.server.utils.LogUtil.fatalLog;
|
||||
|
||||
/**
|
||||
|
@ -33,7 +33,7 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.LOCAL_IP;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.LOCAL_IP;
|
||||
|
||||
/**
|
||||
* 数据聚合服务。
|
||||
|
@ -35,7 +35,7 @@ import java.sql.Timestamp;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.LOCAL_IP;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.LOCAL_IP;
|
||||
|
||||
/**
|
||||
* Merge task processor
|
||||
|
@ -45,7 +45,7 @@ import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.LOCAL_IP;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.LOCAL_IP;
|
||||
|
||||
/**
|
||||
* Async notify service
|
||||
|
@ -32,7 +32,7 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.LOCAL_IP;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.LOCAL_IP;
|
||||
|
||||
/**
|
||||
* 通知服务。数据库变更后,通知所有server,包括自己,加载新数据。
|
||||
|
@ -23,7 +23,7 @@ import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.LOCAL_IP;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.LOCAL_IP;
|
||||
|
||||
/**
|
||||
* Config trace
|
||||
|
@ -21,7 +21,7 @@ import org.springframework.jdbc.core.RowMapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.STANDALONE_MODE;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE;
|
||||
|
||||
/**
|
||||
* 分页辅助类
|
||||
|
@ -22,7 +22,7 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.STANDALONE_MODE;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE;
|
||||
|
||||
/**
|
||||
* properties utils
|
||||
|
3
console/src/main/resources/application.properties
Normal file
3
console/src/main/resources/application.properties
Normal file
@ -0,0 +1,3 @@
|
||||
|
||||
|
||||
|
12
core/pom.xml
12
core/pom.xml
@ -29,6 +29,18 @@
|
||||
|
||||
<name>nacos-core ${project.version}</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>7</source>
|
||||
<target>7</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
|
@ -25,9 +25,9 @@ import org.springframework.core.env.ConfigurableEnvironment;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static com.alibaba.nacos.common.util.Constants.STANDALONE_SPRING_PROFILE;
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.STANDALONE_MODE;
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.STANDALONE_MODE_PROPERTY_NAME;
|
||||
import static com.alibaba.nacos.core.utils.Constants.STANDALONE_SPRING_PROFILE;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE_PROPERTY_NAME;
|
||||
|
||||
/**
|
||||
* Standalone {@link Profile} {@link ApplicationListener} for {@link ApplicationEnvironmentPreparedEvent}
|
||||
|
@ -31,10 +31,10 @@ import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.LOCAL_IP;
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.NACOS_HOME;
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.STANDALONE_MODE;
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.readClusterConf;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.LOCAL_IP;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.NACOS_HOME;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.readClusterConf;
|
||||
|
||||
/**
|
||||
* Logging starting message {@link SpringApplicationRunListener} before {@link EventPublishingRunListener} execution
|
||||
|
@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.nacos.common.util;
|
||||
package com.alibaba.nacos.core.utils;
|
||||
|
||||
/**
|
||||
* Nacos common constants
|
||||
@ -27,4 +27,9 @@ public interface Constants {
|
||||
* Spring Profile : "standalone"
|
||||
*/
|
||||
String STANDALONE_SPRING_PROFILE = "standalone";
|
||||
String USE_ONLY_SITE_INTERFACES = "nacos.inetutils.use-only-site-local-interfaces";
|
||||
String PREFERRED_NETWORKS = "nacos.inetutils.preferred-networks";
|
||||
String IGNORED_INTERFACES = "nacos.inetutils.ignored-interfaces";
|
||||
String IP_ADDRESS = "nacos.inetutils.ip-address";
|
||||
String PREFER_HOSTNAME_OVER_IP = "nacos.inetutils.prefer-hostname-over-ip";
|
||||
}
|
198
core/src/main/java/com/alibaba/nacos/core/utils/InetUtils.java
Normal file
198
core/src/main/java/com/alibaba/nacos/core/utils/InetUtils.java
Normal file
@ -0,0 +1,198 @@
|
||||
/*
|
||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.nacos.core.utils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Inet4Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import static com.alibaba.nacos.core.utils.Constants.*;
|
||||
|
||||
/**
|
||||
* @author Nacos
|
||||
*/
|
||||
public class InetUtils {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(InetUtils.class);
|
||||
|
||||
private static String selfIp;
|
||||
|
||||
private static boolean useOnlySiteLocalInterface = false;
|
||||
|
||||
private static boolean preferHostnameOverIp = false;
|
||||
|
||||
private static List<String> preferredNetworks = new ArrayList<String>();
|
||||
|
||||
private static List<String> ignoredInterfaces = new ArrayList<String>();
|
||||
|
||||
static {
|
||||
useOnlySiteLocalInterface = Boolean.valueOf(PropertyUtil.getProperty(USE_ONLY_SITE_INTERFACES));
|
||||
|
||||
List<String> networks = PropertyUtil.getPropertyList(PREFERRED_NETWORKS);
|
||||
for (String preferred : networks) {
|
||||
preferredNetworks.add(preferred);
|
||||
}
|
||||
|
||||
List<String> interfaces = PropertyUtil.getPropertyList(IGNORED_INTERFACES);
|
||||
for (String ignored : interfaces) {
|
||||
ignoredInterfaces.add(ignored);
|
||||
}
|
||||
|
||||
String nacosIp = System.getProperty("nacos.server.ip");
|
||||
if (StringUtils.isBlank(nacosIp)) {
|
||||
nacosIp = PropertyUtil.getProperty(IP_ADDRESS);
|
||||
}
|
||||
|
||||
if (!StringUtils.isBlank(nacosIp) && !isIP(nacosIp)) {
|
||||
throw new RuntimeException("nacos address " + nacosIp + " is not ip");
|
||||
}
|
||||
|
||||
selfIp = nacosIp;
|
||||
|
||||
if (StringUtils.isBlank(selfIp)) {
|
||||
preferHostnameOverIp = Boolean.valueOf(System.getProperty("nacos.preferHostnameOverIp"));
|
||||
|
||||
if (!preferHostnameOverIp) {
|
||||
preferHostnameOverIp = Boolean.valueOf(PropertyUtil.getProperty(PREFER_HOSTNAME_OVER_IP));
|
||||
}
|
||||
|
||||
if (preferHostnameOverIp) {
|
||||
InetAddress inetAddress = null;
|
||||
try {
|
||||
inetAddress = InetAddress.getLocalHost();
|
||||
} catch (UnknownHostException e) {
|
||||
}
|
||||
if (inetAddress.getHostName().equals(inetAddress.getCanonicalHostName())) {
|
||||
selfIp = inetAddress.getHostName();
|
||||
} else {
|
||||
selfIp = inetAddress.getCanonicalHostName();
|
||||
}
|
||||
} else {
|
||||
selfIp = findFirstNonLoopbackAddress().getHostAddress();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String getSelfIp() {
|
||||
return selfIp;
|
||||
}
|
||||
|
||||
public static InetAddress findFirstNonLoopbackAddress() {
|
||||
InetAddress result = null;
|
||||
|
||||
try {
|
||||
int lowest = Integer.MAX_VALUE;
|
||||
for (Enumeration<NetworkInterface> nics = NetworkInterface
|
||||
.getNetworkInterfaces(); nics.hasMoreElements();) {
|
||||
NetworkInterface ifc = nics.nextElement();
|
||||
if (ifc.isUp()) {
|
||||
log.info("Testing interface: " + ifc.getDisplayName());
|
||||
if (ifc.getIndex() < lowest || result == null) {
|
||||
lowest = ifc.getIndex();
|
||||
}
|
||||
else if (result != null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!ignoreInterface(ifc.getDisplayName())) {
|
||||
for (Enumeration<InetAddress> addrs = ifc
|
||||
.getInetAddresses(); addrs.hasMoreElements(); ) {
|
||||
InetAddress address = addrs.nextElement();
|
||||
if (address instanceof Inet4Address
|
||||
&& !address.isLoopbackAddress()
|
||||
&& isPreferredAddress(address)) {
|
||||
log.info("Found non-loopback interface: "
|
||||
+ ifc.getDisplayName());
|
||||
result = address;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException ex) {
|
||||
log.error("Cannot get first non-loopback address", ex);
|
||||
}
|
||||
|
||||
if (result != null) {
|
||||
return result;
|
||||
}
|
||||
|
||||
try {
|
||||
return InetAddress.getLocalHost();
|
||||
}
|
||||
catch (UnknownHostException e) {
|
||||
log.warn("Unable to retrieve localhost");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean isPreferredAddress(InetAddress address) {
|
||||
if (useOnlySiteLocalInterface) {
|
||||
final boolean siteLocalAddress = address.isSiteLocalAddress();
|
||||
if (!siteLocalAddress) {
|
||||
log.info("Ignoring address: " + address.getHostAddress());
|
||||
}
|
||||
return siteLocalAddress;
|
||||
}
|
||||
if (preferredNetworks.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
for (String regex : preferredNetworks) {
|
||||
final String hostAddress = address.getHostAddress();
|
||||
if (hostAddress.matches(regex) || hostAddress.startsWith(regex)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean ignoreInterface(String interfaceName) {
|
||||
for (String regex : ignoredInterfaces) {
|
||||
if (interfaceName.matches(regex)) {
|
||||
log.info("Ignoring interface: " + interfaceName);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean isIP(String str) {
|
||||
String num = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)";
|
||||
String regex = "^" + num + "\\." + num + "\\." + num + "\\." + num + "$";
|
||||
return match(regex, str);
|
||||
}
|
||||
|
||||
public static boolean match(String regex, String str) {
|
||||
Pattern pattern = Pattern.compile(regex);
|
||||
Matcher matcher = pattern.matcher(str);
|
||||
return matcher.matches();
|
||||
}
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.nacos.core.utils;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* @author Nacos
|
||||
*/
|
||||
public class PropertyUtil {
|
||||
private static Properties properties = new Properties();
|
||||
|
||||
static {
|
||||
try {
|
||||
URL configURL = PropertyUtil.class
|
||||
.getResource("/application.properties");
|
||||
if (configURL != null) {
|
||||
URI configURI = configURL.toURI();
|
||||
File file = new File(configURI);
|
||||
properties = new Properties();
|
||||
properties.load(new FileInputStream(file));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
|
||||
public static String getProperty(String key) {
|
||||
return properties.getProperty(key);
|
||||
}
|
||||
|
||||
public static String getProperty(String key, String defaultValue) {
|
||||
return properties.getProperty(key, defaultValue);
|
||||
}
|
||||
|
||||
public static List<String> getPropertyList(String key) {
|
||||
List<String> valueList = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < Integer.MAX_VALUE; i++) {
|
||||
String value = properties.getProperty(key + "[" + i + "]");
|
||||
if (StringUtils.isBlank(value)) {
|
||||
break;
|
||||
}
|
||||
|
||||
valueList.add(value);
|
||||
}
|
||||
|
||||
return valueList;
|
||||
}
|
||||
|
||||
}
|
@ -14,8 +14,9 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.nacos.common.util;
|
||||
package com.alibaba.nacos.core.utils;
|
||||
|
||||
import com.alibaba.nacos.common.util.IoUtils;
|
||||
import com.sun.management.OperatingSystemMXBean;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
@ -27,10 +28,9 @@ import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
|
||||
import static org.apache.commons.lang3.CharEncoding.UTF_8;
|
||||
@ -65,7 +65,11 @@ public class SystemUtils {
|
||||
private static OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean)ManagementFactory
|
||||
.getOperatingSystemMXBean();
|
||||
|
||||
public static final String LOCAL_IP = getHostAddress();
|
||||
/**
|
||||
* nacos local ip
|
||||
*/
|
||||
public static final String LOCAL_IP = InetUtils.getSelfIp();
|
||||
|
||||
|
||||
/**
|
||||
* The key of nacos home.
|
||||
@ -108,34 +112,6 @@ public class SystemUtils {
|
||||
.getTotalPhysicalMemorySize());
|
||||
}
|
||||
|
||||
private static String getHostAddress() {
|
||||
String address = System.getProperty("nacos.server.ip");
|
||||
if (StringUtils.isNotEmpty(address)) {
|
||||
return address;
|
||||
}
|
||||
|
||||
address = "127.0.0.1";
|
||||
|
||||
try {
|
||||
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
|
||||
while (networkInterfaces.hasMoreElements()) {
|
||||
NetworkInterface networkInterface = networkInterfaces.nextElement();
|
||||
Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
|
||||
while (inetAddresses.hasMoreElements()) {
|
||||
InetAddress ip = inetAddresses.nextElement();
|
||||
// 兼容不规范网段
|
||||
if (!ip.isLoopbackAddress() && !ip.getHostAddress().contains(":")) {
|
||||
return ip.getHostAddress();
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("get local host address error", e);
|
||||
}
|
||||
|
||||
return address;
|
||||
}
|
||||
|
||||
private static String getNacosHome() {
|
||||
String nacosHome = System.getProperty(NACOS_HOME_KEY);
|
||||
if (StringUtils.isBlank(nacosHome)) {
|
@ -25,7 +25,7 @@ import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static com.alibaba.nacos.common.util.Constants.STANDALONE_SPRING_PROFILE;
|
||||
import static com.alibaba.nacos.core.utils.Constants.STANDALONE_SPRING_PROFILE;
|
||||
|
||||
/**
|
||||
* {@link StandaloneProfileApplicationListener} Test
|
||||
|
@ -13,9 +13,10 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.nacos.common;
|
||||
package com.alibaba.nacos.core.util;
|
||||
|
||||
import com.alibaba.nacos.common.util.SystemUtils;
|
||||
|
||||
import com.alibaba.nacos.core.utils.SystemUtils;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
@ -15,7 +15,7 @@
|
||||
*/
|
||||
package com.alibaba.nacos.naming.misc;
|
||||
|
||||
import com.alibaba.nacos.common.util.SystemUtils;
|
||||
import com.alibaba.nacos.core.utils.SystemUtils;
|
||||
import com.alibaba.nacos.naming.boot.RunningConfig;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@ -29,7 +29,7 @@ import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.*;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.*;
|
||||
|
||||
/**
|
||||
* @author nacos
|
||||
|
@ -15,13 +15,10 @@
|
||||
*/
|
||||
package com.alibaba.nacos.naming.misc;
|
||||
|
||||
import com.alibaba.nacos.core.utils.InetUtils;
|
||||
import com.alibaba.nacos.naming.boot.RunningConfig;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.PREFER_HOSTNAME_OVER_IP;
|
||||
|
||||
/**
|
||||
* @author nacos
|
||||
@ -31,24 +28,12 @@ public class NetUtils {
|
||||
private static String serverAddress = null;
|
||||
|
||||
public static String localServer() {
|
||||
try {
|
||||
if (StringUtils.isNotBlank(serverAddress)) {
|
||||
return serverAddress + UtilsAndCommons.CLUSTER_CONF_IP_SPLITER + RunningConfig.getServerPort();
|
||||
}
|
||||
|
||||
InetAddress inetAddress = InetAddress.getLocalHost();
|
||||
serverAddress = inetAddress.getHostAddress();
|
||||
if (PREFER_HOSTNAME_OVER_IP) {
|
||||
if (inetAddress.getHostName().equals(inetAddress.getCanonicalHostName())) {
|
||||
serverAddress = inetAddress.getHostName();
|
||||
} else {
|
||||
serverAddress = inetAddress.getCanonicalHostName();
|
||||
}
|
||||
}
|
||||
if (StringUtils.isNotBlank(serverAddress)) {
|
||||
return serverAddress + UtilsAndCommons.CLUSTER_CONF_IP_SPLITER + RunningConfig.getServerPort();
|
||||
} catch (UnknownHostException e) {
|
||||
return "resolve_failed";
|
||||
}
|
||||
|
||||
serverAddress = InetUtils.getSelfIp();
|
||||
return serverAddress + UtilsAndCommons.CLUSTER_CONF_IP_SPLITER + RunningConfig.getServerPort();
|
||||
}
|
||||
|
||||
public static String num2ip(int ip) {
|
||||
|
@ -28,7 +28,7 @@ import org.apache.commons.lang3.StringUtils;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.util.*;
|
||||
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.STANDALONE_MODE;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE;
|
||||
|
||||
/**
|
||||
* @author nacos
|
||||
|
@ -41,7 +41,7 @@ import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.STANDALONE_MODE;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE;
|
||||
|
||||
/**
|
||||
* @author nacos
|
||||
|
@ -31,8 +31,8 @@ import java.nio.ByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.util.Properties;
|
||||
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.NACOS_HOME;
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.NACOS_HOME_KEY;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.NACOS_HOME;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.NACOS_HOME_KEY;
|
||||
|
||||
/**
|
||||
* @author nacos
|
||||
|
@ -22,7 +22,7 @@ import com.alibaba.fastjson.TypeReference;
|
||||
import com.alibaba.nacos.api.common.Constants;
|
||||
import com.alibaba.nacos.api.naming.pojo.AbstractHealthChecker;
|
||||
import com.alibaba.nacos.common.util.Md5Utils;
|
||||
import com.alibaba.nacos.common.util.SystemUtils;
|
||||
import com.alibaba.nacos.core.utils.SystemUtils;
|
||||
import com.alibaba.nacos.core.utils.WebUtils;
|
||||
import com.alibaba.nacos.naming.boot.RunningConfig;
|
||||
import com.alibaba.nacos.naming.core.*;
|
||||
@ -68,9 +68,9 @@ import java.util.concurrent.locks.Condition;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.STANDALONE_MODE;
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.readClusterConf;
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.writeClusterConf;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.readClusterConf;
|
||||
import static com.alibaba.nacos.core.utils.SystemUtils.writeClusterConf;
|
||||
|
||||
/**
|
||||
* Old API entry
|
||||
|
Loading…
Reference in New Issue
Block a user