Skip to content

Commit 2cc11dd

Browse files
authored
Creates InstanceProperties (#1604)
* Creates InstanceProperties This allows SimpleDiscoveryClient and SimpleReactiveDiscoveryClient to use InstanceProperties rather than DefaultServiceInstance. This then allows ServiceInstance to remove nullable from host and serviceId. * Removes duplicate DefaultServiceInstance.getUri * Renames ServiceInstance.createUri()
1 parent 52966a4 commit 2cc11dd

File tree

18 files changed

+268
-86
lines changed

18 files changed

+268
-86
lines changed

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

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ public class DefaultServiceInstance implements ServiceInstance {
3535

3636
private @Nullable String instanceId;
3737

38-
private @Nullable String serviceId;
38+
private String serviceId;
3939

40-
private @Nullable String host;
40+
private String host;
4141

4242
private int port;
4343

@@ -55,8 +55,8 @@ public class DefaultServiceInstance implements ServiceInstance {
5555
* @param secure indicates whether or not the connection needs to be secure.
5656
* @param metadata a map containing metadata.
5757
*/
58-
public DefaultServiceInstance(@Nullable String instanceId, @Nullable String serviceId, @Nullable String host,
59-
int port, boolean secure, @Nullable Map<String, String> metadata) {
58+
public DefaultServiceInstance(@Nullable String instanceId, String serviceId, String host, int port, boolean secure,
59+
@Nullable Map<String, String> metadata) {
6060
this.instanceId = instanceId;
6161
this.serviceId = serviceId;
6262
this.host = host;
@@ -74,33 +74,14 @@ public DefaultServiceInstance(@Nullable String instanceId, @Nullable String serv
7474
* @param port the port on which the service is running.
7575
* @param secure indicates whether or not the connection needs to be secure.
7676
*/
77-
public DefaultServiceInstance(@Nullable String instanceId, @Nullable String serviceId, @Nullable String host,
78-
int port, boolean secure) {
77+
public DefaultServiceInstance(@Nullable String instanceId, String serviceId, String host, int port,
78+
boolean secure) {
7979
this(instanceId, serviceId, host, port, secure, new LinkedHashMap<>());
8080
}
8181

82-
public DefaultServiceInstance() {
83-
}
84-
85-
/**
86-
* Creates a URI from the given ServiceInstance's host:port.
87-
* @param instance the ServiceInstance.
88-
* @return URI of the form (secure)?https:http + "host:port". Scheme port default used
89-
* if port not set.
90-
*/
91-
public static URI getUri(ServiceInstance instance) {
92-
String scheme = (instance.isSecure()) ? "https" : "http";
93-
int port = instance.getPort();
94-
if (port <= 0) {
95-
port = (instance.isSecure()) ? 443 : 80;
96-
}
97-
String uri = String.format("%s://%s:%s", scheme, instance.getHost(), port);
98-
return URI.create(uri);
99-
}
100-
10182
@Override
10283
public URI getUri() {
103-
return getUri(this);
84+
return ServiceInstance.createUri(this);
10485
}
10586

10687
@Override
@@ -114,12 +95,12 @@ public Map<String, String> getMetadata() {
11495
}
11596

11697
@Override
117-
public @Nullable String getServiceId() {
98+
public String getServiceId() {
11899
return serviceId;
119100
}
120101

121102
@Override
122-
public @Nullable String getHost() {
103+
public String getHost() {
123104
return host;
124105
}
125106

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ public interface ServiceInstance {
3939
/**
4040
* @return The service ID as registered.
4141
*/
42-
@Nullable String getServiceId();
42+
String getServiceId();
4343

4444
/**
4545
* @return The hostname of the registered service instance.
4646
*/
47-
@Nullable String getHost();
47+
String getHost();
4848

4949
/**
5050
* @return The port of the registered service instance.
@@ -73,4 +73,20 @@ public interface ServiceInstance {
7373
return null;
7474
}
7575

76+
/**
77+
* Creates a URI from the given ServiceInstance's host:port.
78+
* @param instance the ServiceInstance.
79+
* @return URI of the form (secure)?https:http + "host:port". Scheme port default used
80+
* if port not set.
81+
*/
82+
static URI createUri(ServiceInstance instance) {
83+
String scheme = (instance.isSecure()) ? "https" : "http";
84+
int port = instance.getPort();
85+
if (port <= 0) {
86+
port = (instance.isSecure()) ? 443 : 80;
87+
}
88+
String uri = String.format("%s://%s:%s", scheme, instance.getHost(), port);
89+
return URI.create(uri);
90+
}
91+
7692
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
* Copyright 2012-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.client.discovery.simple;
18+
19+
import java.net.URI;
20+
import java.util.LinkedHashMap;
21+
import java.util.Map;
22+
import java.util.Objects;
23+
24+
import org.jspecify.annotations.Nullable;
25+
26+
import org.springframework.cloud.client.DefaultServiceInstance;
27+
import org.springframework.cloud.client.ServiceInstance;
28+
import org.springframework.core.style.ToStringCreator;
29+
import org.springframework.util.Assert;
30+
31+
public class InstanceProperties {
32+
33+
private @Nullable String instanceId;
34+
35+
private @Nullable String serviceId;
36+
37+
private @Nullable String host;
38+
39+
private int port;
40+
41+
private boolean secure;
42+
43+
private Map<String, String> metadata = new LinkedHashMap<>();
44+
45+
private @Nullable URI uri;
46+
47+
public @Nullable String getInstanceId() {
48+
return instanceId;
49+
}
50+
51+
public void setInstanceId(@Nullable String instanceId) {
52+
this.instanceId = instanceId;
53+
}
54+
55+
public @Nullable String getServiceId() {
56+
return serviceId;
57+
}
58+
59+
public void setServiceId(@Nullable String serviceId) {
60+
this.serviceId = serviceId;
61+
}
62+
63+
public @Nullable String getHost() {
64+
return host;
65+
}
66+
67+
public void setHost(@Nullable String host) {
68+
this.host = host;
69+
}
70+
71+
public int getPort() {
72+
return port;
73+
}
74+
75+
public void setPort(int port) {
76+
this.port = port;
77+
}
78+
79+
public boolean isSecure() {
80+
return secure;
81+
}
82+
83+
public void setSecure(boolean secure) {
84+
this.secure = secure;
85+
}
86+
87+
public Map<String, String> getMetadata() {
88+
return metadata;
89+
}
90+
91+
public void setMetadata(Map<String, String> metadata) {
92+
this.metadata = metadata;
93+
}
94+
95+
public @Nullable URI getUri() {
96+
return uri;
97+
}
98+
99+
public void setUri(@Nullable URI uri) {
100+
this.uri = uri;
101+
this.host = this.uri.getHost();
102+
this.port = this.uri.getPort();
103+
String scheme = this.uri.getScheme();
104+
if ("https".equals(scheme)) {
105+
this.secure = true;
106+
}
107+
}
108+
109+
@Override
110+
public boolean equals(Object o) {
111+
if (o == null || getClass() != o.getClass()) {
112+
return false;
113+
}
114+
InstanceProperties instanceProperties = (InstanceProperties) o;
115+
return port == instanceProperties.port && secure == instanceProperties.secure
116+
&& Objects.equals(instanceId, instanceProperties.instanceId)
117+
&& Objects.equals(serviceId, instanceProperties.serviceId)
118+
&& Objects.equals(host, instanceProperties.host)
119+
&& Objects.equals(metadata, instanceProperties.metadata) && Objects.equals(uri, instanceProperties.uri);
120+
}
121+
122+
@Override
123+
public int hashCode() {
124+
return Objects.hash(instanceId, serviceId, host, port, secure, metadata, uri);
125+
}
126+
127+
@Override
128+
public String toString() {
129+
return new ToStringCreator(this).append("instanceId", instanceId)
130+
.append("serviceId", serviceId)
131+
.append("host", host)
132+
.append("port", port)
133+
.append("secure", secure)
134+
.append("metadata", metadata)
135+
.append("uri", uri)
136+
.toString();
137+
}
138+
139+
public ServiceInstance toServiceInstance() {
140+
Assert.notNull(serviceId, "serviceId is required");
141+
Assert.notNull(host, "host is required");
142+
return new DefaultServiceInstance(instanceId, serviceId, host, port, secure, metadata);
143+
}
144+
145+
}

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

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

22-
import org.springframework.cloud.client.DefaultServiceInstance;
2322
import org.springframework.cloud.client.ServiceInstance;
2423
import org.springframework.cloud.client.discovery.DiscoveryClient;
2524

@@ -47,10 +46,9 @@ public String description() {
4746
@Override
4847
public List<ServiceInstance> getInstances(String serviceId) {
4948
List<ServiceInstance> serviceInstances = new ArrayList<>();
50-
List<DefaultServiceInstance> serviceInstanceForService = this.simpleDiscoveryProperties.getInstances()
51-
.get(serviceId);
52-
if (serviceInstanceForService != null) {
53-
serviceInstances.addAll(serviceInstanceForService);
49+
List<InstanceProperties> instanceProperties = this.simpleDiscoveryProperties.getInstances().get(serviceId);
50+
if (instanceProperties != null) {
51+
instanceProperties.stream().map(InstanceProperties::toServiceInstance).forEach(serviceInstances::add);
5452
}
5553
return serviceInstances;
5654
}

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
import org.springframework.beans.factory.InitializingBean;
2424
import org.springframework.boot.context.properties.ConfigurationProperties;
2525
import org.springframework.boot.context.properties.NestedConfigurationProperty;
26-
import org.springframework.cloud.client.DefaultServiceInstance;
2726
import org.springframework.cloud.client.discovery.DiscoveryClient;
27+
import org.springframework.core.style.ToStringCreator;
2828

2929
/**
3030
* Properties to hold the details of a
@@ -42,27 +42,27 @@
4242
@ConfigurationProperties(prefix = "spring.cloud.discovery.client.simple")
4343
public class SimpleDiscoveryProperties implements InitializingBean {
4444

45-
private Map<String, List<DefaultServiceInstance>> instances = new HashMap<>();
45+
private Map<String, List<InstanceProperties>> instances = new HashMap<>();
4646

4747
/**
4848
* The properties of the local instance (if it exists). Users should set these
4949
* properties explicitly if they are exporting data (e.g. metrics) that need to be
5050
* identified by the service instance.
5151
*/
5252
@NestedConfigurationProperty
53-
private DefaultServiceInstance local = new DefaultServiceInstance(null, null, null, 0, false);
53+
private InstanceProperties local = new InstanceProperties();
5454

5555
private int order = DiscoveryClient.DEFAULT_ORDER;
5656

57-
public Map<String, List<DefaultServiceInstance>> getInstances() {
57+
public Map<String, List<InstanceProperties>> getInstances() {
5858
return this.instances;
5959
}
6060

61-
public void setInstances(Map<String, List<DefaultServiceInstance>> instances) {
61+
public void setInstances(Map<String, List<InstanceProperties>> instances) {
6262
this.instances = instances;
6363
}
6464

65-
public DefaultServiceInstance getLocal() {
65+
public InstanceProperties getLocal() {
6666
return this.local;
6767
}
6868

@@ -77,14 +77,18 @@ public void setOrder(int order) {
7777
@Override
7878
public void afterPropertiesSet() {
7979
for (String key : this.instances.keySet()) {
80-
for (DefaultServiceInstance instance : this.instances.get(key)) {
80+
for (InstanceProperties instance : this.instances.get(key)) {
8181
instance.setServiceId(key);
8282
}
8383
}
8484
}
8585

86-
public void setInstance(String serviceId, String host, int port) {
87-
local = new DefaultServiceInstance(null, serviceId, host, port, false);
86+
@Override
87+
public String toString() {
88+
return new ToStringCreator(this).append("instances", instances)
89+
.append("local", local)
90+
.append("order", order)
91+
.toString();
8892
}
8993

9094
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.springframework.cloud.client.ServiceInstance;
2222
import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient;
23+
import org.springframework.cloud.client.discovery.simple.InstanceProperties;
2324

2425
/**
2526
* A {@link ReactiveDiscoveryClient} that will use the properties file as a source of
@@ -42,7 +43,10 @@ public String description() {
4243

4344
@Override
4445
public Flux<ServiceInstance> getInstances(String serviceId) {
45-
return this.simpleDiscoveryProperties.getInstances(serviceId);
46+
return Flux.fromIterable(this.simpleDiscoveryProperties.getInstances(serviceId)
47+
.stream()
48+
.map(InstanceProperties::toServiceInstance)
49+
.toList());
4650
}
4751

4852
@Override

0 commit comments

Comments
 (0)