From 1a956c1a34c01ab334960f7efb565f86dbbf6fb9 Mon Sep 17 00:00:00 2001 From: Olga MaciaszekSharma Date: Tue, 30 Mar 2021 15:12:38 +0200 Subject: [PATCH] Support getScheme() in DefaultServiceInstane. --- .../cloud/client/DefaultServiceInstance.java | 45 +++++++++++++++++-- .../simple/SimpleDiscoveryProperties.java | 6 ++- ...DiscoveryClientPropertiesMappingTests.java | 12 ++++- .../simple/SimpleDiscoveryClientTests.java | 3 +- ...CheckServiceInstanceListSupplierTests.java | 2 +- 5 files changed, 60 insertions(+), 8 deletions(-) diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/DefaultServiceInstance.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/DefaultServiceInstance.java index 89b200afe..6bc324b85 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/DefaultServiceInstance.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/DefaultServiceInstance.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,7 @@ package org.springframework.cloud.client; import java.net.URI; +import java.util.Arrays; import java.util.LinkedHashMap; import java.util.Map; import java.util.Objects; @@ -27,6 +28,7 @@ * @author Spencer Gibb * @author Tim Ysewyn * @author Charu Covindane + * @author Olga Maciaszek-Sharma */ public class DefaultServiceInstance implements ServiceInstance { @@ -36,6 +38,8 @@ public class DefaultServiceInstance implements ServiceInstance { private String host; + private String scheme; + private int port; private boolean secure; @@ -54,12 +58,27 @@ public class DefaultServiceInstance implements ServiceInstance { */ public DefaultServiceInstance(String instanceId, String serviceId, String host, int port, boolean secure, Map metadata) { + this(instanceId, serviceId, host, port, secure, metadata, null); + } + + /** + * @param instanceId the id of the instance. + * @param serviceId the id of the service. + * @param host the host where the service instance can be found. + * @param port the port on which the service is running. + * @param secure indicates whether or not the connection needs to be secure. + * @param metadata a map containing metadata. + * @param scheme the protocol used to connect to the service instance. + */ + public DefaultServiceInstance(String instanceId, String serviceId, String host, int port, boolean secure, + Map metadata, String scheme) { this.instanceId = instanceId; this.serviceId = serviceId; this.host = host; this.port = port; this.secure = secure; this.metadata = metadata; + this.scheme = scheme; } /** @@ -76,13 +95,26 @@ public DefaultServiceInstance(String instanceId, String serviceId, String host, public DefaultServiceInstance() { } + /** + * @param instanceId the id of the instance. + * @param serviceId the id of the service. + * @param host the host where the service instance can be found. + * @param port the port on which the service is running. + * @param secure indicates whether or not the connection needs to be secure. + * @param scheme the protocol used to connect to the service instance. + */ + public DefaultServiceInstance(String instanceId, String serviceId, String host, int port, boolean secure, + String scheme) { + this(instanceId, serviceId, host, port, secure, new LinkedHashMap<>(), scheme); + } + /** * Creates a URI from the given ServiceInstance's host:port. * @param instance the ServiceInstance. * @return URI of the form (secure)?https:http + "host:port". */ public static URI getUri(ServiceInstance instance) { - String scheme = (instance.isSecure()) ? "https" : "http"; + String scheme = instance.getScheme(); String uri = String.format("%s://%s:%s", scheme, instance.getHost(), instance.getPort()); return URI.create(uri); } @@ -142,8 +174,8 @@ public void setUri(URI uri) { this.uri = uri; this.host = this.uri.getHost(); this.port = this.uri.getPort(); - String scheme = this.uri.getScheme(); - if ("https".equals(scheme)) { + scheme = this.uri.getScheme(); + if (Arrays.asList("https", "wss").contains(scheme)) { this.secure = true; } } @@ -173,4 +205,9 @@ public int hashCode() { return Objects.hash(instanceId, serviceId, host, port, secure, metadata); } + @Override + public String getScheme() { + return scheme; + } + } diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryProperties.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryProperties.java index b5f9335a1..3ba29c470 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryProperties.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -86,4 +86,8 @@ public void setInstance(String serviceId, String host, int port) { local = new DefaultServiceInstance(null, serviceId, host, port, false); } + public void setInstance(String serviceId, String host, int port, String scheme) { + local = new DefaultServiceInstance(null, serviceId, host, port, false, scheme); + } + } diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryClientPropertiesMappingTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryClientPropertiesMappingTests.java index 3d1a4188b..97d15315b 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryClientPropertiesMappingTests.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryClientPropertiesMappingTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,6 +34,7 @@ * Tests for mapping properties to instances in {@link SimpleDiscoveryClient} * * @author Biju Kunjummen + * @author Olga Maciaszek-Sharma */ @RunWith(SpringRunner.class) @@ -76,6 +77,15 @@ public void testDiscoveryClientShouldResolveSimpleValues() { then(s1.getPort()).isEqualTo(8080); then(s1.getUri()).isEqualTo(URI.create("http://s11:8080")); then(s1.isSecure()).isEqualTo(false); + + then(s1.getScheme()).isEqualTo("http"); + + ServiceInstance s2 = this.discoveryClient.getInstances("service1").get(1); + then(s2.getHost()).isEqualTo("s12"); + then(s2.getPort()).isEqualTo(8443); + then(s2.getUri()).isEqualTo(URI.create("https://s12:8443")); + then(s2.isSecure()).isEqualTo(true); + then(s2.getScheme()).isEqualTo("https"); } @Test diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryClientTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryClientTests.java index 00f84cc91..8bc5956e3 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryClientTests.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryClientTests.java @@ -33,6 +33,7 @@ /** * @author Biju Kunjummen * @author Charu Covindane + * @author Olga Maciaszek-Sharma */ public class SimpleDiscoveryClientTests { @@ -43,7 +44,7 @@ public void setUp() { SimpleDiscoveryProperties simpleDiscoveryProperties = new SimpleDiscoveryProperties(); Map> map = new HashMap<>(); - DefaultServiceInstance service1Inst1 = new DefaultServiceInstance(null, null, "host1", 8080, false); + DefaultServiceInstance service1Inst1 = new DefaultServiceInstance(null, null, "host1", 8080, false, "http"); DefaultServiceInstance service1Inst2 = new DefaultServiceInstance(null, null, "host2", 8443, true); map.put("service1", Arrays.asList(service1Inst1, service1Inst2)); simpleDiscoveryProperties.setInstances(map); diff --git a/spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/core/HealthCheckServiceInstanceListSupplierTests.java b/spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/core/HealthCheckServiceInstanceListSupplierTests.java index d568cc164..318d250ec 100644 --- a/spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/core/HealthCheckServiceInstanceListSupplierTests.java +++ b/spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/core/HealthCheckServiceInstanceListSupplierTests.java @@ -117,7 +117,7 @@ void shouldCheckInstanceWithProvidedHealthCheckPathWithRestTemplate() { String serviceId = "ignored-service"; healthCheck.getPath().put("ignored-service", "/health"); ServiceInstance serviceInstance = new DefaultServiceInstance("ignored-service-1", serviceId, "127.0.0.1", port, - false); + false, "http"); listSupplier = new HealthCheckServiceInstanceListSupplier( ServiceInstanceListSuppliers.from(serviceId, serviceInstance), healthCheck, healthCheckFunction(restTemplate));