Skip to content

Commit 5015b13

Browse files
Allow overriding getScheme on DefaultServiceInstance. (#1168)
1 parent 0637a76 commit 5015b13

File tree

5 files changed

+71
-9
lines changed

5 files changed

+71
-9
lines changed

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

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import java.net.URI;
2020
import java.util.LinkedHashMap;
21+
import java.util.List;
2122
import java.util.Map;
2223
import java.util.Objects;
2324

@@ -28,15 +29,20 @@
2829
* @author Tim Ysewyn
2930
* @author Charu Covindane
3031
* @author Neil Powell
32+
* @author Olga Maciaszek-Sharma
3133
*/
3234
public class DefaultServiceInstance implements ServiceInstance {
3335

36+
private final List<String> secureSchemes = List.of("https", "wss");
37+
3438
private String instanceId;
3539

3640
private String serviceId;
3741

3842
private String host;
3943

44+
private String scheme;
45+
4046
private int port;
4147

4248
private boolean secure;
@@ -45,6 +51,7 @@ public class DefaultServiceInstance implements ServiceInstance {
4551

4652
private URI uri;
4753

54+
4855
/**
4956
* @param instanceId the id of the instance.
5057
* @param serviceId the id of the service.
@@ -55,12 +62,27 @@ public class DefaultServiceInstance implements ServiceInstance {
5562
*/
5663
public DefaultServiceInstance(String instanceId, String serviceId, String host, int port, boolean secure,
5764
Map<String, String> metadata) {
65+
this(instanceId, serviceId, host, port, secure, metadata, secure ? "https" : "http");
66+
}
67+
68+
/**
69+
* @param instanceId the id of the instance.
70+
* @param serviceId the id of the service.
71+
* @param host the host where the service instance can be found.
72+
* @param port the port on which the service is running.
73+
* @param secure indicates whether or not the connection needs to be secure.
74+
* @param metadata a map containing metadata.
75+
* @param scheme the protocol used to connect to the service instance.
76+
*/
77+
public DefaultServiceInstance(String instanceId, String serviceId, String host, int port, boolean secure,
78+
Map<String, String> metadata, String scheme) {
5879
this.instanceId = instanceId;
5980
this.serviceId = serviceId;
6081
this.host = host;
6182
this.port = port;
6283
this.secure = secure;
6384
this.metadata = metadata;
85+
this.scheme = scheme;
6486
}
6587

6688
/**
@@ -77,14 +99,27 @@ public DefaultServiceInstance(String instanceId, String serviceId, String host,
7799
public DefaultServiceInstance() {
78100
}
79101

102+
/**
103+
* @param instanceId the id of the instance.
104+
* @param serviceId the id of the service.
105+
* @param host the host where the service instance can be found.
106+
* @param port the port on which the service is running.
107+
* @param secure indicates whether or not the connection needs to be secure.
108+
* @param scheme the protocol used to connect to the service instance.
109+
*/
110+
public DefaultServiceInstance(String instanceId, String serviceId, String host, int port, boolean secure,
111+
String scheme) {
112+
this(instanceId, serviceId, host, port, secure, new LinkedHashMap<>(), scheme);
113+
}
114+
80115
/**
81116
* Creates a URI from the given ServiceInstance's host:port.
82117
* @param instance the ServiceInstance.
83118
* @return URI of the form (secure)?https:http + "host:port". Scheme port default used
84119
* if port not set.
85120
*/
86121
public static URI getUri(ServiceInstance instance) {
87-
String scheme = (instance.isSecure()) ? "https" : "http";
122+
String scheme = instance.getScheme();
88123
int port = instance.getPort();
89124
if (port <= 0) {
90125
port = (instance.isSecure()) ? 443 : 80;
@@ -148,8 +183,8 @@ public void setUri(URI uri) {
148183
this.uri = uri;
149184
this.host = this.uri.getHost();
150185
this.port = this.uri.getPort();
151-
String scheme = this.uri.getScheme();
152-
if ("https".equals(scheme)) {
186+
scheme = this.uri.getScheme();
187+
if (secureSchemes.contains(scheme)) {
153188
this.secure = true;
154189
}
155190
}
@@ -179,4 +214,9 @@ public int hashCode() {
179214
return Objects.hash(instanceId, serviceId, host, port, secure, metadata);
180215
}
181216

217+
@Override
218+
public String getScheme() {
219+
return scheme;
220+
}
221+
182222
}

spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryProperties.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -87,4 +87,8 @@ public void setInstance(String serviceId, String host, int port) {
8787
local = new DefaultServiceInstance(null, serviceId, host, port, false);
8888
}
8989

90+
public void setInstance(String serviceId, String host, int port, String scheme) {
91+
local = new DefaultServiceInstance(null, serviceId, host, port, false, scheme);
92+
}
93+
9094
}

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@
3232
* Tests for mapping properties to instances in {@link SimpleDiscoveryClient}.
3333
*
3434
* @author Biju Kunjummen
35+
* @author Olga Maciaszek-Sharma
3536
*/
3637

3738
@SpringBootTest(properties = { "spring.application.name=service0",
@@ -73,6 +74,14 @@ public void testDiscoveryClientShouldResolveSimpleValues() {
7374
then(s1.getPort()).isEqualTo(8080);
7475
then(s1.getUri()).isEqualTo(URI.create("http://s11:8080"));
7576
then(s1.isSecure()).isEqualTo(false);
77+
then(s1.getScheme()).isEqualTo("http");
78+
79+
ServiceInstance s2 = this.discoveryClient.getInstances("service1").get(1);
80+
then(s2.getHost()).isEqualTo("s12");
81+
then(s2.getPort()).isEqualTo(8443);
82+
then(s2.getUri()).isEqualTo(URI.create("https://s12:8443"));
83+
then(s2.isSecure()).isEqualTo(true);
84+
then(s2.getScheme()).isEqualTo("https");
7685
}
7786

7887
@Test

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
* @author Biju Kunjummen
3535
* @author Charu Covindane
3636
* @author Neil Powell
37+
* @author Olga Maciaszek-Sharma
3738
*/
3839
public class SimpleDiscoveryClientTests {
3940

@@ -47,7 +48,8 @@ public void setUp() {
4748
DefaultServiceInstance service1Inst1 = new DefaultServiceInstance(null, null, "host1", 8080, false);
4849
DefaultServiceInstance service1Inst2 = new DefaultServiceInstance(null, null, "host2", 0, true);
4950
DefaultServiceInstance service1Inst3 = new DefaultServiceInstance(null, null, "host3", 0, false);
50-
map.put("service1", Arrays.asList(service1Inst1, service1Inst2, service1Inst3));
51+
DefaultServiceInstance service1Inst4 = new DefaultServiceInstance(null, null, "host4", 8443, true);
52+
map.put("service1", Arrays.asList(service1Inst1, service1Inst2, service1Inst3, service1Inst4));
5153
simpleDiscoveryProperties.setInstances(map);
5254
simpleDiscoveryProperties.afterPropertiesSet();
5355
this.simpleDiscoveryClient = new SimpleDiscoveryClient(simpleDiscoveryProperties);
@@ -56,7 +58,7 @@ public void setUp() {
5658
@Test
5759
public void shouldBeAbleToRetrieveServiceDetailsByName() {
5860
List<ServiceInstance> instances = this.simpleDiscoveryClient.getInstances("service1");
59-
then(instances.size()).isEqualTo(3);
61+
then(instances.size()).isEqualTo(4);
6062
then(instances.get(0).getServiceId()).isEqualTo("service1");
6163
then(instances.get(0).getHost()).isEqualTo("host1");
6264
then(instances.get(0).getPort()).isEqualTo(8080);
@@ -77,6 +79,13 @@ public void shouldBeAbleToRetrieveServiceDetailsByName() {
7779
then(instances.get(2).getUri()).isEqualTo(URI.create("http://host3:80"));
7880
then(instances.get(2).isSecure()).isEqualTo(false);
7981
then(instances.get(2).getMetadata()).isNotNull();
82+
83+
then(instances.get(3).getServiceId()).isEqualTo("service1");
84+
then(instances.get(3).getHost()).isEqualTo("host4");
85+
then(instances.get(3).getPort()).isEqualTo(8443);
86+
then(instances.get(3).getUri()).isEqualTo(URI.create("https://host4:8443"));
87+
then(instances.get(3).isSecure()).isEqualTo(true);
88+
then(instances.get(3).getMetadata()).isNotNull();
8089
}
8190

8291
}

spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/core/HealthCheckServiceInstanceListSupplierTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ void shouldCheckInstanceWithProvidedHealthCheckPathWithRestTemplate() {
156156
String serviceId = "ignored-service";
157157
properties.getHealthCheck().getPath().put("ignored-service", "/health");
158158
ServiceInstance serviceInstance = new DefaultServiceInstance("ignored-service-1", serviceId, "127.0.0.1", port,
159-
false);
159+
false, "http");
160160
listSupplier = new HealthCheckServiceInstanceListSupplier(
161161
ServiceInstanceListSuppliers.from(serviceId, serviceInstance),
162162
buildLoadBalancerClientFactory(serviceId, properties), healthCheckFunction(restTemplate));

0 commit comments

Comments
 (0)