Skip to content

Commit 72762d7

Browse files
authored
Deprecates SimpleServiceInstance in favor of DefaultServiceInstance (#835)
* Initial Commit * Added URI to DefaultServiceInstance * Added default constructor * Fixed PR Comments
1 parent 653222d commit 72762d7

File tree

11 files changed

+103
-58
lines changed

11 files changed

+103
-58
lines changed

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

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,23 @@
2626
*
2727
* @author Spencer Gibb
2828
* @author Tim Ysewyn
29+
* @author Charu Covindane
2930
*/
3031
public class DefaultServiceInstance implements ServiceInstance {
3132

32-
private final String instanceId;
33+
private String instanceId;
3334

34-
private final String serviceId;
35+
private String serviceId;
3536

36-
private final String host;
37+
private String host;
3738

38-
private final int port;
39+
private int port;
3940

40-
private final boolean secure;
41+
private boolean secure;
4142

42-
private final Map<String, String> metadata;
43+
private Map<String, String> metadata = new LinkedHashMap<>();
44+
45+
private URI uri;
4346

4447
/**
4548
* @param instanceId the id of the instance.
@@ -98,6 +101,9 @@ public DefaultServiceInstance(String serviceId, String host, int port,
98101
this(serviceId, host, port, secure, new LinkedHashMap<>());
99102
}
100103

104+
public DefaultServiceInstance() {
105+
}
106+
101107
/**
102108
* Creates a URI from the given ServiceInstance's host:port.
103109
* @param instance the ServiceInstance.
@@ -145,6 +151,32 @@ public boolean isSecure() {
145151
return this.secure;
146152
}
147153

154+
public void setInstanceId(String instanceId) {
155+
this.instanceId = instanceId;
156+
}
157+
158+
public void setServiceId(String serviceId) {
159+
this.serviceId = serviceId;
160+
}
161+
162+
public void setHost(String host) {
163+
this.host = host;
164+
}
165+
166+
public void setPort(int port) {
167+
this.port = port;
168+
}
169+
170+
public void setUri(URI uri) {
171+
this.uri = uri;
172+
this.host = this.uri.getHost();
173+
this.port = this.uri.getPort();
174+
String scheme = this.uri.getScheme();
175+
if ("https".equals(scheme)) {
176+
this.secure = true;
177+
}
178+
}
179+
148180
@Override
149181
public String toString() {
150182
return "DefaultServiceInstance{" + "instanceId='" + this.instanceId + '\''

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,17 @@
1919
import java.util.ArrayList;
2020
import java.util.List;
2121

22+
import org.springframework.cloud.client.DefaultServiceInstance;
2223
import org.springframework.cloud.client.ServiceInstance;
2324
import org.springframework.cloud.client.discovery.DiscoveryClient;
24-
import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryProperties.SimpleServiceInstance;
2525

2626
/**
2727
* A {@link org.springframework.cloud.client.discovery.DiscoveryClient} that will use the
2828
* properties file as a source of service instances.
2929
*
3030
* @author Biju Kunjummen
3131
* @author Olga Maciaszek-Sharma
32+
* @author Charu Covindane
3233
*/
3334
public class SimpleDiscoveryClient implements DiscoveryClient {
3435

@@ -46,7 +47,7 @@ public String description() {
4647
@Override
4748
public List<ServiceInstance> getInstances(String serviceId) {
4849
List<ServiceInstance> serviceInstances = new ArrayList<>();
49-
List<SimpleServiceInstance> serviceInstanceForService = this.simpleDiscoveryProperties
50+
List<DefaultServiceInstance> serviceInstanceForService = this.simpleDiscoveryProperties
5051
.getInstances().get(serviceId);
5152

5253
if (serviceInstanceForService != null) {

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.cloud.client.discovery.simple;
1818

19-
import java.net.URI;
20-
2119
import org.springframework.beans.factory.annotation.Autowired;
2220
import org.springframework.beans.factory.annotation.Value;
2321
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
@@ -37,6 +35,7 @@
3735
* Spring Boot auto-configuration for simple properties-based discovery client.
3836
*
3937
* @author Biju Kunjummen
38+
* @author Charu Covindane
4039
*/
4140
@Configuration(proxyBeanMethods = false)
4241
@AutoConfigureBefore({ NoopDiscoveryClientAutoConfiguration.class,
@@ -67,10 +66,9 @@ public void setInet(InetUtils inet) {
6766
public SimpleDiscoveryProperties simpleDiscoveryProperties(
6867
@Value("${spring.application.name:application}") String serviceId) {
6968
simple.getLocal().setServiceId(serviceId);
70-
simple.getLocal()
71-
.setUri(URI.create(
72-
"http://" + this.inet.findFirstNonLoopbackHostInfo().getHostname()
73-
+ ":" + findPort()));
69+
simple.getLocal().setHost(this.inet.findFirstNonLoopbackHostInfo().getHostname());
70+
simple.getLocal().setPort(findPort());
71+
7472
return simple;
7573
}
7674

@@ -95,10 +93,7 @@ private int findPort() {
9593
public void onApplicationEvent(WebServerInitializedEvent webServerInitializedEvent) {
9694
this.port = webServerInitializedEvent.getWebServer().getPort();
9795
if (this.port > 0) {
98-
simple.getLocal()
99-
.setUri(URI.create("http://"
100-
+ this.inet.findFirstNonLoopbackHostInfo().getHostname() + ":"
101-
+ this.port));
96+
simple.getLocal().setPort(this.port);
10297
}
10398
}
10499

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import javax.annotation.PostConstruct;
2626

2727
import org.springframework.boot.context.properties.ConfigurationProperties;
28+
import org.springframework.cloud.client.DefaultServiceInstance;
2829
import org.springframework.cloud.client.ServiceInstance;
2930
import org.springframework.cloud.client.discovery.DiscoveryClient;
3031

@@ -38,31 +39,33 @@
3839
* @author Biju Kunjummen
3940
* @author Olga Maciaszek-Sharma
4041
* @author Tim Ysewyn
42+
* @author Charu Covindane
4143
*/
4244

4345
@ConfigurationProperties(prefix = "spring.cloud.discovery.client.simple")
4446
public class SimpleDiscoveryProperties {
4547

46-
private Map<String, List<SimpleServiceInstance>> instances = new HashMap<>();
48+
private Map<String, List<DefaultServiceInstance>> instances = new HashMap<>();
4749

4850
/**
4951
* The properties of the local instance (if it exists). Users should set these
5052
* properties explicitly if they are exporting data (e.g. metrics) that need to be
5153
* identified by the service instance.
5254
*/
53-
private SimpleServiceInstance local = new SimpleServiceInstance();
55+
private DefaultServiceInstance local = new DefaultServiceInstance(null, null, null, 0,
56+
false);
5457

5558
private int order = DiscoveryClient.DEFAULT_ORDER;
5659

57-
public Map<String, List<SimpleServiceInstance>> getInstances() {
60+
public Map<String, List<DefaultServiceInstance>> getInstances() {
5861
return this.instances;
5962
}
6063

61-
public void setInstances(Map<String, List<SimpleServiceInstance>> instances) {
64+
public void setInstances(Map<String, List<DefaultServiceInstance>> instances) {
6265
this.instances = instances;
6366
}
6467

65-
public SimpleServiceInstance getLocal() {
68+
public DefaultServiceInstance getLocal() {
6669
return this.local;
6770
}
6871

@@ -77,15 +80,22 @@ public void setOrder(int order) {
7780
@PostConstruct
7881
public void init() {
7982
for (String key : this.instances.keySet()) {
80-
for (SimpleServiceInstance instance : this.instances.get(key)) {
83+
for (DefaultServiceInstance instance : this.instances.get(key)) {
8184
instance.setServiceId(key);
8285
}
8386
}
8487
}
8588

89+
public void setInstance(String serviceId, String host, int port) {
90+
local = new DefaultServiceInstance(null, serviceId, host, port, false);
91+
}
92+
8693
/**
8794
* Basic implementation of {@link ServiceInstance}.
95+
*
96+
* @deprecated in favor of {@link DefaultServiceInstance}
8897
*/
98+
@Deprecated
8999
public static class SimpleServiceInstance implements ServiceInstance {
90100

91101
/**

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.cloud.client.discovery.simple.reactive;
1818

19-
import java.net.URI;
20-
2119
import org.springframework.beans.factory.annotation.Autowired;
2220
import org.springframework.beans.factory.annotation.Value;
2321
import org.springframework.boot.actuate.health.ReactiveHealthIndicator;
@@ -44,6 +42,7 @@
4442
* Spring Boot auto-configuration for simple properties-based reactive discovery client.
4543
*
4644
* @author Tim Ysewyn
45+
* @author Charu Covindane
4746
* @since 2.2.0
4847
*/
4948
@Configuration(proxyBeanMethods = false)
@@ -71,8 +70,8 @@ public class SimpleReactiveDiscoveryClientAutoConfiguration
7170
@Bean
7271
public SimpleReactiveDiscoveryProperties simpleReactiveDiscoveryProperties() {
7372
simple.getLocal().setServiceId(serviceId);
74-
simple.getLocal().setUri(URI.create("http://"
75-
+ inet.findFirstNonLoopbackHostInfo().getHostname() + ":" + findPort()));
73+
simple.getLocal().setHost(inet.findFirstNonLoopbackHostInfo().getHostname());
74+
simple.getLocal().setPort(findPort());
7675
return simple;
7776
}
7877

@@ -96,8 +95,7 @@ private int findPort() {
9695
public void onApplicationEvent(WebServerInitializedEvent webServerInitializedEvent) {
9796
port = webServerInitializedEvent.getWebServer().getPort();
9897
if (port > 0) {
99-
simple.getLocal().setUri(URI.create("http://"
100-
+ inet.findFirstNonLoopbackHostInfo().getHostname() + ":" + port));
98+
simple.getLocal().setPort(port);
10199
}
102100
}
103101

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import reactor.core.publisher.Flux;
2828

2929
import org.springframework.boot.context.properties.ConfigurationProperties;
30+
import org.springframework.cloud.client.DefaultServiceInstance;
3031
import org.springframework.cloud.client.ServiceInstance;
3132
import org.springframework.cloud.client.discovery.DiscoveryClient;
3233
import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient;
@@ -40,35 +41,36 @@
4041
* {@link org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClient}.
4142
*
4243
* @author Tim Ysewyn
44+
* @author Charu Covindane
4345
* @since 2.2.0
4446
*/
4547
@ConfigurationProperties(prefix = "spring.cloud.discovery.client.simple")
4648
public class SimpleReactiveDiscoveryProperties {
4749

48-
private Map<String, List<SimpleServiceInstance>> instances = new HashMap<>();
50+
private Map<String, List<DefaultServiceInstance>> instances = new HashMap<>();
4951

5052
/**
5153
* The properties of the local instance (if it exists). Users should set these
5254
* properties explicitly if they are exporting data (e.g. metrics) that need to be
5355
* identified by the service instance.
5456
*/
55-
private SimpleServiceInstance local = new SimpleServiceInstance();
57+
private DefaultServiceInstance local = new DefaultServiceInstance();
5658

5759
private int order = DiscoveryClient.DEFAULT_ORDER;
5860

5961
public Flux<ServiceInstance> getInstances(String service) {
6062
return Flux.fromIterable(instances.getOrDefault(service, emptyList()));
6163
}
6264

63-
Map<String, List<SimpleServiceInstance>> getInstances() {
65+
Map<String, List<DefaultServiceInstance>> getInstances() {
6466
return instances;
6567
}
6668

67-
public void setInstances(Map<String, List<SimpleServiceInstance>> instances) {
69+
public void setInstances(Map<String, List<DefaultServiceInstance>> instances) {
6870
this.instances = instances;
6971
}
7072

71-
public SimpleServiceInstance getLocal() {
73+
public DefaultServiceInstance getLocal() {
7274
return this.local;
7375
}
7476

@@ -83,7 +85,7 @@ public void setOrder(int order) {
8385
@PostConstruct
8486
public void init() {
8587
for (String key : this.instances.keySet()) {
86-
for (SimpleServiceInstance instance : this.instances.get(key)) {
88+
for (DefaultServiceInstance instance : this.instances.get(key)) {
8789
instance.setServiceId(key);
8890
}
8991
}
@@ -92,6 +94,7 @@ public void init() {
9294
/**
9395
* Basic implementation of {@link ServiceInstance}.
9496
*/
97+
@Deprecated
9598
public static class SimpleServiceInstance implements ServiceInstance {
9699

97100
/**

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@
2525
import org.junit.Before;
2626
import org.junit.Test;
2727

28+
import org.springframework.cloud.client.DefaultServiceInstance;
2829
import org.springframework.cloud.client.ServiceInstance;
29-
import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryProperties.SimpleServiceInstance;
3030

3131
import static org.assertj.core.api.BDDAssertions.then;
3232

3333
/**
3434
* @author Biju Kunjummen
35+
* @author Charu Covindane
3536
*/
3637
public class SimpleDiscoveryClientTests {
3738

@@ -41,11 +42,11 @@ public class SimpleDiscoveryClientTests {
4142
public void setUp() {
4243
SimpleDiscoveryProperties simpleDiscoveryProperties = new SimpleDiscoveryProperties();
4344

44-
Map<String, List<SimpleServiceInstance>> map = new HashMap<>();
45-
SimpleServiceInstance service1Inst1 = new SimpleServiceInstance(
46-
URI.create("http://host1:8080"));
47-
SimpleServiceInstance service1Inst2 = new SimpleServiceInstance(
48-
URI.create("https://host2:8443"));
45+
Map<String, List<DefaultServiceInstance>> map = new HashMap<>();
46+
DefaultServiceInstance service1Inst1 = new DefaultServiceInstance(null, null,
47+
"host1", 8080, false);
48+
DefaultServiceInstance service1Inst2 = new DefaultServiceInstance(null, null,
49+
"host2", 8443, true);
4950
map.put("service1", Arrays.asList(service1Inst1, service1Inst2));
5051
simpleDiscoveryProperties.setInstances(map);
5152
simpleDiscoveryProperties.init();

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,31 @@
1616

1717
package org.springframework.cloud.client.discovery.simple.reactive;
1818

19-
import java.net.URI;
2019
import java.util.Arrays;
2120

2221
import org.junit.jupiter.api.BeforeEach;
2322
import org.junit.jupiter.api.Test;
2423
import reactor.core.publisher.Flux;
2524
import reactor.test.StepVerifier;
2625

26+
import org.springframework.cloud.client.DefaultServiceInstance;
2727
import org.springframework.cloud.client.ServiceInstance;
2828
import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient;
29-
import org.springframework.cloud.client.discovery.simple.reactive.SimpleReactiveDiscoveryProperties.SimpleServiceInstance;
3029

3130
import static java.util.Collections.singletonMap;
3231
import static org.assertj.core.api.Assertions.assertThat;
3332

3433
/**
3534
* @author Tim Ysewyn
35+
* @author Charu Covindane
3636
*/
3737
public class SimpleReactiveDiscoveryClientTests {
3838

39-
private final SimpleServiceInstance service1Inst1 = new SimpleServiceInstance(
40-
URI.create("http://host1:8080"));
39+
private final DefaultServiceInstance service1Inst1 = new DefaultServiceInstance(null,
40+
null, "host1", 8080, false);
4141

42-
private final SimpleServiceInstance service1Inst2 = new SimpleServiceInstance(
43-
URI.create("https://host2:8443"));
42+
private final DefaultServiceInstance service1Inst2 = new DefaultServiceInstance(null,
43+
null, "host2", 8443, true);
4444

4545
private SimpleReactiveDiscoveryClient client;
4646

0 commit comments

Comments
 (0)