From 3628732dade4453c9dde527a906078cd98c09e35 Mon Sep 17 00:00:00 2001 From: "chengpu.rzh" Date: Thu, 14 Jul 2022 22:30:56 +0800 Subject: [PATCH] Reformat the codes --- pom.xml | 2 +- spring-cloud-alibaba-dependencies/pom.xml | 2 +- .../cloud/nacos/NacosDiscoveryProperties.java | 19 +- .../cloud/nacos/intetuntil/InetIPv6Utils.java | 295 ++++++++++-------- .../intetuntil/UtilIPv6AutoConfiguration.java | 34 +- 5 files changed, 194 insertions(+), 158 deletions(-) diff --git a/pom.xml b/pom.xml index 26cc9b9d7..55dd872aa 100644 --- a/pom.xml +++ b/pom.xml @@ -80,7 +80,7 @@ - 2.2.8.RELEASE + 2.2.9-SNAPSHOT Hoxton.SR12 diff --git a/spring-cloud-alibaba-dependencies/pom.xml b/spring-cloud-alibaba-dependencies/pom.xml index d471f7cac..059d85a87 100644 --- a/spring-cloud-alibaba-dependencies/pom.xml +++ b/spring-cloud-alibaba-dependencies/pom.xml @@ -18,7 +18,7 @@ Spring Cloud Alibaba Dependencies - 2.2.8.RELEASE + 2.2.9-SNAPSHOT 1.8.4 1.5.1 2.1.0 diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/NacosDiscoveryProperties.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/NacosDiscoveryProperties.java index 8bb7e3d2b..43ac91b77 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/NacosDiscoveryProperties.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/NacosDiscoveryProperties.java @@ -17,7 +17,6 @@ package com.alibaba.cloud.nacos; import java.net.Inet4Address; - import java.net.Inet6Address; import java.net.InetAddress; import java.net.NetworkInterface; @@ -166,9 +165,9 @@ public class NacosDiscoveryProperties { private String networkInterface = ""; /** - * choose IPV4 or IPV6,if you don't set it will choose IPV4 + * choose IPV4 or IPV6,if you don't set it will choose IPV4. */ - private String ipType = "IPv4" ; + private String ipType = "IPv4"; /** * The port your want to register for your service instance, needn't to set it if the @@ -256,14 +255,16 @@ public class NacosDiscoveryProperties { if (StringUtils.isEmpty(ip)) { // traversing network interfaces if didn't specify a interface if (StringUtils.isEmpty(networkInterface)) { - if (ipType.equalsIgnoreCase("IPv4")){ + if (ipType.equalsIgnoreCase("IPv4")) { ip = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress(); - }else if (ipType.equalsIgnoreCase("IPv6")){ + } + else if (ipType.equalsIgnoreCase("IPv6")) { ip = inetIPUtils.findFirstNonLoopbackHostInfo().getIpAddress(); int index = ip.indexOf('%'); ip = index > 0 ? ip.substring(0, index) : ip; - ip = "["+ip+"]"; - }else { + ip = "[" + ip + "]"; + } + else { throw new IllegalArgumentException( "please checking the type of IP " + ipType); } @@ -281,7 +282,7 @@ public class NacosDiscoveryProperties { InetAddress currentAddress = inetAddress.nextElement(); if (currentAddress instanceof Inet4Address || currentAddress instanceof Inet6Address - && !currentAddress.isLoopbackAddress()) { + && !currentAddress.isLoopbackAddress()) { ip = currentAddress.getHostAddress(); break; } @@ -335,7 +336,7 @@ public class NacosDiscoveryProperties { this.logName = logName; } - public void setInetIPUtils(InetIPv6Utils inetIPUtils){ + public void setInetIPUtils(InetIPv6Utils inetIPUtils) { this.inetIPUtils = inetIPUtils; } diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/intetuntil/InetIPv6Utils.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/intetuntil/InetIPv6Utils.java index 3a596d7e2..e0511588d 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/intetuntil/InetIPv6Utils.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/intetuntil/InetIPv6Utils.java @@ -1,13 +1,23 @@ -package com.alibaba.cloud.nacos.intetuntil; +/* + * Copyright 2013-2018 the original author or authors. + * + * 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 + * + * https://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. + */ -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.springframework.cloud.commons.util.InetUtils; -import org.springframework.cloud.commons.util.InetUtilsProperties; +package com.alibaba.cloud.nacos.intetuntil; import java.io.Closeable; import java.io.IOException; - import java.net.Inet6Address; import java.net.InetAddress; import java.net.NetworkInterface; @@ -19,140 +29,149 @@ import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import org.springframework.cloud.commons.util.InetUtils; +import org.springframework.cloud.commons.util.InetUtilsProperties; + /** * @author HH */ public class InetIPv6Utils implements Closeable { - private final ExecutorService executorService; - - private final Log log = LogFactory.getLog(InetIPv6Utils.class); - - private final InetUtilsProperties properties; - - public InetIPv6Utils(final InetUtilsProperties properties) { - this.properties = properties; - this.executorService = Executors.newSingleThreadExecutor((r) -> { - Thread thread = new Thread(r); - thread.setName("spring.cloud.alibaba.inetutilsIPV6"); - thread.setDaemon(true); - return thread; - }); - } - - public void close() { - this.executorService.shutdown(); - } - - public InetUtils.HostInfo findFirstNonLoopbackHostInfo() { - InetAddress address = this.findFirstNonLoopbackIPv6Address(); - if (address != null) { - return this.convertAddress(address); - } else { - InetUtils.HostInfo hostInfo = new InetUtils.HostInfo(); - this.properties.setDefaultIpAddress("0:0:0:0:0:0:0:1"); - hostInfo.setHostname(this.properties.getDefaultHostname()); - hostInfo.setIpAddress(this.properties.getDefaultIpAddress()); - return hostInfo; - } - } - - public InetAddress findFirstNonLoopbackIPv6Address() { - InetAddress address = null; - - try { - int lowest = Integer.MAX_VALUE; - for (Enumeration nics = NetworkInterface.getNetworkInterfaces(); - nics.hasMoreElements();) { - NetworkInterface ifc = nics.nextElement(); - if (ifc.isUp()) { - log.trace("Testing interface:" + ifc.getDisplayName()); - if (ifc.getIndex() < lowest || address == null) { - lowest = ifc.getIndex(); - } - else if (address != null) { - continue; - } - - if (!ignoreInterface(ifc.getDisplayName())) { - for (Enumeration addrs = ifc - .getInetAddresses(); addrs.hasMoreElements();) { - InetAddress inetAddress = addrs.nextElement(); - if (inetAddress instanceof Inet6Address - && !inetAddress.isLoopbackAddress() - && isPreferredAddress(inetAddress)) { - log.trace("Found non-loopback interface: " - + ifc.getDisplayName()); - address = inetAddress; - } - } - } - } - } - } - catch (IOException e) { - log.error("Cannot get first non-loopback address", e); - } - - if (address != null) { - return address; - } - - try { - return InetAddress.getLocalHost(); - } - catch (UnknownHostException e) { - log.warn("Unable to retrieve localhost"); - } - - return null; - } - - boolean isPreferredAddress(InetAddress address) { - if (this.properties.isUseOnlySiteLocalInterfaces()) { - final boolean siteLocalAddress = address.isSiteLocalAddress(); - if (!siteLocalAddress) { - log.trace("Ignoring address"+address.getHostAddress()); - } - return siteLocalAddress; - } - final List preferredNetworks = this.properties.getPreferredNetworks(); - if (preferredNetworks.isEmpty()) { - return true; - } - for (String regex : preferredNetworks) { - final String hostAddress = address.getHostAddress(); - if (hostAddress.matches(regex) || hostAddress.startsWith(regex)) { - return true; - } - } - log.trace("Ignoring address: " + address.getHostAddress()); - return false; - } - - boolean ignoreInterface(String interfaceName) { - for (String regex : this.properties.getIgnoredInterfaces()) { - if (interfaceName.matches(regex)) { - log.trace("Ignoring interface: " + interfaceName); - return true; - } - } - return false; - } - - public InetUtils.HostInfo convertAddress(final InetAddress address) { - InetUtils.HostInfo hostInfo = new InetUtils.HostInfo(); - Future result = this.executorService.submit(address::getHostName); - - String hostname; - try { - hostname = result.get(this.properties.getTimeoutSeconds(), TimeUnit.SECONDS); - } - catch (Exception e) { - log.info("Cannot determine local hostname"); - hostname = "localhost"; - } - hostInfo.setHostname(hostname); - hostInfo.setIpAddress(address.getHostAddress()); - return hostInfo; - } + + private final ExecutorService executorService; + + private final Log log = LogFactory.getLog(InetIPv6Utils.class); + + private final InetUtilsProperties properties; + + public InetIPv6Utils(final InetUtilsProperties properties) { + this.properties = properties; + this.executorService = Executors.newSingleThreadExecutor((r) -> { + Thread thread = new Thread(r); + thread.setName("spring.cloud.alibaba.inetutilsIPV6"); + thread.setDaemon(true); + return thread; + }); + } + + public void close() { + this.executorService.shutdown(); + } + + public InetUtils.HostInfo findFirstNonLoopbackHostInfo() { + InetAddress address = this.findFirstNonLoopbackIPv6Address(); + if (address != null) { + return this.convertAddress(address); + } + else { + InetUtils.HostInfo hostInfo = new InetUtils.HostInfo(); + this.properties.setDefaultIpAddress("0:0:0:0:0:0:0:1"); + hostInfo.setHostname(this.properties.getDefaultHostname()); + hostInfo.setIpAddress(this.properties.getDefaultIpAddress()); + return hostInfo; + } + } + + public InetAddress findFirstNonLoopbackIPv6Address() { + InetAddress address = null; + + try { + int lowest = Integer.MAX_VALUE; + for (Enumeration nics = NetworkInterface + .getNetworkInterfaces(); nics.hasMoreElements();) { + NetworkInterface ifc = nics.nextElement(); + if (ifc.isUp()) { + log.trace("Testing interface:" + ifc.getDisplayName()); + if (ifc.getIndex() < lowest || address == null) { + lowest = ifc.getIndex(); + } + else if (address != null) { + continue; + } + + if (!ignoreInterface(ifc.getDisplayName())) { + for (Enumeration addrs = ifc + .getInetAddresses(); addrs.hasMoreElements();) { + InetAddress inetAddress = addrs.nextElement(); + if (inetAddress instanceof Inet6Address + && !inetAddress.isLoopbackAddress() + && isPreferredAddress(inetAddress)) { + log.trace("Found non-loopback interface: " + + ifc.getDisplayName()); + address = inetAddress; + } + } + } + } + } + } + catch (IOException e) { + log.error("Cannot get first non-loopback address", e); + } + + if (address != null) { + return address; + } + + try { + return InetAddress.getLocalHost(); + } + catch (UnknownHostException e) { + log.warn("Unable to retrieve localhost"); + } + + return null; + } + + boolean isPreferredAddress(InetAddress address) { + if (this.properties.isUseOnlySiteLocalInterfaces()) { + final boolean siteLocalAddress = address.isSiteLocalAddress(); + if (!siteLocalAddress) { + log.trace("Ignoring address" + address.getHostAddress()); + } + return siteLocalAddress; + } + final List preferredNetworks = this.properties.getPreferredNetworks(); + if (preferredNetworks.isEmpty()) { + return true; + } + for (String regex : preferredNetworks) { + final String hostAddress = address.getHostAddress(); + if (hostAddress.matches(regex) || hostAddress.startsWith(regex)) { + return true; + } + } + log.trace("Ignoring address: " + address.getHostAddress()); + return false; + } + + boolean ignoreInterface(String interfaceName) { + for (String regex : this.properties.getIgnoredInterfaces()) { + if (interfaceName.matches(regex)) { + log.trace("Ignoring interface: " + interfaceName); + return true; + } + } + return false; + } + + public InetUtils.HostInfo convertAddress(final InetAddress address) { + InetUtils.HostInfo hostInfo = new InetUtils.HostInfo(); + Future result = this.executorService.submit(address::getHostName); + + String hostname; + try { + hostname = result.get(this.properties.getTimeoutSeconds(), TimeUnit.SECONDS); + } + catch (Exception e) { + log.info("Cannot determine local hostname"); + hostname = "localhost"; + } + hostInfo.setHostname(hostname); + hostInfo.setIpAddress(address.getHostAddress()); + return hostInfo; + } + } diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/intetuntil/UtilIPv6AutoConfiguration.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/intetuntil/UtilIPv6AutoConfiguration.java index f7588ec9e..0e581f00e 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/intetuntil/UtilIPv6AutoConfiguration.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/intetuntil/UtilIPv6AutoConfiguration.java @@ -1,3 +1,19 @@ +/* + * Copyright 2013-2018 the original author or authors. + * + * 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 + * + * https://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.cloud.nacos.intetuntil; import com.alibaba.cloud.nacos.ConditionalOnNacosDiscoveryEnabled; @@ -8,7 +24,6 @@ import org.springframework.cloud.commons.util.InetUtilsProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; - /** * @author HH */ @@ -16,13 +31,14 @@ import org.springframework.context.annotation.Configuration; @ConditionalOnDiscoveryEnabled @ConditionalOnNacosDiscoveryEnabled public class UtilIPv6AutoConfiguration { - public UtilIPv6AutoConfiguration() { - } - @Bean - @ConditionalOnMissingBean - public InetIPv6Utils inetUtils(InetUtilsProperties properties) { - return new InetIPv6Utils(properties); - } -} + public UtilIPv6AutoConfiguration() { + } + + @Bean + @ConditionalOnMissingBean + public InetIPv6Utils inetUtils(InetUtilsProperties properties) { + return new InetIPv6Utils(properties); + } +}