diff --git a/spring-cloud-cloudfoundry-discovery/src/main/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryDiscoveryClient.java b/spring-cloud-cloudfoundry-discovery/src/main/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryDiscoveryClient.java index ad9d3f7..533bfbc 100644 --- a/spring-cloud-cloudfoundry-discovery/src/main/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryDiscoveryClient.java +++ b/spring-cloud-cloudfoundry-discovery/src/main/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryDiscoveryClient.java @@ -30,23 +30,28 @@ import java.util.HashMap; import java.util.List; /** - * Cloud Foundry maintains a registry of running applications which we expose here as CloudFoundryService instances. + * Cloud Foundry maintains a registry of running applications which we expose here as + * CloudFoundryService instances. * * @author Josh Long * @author Spencer Gibb * @author Dave Syer + * @author Olga Maciaszek-Sharma */ public class CloudFoundryDiscoveryClient implements DiscoveryClient { private final CloudFoundryService cloudFoundryService; private final CloudFoundryOperations cloudFoundryOperations; + private final CloudFoundryDiscoveryProperties cloudFoundryDiscoveryProperties; private final String description = "Cloud Foundry " + DiscoveryClient.class.getName() + " implementation"; CloudFoundryDiscoveryClient(CloudFoundryOperations cloudFoundryOperations, - CloudFoundryService svc) { + CloudFoundryService svc, + CloudFoundryDiscoveryProperties cloudFoundryDiscoveryProperties) { this.cloudFoundryService = svc; this.cloudFoundryOperations = cloudFoundryOperations; + this.cloudFoundryDiscoveryProperties = cloudFoundryDiscoveryProperties; } @Override @@ -89,4 +94,9 @@ public class CloudFoundryDiscoveryClient implements DiscoveryClient { .blockOptional() .orElse(new ArrayList<>()); } + + @Override + public int getOrder() { + return this.cloudFoundryDiscoveryProperties.getOrder(); + } } \ No newline at end of file diff --git a/spring-cloud-cloudfoundry-discovery/src/main/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryDiscoveryClientConfiguration.java b/spring-cloud-cloudfoundry-discovery/src/main/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryDiscoveryClientConfiguration.java index bf63744..97cc709 100644 --- a/spring-cloud-cloudfoundry-discovery/src/main/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryDiscoveryClientConfiguration.java +++ b/spring-cloud-cloudfoundry-discovery/src/main/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryDiscoveryClientConfiguration.java @@ -17,6 +17,7 @@ package org.springframework.cloud.cloudfoundry.discovery; import org.cloudfoundry.operations.CloudFoundryOperations; + import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -37,8 +38,9 @@ public class CloudFoundryDiscoveryClientConfiguration { @Bean @ConditionalOnMissingBean(CloudFoundryDiscoveryClient.class) public CloudFoundryDiscoveryClient cloudFoundryDiscoveryClient( - CloudFoundryOperations cf, CloudFoundryService svc) { - return new CloudFoundryDiscoveryClient(cf, svc); + CloudFoundryOperations cf, CloudFoundryService svc, + CloudFoundryDiscoveryProperties cloudFoundryDiscoveryProperties) { + return new CloudFoundryDiscoveryClient(cf, svc, cloudFoundryDiscoveryProperties); } @Bean diff --git a/spring-cloud-cloudfoundry-discovery/src/main/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryDiscoveryClientProperties.java b/spring-cloud-cloudfoundry-discovery/src/main/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryDiscoveryClientProperties.java new file mode 100644 index 0000000..5e6c07d --- /dev/null +++ b/spring-cloud-cloudfoundry-discovery/src/main/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryDiscoveryClientProperties.java @@ -0,0 +1,40 @@ +/* + * 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.cloudfoundry.discovery; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.core.Ordered; + +/** + * Properties used for configuring the CloudFoundry implementation of + * {@link org.springframework.cloud.client.discovery.DiscoveryClient} + * + * @author Olga Maciaszek-Sharma + */ +@ConfigurationProperties("spring.cloud.discovery.client.cloudfoundry") +public class CloudFoundryDiscoveryClientProperties { + + private int order = Ordered.LOWEST_PRECEDENCE; + + public int getOrder() { + return this.order; + } + + public void setOrder(int order) { + this.order = order; + } +} diff --git a/spring-cloud-cloudfoundry-discovery/src/main/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryDiscoveryProperties.java b/spring-cloud-cloudfoundry-discovery/src/main/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryDiscoveryProperties.java index a2ff8d2..92552e9 100644 --- a/spring-cloud-cloudfoundry-discovery/src/main/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryDiscoveryProperties.java +++ b/spring-cloud-cloudfoundry-discovery/src/main/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryDiscoveryProperties.java @@ -40,6 +40,11 @@ public class CloudFoundryDiscoveryProperties { */ private int defaultServerPort = 80; + /** + * Order of the discovery client used by `CompositeDiscoveryClient` for sorting available clients. + */ + private int order = 0; + public boolean isEnabled() { return enabled; } @@ -64,12 +69,21 @@ public class CloudFoundryDiscoveryProperties { this.defaultServerPort = defaultServerPort; } + public int getOrder() { + return order; + } + + public void setOrder(int order) { + this.order = order; + } + @Override public String toString() { return "CloudFoundryDiscoveryProperties{" + "enabled=" + enabled + ", heartbeatFrequency=" + heartbeatFrequency + ", defaultServerPort=" + defaultServerPort + + ", order=" + order + '}'; } } diff --git a/spring-cloud-cloudfoundry-discovery/src/test/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryDiscoveryClientTest.java b/spring-cloud-cloudfoundry-discovery/src/test/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryDiscoveryClientTest.java index a4aa047..2b3e853 100644 --- a/spring-cloud-cloudfoundry-discovery/src/test/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryDiscoveryClientTest.java +++ b/spring-cloud-cloudfoundry-discovery/src/test/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryDiscoveryClientTest.java @@ -54,7 +54,8 @@ public class CloudFoundryDiscoveryClientTest { public void setUp() { this.ops = mock(CloudFoundryOperations.class); this.svc = mock(CloudFoundryService.class); - this.cloudFoundryDiscoveryClient = new CloudFoundryDiscoveryClient(this.ops, this.svc); + this.cloudFoundryDiscoveryClient = new CloudFoundryDiscoveryClient(this.ops, this.svc, + new CloudFoundryDiscoveryProperties()); } @Test