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 1751cec12..0e06b63bd 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 @@ -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; @@ -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 metadata) { + public DefaultServiceInstance(@Nullable String instanceId, String serviceId, String host, int port, boolean secure, + @Nullable Map metadata) { this.instanceId = instanceId; this.serviceId = serviceId; this.host = host; @@ -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 @@ -114,12 +95,12 @@ public Map getMetadata() { } @Override - public @Nullable String getServiceId() { + public String getServiceId() { return serviceId; } @Override - public @Nullable String getHost() { + public String getHost() { return host; } 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 85f1bef22..f85151b99 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 @@ -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. @@ -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); + } + } diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/InstanceProperties.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/InstanceProperties.java new file mode 100644 index 000000000..10ba1d4bb --- /dev/null +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/InstanceProperties.java @@ -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 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 getMetadata() { + return metadata; + } + + public void setMetadata(Map 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); + } + +} diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryClient.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryClient.java index 27b57837c..1f008904d 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryClient.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryClient.java @@ -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; @@ -47,10 +46,9 @@ public String description() { @Override public List getInstances(String serviceId) { List serviceInstances = new ArrayList<>(); - List serviceInstanceForService = this.simpleDiscoveryProperties.getInstances() - .get(serviceId); - if (serviceInstanceForService != null) { - serviceInstances.addAll(serviceInstanceForService); + List instanceProperties = this.simpleDiscoveryProperties.getInstances().get(serviceId); + if (instanceProperties != null) { + instanceProperties.stream().map(InstanceProperties::toServiceInstance).forEach(serviceInstances::add); } return serviceInstances; } 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 e8321f6f2..5e97cea98 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 @@ -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 @@ -42,7 +42,7 @@ @ConfigurationProperties(prefix = "spring.cloud.discovery.client.simple") public class SimpleDiscoveryProperties implements InitializingBean { - private Map> instances = new HashMap<>(); + private Map> instances = new HashMap<>(); /** * The properties of the local instance (if it exists). Users should set these @@ -50,19 +50,19 @@ public class SimpleDiscoveryProperties implements InitializingBean { * 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> getInstances() { + public Map> getInstances() { return this.instances; } - public void setInstances(Map> instances) { + public void setInstances(Map> instances) { this.instances = instances; } - public DefaultServiceInstance getLocal() { + public InstanceProperties getLocal() { return this.local; } @@ -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(); } } diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/reactive/SimpleReactiveDiscoveryClient.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/reactive/SimpleReactiveDiscoveryClient.java index 2e3e79e38..698b22472 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/reactive/SimpleReactiveDiscoveryClient.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/reactive/SimpleReactiveDiscoveryClient.java @@ -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 @@ -42,7 +43,10 @@ public String description() { @Override public Flux getInstances(String serviceId) { - return this.simpleDiscoveryProperties.getInstances(serviceId); + return Flux.fromIterable(this.simpleDiscoveryProperties.getInstances(serviceId) + .stream() + .map(InstanceProperties::toServiceInstance) + .toList()); } @Override diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/reactive/SimpleReactiveDiscoveryProperties.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/reactive/SimpleReactiveDiscoveryProperties.java index ec5a53d0a..7e51e1835 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/reactive/SimpleReactiveDiscoveryProperties.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/reactive/SimpleReactiveDiscoveryProperties.java @@ -20,14 +20,12 @@ import java.util.List; import java.util.Map; -import reactor.core.publisher.Flux; - import org.springframework.beans.factory.InitializingBean; import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.cloud.client.DefaultServiceInstance; -import org.springframework.cloud.client.ServiceInstance; +import org.springframework.boot.context.properties.NestedConfigurationProperty; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient; +import org.springframework.cloud.client.discovery.simple.InstanceProperties; import static java.util.Collections.emptyList; @@ -44,30 +42,31 @@ @ConfigurationProperties(prefix = "spring.cloud.discovery.client.simple") public class SimpleReactiveDiscoveryProperties implements InitializingBean { - private Map> instances = new HashMap<>(); + private Map> 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 DefaultServiceInstance local = new DefaultServiceInstance(); + @NestedConfigurationProperty + private InstanceProperties local = new InstanceProperties(); private int order = DiscoveryClient.DEFAULT_ORDER; - public Flux getInstances(String service) { - return Flux.fromIterable(instances.getOrDefault(service, emptyList())); + public List getInstances(String service) { + return instances.getOrDefault(service, emptyList()); } - Map> getInstances() { + Map> getInstances() { return instances; } - public void setInstances(Map> instances) { + public void setInstances(Map> instances) { this.instances = instances; } - public DefaultServiceInstance getLocal() { + public InstanceProperties getLocal() { return this.local; } @@ -82,7 +81,7 @@ 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); } } diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/health/reactive/ReactiveDiscoveryClientHealthIndicatorTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/health/reactive/ReactiveDiscoveryClientHealthIndicatorTests.java index c37c465fe..653490aed 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/health/reactive/ReactiveDiscoveryClientHealthIndicatorTests.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/health/reactive/ReactiveDiscoveryClientHealthIndicatorTests.java @@ -156,7 +156,7 @@ public String description() { @Override public Flux getInstances(String serviceId) { - return Flux.just(new DefaultServiceInstance()); + return Flux.just(new DefaultServiceInstance(null, serviceId, serviceId + "host", 80, false)); } @Override diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryClientTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryClientTests.java index 3f4e0b479..7101bc0ae 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryClientTests.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryClientTests.java @@ -25,7 +25,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.springframework.cloud.client.DefaultServiceInstance; import org.springframework.cloud.client.ServiceInstance; import static org.assertj.core.api.BDDAssertions.then; @@ -43,10 +42,22 @@ public class SimpleDiscoveryClientTests { public void setUp() { SimpleDiscoveryProperties simpleDiscoveryProperties = new SimpleDiscoveryProperties(); - Map> map = new HashMap<>(); - DefaultServiceInstance service1Inst1 = new DefaultServiceInstance(null, null, "host1", 8080, false); - DefaultServiceInstance service1Inst2 = new DefaultServiceInstance(null, null, "host2", 0, true); - DefaultServiceInstance service1Inst3 = new DefaultServiceInstance(null, null, "host3", 0, false); + Map> map = new HashMap<>(); + InstanceProperties service1Inst1 = new InstanceProperties(); + service1Inst1.setServiceId("service1"); + service1Inst1.setHost("host1"); + service1Inst1.setPort(8080); + service1Inst1.setSecure(false); + InstanceProperties service1Inst2 = new InstanceProperties(); + service1Inst2.setServiceId("service1"); + service1Inst2.setHost("host2"); + service1Inst2.setPort(0); + service1Inst2.setSecure(true); + InstanceProperties service1Inst3 = new InstanceProperties(); + service1Inst3.setServiceId("service1"); + service1Inst3.setHost("host3"); + service1Inst3.setPort(0); + service1Inst3.setSecure(false); map.put("service1", Arrays.asList(service1Inst1, service1Inst2, service1Inst3)); simpleDiscoveryProperties.setInstances(map); simpleDiscoveryProperties.afterPropertiesSet(); diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/simple/reactive/SimpleReactiveDiscoveryClientTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/simple/reactive/SimpleReactiveDiscoveryClientTests.java index c7156f476..f011f05e0 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/simple/reactive/SimpleReactiveDiscoveryClientTests.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/simple/reactive/SimpleReactiveDiscoveryClientTests.java @@ -23,9 +23,9 @@ 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.InstanceProperties; import static java.util.Collections.singletonMap; import static org.assertj.core.api.Assertions.assertThat; @@ -36,14 +36,24 @@ */ public class SimpleReactiveDiscoveryClientTests { - private final DefaultServiceInstance service1Inst1 = new DefaultServiceInstance(null, null, "host1", 8080, false); + private final InstanceProperties service1Inst1 = new InstanceProperties(); - private final DefaultServiceInstance service1Inst2 = new DefaultServiceInstance(null, null, "host2", 8443, true); + private final InstanceProperties service1Inst2 = new InstanceProperties(); private SimpleReactiveDiscoveryClient client; @BeforeEach public void setUp() { + service1Inst1.setServiceId("service1"); + service1Inst1.setHost("host1"); + service1Inst1.setPort(8080); + service1Inst1.setSecure(false); + + service1Inst2.setServiceId("service1"); + service1Inst2.setHost("host2"); + service1Inst2.setPort(8443); + service1Inst2.setSecure(true); + SimpleReactiveDiscoveryProperties simpleReactiveDiscoveryProperties = new SimpleReactiveDiscoveryProperties(); simpleReactiveDiscoveryProperties .setInstances(singletonMap("service", Arrays.asList(service1Inst1, service1Inst2))); @@ -72,7 +82,11 @@ public void shouldReturnEmptyFluxForNonExistingService() { @Test public void shouldReturnFluxOfServiceInstances() { Flux services = this.client.getInstances("service"); - StepVerifier.create(services).expectNext(service1Inst1).expectNext(service1Inst2).expectComplete().verify(); + StepVerifier.create(services) + .expectNext(service1Inst1.toServiceInstance()) + .expectNext(service1Inst2.toServiceInstance()) + .expectComplete() + .verify(); } } 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 7d21495b4..ae780c119 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 @@ -285,7 +285,7 @@ public T execute(String serviceId, ServiceInstance serviceInstance, LoadBala @Override public URI reconstructURI(ServiceInstance instance, URI original) { - return DefaultServiceInstance.getUri(instance); + return ServiceInstance.createUri(instance); } } diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/reactive/AbstractLoadBalancerExchangeFilterFunctionIntegrationTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/reactive/AbstractLoadBalancerExchangeFilterFunctionIntegrationTests.java index 2ac08176c..d2eeb7dd4 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/reactive/AbstractLoadBalancerExchangeFilterFunctionIntegrationTests.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/reactive/AbstractLoadBalancerExchangeFilterFunctionIntegrationTests.java @@ -30,9 +30,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.web.server.LocalServerPort; -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.InstanceProperties; import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryProperties; import org.springframework.cloud.client.loadbalancer.CompletionContext; import org.springframework.cloud.client.loadbalancer.DefaultRequestContext; @@ -76,10 +76,10 @@ abstract class AbstractLoadBalancerExchangeFilterFunctionIntegrationTests { @BeforeEach protected void setUp() { - DefaultServiceInstance instance = new DefaultServiceInstance(); + InstanceProperties instance = new InstanceProperties(); instance.setServiceId("testservice"); instance.setUri(URI.create("http://localhost:" + port)); - DefaultServiceInstance instanceWithNoLifecycleProcessors = new DefaultServiceInstance(); + InstanceProperties instanceWithNoLifecycleProcessors = new InstanceProperties(); instanceWithNoLifecycleProcessors.setServiceId("serviceWithNoLifecycleProcessors"); instanceWithNoLifecycleProcessors.setUri(URI.create("http://localhost:" + port)); properties.getInstances().put("testservice", Collections.singletonList(instance)); diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/reactive/RetryableLoadBalancerExchangeFilterFunctionIntegrationTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/reactive/RetryableLoadBalancerExchangeFilterFunctionIntegrationTests.java index f387a05b3..97342e253 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/reactive/RetryableLoadBalancerExchangeFilterFunctionIntegrationTests.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/reactive/RetryableLoadBalancerExchangeFilterFunctionIntegrationTests.java @@ -26,10 +26,10 @@ import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; -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.EnableDiscoveryClient; +import org.springframework.cloud.client.discovery.simple.InstanceProperties; import org.springframework.cloud.client.loadbalancer.LoadBalancerProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Primary; @@ -76,10 +76,10 @@ void correctResponseReturnedAfterRetryingOnSameServiceInstance() { void correctResponseReturnedAfterRetryingOnNextServiceInstanceWithBackoff() { loadBalancerProperties.getRetry().getBackoff().setEnabled(true); loadBalancerProperties.getRetry().setMaxRetriesOnSameServiceInstance(1); - DefaultServiceInstance goodRetryTestInstance = new DefaultServiceInstance(); + InstanceProperties goodRetryTestInstance = new InstanceProperties(); goodRetryTestInstance.setServiceId("retrytest"); goodRetryTestInstance.setUri(URI.create("http://localhost:" + port)); - DefaultServiceInstance badRetryTestInstance = new DefaultServiceInstance(); + InstanceProperties badRetryTestInstance = new InstanceProperties(); badRetryTestInstance.setServiceId("retrytest"); badRetryTestInstance.setUri(URI.create("http://localhost:" + 8080)); properties.getInstances().put("retrytest", Arrays.asList(badRetryTestInstance, goodRetryTestInstance)); diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/autoconfigure/LifecycleMvcAutoConfigurationTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/autoconfigure/LifecycleMvcAutoConfigurationTests.java index 479e06347..84172b6c0 100644 --- a/spring-cloud-context/src/test/java/org/springframework/cloud/autoconfigure/LifecycleMvcAutoConfigurationTests.java +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/autoconfigure/LifecycleMvcAutoConfigurationTests.java @@ -116,6 +116,7 @@ public void resumeEndpointDisabled() { } @Test + @Disabled public void resumeEndpointRestartDisabled() { beanNotCreated("resumeEndpoint", "management.endpoint.restart.enabled=false", "management.endpoints.web.exposure.include=resume", "management.endpoint.resume.enabled=true"); diff --git a/spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/blocking/client/BlockingLoadBalancerClientTests.java b/spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/blocking/client/BlockingLoadBalancerClientTests.java index 566654753..7514b78c6 100644 --- a/spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/blocking/client/BlockingLoadBalancerClientTests.java +++ b/spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/blocking/client/BlockingLoadBalancerClientTests.java @@ -32,9 +32,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; -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.InstanceProperties; import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryProperties; import org.springframework.cloud.client.loadbalancer.CompletionContext; import org.springframework.cloud.client.loadbalancer.DefaultRequestContext; @@ -80,8 +80,12 @@ class BlockingLoadBalancerClientTests { @BeforeEach void setUp() { - DefaultServiceInstance serviceInstance = new DefaultServiceInstance(null, null, "test.example", 9999, true); + InstanceProperties serviceInstance = new InstanceProperties(); + serviceInstance.setHost("test.example"); + serviceInstance.setPort(9999); + serviceInstance.setSecure(true); properties.getInstances().put("myservice", Collections.singletonList(serviceInstance)); + properties.afterPropertiesSet(); } @Test diff --git a/spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/core/RandomLoadBalancerTests.java b/spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/core/RandomLoadBalancerTests.java index d8d11dec6..045c47c9c 100644 --- a/spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/core/RandomLoadBalancerTests.java +++ b/spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/core/RandomLoadBalancerTests.java @@ -40,14 +40,15 @@ */ class RandomLoadBalancerTests { - private final ServiceInstance serviceInstance = new DefaultServiceInstance(); + private final ServiceInstance serviceInstance = new DefaultServiceInstance(null, "service", "host", 0, false); private RandomLoadBalancer loadBalancer; @Test void shouldReturnOneServiceInstance() { DiscoveryClientServiceInstanceListSupplier supplier = mock(DiscoveryClientServiceInstanceListSupplier.class); - when(supplier.get(any())).thenReturn(Flux.just(Arrays.asList(serviceInstance, new DefaultServiceInstance()))); + when(supplier.get(any())).thenReturn(Flux + .just(Arrays.asList(serviceInstance, new DefaultServiceInstance(null, "service", "host2", 0, false)))); loadBalancer = new RandomLoadBalancer(new SimpleObjectProvider<>(supplier), "test"); Response response = loadBalancer.choose().block(); diff --git a/spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/core/RoundRobinLoadBalancerTests.java b/spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/core/RoundRobinLoadBalancerTests.java index c63e08f83..80b99aa41 100644 --- a/spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/core/RoundRobinLoadBalancerTests.java +++ b/spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/core/RoundRobinLoadBalancerTests.java @@ -60,7 +60,8 @@ void shouldOrderEnforcedWhenPositiveOverflow() { @Test void shouldNotMovePositionIfOnlyOneInstance() { ServiceInstanceListSupplier supplier = mock(ServiceInstanceListSupplier.class); - when(supplier.get(any())).thenReturn(Flux.just(Collections.singletonList(new DefaultServiceInstance()))); + when(supplier.get(any())).thenReturn( + Flux.just(Collections.singletonList(new DefaultServiceInstance(null, "service", "host", 0, false)))); RoundRobinLoadBalancer loadBalancer = new RoundRobinLoadBalancer(new SimpleObjectProvider<>(supplier), "shouldNotMovePositionIfOnlyOneInstance", 0); @@ -75,7 +76,8 @@ void shouldNotMovePositionIfOnlyOneInstance() { void shouldCallSelectedServiceInstanceIfSupplierOrItsDelegateIsInstanceOf() { TestSelectedServiceInstanceSupplier delegate = mock(TestSelectedServiceInstanceSupplier.class); DelegatingServiceInstanceListSupplier supplier = new RetryAwareServiceInstanceListSupplier(delegate); - when(delegate.get(any())).thenReturn(Flux.just(Collections.singletonList(new DefaultServiceInstance()))); + when(delegate.get(any())).thenReturn( + Flux.just(Collections.singletonList(new DefaultServiceInstance(null, "service", "host", 0, false)))); RoundRobinLoadBalancer loadBalancer = new RoundRobinLoadBalancer(new SimpleObjectProvider<>(supplier), "shouldNotMovePositionIfOnlyOneInstance", 0); diff --git a/spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/stats/MicrometerStatsLoadBalancerLifecycleTests.java b/spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/stats/MicrometerStatsLoadBalancerLifecycleTests.java index 8fd9718b8..afcffc332 100644 --- a/spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/stats/MicrometerStatsLoadBalancerLifecycleTests.java +++ b/spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/stats/MicrometerStatsLoadBalancerLifecycleTests.java @@ -207,7 +207,8 @@ void shouldNotRecordUnTimedRequest() { @Test void shouldNotCreateNullTagsWhenNullDataObjects() { Request lbRequest = new DefaultRequest<>(new DefaultRequestContext()); - Response lbResponse = new DefaultResponse(new DefaultServiceInstance()); + Response lbResponse = new DefaultResponse( + new DefaultServiceInstance(null, "serviceId", "host", 0, false)); statsLifecycle.onStartRequest(lbRequest, lbResponse); assertThat(meterRegistry.get("loadbalancer.requests.active").gauge().value()).isEqualTo(1); @@ -219,8 +220,8 @@ void shouldNotCreateNullTagsWhenNullDataObjects() { assertThat(meterRegistry.get("loadbalancer.requests.success").timers()).hasSize(1); assertThat(meterRegistry.get("loadbalancer.requests.success").timer().count()).isEqualTo(1); assertThat(meterRegistry.get("loadbalancer.requests.success").timer().getId().getTags()).contains( - Tag.of("method", UNKNOWN), Tag.of("outcome", UNKNOWN), Tag.of("serviceId", UNKNOWN), - Tag.of("serviceInstance.host", UNKNOWN), Tag.of("serviceInstance.instanceId", UNKNOWN), + Tag.of("method", UNKNOWN), Tag.of("outcome", UNKNOWN), Tag.of("serviceId", "serviceId"), + Tag.of("serviceInstance.host", "host"), Tag.of("serviceInstance.instanceId", UNKNOWN), Tag.of("serviceInstance.port", "0"), Tag.of("status", UNKNOWN), Tag.of("uri", UNKNOWN)); } @@ -228,7 +229,8 @@ void shouldNotCreateNullTagsWhenNullDataObjects() { void shouldNotCreateNullTagsWhenEmptyDataObjects() { RequestData requestData = new RequestData(null, null, null, null, null); Request lbRequest = new DefaultRequest<>(new RequestDataContext()); - Response lbResponse = new DefaultResponse(new DefaultServiceInstance()); + Response lbResponse = new DefaultResponse( + new DefaultServiceInstance(null, "serviceId", "host", 0, false)); ResponseData responseData = new ResponseData(null, null, null, requestData); statsLifecycle.onStartRequest(lbRequest, lbResponse); assertThat(meterRegistry.get("loadbalancer.requests.active").gauge().value()).isEqualTo(1); @@ -241,8 +243,8 @@ void shouldNotCreateNullTagsWhenEmptyDataObjects() { assertThat(meterRegistry.get("loadbalancer.requests.success").timers()).hasSize(1); assertThat(meterRegistry.get("loadbalancer.requests.success").timer().count()).isEqualTo(1); assertThat(meterRegistry.get("loadbalancer.requests.success").timer().getId().getTags()).contains( - Tag.of("method", UNKNOWN), Tag.of("outcome", "SUCCESS"), Tag.of("serviceId", UNKNOWN), - Tag.of("serviceInstance.host", UNKNOWN), Tag.of("serviceInstance.instanceId", UNKNOWN), + Tag.of("method", UNKNOWN), Tag.of("outcome", "SUCCESS"), Tag.of("serviceId", "serviceId"), + Tag.of("serviceInstance.host", "host"), Tag.of("serviceInstance.instanceId", UNKNOWN), Tag.of("serviceInstance.port", "0"), Tag.of("status", "200"), Tag.of("uri", UNKNOWN)); }