[Feature]support register IPv6

pull/2732/head
lai hui 3 years ago committed by Steve Rao
parent af75b81424
commit 7544020047

@ -17,6 +17,7 @@
package com.alibaba.cloud.nacos;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
@ -31,6 +32,7 @@ import javax.annotation.PostConstruct;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.cloud.nacos.event.NacosDiscoveryInfoChangedEvent;
import com.alibaba.cloud.nacos.utils.InetIPv6Util;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.PreservedMetadataKeys;
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
@ -162,6 +164,11 @@ public class NacosDiscoveryProperties {
*/
private String networkInterface = "";
/**
* choose IPv4 or IPv6,if you don't set it will choose IPv4.
*/
private String ipType = "IPv4";
/**
* The port your want to register for your service instance, needn't to set it if the
* auto detect port works well.
@ -220,6 +227,9 @@ public class NacosDiscoveryProperties {
*/
private boolean failFast = true;
@Autowired
private InetIPv6Util inetIPv6Util;
@Autowired
private InetUtils inetUtils;
@ -251,7 +261,16 @@ public class NacosDiscoveryProperties {
if (StringUtils.isEmpty(ip)) {
// traversing network interfaces if didn't specify a interface
if (StringUtils.isEmpty(networkInterface)) {
ip = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
if ("IPv4".equalsIgnoreCase(ipType)) {
ip = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
}
else if ("IPv6".equalsIgnoreCase(ipType)) {
ip = inetIPv6Util.findAddress();
}
else {
throw new IllegalArgumentException(
"please checking the type of IP " + ipType);
}
}
else {
NetworkInterface netInterface = NetworkInterface
@ -265,6 +284,7 @@ public class NacosDiscoveryProperties {
while (inetAddress.hasMoreElements()) {
InetAddress currentAddress = inetAddress.nextElement();
if (currentAddress instanceof Inet4Address
|| currentAddress instanceof Inet6Address
&& !currentAddress.isLoopbackAddress()) {
ip = currentAddress.getHostAddress();
break;
@ -364,6 +384,14 @@ public class NacosDiscoveryProperties {
this.ip = ip;
}
public String getIpType() {
return ipType;
}
public void setIpType(String ipType) {
this.ipType = ipType;
}
public String getNetworkInterface() {
return networkInterface;
}

@ -0,0 +1,190 @@
/*
* 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.utils;
import java.io.Closeable;
import java.io.IOException;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import com.fasterxml.jackson.databind.deser.DataFormatReaders;
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 InetIPv6Util implements Closeable {
private final ExecutorService executorService;
private final Log log = LogFactory.getLog(InetIPv6Util.class);
private final InetUtilsProperties properties;
@Override
public void close() {
this.executorService.shutdown();
}
public InetIPv6Util(final InetUtilsProperties properties) {
this.properties = properties;
this.executorService = Executors.newSingleThreadExecutor((r) -> {
Thread thread = new Thread(r);
thread.setName("spring.cloud.alibaba.inetIPV6Util");
thread.setDaemon(true);
return thread;
});
}
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<NetworkInterface> 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<InetAddress> 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;
}
public String findAddress(){
String ip = findFirstNonLoopbackHostInfo().getIpAddress();
int index = ip.indexOf('%');
ip = index > 0 ? ip.substring(0, index) : ip;
return iPv6Format(ip);
}
public String iPv6Format(String ip){
return "[" + ip + "]";
}
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<String> 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<String> 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;
}
}

@ -0,0 +1,44 @@
/*
* 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.utils;
import com.alibaba.cloud.nacos.ConditionalOnNacosDiscoveryEnabled;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled;
import org.springframework.cloud.commons.util.InetUtilsProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author HH
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnDiscoveryEnabled
@ConditionalOnNacosDiscoveryEnabled
public class UtilIPv6AutoConfiguration {
public UtilIPv6AutoConfiguration() {
}
@Bean
@ConditionalOnMissingBean
public InetIPv6Util inetIPv6Util(InetUtilsProperties properties) {
return new InetIPv6Util(properties);
}
}

@ -6,7 +6,8 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.alibaba.cloud.nacos.discovery.reactive.NacosReactiveDiscoveryClientConfiguration,\
com.alibaba.cloud.nacos.discovery.configclient.NacosConfigServerAutoConfiguration,\
com.alibaba.cloud.nacos.loadbalancer.LoadBalancerNacosAutoConfiguration,\
com.alibaba.cloud.nacos.NacosServiceAutoConfiguration
com.alibaba.cloud.nacos.NacosServiceAutoConfiguration,\
com.alibaba.cloud.nacos.utils.UtilIPv6AutoConfiguration
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.alibaba.cloud.nacos.discovery.configclient.NacosDiscoveryClientConfigServiceBootstrapConfiguration
org.springframework.context.ApplicationListener=\

@ -18,6 +18,7 @@ package com.alibaba.cloud.nacos.discovery;
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.cloud.nacos.NacosServiceAutoConfiguration;
import com.alibaba.cloud.nacos.utils.UtilIPv6AutoConfiguration;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
@ -33,6 +34,7 @@ public class NacosDiscoveryAutoConfigurationTests {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(UtilAutoConfiguration.class,
UtilIPv6AutoConfiguration.class,
NacosServiceAutoConfiguration.class,
NacosDiscoveryAutoConfiguration.class));

@ -18,6 +18,7 @@ package com.alibaba.cloud.nacos.discovery;
import com.alibaba.cloud.nacos.NacosServiceAutoConfiguration;
import com.alibaba.cloud.nacos.registry.NacosServiceRegistryAutoConfiguration;
import com.alibaba.cloud.nacos.utils.UtilIPv6AutoConfiguration;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
@ -41,6 +42,7 @@ public class NacosDiscoveryClientConfigurationTest {
AutoServiceRegistrationConfiguration.class,
NacosServiceRegistryAutoConfiguration.class,
UtilAutoConfiguration.class,
UtilIPv6AutoConfiguration.class,
NacosServiceAutoConfiguration.class,
NacosDiscoveryAutoConfiguration.class,
NacosDiscoveryClientConfiguration.class, this.getClass()));

@ -18,6 +18,7 @@ package com.alibaba.cloud.nacos.discovery.reactive;
import com.alibaba.cloud.nacos.NacosServiceAutoConfiguration;
import com.alibaba.cloud.nacos.discovery.NacosDiscoveryAutoConfiguration;
import com.alibaba.cloud.nacos.utils.UtilIPv6AutoConfiguration;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
@ -34,6 +35,7 @@ public class NacosReactiveDiscoveryClientConfigurationTests {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(UtilAutoConfiguration.class,
UtilIPv6AutoConfiguration.class,
NacosDiscoveryAutoConfiguration.class,
NacosServiceAutoConfiguration.class,
NacosReactiveDiscoveryClientConfiguration.class));

Loading…
Cancel
Save