Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions docs/src/main/asciidoc/spring-cloud-commons.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ TIP: If you see errors like `java.lang.IllegalArgumentException: Can not set org
[[ignore-network-interfaces]]
=== Ignore Network Interfaces

Sometimes it is useful to ignore certain named network interfaces so they can be excluded from Service Discovery registration (eg. running in a Docker container). A list of regular expressions can be set that will cause the desired network interfaces to be ignored. The following configuration will ignore the "docker0" interface and all interfaces that start with "veth".
Sometimes it is useful to ignore certain named network interfaces so they can be excluded from Service Discovery registration (eg. running in a Docker container). A list of regular expressions can be set that will cause the desired network interfaces to be ignored. The following configuration will ignore the "docker0" interface and all interfaces that start with "veth".

.application.yml
----
Expand All @@ -403,4 +403,26 @@ spring:
ignoredInterfaces:
- docker0
- veth.*
----
----

You can also force to use only specified network addresses using list of regular expressions:

.application.yml
----
spring:
cloud:
inetutils:
preferredNetworks:
- 192.168
- 10.0
----

You can also force to use only site local addresses. See https://docs.oracle.com/javase/8/docs/api/java/net/Inet4Address.html#isSiteLocalAddress--[Inet4Address.html.isSiteLocalAddress()] for more details what is site local address.

.application.yml
----
spring:
cloud:
inetutils:
useOnlySiteLocalInterfaces: true
----
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ else if (result != null) {
.getInetAddresses(); addrs.hasMoreElements();) {
InetAddress address = addrs.nextElement();
if (address instanceof Inet4Address
&& !address.isLoopbackAddress()) {
&& !address.isLoopbackAddress()
&& !ignoreAddress(address)) {
log.trace("Found non-loopback interface: "
+ ifc.getDisplayName());
result = address;
Expand Down Expand Up @@ -127,6 +128,22 @@ else if (result != null) {
return null;
}

boolean ignoreAddress(InetAddress address) {

if (this.properties.isUseOnlySiteLocalInterfaces() && !address.isSiteLocalAddress()) {
log.trace("Ignoring address: " + address.getHostAddress());
return true;
}

for (String regex : this.properties.getPreferredNetworks()) {
Copy link
Member

@spencergibb spencergibb Aug 17, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused here. Why is prefferedNetworks being ignored?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look like a bug.

if (address.getHostAddress().matches(regex) || address.getHostAddress().startsWith(regex))

must be replaced by:

if (!address.getHostAddress().matches(regex) && !address.getHostAddress().startsWith(regex))

Now I use only useOnlySiteLocalInterfaces on production. prefferedNetworks is planned to be used in next months.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

if (address.getHostAddress().matches(regex) || address.getHostAddress().startsWith(regex)) {
log.trace("Ignoring address: " + address.getHostAddress());
return true;
}
}
return false;
}

boolean ignoreInterface(String interfaceName) {
for (String regex : this.properties.getIgnoredInterfaces()) {
if (interfaceName.matches(regex)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.springframework.cloud.commons.util;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -36,4 +37,14 @@ public class InetUtilsProperties {
* List of Java regex expressions for network interfaces that will be ignored.
*/
private List<String> ignoredInterfaces = new ArrayList<>();

/**
* Use only interfaces with site local addresses. See {@link InetAddress#isSiteLocalAddress()} for more details.
*/
private boolean useOnlySiteLocalInterfaces = false;

/**
* List of Java regex expressions for network addresses that will be ignored.
*/
private List<String> preferredNetworks = new ArrayList<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,36 @@ public void testDefaultIgnoreInterface() {
}
}

@Test
public void testSiteLocalAddresses() throws Exception {
InetUtilsProperties properties = new InetUtilsProperties();
properties.setUseOnlySiteLocalInterfaces(true);

try (InetUtils utils = new InetUtils(properties)) {
assertFalse(utils.ignoreAddress(InetAddress.getByName("192.168.0.1")));
assertTrue(utils.ignoreAddress(InetAddress.getByName("5.5.8.1")));
}
}

@Test
public void testPrefferedNetworksRegex() throws Exception {
InetUtilsProperties properties = new InetUtilsProperties();
properties.setPreferredNetworks(Arrays.asList("192.168.*"));

try (InetUtils utils = new InetUtils(properties)) {
assertTrue(utils.ignoreAddress(InetAddress.getByName("192.168.0.1")));
assertFalse(utils.ignoreAddress(InetAddress.getByName("5.5.8.1")));
}
}

@Test
public void testPrefferedNetworksSimple() throws Exception {
InetUtilsProperties properties = new InetUtilsProperties();
properties.setPreferredNetworks(Arrays.asList("192"));

try (InetUtils utils = new InetUtils(properties)) {
assertTrue(utils.ignoreAddress(InetAddress.getByName("192.168.0.1")));
assertFalse(utils.ignoreAddress(InetAddress.getByName("5.5.8.1")));
}
}
}