Browse Source

Support uri without port for default service instance (#1054)

pull/1073/head
Neil Powell 3 years ago committed by GitHub
parent
commit
757900e77a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      spring-cloud-commons/src/main/java/org/springframework/cloud/client/DefaultServiceInstance.java
  2. 22
      spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryClientTests.java

9
spring-cloud-commons/src/main/java/org/springframework/cloud/client/DefaultServiceInstance.java

@ -27,6 +27,7 @@ import java.util.Objects; @@ -27,6 +27,7 @@ import java.util.Objects;
* @author Spencer Gibb
* @author Tim Ysewyn
* @author Charu Covindane
* @author Neil Powell
*/
public class DefaultServiceInstance implements ServiceInstance {
@ -79,11 +80,15 @@ public class DefaultServiceInstance implements ServiceInstance { @@ -79,11 +80,15 @@ public class DefaultServiceInstance implements ServiceInstance {
/**
* Creates a URI from the given ServiceInstance's host:port.
* @param instance the ServiceInstance.
* @return URI of the form (secure)?https:http + "host:port".
* @return URI of the form (secure)?https:http + "host:port". Scheme port default used if port not set.
*/
public static URI getUri(ServiceInstance instance) {
String scheme = (instance.isSecure()) ? "https" : "http";
String uri = String.format("%s://%s:%s", scheme, instance.getHost(), instance.getPort());
int port = instance.getPort();
if (port <= 0) {
port = (instance.isSecure()) ? 443 : 80;
}
String uri = String.format("%s://%s:%s", scheme, instance.getHost(), port);
return URI.create(uri);
}

22
spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryClientTests.java

@ -33,6 +33,7 @@ import static org.assertj.core.api.BDDAssertions.then; @@ -33,6 +33,7 @@ import static org.assertj.core.api.BDDAssertions.then;
/**
* @author Biju Kunjummen
* @author Charu Covindane
* @author Neil Powell
*/
public class SimpleDiscoveryClientTests {
@ -44,8 +45,9 @@ public class SimpleDiscoveryClientTests { @@ -44,8 +45,9 @@ public class SimpleDiscoveryClientTests {
Map<String, List<DefaultServiceInstance>> map = new HashMap<>();
DefaultServiceInstance service1Inst1 = new DefaultServiceInstance(null, null, "host1", 8080, false);
DefaultServiceInstance service1Inst2 = new DefaultServiceInstance(null, null, "host2", 8443, true);
map.put("service1", Arrays.asList(service1Inst1, service1Inst2));
DefaultServiceInstance service1Inst2 = new DefaultServiceInstance(null, null, "host2", 0, true);
DefaultServiceInstance service1Inst3 = new DefaultServiceInstance(null, null, "host3", 0, false);
map.put("service1", Arrays.asList(service1Inst1, service1Inst2, service1Inst3));
simpleDiscoveryProperties.setInstances(map);
simpleDiscoveryProperties.afterPropertiesSet();
this.simpleDiscoveryClient = new SimpleDiscoveryClient(simpleDiscoveryProperties);
@ -54,13 +56,27 @@ public class SimpleDiscoveryClientTests { @@ -54,13 +56,27 @@ public class SimpleDiscoveryClientTests {
@Test
public void shouldBeAbleToRetrieveServiceDetailsByName() {
List<ServiceInstance> instances = this.simpleDiscoveryClient.getInstances("service1");
then(instances.size()).isEqualTo(2);
then(instances.size()).isEqualTo(3);
then(instances.get(0).getServiceId()).isEqualTo("service1");
then(instances.get(0).getHost()).isEqualTo("host1");
then(instances.get(0).getPort()).isEqualTo(8080);
then(instances.get(0).getUri()).isEqualTo(URI.create("http://host1:8080"));
then(instances.get(0).isSecure()).isEqualTo(false);
then(instances.get(0).getMetadata()).isNotNull();
then(instances.get(1).getServiceId()).isEqualTo("service1");
then(instances.get(1).getHost()).isEqualTo("host2");
then(instances.get(1).getPort()).isEqualTo(0);
then(instances.get(1).getUri()).isEqualTo(URI.create("https://host2:443"));
then(instances.get(1).isSecure()).isEqualTo(true);
then(instances.get(1).getMetadata()).isNotNull();
then(instances.get(2).getServiceId()).isEqualTo("service1");
then(instances.get(2).getHost()).isEqualTo("host3");
then(instances.get(2).getPort()).isEqualTo(0);
then(instances.get(2).getUri()).isEqualTo(URI.create("http://host3:80"));
then(instances.get(2).isSecure()).isEqualTo(false);
then(instances.get(2).getMetadata()).isNotNull();
}
}

Loading…
Cancel
Save