Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public class DefaultServiceInstance implements ServiceInstance {

private @Nullable String instanceId;

private @Nullable String serviceId;
private String serviceId;

private @Nullable String host;
private String host;

private int port;

Expand All @@ -55,8 +55,8 @@ public class DefaultServiceInstance implements ServiceInstance {
* @param secure indicates whether or not the connection needs to be secure.
* @param metadata a map containing metadata.
*/
public DefaultServiceInstance(@Nullable String instanceId, @Nullable String serviceId, @Nullable String host,
int port, boolean secure, @Nullable Map<String, String> metadata) {
public DefaultServiceInstance(@Nullable String instanceId, String serviceId, String host, int port, boolean secure,
@Nullable Map<String, String> metadata) {
this.instanceId = instanceId;
this.serviceId = serviceId;
this.host = host;
Expand All @@ -74,33 +74,14 @@ public DefaultServiceInstance(@Nullable String instanceId, @Nullable String serv
* @param port the port on which the service is running.
* @param secure indicates whether or not the connection needs to be secure.
*/
public DefaultServiceInstance(@Nullable String instanceId, @Nullable String serviceId, @Nullable String host,
int port, boolean secure) {
public DefaultServiceInstance(@Nullable String instanceId, String serviceId, String host, int port,
boolean secure) {
this(instanceId, serviceId, host, port, secure, new LinkedHashMap<>());
}

public DefaultServiceInstance() {
}

/**
* Creates a URI from the given ServiceInstance's host:port.
* @param instance the ServiceInstance.
* @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";
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);
}

@Override
public URI getUri() {
return getUri(this);
return ServiceInstance.createUri(this);
}

@Override
Expand All @@ -114,12 +95,12 @@ public Map<String, String> getMetadata() {
}

@Override
public @Nullable String getServiceId() {
public String getServiceId() {
return serviceId;
}

@Override
public @Nullable String getHost() {
public String getHost() {
return host;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ public interface ServiceInstance {
/**
* @return The service ID as registered.
*/
@Nullable String getServiceId();
String getServiceId();

/**
* @return The hostname of the registered service instance.
*/
@Nullable String getHost();
String getHost();

/**
* @return The port of the registered service instance.
Expand Down Expand Up @@ -73,4 +73,20 @@ public interface ServiceInstance {
return null;
}

/**
* Creates a URI from the given ServiceInstance's host:port.
* @param instance the ServiceInstance.
* @return URI of the form (secure)?https:http + "host:port". Scheme port default used
* if port not set.
*/
static URI createUri(ServiceInstance instance) {
String scheme = (instance.isSecure()) ? "https" : "http";
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);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
* Copyright 2012-present 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

import java.net.URI;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

import org.jspecify.annotations.Nullable;

import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.core.style.ToStringCreator;
import org.springframework.util.Assert;

public class InstanceProperties {

private @Nullable String instanceId;

private @Nullable String serviceId;

private @Nullable String host;

private int port;

private boolean secure;

private Map<String, String> metadata = new LinkedHashMap<>();

private @Nullable URI uri;

public @Nullable String getInstanceId() {
return instanceId;
}

public void setInstanceId(@Nullable String instanceId) {
this.instanceId = instanceId;
}

public @Nullable String getServiceId() {
return serviceId;
}

public void setServiceId(@Nullable String serviceId) {
this.serviceId = serviceId;
}

public @Nullable String getHost() {
return host;
}

public void setHost(@Nullable String host) {
this.host = host;
}

public int getPort() {
return port;
}

public void setPort(int port) {
this.port = port;
}

public boolean isSecure() {
return secure;
}

public void setSecure(boolean secure) {
this.secure = secure;
}

public Map<String, String> getMetadata() {
return metadata;
}

public void setMetadata(Map<String, String> metadata) {
this.metadata = metadata;
}

public @Nullable URI getUri() {
return uri;
}

public void setUri(@Nullable URI uri) {
this.uri = uri;
this.host = this.uri.getHost();
this.port = this.uri.getPort();
String scheme = this.uri.getScheme();
if ("https".equals(scheme)) {
this.secure = true;
}
}

@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}
InstanceProperties instanceProperties = (InstanceProperties) o;
return port == instanceProperties.port && secure == instanceProperties.secure
&& Objects.equals(instanceId, instanceProperties.instanceId)
&& Objects.equals(serviceId, instanceProperties.serviceId)
&& Objects.equals(host, instanceProperties.host)
&& Objects.equals(metadata, instanceProperties.metadata) && Objects.equals(uri, instanceProperties.uri);
}

@Override
public int hashCode() {
return Objects.hash(instanceId, serviceId, host, port, secure, metadata, uri);
}

@Override
public String toString() {
return new ToStringCreator(this).append("instanceId", instanceId)
.append("serviceId", serviceId)
.append("host", host)
.append("port", port)
.append("secure", secure)
.append("metadata", metadata)
.append("uri", uri)
.toString();
}

public ServiceInstance toServiceInstance() {
Assert.notNull(serviceId, "serviceId is required");
Assert.notNull(host, "host is required");
return new DefaultServiceInstance(instanceId, serviceId, host, port, secure, metadata);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.ArrayList;
import java.util.List;

import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;

Expand Down Expand Up @@ -47,10 +46,9 @@ public String description() {
@Override
public List<ServiceInstance> getInstances(String serviceId) {
List<ServiceInstance> serviceInstances = new ArrayList<>();
List<DefaultServiceInstance> serviceInstanceForService = this.simpleDiscoveryProperties.getInstances()
.get(serviceId);
if (serviceInstanceForService != null) {
serviceInstances.addAll(serviceInstanceForService);
List<InstanceProperties> instanceProperties = this.simpleDiscoveryProperties.getInstances().get(serviceId);
if (instanceProperties != null) {
instanceProperties.stream().map(InstanceProperties::toServiceInstance).forEach(serviceInstances::add);
}
return serviceInstances;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.core.style.ToStringCreator;

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

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

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

private int order = DiscoveryClient.DEFAULT_ORDER;

public Map<String, List<DefaultServiceInstance>> getInstances() {
public Map<String, List<InstanceProperties>> getInstances() {
return this.instances;
}

public void setInstances(Map<String, List<DefaultServiceInstance>> instances) {
public void setInstances(Map<String, List<InstanceProperties>> instances) {
this.instances = instances;
}

public DefaultServiceInstance getLocal() {
public InstanceProperties getLocal() {
return this.local;
}

Expand All @@ -77,14 +77,18 @@ public void setOrder(int order) {
@Override
public void afterPropertiesSet() {
for (String key : this.instances.keySet()) {
for (DefaultServiceInstance instance : this.instances.get(key)) {
for (InstanceProperties instance : this.instances.get(key)) {
instance.setServiceId(key);
}
}
}

public void setInstance(String serviceId, String host, int port) {
local = new DefaultServiceInstance(null, serviceId, host, port, false);
@Override
public String toString() {
return new ToStringCreator(this).append("instances", instances)
.append("local", local)
.append("order", order)
.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient;
import org.springframework.cloud.client.discovery.simple.InstanceProperties;

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

@Override
public Flux<ServiceInstance> getInstances(String serviceId) {
return this.simpleDiscoveryProperties.getInstances(serviceId);
return Flux.fromIterable(this.simpleDiscoveryProperties.getInstances(serviceId)
.stream()
.map(InstanceProperties::toServiceInstance)
.toList());
}

@Override
Expand Down
Loading
Loading