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 @@ -26,20 +26,23 @@
*
* @author Spencer Gibb
* @author Tim Ysewyn
* @author Charu Covindane
*/
public class DefaultServiceInstance implements ServiceInstance {

private final String instanceId;
private String instanceId;

private final String serviceId;
private String serviceId;

private final String host;
private String host;

private final int port;
private int port;

private final boolean secure;
private boolean secure;

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

private URI uri;

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

public DefaultServiceInstance() {
}

/**
* Creates a URI from the given ServiceInstance's host:port.
* @param instance the ServiceInstance.
Expand Down Expand Up @@ -145,6 +151,32 @@ public boolean isSecure() {
return this.secure;
}

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

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

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

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

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)) {
this.secure = true;
}
}

@Override
public String toString() {
return "DefaultServiceInstance{" + "instanceId='" + this.instanceId + '\''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@
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;
import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryProperties.SimpleServiceInstance;

/**
* A {@link org.springframework.cloud.client.discovery.DiscoveryClient} that will use the
* properties file as a source of service instances.
*
* @author Biju Kunjummen
* @author Olga Maciaszek-Sharma
* @author Charu Covindane
*/
public class SimpleDiscoveryClient implements DiscoveryClient {

Expand All @@ -46,7 +47,7 @@ public String description() {
@Override
public List<ServiceInstance> getInstances(String serviceId) {
List<ServiceInstance> serviceInstances = new ArrayList<>();
List<SimpleServiceInstance> serviceInstanceForService = this.simpleDiscoveryProperties
List<DefaultServiceInstance> serviceInstanceForService = this.simpleDiscoveryProperties
.getInstances().get(serviceId);

if (serviceInstanceForService != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

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

import java.net.URI;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
Expand All @@ -37,6 +35,7 @@
* Spring Boot auto-configuration for simple properties-based discovery client.
*
* @author Biju Kunjummen
* @author Charu Covindane
*/
@Configuration(proxyBeanMethods = false)
@AutoConfigureBefore({ NoopDiscoveryClientAutoConfiguration.class,
Expand Down Expand Up @@ -67,10 +66,9 @@ public void setInet(InetUtils inet) {
public SimpleDiscoveryProperties simpleDiscoveryProperties(
@Value("${spring.application.name:application}") String serviceId) {
simple.getLocal().setServiceId(serviceId);
simple.getLocal()
.setUri(URI.create(
"http://" + this.inet.findFirstNonLoopbackHostInfo().getHostname()
+ ":" + findPort()));
simple.getLocal().setHost(this.inet.findFirstNonLoopbackHostInfo().getHostname());
simple.getLocal().setPort(findPort());

return simple;
}

Expand All @@ -95,10 +93,7 @@ private int findPort() {
public void onApplicationEvent(WebServerInitializedEvent webServerInitializedEvent) {
this.port = webServerInitializedEvent.getWebServer().getPort();
if (this.port > 0) {
simple.getLocal()
.setUri(URI.create("http://"
+ this.inet.findFirstNonLoopbackHostInfo().getHostname() + ":"
+ this.port));
simple.getLocal().setPort(this.port);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import javax.annotation.PostConstruct;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;

Expand All @@ -38,31 +39,33 @@
* @author Biju Kunjummen
* @author Olga Maciaszek-Sharma
* @author Tim Ysewyn
* @author Charu Covindane
*/

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

private Map<String, List<SimpleServiceInstance>> instances = new HashMap<>();
private Map<String, List<DefaultServiceInstance>> 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.
*/
private SimpleServiceInstance local = new SimpleServiceInstance();
private DefaultServiceInstance local = new DefaultServiceInstance(null, null, null, 0,
false);

private int order = DiscoveryClient.DEFAULT_ORDER;

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

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

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

Expand All @@ -77,15 +80,22 @@ public void setOrder(int order) {
@PostConstruct
public void init() {
for (String key : this.instances.keySet()) {
for (SimpleServiceInstance instance : this.instances.get(key)) {
for (DefaultServiceInstance 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);
}

/**
* Basic implementation of {@link ServiceInstance}.
*
* @deprecated in favor of {@link DefaultServiceInstance}
*/
@Deprecated
public static class SimpleServiceInstance implements ServiceInstance {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

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

import java.net.URI;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.health.ReactiveHealthIndicator;
Expand All @@ -44,6 +42,7 @@
* Spring Boot auto-configuration for simple properties-based reactive discovery client.
*
* @author Tim Ysewyn
* @author Charu Covindane
* @since 2.2.0
*/
@Configuration(proxyBeanMethods = false)
Expand Down Expand Up @@ -71,8 +70,8 @@ public class SimpleReactiveDiscoveryClientAutoConfiguration
@Bean
public SimpleReactiveDiscoveryProperties simpleReactiveDiscoveryProperties() {
simple.getLocal().setServiceId(serviceId);
simple.getLocal().setUri(URI.create("http://"
+ inet.findFirstNonLoopbackHostInfo().getHostname() + ":" + findPort()));
simple.getLocal().setHost(inet.findFirstNonLoopbackHostInfo().getHostname());
simple.getLocal().setPort(findPort());
return simple;
}

Expand All @@ -96,8 +95,7 @@ private int findPort() {
public void onApplicationEvent(WebServerInitializedEvent webServerInitializedEvent) {
port = webServerInitializedEvent.getWebServer().getPort();
if (port > 0) {
simple.getLocal().setUri(URI.create("http://"
+ inet.findFirstNonLoopbackHostInfo().getHostname() + ":" + port));
simple.getLocal().setPort(port);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import reactor.core.publisher.Flux;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient;
Expand All @@ -40,35 +41,36 @@
* {@link org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClient}.
*
* @author Tim Ysewyn
* @author Charu Covindane
* @since 2.2.0
*/
@ConfigurationProperties(prefix = "spring.cloud.discovery.client.simple")
public class SimpleReactiveDiscoveryProperties {

private Map<String, List<SimpleServiceInstance>> instances = new HashMap<>();
private Map<String, List<DefaultServiceInstance>> 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.
*/
private SimpleServiceInstance local = new SimpleServiceInstance();
private DefaultServiceInstance local = new DefaultServiceInstance();

private int order = DiscoveryClient.DEFAULT_ORDER;

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

Map<String, List<SimpleServiceInstance>> getInstances() {
Map<String, List<DefaultServiceInstance>> getInstances() {
return instances;
}

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

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

Expand All @@ -83,7 +85,7 @@ public void setOrder(int order) {
@PostConstruct
public void init() {
for (String key : this.instances.keySet()) {
for (SimpleServiceInstance instance : this.instances.get(key)) {
for (DefaultServiceInstance instance : this.instances.get(key)) {
instance.setServiceId(key);
}
}
Expand All @@ -92,6 +94,7 @@ public void init() {
/**
* Basic implementation of {@link ServiceInstance}.
*/
@Deprecated
public static class SimpleServiceInstance implements ServiceInstance {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@
import org.junit.Before;
import org.junit.Test;

import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryProperties.SimpleServiceInstance;

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

/**
* @author Biju Kunjummen
* @author Charu Covindane
*/
public class SimpleDiscoveryClientTests {

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

Map<String, List<SimpleServiceInstance>> map = new HashMap<>();
SimpleServiceInstance service1Inst1 = new SimpleServiceInstance(
URI.create("http://host1:8080"));
SimpleServiceInstance service1Inst2 = new SimpleServiceInstance(
URI.create("https://host2:8443"));
Map<String, List<DefaultServiceInstance>> map = new HashMap<>();
DefaultServiceInstance service1Inst1 = new DefaultServiceInstance(null, null,
"host1", 8080, false);
DefaultServiceInstance service1Inst2 = new DefaultServiceInstance(null, null,
"host2", 8443, true);
map.put("service1", Arrays.asList(service1Inst1, service1Inst2));
simpleDiscoveryProperties.setInstances(map);
simpleDiscoveryProperties.init();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,31 @@

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

import java.net.URI;
import java.util.Arrays;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;

import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient;
import org.springframework.cloud.client.discovery.simple.reactive.SimpleReactiveDiscoveryProperties.SimpleServiceInstance;

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

/**
* @author Tim Ysewyn
* @author Charu Covindane
*/
public class SimpleReactiveDiscoveryClientTests {

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

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

private SimpleReactiveDiscoveryClient client;

Expand Down
Loading