diff --git a/spring-cloud-sleuth-stream/src/main/java/org/springframework/cloud/sleuth/stream/ServerPropertiesHostLocator.java b/spring-cloud-sleuth-stream/src/main/java/org/springframework/cloud/sleuth/stream/ServerPropertiesHostLocator.java index 1a9c90f4d..2bc0c1c3b 100644 --- a/spring-cloud-sleuth-stream/src/main/java/org/springframework/cloud/sleuth/stream/ServerPropertiesHostLocator.java +++ b/spring-cloud-sleuth-stream/src/main/java/org/springframework/cloud/sleuth/stream/ServerPropertiesHostLocator.java @@ -89,7 +89,7 @@ public class ServerPropertiesHostLocator implements HostLocator { return this.port; } Integer port; - if (this.serverProperties != null && this.serverProperties.getPort() != null) { + if (this.serverProperties != null && this.serverProperties.getPort() != null && this.serverProperties.getPort() > 0) { port = this.serverProperties.getPort(); } else { diff --git a/spring-cloud-sleuth-stream/src/test/java/org/springframework/cloud/sleuth/stream/ServerPropertiesHostLocatorTests.java b/spring-cloud-sleuth-stream/src/test/java/org/springframework/cloud/sleuth/stream/ServerPropertiesHostLocatorTests.java index 22ca4ea5c..6c3cc0ae9 100644 --- a/spring-cloud-sleuth-stream/src/test/java/org/springframework/cloud/sleuth/stream/ServerPropertiesHostLocatorTests.java +++ b/spring-cloud-sleuth-stream/src/test/java/org/springframework/cloud/sleuth/stream/ServerPropertiesHostLocatorTests.java @@ -89,6 +89,17 @@ public class ServerPropertiesHostLocatorTests { assertThat(locator.locate(this.span).getServiceName()).isEqualTo("foo"); } + @Test + public void negativePortFromServerProperties() throws UnknownHostException { + ServerProperties properties = new ServerProperties(); + properties.setPort(-1); + + ServerPropertiesHostLocator locator = new ServerPropertiesHostLocator(properties, + "unknown", new ZipkinProperties(),localAddress(ADR1234)); + + assertThat(locator.locate(this.span).getPort()).isEqualTo((short) 8080); + } + private InetUtils localAddress(byte[] address) throws UnknownHostException { InetUtils mocked = Mockito.spy(new InetUtils(new InetUtilsProperties())); Mockito.when(mocked.findFirstNonLoopbackAddress()) diff --git a/spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin/ServerPropertiesEndpointLocator.java b/spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin/ServerPropertiesEndpointLocator.java index 405447adc..abc481d95 100644 --- a/spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin/ServerPropertiesEndpointLocator.java +++ b/spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin/ServerPropertiesEndpointLocator.java @@ -94,7 +94,7 @@ public class ServerPropertiesEndpointLocator implements EndpointLocator { return this.port; } Integer port; - if (this.serverProperties!=null && this.serverProperties.getPort() != null) { + if (this.serverProperties!=null && this.serverProperties.getPort() != null && this.serverProperties.getPort() > 0) { port = this.serverProperties.getPort(); } else { diff --git a/spring-cloud-sleuth-zipkin/src/test/java/org/springframework/cloud/sleuth/zipkin/ServerPropertiesEndpointLocatorTests.java b/spring-cloud-sleuth-zipkin/src/test/java/org/springframework/cloud/sleuth/zipkin/ServerPropertiesEndpointLocatorTests.java index 92304542c..46e826eb6 100644 --- a/spring-cloud-sleuth-zipkin/src/test/java/org/springframework/cloud/sleuth/zipkin/ServerPropertiesEndpointLocatorTests.java +++ b/spring-cloud-sleuth-zipkin/src/test/java/org/springframework/cloud/sleuth/zipkin/ServerPropertiesEndpointLocatorTests.java @@ -83,6 +83,17 @@ public class ServerPropertiesEndpointLocatorTests { assertThat(locator.local().serviceName).isEqualTo("foo"); } + @Test + public void negativePortFromServerProperties() throws UnknownHostException { + ServerProperties properties = new ServerProperties(); + properties.setPort(-1); + + ServerPropertiesEndpointLocator locator = new ServerPropertiesEndpointLocator( + properties, "unknown", new ZipkinProperties(),localAddress(ADDRESS1234)); + + assertThat(locator.local().port).isEqualTo((short) 8080); + } + private InetUtils localAddress(byte[] address) throws UnknownHostException { InetUtils mocked = Mockito.spy(new InetUtils(new InetUtilsProperties())); Mockito.when(mocked.findFirstNonLoopbackAddress())