spring-cloud-commons icon indicating copy to clipboard operation
spring-cloud-commons copied to clipboard

${spring.cloud.client.ip-address} ignore spring.cloud.inetutils.preferred-networks

Open cooperlyt opened this issue 2 years ago • 4 comments

Describe the bug Environment:

Spring boot:3.0.4 Spring cloud:2022.0.1

spring-cloud-commons: 4.0.1 OpenJdk: 17

When preferredNetworks are configured in a multi-network interface card environment, ${spring.cloud.client.ip-address} return a wrong IP. application.yml

spring:
  cloud:
    inetutils:
      use-only-site-local-interfaces: false
      preferred-networks:
        - 172.16.

Look like a configured loading error. I trace the source code.

image

preferredNetworks list is always empty!

Sample

set spring.cloud.inetutils.preferred-networks is 172.16. in application.yml,test fail!

@Slf4j
@ExtendWith(SpringExtension.class)
@SpringBootTest
public class IpTest {

  @Autowired
  private InetUtils inetUtils;

  @Value("${spring.cloud.client.ip-address}")
  private String ip;

  @Autowired
  private InetUtilsProperties properties;

//fail
  @Test
  public void testEnvironmentIP(){
    String regex = "172.16.";
    assertTrue(ip == null || "127.0.0.1".equals(ip) ||  ip.matches(regex) || ip.startsWith(regex));
  }

//pass
  @Test
  public void testFirstNonLoopbackAddress(){
    String regex = "172.16.";
    InetAddress hostAddress = inetUtils.findFirstNonLoopbackAddress();
    assertTrue(hostAddress == null || "127.0.0.1".equals(hostAddress.getHostAddress()) || hostAddress.getHostAddress().matches(regex) || hostAddress.getHostAddress().startsWith(regex));
  }


 //pass
  @Test
  public void testProperties(){
    String regex = "172.16.";

    assertFalse(properties.isUseOnlySiteLocalInterfaces());  //pass
    assertTrue(properties.getPreferredNetworks().contains(regex)); //pass

    InetAddress hostAddress = new InetUtils(properties).findFirstNonLoopbackAddress();
    assertTrue(hostAddress == null || "127.0.0.1".equals(hostAddress.getHostAddress()) || hostAddress.getHostAddress().matches(regex) || hostAddress.getHostAddress().startsWith(regex));
  }

}

cooperlyt avatar Mar 16 '23 03:03 cooperlyt

oh! if I want used preferred-networks, then use-only-site-local-interfaces must be false!

cooperlyt avatar Mar 18 '23 00:03 cooperlyt

${spring.cloud.client.ip-address} still ignore preferred-networks

cooperlyt avatar Mar 18 '23 00:03 cooperlyt