Skip to content

NacosDiscoveryProperties picks loopback IP when network-interface is configured #4342

Description

@cowbi

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:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior

Screenshots

Additional context
OS: any (verified by static analysis)
JDK: 17
Spring Cloud Alibaba: 2025.1.0.1-SNAPSHOT

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions