Added the service instance id to the ServiceInstance interface (#419)
* Added the service instance id to the `ServiceInstance` interface * Updated `toString()`, `equals()` and `hashCode()` * Updated Javadoc to fix warnings
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2017 the original author or authors.
|
||||
* Copyright 2013-2018 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.
|
||||
@@ -26,9 +26,12 @@ import java.util.Objects;
|
||||
* Default implementation of {@link ServiceInstance}.
|
||||
*
|
||||
* @author Spencer Gibb
|
||||
* @author Tim Ysewyn
|
||||
*/
|
||||
public class DefaultServiceInstance implements ServiceInstance {
|
||||
|
||||
private final String instanceId;
|
||||
|
||||
private final String serviceId;
|
||||
|
||||
private final String host;
|
||||
@@ -39,8 +42,17 @@ public class DefaultServiceInstance implements ServiceInstance {
|
||||
|
||||
private final Map<String, String> metadata;
|
||||
|
||||
public DefaultServiceInstance(String serviceId, String host, int port, boolean secure,
|
||||
/**
|
||||
* @param instanceId the id of the instance.
|
||||
* @param serviceId the id of the service.
|
||||
* @param host the host where the service instance can be found.
|
||||
* @param port the port on which the service is running.
|
||||
* @param secure indicates whether or not the connection needs to be secure.
|
||||
* @param metadata a map containing metadata.
|
||||
*/
|
||||
public DefaultServiceInstance(String instanceId, String serviceId, String host, int port, boolean secure,
|
||||
Map<String, String> metadata) {
|
||||
this.instanceId = instanceId;
|
||||
this.serviceId = serviceId;
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
@@ -48,6 +60,39 @@ public class DefaultServiceInstance implements ServiceInstance {
|
||||
this.metadata = metadata;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param instanceId the id of the instance.
|
||||
* @param serviceId the id of the service.
|
||||
* @param host the host where the service instance can be found.
|
||||
* @param port the port on which the service is running.
|
||||
* @param secure indicates whether or not the connection needs to be secure.
|
||||
*/
|
||||
public DefaultServiceInstance(String instanceId, String serviceId, String host, int port, boolean secure) {
|
||||
this(instanceId, serviceId, host, port, secure, new LinkedHashMap<>());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param serviceId the id of the service.
|
||||
* @param host the host where the service instance can be found.
|
||||
* @param port the port on which the service is running.
|
||||
* @param secure indicates whether or not the connection needs to be secure.
|
||||
* @param metadata a map containing metadata.
|
||||
* @deprecated
|
||||
*/
|
||||
@Deprecated
|
||||
public DefaultServiceInstance(String serviceId, String host, int port, boolean secure,
|
||||
Map<String, String> metadata) {
|
||||
this(null, serviceId, host, port, secure, metadata);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param serviceId the id of the service.
|
||||
* @param host the host where the service instance can be found.
|
||||
* @param port the port on which the service is running.
|
||||
* @param secure indicates whether or not the connection needs to be secure.
|
||||
* @deprecated
|
||||
*/
|
||||
@Deprecated
|
||||
public DefaultServiceInstance(String serviceId, String host, int port,
|
||||
boolean secure) {
|
||||
this(serviceId, host, port, secure, new LinkedHashMap<>());
|
||||
@@ -65,7 +110,7 @@ public class DefaultServiceInstance implements ServiceInstance {
|
||||
|
||||
/**
|
||||
* Creates a URI from the given ServiceInstance's host:port.
|
||||
* @param instance
|
||||
* @param instance the ServiceInstance.
|
||||
* @return URI of the form (secure)?https:http + "host:port".
|
||||
*/
|
||||
public static URI getUri(ServiceInstance instance) {
|
||||
@@ -75,6 +120,11 @@ public class DefaultServiceInstance implements ServiceInstance {
|
||||
return URI.create(uri);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInstanceId() {
|
||||
return instanceId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServiceId() {
|
||||
return serviceId;
|
||||
@@ -98,7 +148,8 @@ public class DefaultServiceInstance implements ServiceInstance {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "DefaultServiceInstance{" +
|
||||
"serviceId='" + serviceId + '\'' +
|
||||
"instanceId='" + instanceId + '\'' +
|
||||
", serviceId='" + serviceId + '\'' +
|
||||
", host='" + host + '\'' +
|
||||
", port=" + port +
|
||||
", secure=" + secure +
|
||||
@@ -113,6 +164,7 @@ public class DefaultServiceInstance implements ServiceInstance {
|
||||
DefaultServiceInstance that = (DefaultServiceInstance) o;
|
||||
return port == that.port &&
|
||||
secure == that.secure &&
|
||||
Objects.equals(instanceId, that.instanceId) &&
|
||||
Objects.equals(serviceId, that.serviceId) &&
|
||||
Objects.equals(host, that.host) &&
|
||||
Objects.equals(metadata, that.metadata);
|
||||
@@ -120,6 +172,6 @@ public class DefaultServiceInstance implements ServiceInstance {
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(serviceId, host, port, secure, metadata);
|
||||
return Objects.hash(instanceId, serviceId, host, port, secure, metadata);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2015 the original author or authors.
|
||||
* Copyright 2013-2018 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.
|
||||
@@ -22,9 +22,17 @@ import java.util.Map;
|
||||
/**
|
||||
* Represents an instance of a service in a discovery system.
|
||||
* @author Spencer Gibb
|
||||
* @author Tim Ysewyn
|
||||
*/
|
||||
public interface ServiceInstance {
|
||||
|
||||
/**
|
||||
* @return The unique instance ID as registered.
|
||||
*/
|
||||
default String getInstanceId() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The service ID as registered.
|
||||
*/
|
||||
|
||||
@@ -38,6 +38,7 @@ import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
*
|
||||
* @author Biju Kunjummen
|
||||
* @author Olga Maciaszek-Sharma
|
||||
* @author Tim Ysewyn
|
||||
*/
|
||||
|
||||
@ConfigurationProperties(prefix = "spring.cloud.discovery.client.simple")
|
||||
@@ -97,6 +98,10 @@ public class SimpleDiscoveryProperties {
|
||||
* their behaviour per instance, e.g. when load balancing.
|
||||
*/
|
||||
private Map<String, String> metadata = new LinkedHashMap<>();
|
||||
/**
|
||||
* The unique identifier or name for the service instance.
|
||||
*/
|
||||
private String instanceId;
|
||||
/**
|
||||
* The identifier or name for the service. Multiple instances might share the same
|
||||
* service ID.
|
||||
@@ -120,6 +125,15 @@ public class SimpleDiscoveryProperties {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInstanceId() {
|
||||
return this.instanceId;
|
||||
}
|
||||
|
||||
public void setInstanceId(String id) {
|
||||
this.instanceId = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServiceId() {
|
||||
return this.serviceId;
|
||||
|
||||
@@ -32,6 +32,7 @@ import static java.util.Collections.singletonList;
|
||||
* Test configuration for {@link CompositeDiscoveryClient} tests.
|
||||
*
|
||||
* @author Olga Maciaszek-Sharma
|
||||
* @author Tim Ysewyn
|
||||
*/
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@@ -67,7 +68,7 @@ public class CompositeDiscoveryClientTestsConfig {
|
||||
@Override
|
||||
public List<ServiceInstance> getInstances(String serviceId) {
|
||||
if (serviceId.equals(CUSTOM_SERVICE_ID)) {
|
||||
ServiceInstance s1 = new DefaultServiceInstance(CUSTOM_SERVICE_ID,
|
||||
ServiceInstance s1 = new DefaultServiceInstance("customInstance", CUSTOM_SERVICE_ID,
|
||||
"host", 123, false);
|
||||
return singletonList(s1);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2015-2016 the original author or authors.
|
||||
* Copyright 2015-2018 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.
|
||||
@@ -23,8 +23,6 @@ import org.springframework.cloud.client.hypermedia.CloudHypermediaAutoConfigurat
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.hateoas.client.Traverson;
|
||||
import org.springframework.hateoas.client.Traverson.TraversalBuilder;
|
||||
|
||||
import static org.hamcrest.Matchers.arrayWithSize;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
@@ -36,6 +34,7 @@ import static org.junit.Assert.assertThat;
|
||||
* Integration tests for {@link CloudHypermediaAutoConfiguration}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Tim Ysewyn
|
||||
*/
|
||||
public class CloudHypermediaAutoConfigurationIntegrationTests {
|
||||
|
||||
@@ -106,14 +105,8 @@ public class CloudHypermediaAutoConfigurationIntegrationTests {
|
||||
public RemoteResource resource() {
|
||||
|
||||
ServiceInstanceProvider provider = new StaticServiceInstanceProvider(
|
||||
new DefaultServiceInstance("service", "localhost", 80, false));
|
||||
return new DiscoveredResource(provider, new TraversalDefinition() {
|
||||
|
||||
@Override
|
||||
public TraversalBuilder buildTraversal(Traverson traverson) {
|
||||
return traverson.follow("rel");
|
||||
}
|
||||
});
|
||||
new DefaultServiceInstance("instance", "service", "localhost", 80, false));
|
||||
return new DiscoveredResource(provider, traverson -> traverson.follow("rel"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2015 the original author or authors.
|
||||
* Copyright 2015-2018 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.
|
||||
@@ -40,6 +40,7 @@ import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
* @author Tim Ysewyn
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class DiscoveredResourceUnitTests {
|
||||
@@ -69,7 +70,7 @@ public class DiscoveredResourceUnitTests {
|
||||
|
||||
Link link = new Link("target", "rel");
|
||||
|
||||
when(provider.getServiceInstance()).thenReturn(new DefaultServiceInstance("service", "localhost", 8080, false));
|
||||
when(provider.getServiceInstance()).thenReturn(new DefaultServiceInstance("instance", "service", "localhost", 8080, false));
|
||||
when(builder.asTemplatedLink()).thenReturn(link);
|
||||
|
||||
resource.verifyOrDiscover();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2017 the original author or authors.
|
||||
* Copyright 2017-2018 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.
|
||||
@@ -43,6 +43,7 @@ import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* @author Ryan Baxter
|
||||
* @author Tim Ysewyn
|
||||
*/
|
||||
public abstract class AbstractLoadBalancerAutoConfigurationTests {
|
||||
|
||||
@@ -139,7 +140,7 @@ public abstract class AbstractLoadBalancerAutoConfigurationTests {
|
||||
|
||||
@Override
|
||||
public ServiceInstance choose(String serviceId) {
|
||||
return new DefaultServiceInstance(serviceId, serviceId,
|
||||
return new DefaultServiceInstance(serviceId, serviceId, serviceId,
|
||||
this.random.nextInt(40000), false);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2017 the original author or authors.
|
||||
* Copyright 2017-2018 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.
|
||||
@@ -46,6 +46,7 @@ import static org.hamcrest.Matchers.empty;
|
||||
|
||||
/**
|
||||
* @author Rob Worsnop
|
||||
* @author Tim Ysewyn
|
||||
*/
|
||||
public class AsyncLoadBalancerAutoConfigurationTests {
|
||||
|
||||
@@ -151,7 +152,7 @@ public class AsyncLoadBalancerAutoConfigurationTests {
|
||||
|
||||
@Override
|
||||
public ServiceInstance choose(String serviceId) {
|
||||
return new DefaultServiceInstance(serviceId, serviceId,
|
||||
return new DefaultServiceInstance(serviceId, serviceId, serviceId,
|
||||
this.random.nextInt(40000), false);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2017 the original author or authors.
|
||||
* Copyright 2017-2018 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.
|
||||
@@ -47,6 +47,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
* @author Tim Ysewyn
|
||||
*/
|
||||
public class ReactiveLoadBalancerAutoConfigurationTests {
|
||||
|
||||
@@ -163,7 +164,7 @@ public class ReactiveLoadBalancerAutoConfigurationTests {
|
||||
|
||||
@Override
|
||||
public ServiceInstance choose(String serviceId) {
|
||||
return new DefaultServiceInstance(serviceId, serviceId,
|
||||
return new DefaultServiceInstance(serviceId, serviceId, serviceId,
|
||||
this.random.nextInt(40000), false);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,19 @@
|
||||
/*
|
||||
* Copyright 2018 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
|
||||
*
|
||||
* http://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.serviceregistry;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
@@ -18,6 +34,7 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
* @author Tim Ysewyn
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = AbstractAutoServiceRegistrationMgmtDisabledTests.Config.class,
|
||||
@@ -43,6 +60,11 @@ public class AbstractAutoServiceRegistrationMgmtDisabledTests {
|
||||
}
|
||||
|
||||
public static class TestRegistration implements Registration {
|
||||
@Override
|
||||
public String getInstanceId() {
|
||||
return "testRegistrationInstance3";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServiceId() {
|
||||
return "testRegistration3";
|
||||
|
||||
@@ -1,3 +1,19 @@
|
||||
/*
|
||||
* Copyright 2016-2018 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
|
||||
*
|
||||
* http://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.serviceregistry;
|
||||
|
||||
import java.net.URI;
|
||||
@@ -29,6 +45,7 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
* @author Tim Ysewyn
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = AbstractAutoServiceRegistrationTests.Config.class,
|
||||
@@ -110,6 +127,11 @@ public class AbstractAutoServiceRegistrationTests {
|
||||
}
|
||||
|
||||
public static class TestRegistration implements Registration {
|
||||
@Override
|
||||
public String getInstanceId() {
|
||||
return "testRegistrationInstance2";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServiceId() {
|
||||
return "testRegistration2";
|
||||
|
||||
@@ -47,6 +47,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
* @author Tim Ysewyn
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = ServiceRegistryEndpointTests.TestConfiguration.class, properties = "management.endpoints.web.exposure.include=*")
|
||||
@@ -85,6 +86,11 @@ public class ServiceRegistryEndpointTests {
|
||||
@Bean
|
||||
Registration registration() {
|
||||
return new Registration() {
|
||||
@Override
|
||||
public String getInstanceId() {
|
||||
return "testRegistrationInstance1";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServiceId() {
|
||||
return "testRegistration1";
|
||||
|
||||
Reference in New Issue
Block a user