Which Component
Nacos Discovery (spring-cloud-starter-alibaba-nacos-discovery)
Describe the bug
In NacosDiscoveryProperties.init(), the condition that filters addresses on a given network interface has an operator-precedence bug: && binds tighter than ||, so the loopback check only applies to IPv6 addresses. As a result, when a user configures spring.cloud.nacos.discovery.network-interface, an IPv4 loopback address (e.g. 127.0.0.1) may be selected as the registration IP, making the service unreachable from other nodes in the cluster.
The current code:
// File: NacosDiscoveryProperties.java (line 309-314) if (currentAddress instanceof Inet4Address || currentAddress instanceof Inet6Address && !currentAddress.isLoopbackAddress()) { ip = currentAddress.getHostAddress(); break; }
is parsed by Java as:
if (currentAddress instanceof Inet4Address || (currentAddress instanceof Inet6Address && !currentAddress.isLoopbackAddress()))
中文原文:由于 && 优先级高于 ||,IPv4 的 loopback 地址不会被排除,只有 IPv6 的 loopback 地址会被排除。当用户指定网络接口时,可能选到 127.0.0.1 作为注册 IP。
Simplest demo
Run any nacos-discovery example (e.g. nacos-discovery-provider-example) on a machine where the chosen network interface contains both a loopback IPv4 entry and a regular IPv4 entry, and set:
spring:
cloud:
nacos:
discovery:
network-interface: lo0 # or any interface where loopback is enumerated first
To Reproduce
Steps to reproduce the behavior:
- Go to '...'
- Click on '....'
- Scroll down to '....'
- See error
Expected behavior
Screenshots
Additional context
OS: any (verified by static analysis)
JDK: 17
Spring Cloud Alibaba: 2025.1.0.1-SNAPSHOT
Which Component
Nacos Discovery (spring-cloud-starter-alibaba-nacos-discovery)
Describe the bug
In NacosDiscoveryProperties.init(), the condition that filters addresses on a given network interface has an operator-precedence bug: && binds tighter than ||, so the loopback check only applies to IPv6 addresses. As a result, when a user configures spring.cloud.nacos.discovery.network-interface, an IPv4 loopback address (e.g. 127.0.0.1) may be selected as the registration IP, making the service unreachable from other nodes in the cluster.
The current code:
// File: NacosDiscoveryProperties.java (line 309-314) if (currentAddress instanceof Inet4Address || currentAddress instanceof Inet6Address && !currentAddress.isLoopbackAddress()) { ip = currentAddress.getHostAddress(); break; }is parsed by Java as:
if (currentAddress instanceof Inet4Address || (currentAddress instanceof Inet6Address && !currentAddress.isLoopbackAddress()))中文原文:由于 && 优先级高于 ||,IPv4 的 loopback 地址不会被排除,只有 IPv6 的 loopback 地址会被排除。当用户指定网络接口时,可能选到 127.0.0.1 作为注册 IP。
Simplest demo
Run any nacos-discovery example (e.g. nacos-discovery-provider-example) on a machine where the chosen network interface contains both a loopback IPv4 entry and a regular IPv4 entry, and set:
spring:
cloud:
nacos:
discovery:
network-interface: lo0 # or any interface where loopback is enumerated first
To Reproduce
Steps to reproduce the behavior:
Expected behavior
Screenshots
Additional context
OS: any (verified by static analysis)
JDK: 17
Spring Cloud Alibaba: 2025.1.0.1-SNAPSHOT