Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When ipType is IPv6 but there's no IPv6 found, find IPv4(Fixes gh-2802) #2810

Merged
merged 3 commits into from Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -166,6 +166,8 @@ public class NacosDiscoveryProperties {

/**
* choose IPv4 or IPv6,if you don't set it will choose IPv4.
* When IPv6 is chosen but no IPv6 can be found, system will automatically find IPv4 to ensure there is an
* available service address.
*/
private String ipType = "IPv4";

Expand Down Expand Up @@ -260,6 +262,10 @@ public void init() throws Exception {
}
else if ("IPv6".equalsIgnoreCase(ipType)) {
ip = inetIPv6Utils.findIPv6Address();
if (StringUtils.isEmpty(ip)) {
log.warn("There is no available IPv6 found. Spring Cloud Alibaba will automatically find IPv4.");
ip = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
}
}
else {
throw new IllegalArgumentException(
Expand Down
Expand Up @@ -29,6 +29,7 @@
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import com.alibaba.cloud.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

Expand Down Expand Up @@ -66,13 +67,7 @@ public InetUtils.HostInfo findFirstNonLoopbackHostInfo() {
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;
}
return null;
}

public InetAddress findFirstNonLoopbackIPv6Address() {
Expand Down Expand Up @@ -111,26 +106,30 @@ && isPreferredAddress(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");
if (address == null) {
try {
InetAddress localHost = InetAddress.getLocalHost();
if (localHost instanceof Inet6Address && !localHost.isLoopbackAddress()
&& isPreferredAddress(localHost)) {
address = localHost;
}
}
catch (UnknownHostException e) {
log.warn("Unable to retrieve localhost");
}
}

return null;
return address;
}

public String findIPv6Address() {
String ip = findFirstNonLoopbackHostInfo().getIpAddress();
int index = ip.indexOf('%');
ip = index > 0 ? ip.substring(0, index) : ip;
return iPv6Format(ip);
InetUtils.HostInfo hostInfo = findFirstNonLoopbackHostInfo();
String ip = hostInfo != null ? hostInfo.getIpAddress() : "";
if (!StringUtils.isEmpty(ip)) {
int index = ip.indexOf('%');
ip = index > 0 ? ip.substring(0, index) : ip;
return iPv6Format(ip);
}
return ip;
}

public String iPv6Format(String ip) {
Expand Down