From 532e11aa34ce9f9b6269fa8e952312a6f2517337 Mon Sep 17 00:00:00 2001 From: Steve Rao Date: Tue, 28 Mar 2023 18:27:45 +0800 Subject: [PATCH] fix: added IPv6 address replace condition (#3232) fix: added IPv6 address replace condition --- .../alibaba/cloud/nacos/ribbon/NacosRule.java | 27 +++++-------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/ribbon/NacosRule.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/ribbon/NacosRule.java index 9d4bd929a..b95559b0d 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/ribbon/NacosRule.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/ribbon/NacosRule.java @@ -38,7 +38,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.cloud.commons.util.InetUtils; import org.springframework.util.CollectionUtils; /** @@ -56,8 +55,6 @@ public class NacosRule extends AbstractLoadBalancerRule { private static final String IPV6_KEY = "IPv6"; - private String ipv4; - private String ipv6; @Autowired @@ -69,23 +66,14 @@ public class NacosRule extends AbstractLoadBalancerRule { @Autowired private InetIPv6Utils inetIPv6Utils; - @Autowired - private InetUtils inetUtils; - @PostConstruct public void init() { String ip = nacosDiscoveryProperties.getIp(); if (StringUtils.isNotEmpty(ip)) { - if (Pattern.matches(IPV4_REGEX, ip)) { - this.ipv4 = ip; - this.ipv6 = nacosDiscoveryProperties.getMetadata().get(IPV6_KEY); - } - else { - this.ipv6 = ip; - } + this.ipv6 = Pattern.matches(IPV4_REGEX, ip) + ? nacosDiscoveryProperties.getMetadata().get(IPV6_KEY) : ip; } else { - this.ipv4 = getAppLocalIPv4Address(); this.ipv6 = getAppLocalIPv6Address(); } } @@ -123,7 +111,10 @@ public class NacosRule extends AbstractLoadBalancerRule { } Instance instance = ExtendBalancer.getHostByRandomWeight2(instancesToChoose); - convertIPv4ToIPv6(instance); + // When local support IPv6 address stack, referred to use IPv6 address. + if (StringUtils.isNotEmpty(this.ipv6)) { + convertIPv4ToIPv6(instance); + } return new NacosServer(instance); } @@ -137,10 +128,6 @@ public class NacosRule extends AbstractLoadBalancerRule { public void initWithNiwsConfig(IClientConfig iClientConfig) { } - private String getAppLocalIPv4Address() { - return inetUtils.findFirstNonLoopbackHostInfo().getIpAddress(); - } - private String getAppLocalIPv6Address() { return inetIPv6Utils.findIPv6Address(); } @@ -158,7 +145,7 @@ public class NacosRule extends AbstractLoadBalancerRule { ipv6InstanceList.add(instance); } } - // provider has no IPv6,should use Ipv4. + // provider has no IPv6, should use IPv4. if (ipv6InstanceList.size() == 0) { return instances.stream() .filter(instance -> Pattern.matches(IPV4_REGEX, instance.getIp()))