diff --git a/eureka-client/src/main/java/com/netflix/appinfo/AbstractInstanceConfig.java b/eureka-client/src/main/java/com/netflix/appinfo/AbstractInstanceConfig.java index e723a321e..ccf5ee1ea 100644 --- a/eureka-client/src/main/java/com/netflix/appinfo/AbstractInstanceConfig.java +++ b/eureka-client/src/main/java/com/netflix/appinfo/AbstractInstanceConfig.java @@ -211,6 +211,8 @@ public String getIpAddress() { return hostInfo.first(); } + public boolean shouldBroadcastPublicIpv4Addr () { return false; } + private static Pair getHostInfo() { Pair pair; try { diff --git a/eureka-client/src/main/java/com/netflix/appinfo/CloudInstanceConfig.java b/eureka-client/src/main/java/com/netflix/appinfo/CloudInstanceConfig.java index 835917d22..356a7052c 100644 --- a/eureka-client/src/main/java/com/netflix/appinfo/CloudInstanceConfig.java +++ b/eureka-client/src/main/java/com/netflix/appinfo/CloudInstanceConfig.java @@ -128,8 +128,16 @@ public String getHostName(boolean refresh) { @Override public String getIpAddress() { - String ipAddr = amazonInfoHolder.get().get(MetaDataKey.localIpv4); - return ipAddr == null ? super.getIpAddress() : ipAddr; + return this.shouldBroadcastPublicIpv4Addr() ? getPublicIpv4Addr() : getPrivateIpv4Addr(); + } + + private String getPrivateIpv4Addr() { + String privateIpv4Addr = amazonInfoHolder.get().get(MetaDataKey.localIpv4); + return privateIpv4Addr == null ? super.getIpAddress() : privateIpv4Addr; + } + private String getPublicIpv4Addr() { + String publicIpv4Addr = amazonInfoHolder.get().get(MetaDataKey.publicIpv4); + return publicIpv4Addr == null ? super.getIpAddress() : publicIpv4Addr; } @Override diff --git a/eureka-client/src/main/java/com/netflix/appinfo/PropertiesInstanceConfig.java b/eureka-client/src/main/java/com/netflix/appinfo/PropertiesInstanceConfig.java index 05d35a6c3..85fdeb0d4 100644 --- a/eureka-client/src/main/java/com/netflix/appinfo/PropertiesInstanceConfig.java +++ b/eureka-client/src/main/java/com/netflix/appinfo/PropertiesInstanceConfig.java @@ -294,6 +294,14 @@ public String[] getDefaultAddressResolutionOrder() { return result == null ? new String[0] : result.split(","); } + /** + * Indicates if the public ipv4 address of the instance should be advertised. + * @return true if the public ipv4 address of the instance should be advertised, false otherwise . + */ + public boolean shouldBroadcastPublicIpv4Addr() { + return configInstance.getBooleanProperty(namespace + BROADCAST_PUBLIC_IPV4_ADDR_KEY, super.shouldBroadcastPublicIpv4Addr()).get(); + } + @Override public String getNamespace() { return this.namespace; diff --git a/eureka-client/src/main/java/com/netflix/appinfo/PropertyBasedInstanceConfigConstants.java b/eureka-client/src/main/java/com/netflix/appinfo/PropertyBasedInstanceConfigConstants.java index 1410c43ff..28747d3c0 100644 --- a/eureka-client/src/main/java/com/netflix/appinfo/PropertyBasedInstanceConfigConstants.java +++ b/eureka-client/src/main/java/com/netflix/appinfo/PropertyBasedInstanceConfigConstants.java @@ -36,6 +36,7 @@ final class PropertyBasedInstanceConfigConstants { static final String INSTANCE_METADATA_PREFIX = "metadata"; static final String DEFAULT_ADDRESS_RESOLUTION_ORDER_KEY = "defaultAddressResolutionOrder"; + static final String BROADCAST_PUBLIC_IPV4_ADDR_KEY = "broadcastPublicIpv4"; static final String TRAFFIC_ENABLED_ON_INIT_KEY = "traffic.enabled"; diff --git a/eureka-client/src/test/java/com/netflix/appinfo/CloudInstanceConfigTest.java b/eureka-client/src/test/java/com/netflix/appinfo/CloudInstanceConfigTest.java index c4c8bba81..e7fc18813 100644 --- a/eureka-client/src/test/java/com/netflix/appinfo/CloudInstanceConfigTest.java +++ b/eureka-client/src/test/java/com/netflix/appinfo/CloudInstanceConfigTest.java @@ -8,6 +8,8 @@ import static com.netflix.appinfo.AmazonInfo.MetaDataKey.publicHostname; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertEquals; + /** * @author David Liu @@ -38,6 +40,16 @@ public void testResolveDefaultAddress() { assertThat(config.resolveDefaultAddress(false), is(dummyDefault)); } + @Test + public void testBroadcastPublicIpv4Address() { + AmazonInfo info = (AmazonInfo) instanceInfo.getDataCenterInfo(); + + config = createConfig(info); + + // this should work because the test utils class sets the ipAddr to the public IP of the instance + assertEquals(instanceInfo.getIPAddr(), config.getIpAddress()); + } + private CloudInstanceConfig createConfig(AmazonInfo info) { return new CloudInstanceConfig(info) { @@ -53,6 +65,9 @@ public String[] getDefaultAddressResolutionOrder() { public String getHostName(boolean refresh) { return dummyDefault; } + + @Override + public boolean shouldBroadcastPublicIpv4Addr() { return true; } }; } }