spring-cloud / spring-cloud-commons

Common classes used in different Spring Cloud implementations
Apache License 2.0
707 stars 705 forks source link

Property spring.cloud.client.hostname ignores spring.cloud.inetutils.ignoredInterfaces #113

Open Aloren opened 8 years ago

Aloren commented 8 years ago

I have added the following property to my application.properties:

spring.cloud.inetutils.ignoredInterfaces=lo.*

Enpoint /env shows following:

"applicationConfig: [classpath:/application.properties]": {
# other props ommited
"spring.cloud.inetutils.ignoredInterfaces": "lo.*",
},
"springCloudClientHostInfo": {
"spring.cloud.client.hostname": "222.222.222.222",
"spring.cloud.client.ipAddress": "222.222.222.222"
}

Ip address 222.222.222.222 is loopback interface. ifconfig of the machine:

eth0      Link encap:Ethernet  HWaddr -----
          inet addr:ip1  Bcast:ip2  Mask:mask
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:13203005620 errors:0 dropped:0 overruns:0 frame:0
          TX packets:12536115751 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:19445457457463 (19.4 TB)  TX bytes:13799195707294 (13.7 TB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:49268347035 errors:0 dropped:0 overruns:0 frame:0
          TX packets:49268347035 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:33269183251521 (33.2 TB)  TX bytes:33269183251521 (33.2 TB)

lo:1      Link encap:Local Loopback  
          inet addr:222.222.222.222  Mask:255.255.255.255
          UP LOOPBACK RUNNING  MTU:65536  Metric:1

I suppose this is not correct behaviour for springCloudClientHostInfo. I debugged HostInfoEnvironmentPostProcessor where the properties spring.cloud.client.hostname and spring.cloud.client.ipAddress are added to environment -- at this stage InetUtilsProperties ignored interfaces variable is empty (looks like it was not yet postprocessed). Is there a quick fix for the issue?

spencergibb commented 8 years ago

@Aloren are you using config server? if so it may need to go in bootstrap.properties.

Aloren commented 8 years ago

@spencergibb No, I'm not using config server. How is it related to it? Code in HostInfoEnvironmentPostProcessor uses static method from InetUtils:

    public static HostInfo getFirstNonLoopbackHostInfo() {
        return instance.findFirstNonLoopbackHostInfo();
    }

Instance is initialized the following way:

    private static final InetUtils instance = new InetUtils(new InetUtilsProperties());

This way InetUtilsProperties will not see any set up ignoredInterfaces.

dsyer commented 8 years ago

I want to get rid of that static method. But application.properties is always going to be too late to influence the behaviour of the host info post processor. The whole point is that it sets up reasonable defaults for stuff that you then want to configure using placeholders in application.properties.

Aloren commented 8 years ago

Can't we use bootstrap.properties? As I know it is initialised earlier than application.properties.

dsyer commented 8 years ago

See ff59dc8 for the deprecation. With that change you might be able to set ignoredInterfaces in bootstrap.properties (or via system properties).

Aloren commented 8 years ago

Thank you! That is exactly what I needed!

deripas commented 7 years ago

I have the same problem (version 2.0.0.M4). My bootstrap.yml:

spring:
  cloud:
    inetutils:
      preferred-networks:
        - 10.0.
      ignored-interfaces:
        - lo

But in HostInfoEnvironmentPostProcessor my configure ignored and i can not using @Value("${spring.cloud.client.ip-address}") (because ip not correct).

I have to use:

    @Autowired
    private InetUtils inetUtils;
...
String address = inetUtils.findFirstNonLoopbackAddress().getHostAddress();