From 5eb23a8c3c2c2669e6343720664ce2c656e4c88d Mon Sep 17 00:00:00 2001 From: Tim Ysewyn Date: Mon, 5 Nov 2018 19:22:50 +0100 Subject: [PATCH] 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 --- .../cloud/client/DefaultServiceInstance.java | 62 +++++++++++++++++-- .../cloud/client/ServiceInstance.java | 10 ++- .../simple/SimpleDiscoveryProperties.java | 14 +++++ .../CompositeDiscoveryClientTestsConfig.java | 3 +- ...ediaAutoConfigurationIntegrationTests.java | 15 ++--- .../DiscoveredResourceUnitTests.java | 5 +- ...actLoadBalancerAutoConfigurationTests.java | 5 +- ...yncLoadBalancerAutoConfigurationTests.java | 5 +- ...iveLoadBalancerAutoConfigurationTests.java | 5 +- ...oServiceRegistrationMgmtDisabledTests.java | 22 +++++++ .../AbstractAutoServiceRegistrationTests.java | 22 +++++++ .../ServiceRegistryEndpointTests.java | 6 ++ 12 files changed, 148 insertions(+), 26 deletions(-) diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/DefaultServiceInstance.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/DefaultServiceInstance.java index 69a7a4b9..5facde91 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/DefaultServiceInstance.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/DefaultServiceInstance.java @@ -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 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 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 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); } } diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/ServiceInstance.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/ServiceInstance.java index d380bc02..546143e8 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/ServiceInstance.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/ServiceInstance.java @@ -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. */ diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryProperties.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryProperties.java index 84590862..d920240c 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryProperties.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryProperties.java @@ -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 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; diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientTestsConfig.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientTestsConfig.java index 2a846eaf..443830bc 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientTestsConfig.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientTestsConfig.java @@ -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 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); } diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/hypermedia/CloudHypermediaAutoConfigurationIntegrationTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/hypermedia/CloudHypermediaAutoConfigurationIntegrationTests.java index 970bea32..7df82999 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/hypermedia/CloudHypermediaAutoConfigurationIntegrationTests.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/hypermedia/CloudHypermediaAutoConfigurationIntegrationTests.java @@ -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")); } } } diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/hypermedia/DiscoveredResourceUnitTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/hypermedia/DiscoveredResourceUnitTests.java index 9a7b949a..e580dd1b 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/hypermedia/DiscoveredResourceUnitTests.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/hypermedia/DiscoveredResourceUnitTests.java @@ -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(); diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/AbstractLoadBalancerAutoConfigurationTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/AbstractLoadBalancerAutoConfigurationTests.java index 8df8ccc8..69a4817b 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/AbstractLoadBalancerAutoConfigurationTests.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/AbstractLoadBalancerAutoConfigurationTests.java @@ -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); } diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/AsyncLoadBalancerAutoConfigurationTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/AsyncLoadBalancerAutoConfigurationTests.java index 41d4f5f9..51e14498 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/AsyncLoadBalancerAutoConfigurationTests.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/AsyncLoadBalancerAutoConfigurationTests.java @@ -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); } diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/reactive/ReactiveLoadBalancerAutoConfigurationTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/reactive/ReactiveLoadBalancerAutoConfigurationTests.java index 9cb3cf21..f9c58c02 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/reactive/ReactiveLoadBalancerAutoConfigurationTests.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/reactive/ReactiveLoadBalancerAutoConfigurationTests.java @@ -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); } diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationMgmtDisabledTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationMgmtDisabledTests.java index 9b9e840f..f0540f0d 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationMgmtDisabledTests.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationMgmtDisabledTests.java @@ -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"; diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationTests.java index 92d618cd..f1e1a8f8 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationTests.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationTests.java @@ -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"; diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/endpoint/ServiceRegistryEndpointTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/endpoint/ServiceRegistryEndpointTests.java index 2369af66..7905a4e4 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/endpoint/ServiceRegistryEndpointTests.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/endpoint/ServiceRegistryEndpointTests.java @@ -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";