From 872b93d89e8a49fd1c55ec5a44c7104770babafe Mon Sep 17 00:00:00 2001 From: Andrew Fitzgerald Date: Sat, 2 May 2020 00:32:39 -0400 Subject: [PATCH 1/7] WIP Cloud Map Service Discovery --- pom.xml | 1 + .../pom.xml | 47 ++++++++++++ .../cloudmap/AwsCloudMapDiscoveryClient.java | 71 +++++++++++++++++ .../cloudmap/AwsCloudMapServiceInstance.java | 76 +++++++++++++++++++ 4 files changed, 195 insertions(+) create mode 100644 spring-cloud-aws-cloud-map-service-discovery/pom.xml create mode 100644 spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapDiscoveryClient.java create mode 100644 spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapServiceInstance.java diff --git a/pom.xml b/pom.xml index 3724cc666..9218c751f 100644 --- a/pom.xml +++ b/pom.xml @@ -61,6 +61,7 @@ spring-cloud-aws-actuator spring-cloud-aws-parameter-store-config spring-cloud-aws-secrets-manager-config + spring-cloud-aws-cloud-map-service-discovery spring-cloud-starter-aws spring-cloud-starter-aws-jdbc spring-cloud-starter-aws-messaging diff --git a/spring-cloud-aws-cloud-map-service-discovery/pom.xml b/spring-cloud-aws-cloud-map-service-discovery/pom.xml new file mode 100644 index 000000000..96c1b11f2 --- /dev/null +++ b/spring-cloud-aws-cloud-map-service-discovery/pom.xml @@ -0,0 +1,47 @@ + + + + + 4.0.0 + + org.springframework.cloud + spring-cloud-aws + 3.0.0-SNAPSHOT + + + spring-cloud-aws-cloud-map-service-discovery + Spring Cloud AWS Cloud Map Service Discovery + Spring Cloud AWS Cloud Map Service Discovery + + + + org.springframework + spring-core + + + org.springframework.cloud + spring-cloud-commons + ${spring-cloud-commons.version} + + + com.amazonaws + aws-java-sdk-servicediscovery + + + diff --git a/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapDiscoveryClient.java b/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapDiscoveryClient.java new file mode 100644 index 000000000..0b95cdfd5 --- /dev/null +++ b/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapDiscoveryClient.java @@ -0,0 +1,71 @@ +/* + * Copyright 2013-2019 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.aws.cloudmap; + +import java.util.List; +import java.util.stream.Collectors; + +import com.amazonaws.services.servicediscovery.AWSServiceDiscovery; +import com.amazonaws.services.servicediscovery.model.GetInstanceRequest; +import com.amazonaws.services.servicediscovery.model.Instance; +import com.amazonaws.services.servicediscovery.model.ListInstancesRequest; +import com.amazonaws.services.servicediscovery.model.ListServicesRequest; +import com.amazonaws.services.servicediscovery.model.ServiceSummary; + +import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.client.discovery.DiscoveryClient; + +public class AwsCloudMapDiscoveryClient implements DiscoveryClient { + + private final AWSServiceDiscovery aws; + + public AwsCloudMapDiscoveryClient(AWSServiceDiscovery aws) { + this.aws = aws; + } + + @Override + public String description() { + return "AWS Cloud Map Discovery Client"; + } + + @Override + public List getInstances(String serviceId) { + ListInstancesRequest listInstancesRequest = new ListInstancesRequest() + .withServiceId(serviceId); + // TODO pagination + // TODO parallel requests? + // TODO filter on health? + return aws.listInstances(listInstancesRequest).getInstances().stream() + .map(summary -> getInstance(serviceId, summary.getId())) + .collect(Collectors.toList()); + + } + + private AwsCloudMapServiceInstance getInstance(String service, String id) { + Instance instance = aws.getInstance(new GetInstanceRequest().withInstanceId(id)) + .getInstance(); + return new AwsCloudMapServiceInstance(service, instance); + } + + @Override + public List getServices() { + // TODO pagination + return aws.listServices(new ListServicesRequest()).getServices().stream() + .map(ServiceSummary::getId).collect(Collectors.toList()); + } + +} diff --git a/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapServiceInstance.java b/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapServiceInstance.java new file mode 100644 index 000000000..0ea1b27fd --- /dev/null +++ b/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapServiceInstance.java @@ -0,0 +1,76 @@ +/* + * Copyright 2013-2019 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.aws.cloudmap; + +import java.net.URI; +import java.util.Map; + +import com.amazonaws.services.servicediscovery.model.Instance; + +import org.springframework.cloud.client.ServiceInstance; + +public class AwsCloudMapServiceInstance implements ServiceInstance { + + private static final String AWS_INSTANCE_IPV_4 = "AWS_INSTANCE_IPV4"; + + private static final String AWS_INSTANCE_PORT = "AWS_INSTANCE_PORT"; + + private final String serviceId; + + private final Instance instance; + + public AwsCloudMapServiceInstance(String serviceId, Instance instance) { + this.serviceId = serviceId; + this.instance = instance; + } + + @Override + public String getServiceId() { + return serviceId; + } + + @Override + public String getHost() { + // TODO alternate host attributes + return instance.getAttributes().get(AWS_INSTANCE_IPV_4); + } + + @Override + public int getPort() { + // TODO are there other possible values? + String port = instance.getAttributes().get(AWS_INSTANCE_PORT); + // TODO error handling? + return Integer.parseInt(port); + } + + @Override + public boolean isSecure() { + return getPort() == 443; + } + + @Override + public URI getUri() { + String scheme = isSecure() ? "https" : "http"; + return URI.create(String.format("%s:%s/%s", scheme, getHost(), getPort())); + } + + @Override + public Map getMetadata() { + return instance.getAttributes(); + } + +} From a6a848eaceb70241527cbdfef68e255e59806012 Mon Sep 17 00:00:00 2001 From: Andrew Fitzgerald Date: Sat, 2 May 2020 10:47:43 -0400 Subject: [PATCH 2/7] include service id with getInstance request --- .../cloud/aws/cloudmap/AwsCloudMapDiscoveryClient.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapDiscoveryClient.java b/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapDiscoveryClient.java index 0b95cdfd5..d0fe6bb62 100644 --- a/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapDiscoveryClient.java +++ b/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapDiscoveryClient.java @@ -55,10 +55,10 @@ public List getInstances(String serviceId) { } - private AwsCloudMapServiceInstance getInstance(String service, String id) { - Instance instance = aws.getInstance(new GetInstanceRequest().withInstanceId(id)) + private AwsCloudMapServiceInstance getInstance(String serviceId, String instanceId) { + Instance instance = aws.getInstance(new GetInstanceRequest().withServiceId(serviceId).withInstanceId(instanceId)) .getInstance(); - return new AwsCloudMapServiceInstance(service, instance); + return new AwsCloudMapServiceInstance(serviceId, instance); } @Override From c64702b27f15b9c015c1a49fc3dcb128f11549ba Mon Sep 17 00:00:00 2001 From: Andrew Fitzgerald Date: Tue, 5 May 2020 22:17:31 -0400 Subject: [PATCH 3/7] formatting --- .../aws/cloudmap/AwsCloudMapDiscoveryClient.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapDiscoveryClient.java b/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapDiscoveryClient.java index d0fe6bb62..7d581e529 100644 --- a/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapDiscoveryClient.java +++ b/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapDiscoveryClient.java @@ -44,19 +44,18 @@ public String description() { @Override public List getInstances(String serviceId) { - ListInstancesRequest listInstancesRequest = new ListInstancesRequest() - .withServiceId(serviceId); + ListInstancesRequest listInstancesRequest = new ListInstancesRequest().withServiceId(serviceId); // TODO pagination // TODO parallel requests? // TODO filter on health? return aws.listInstances(listInstancesRequest).getInstances().stream() - .map(summary -> getInstance(serviceId, summary.getId())) - .collect(Collectors.toList()); + .map(summary -> getInstance(serviceId, summary.getId())).collect(Collectors.toList()); } private AwsCloudMapServiceInstance getInstance(String serviceId, String instanceId) { - Instance instance = aws.getInstance(new GetInstanceRequest().withServiceId(serviceId).withInstanceId(instanceId)) + Instance instance = aws + .getInstance(new GetInstanceRequest().withServiceId(serviceId).withInstanceId(instanceId)) .getInstance(); return new AwsCloudMapServiceInstance(serviceId, instance); } @@ -64,8 +63,8 @@ private AwsCloudMapServiceInstance getInstance(String serviceId, String instance @Override public List getServices() { // TODO pagination - return aws.listServices(new ListServicesRequest()).getServices().stream() - .map(ServiceSummary::getId).collect(Collectors.toList()); + return aws.listServices(new ListServicesRequest()).getServices().stream().map(ServiceSummary::getId) + .collect(Collectors.toList()); } } From 5cbbff3129e518f883e550ece57e33f708ba7bc3 Mon Sep 17 00:00:00 2001 From: Andrew Fitzgerald Date: Sat, 9 May 2020 23:18:18 -0400 Subject: [PATCH 4/7] fix scheme --- .../cloud/aws/cloudmap/AwsCloudMapServiceInstance.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapServiceInstance.java b/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapServiceInstance.java index 0ea1b27fd..790ada6b9 100644 --- a/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapServiceInstance.java +++ b/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapServiceInstance.java @@ -65,7 +65,7 @@ public boolean isSecure() { @Override public URI getUri() { String scheme = isSecure() ? "https" : "http"; - return URI.create(String.format("%s:%s/%s", scheme, getHost(), getPort())); + return URI.create(String.format("%s://%s/%s", scheme, getHost(), getPort())); } @Override From fdffdf19a1956e7f4488666bcdb6450958614bc5 Mon Sep 17 00:00:00 2001 From: Andrew Fitzgerald Date: Sat, 9 May 2020 23:46:20 -0400 Subject: [PATCH 5/7] Add Reactive client --- .../pom.xml | 5 ++ .../AwsCloudMapReactiveDiscoveryClient.java | 74 +++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapReactiveDiscoveryClient.java diff --git a/spring-cloud-aws-cloud-map-service-discovery/pom.xml b/spring-cloud-aws-cloud-map-service-discovery/pom.xml index 96c1b11f2..fab7e789c 100644 --- a/spring-cloud-aws-cloud-map-service-discovery/pom.xml +++ b/spring-cloud-aws-cloud-map-service-discovery/pom.xml @@ -34,6 +34,11 @@ org.springframework spring-core + + io.projectreactor + reactor-core + true + org.springframework.cloud spring-cloud-commons diff --git a/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapReactiveDiscoveryClient.java b/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapReactiveDiscoveryClient.java new file mode 100644 index 000000000..66682d11b --- /dev/null +++ b/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapReactiveDiscoveryClient.java @@ -0,0 +1,74 @@ +/* + * Copyright 2013-2019 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.aws.cloudmap; + +import com.amazonaws.services.servicediscovery.AWSServiceDiscovery; +import com.amazonaws.services.servicediscovery.model.GetInstanceRequest; +import com.amazonaws.services.servicediscovery.model.GetInstanceResult; +import com.amazonaws.services.servicediscovery.model.ListInstancesRequest; +import com.amazonaws.services.servicediscovery.model.ListServicesRequest; +import com.amazonaws.services.servicediscovery.model.ListServicesResult; +import com.amazonaws.services.servicediscovery.model.ServiceSummary; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient; + +public class AwsCloudMapReactiveDiscoveryClient implements ReactiveDiscoveryClient { + + private final AWSServiceDiscovery aws; + + public AwsCloudMapReactiveDiscoveryClient(AWSServiceDiscovery aws) { + this.aws = aws; + } + + @Override + public String description() { + return "AWS Cloud Map Reactive Discovery Client"; + } + + @Override + public Flux getInstances(String serviceId) { + ListInstancesRequest request = new ListInstancesRequest() + .withServiceId(serviceId); + + return Mono.fromSupplier(() -> aws.listInstances(request)) + .flatMapMany(resp -> Flux.fromIterable(resp.getInstances()) + .flatMap(summary -> getInstance(serviceId, summary.getId()))); + } + + private Mono getInstance(String serviceId, + String instanceId) { + GetInstanceRequest request = new GetInstanceRequest().withServiceId(serviceId) + .withInstanceId(instanceId); + + return Mono.fromSupplier(() -> aws.getInstance(request)) + .map(GetInstanceResult::getInstance) + .map(instance -> new AwsCloudMapServiceInstance(serviceId, instance)); + } + + @Override + public Flux getServices() { + ListServicesRequest request = new ListServicesRequest(); + + return Mono.fromSupplier(() -> aws.listServices(request)) + .flatMapIterable(ListServicesResult::getServices) + .map(ServiceSummary::getId); + } + +} From d84564ec6ce66a512571b364c88fe673af80977b Mon Sep 17 00:00:00 2001 From: Andrew Fitzgerald Date: Sun, 10 May 2020 00:05:29 -0400 Subject: [PATCH 6/7] Add properties + configuration boilerplate and add reactive client to reactive package. --- .../pom.xml | 10 ++++ ...sCloudMapDiscoveryClientConfiguration.java | 54 ++++++++++++++++++ .../AwsCloudMapDiscoveryProperties.java | 34 +++++++++++ ...ditionalOnAwsCloudMapDiscoveryEnabled.java | 35 ++++++++++++ .../AwsCloudMapReactiveDiscoveryClient.java | 23 +++----- ...pReactiveDiscoveryClientConfiguration.java | 57 +++++++++++++++++++ 6 files changed, 199 insertions(+), 14 deletions(-) create mode 100644 spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapDiscoveryClientConfiguration.java create mode 100644 spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapDiscoveryProperties.java create mode 100644 spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/ConditionalOnAwsCloudMapDiscoveryEnabled.java rename spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/{ => reactive}/AwsCloudMapReactiveDiscoveryClient.java (75%) create mode 100644 spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/reactive/AwsCloudMapReactiveDiscoveryClientConfiguration.java diff --git a/spring-cloud-aws-cloud-map-service-discovery/pom.xml b/spring-cloud-aws-cloud-map-service-discovery/pom.xml index fab7e789c..1aa533356 100644 --- a/spring-cloud-aws-cloud-map-service-discovery/pom.xml +++ b/spring-cloud-aws-cloud-map-service-discovery/pom.xml @@ -39,6 +39,16 @@ reactor-core true + + org.springframework.boot + spring-boot-starter-web + true + + + org.springframework.boot + spring-boot-starter-webflux + true + org.springframework.cloud spring-cloud-commons diff --git a/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapDiscoveryClientConfiguration.java b/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapDiscoveryClientConfiguration.java new file mode 100644 index 000000000..70e3dd566 --- /dev/null +++ b/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapDiscoveryClientConfiguration.java @@ -0,0 +1,54 @@ +/* + * Copyright 2013-2019 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.aws.cloudmap; + +import com.amazonaws.services.servicediscovery.AWSServiceDiscovery; +import com.amazonaws.services.servicediscovery.AWSServiceDiscoveryClientBuilder; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.cloud.client.ConditionalOnBlockingDiscoveryEnabled; +import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration(proxyBeanMethods = false) +@ConditionalOnDiscoveryEnabled +@ConditionalOnBlockingDiscoveryEnabled +@ConditionalOnAwsCloudMapDiscoveryEnabled +@EnableConfigurationProperties +public class AwsCloudMapDiscoveryClientConfiguration { + + @Bean + @ConditionalOnMissingBean + public AWSServiceDiscovery awsServiceDiscovery() { + return AWSServiceDiscoveryClientBuilder.defaultClient(); + } + + @Bean + @ConditionalOnMissingBean + public AwsCloudMapDiscoveryProperties awsCloudMapDiscoveryProperties() { + return new AwsCloudMapDiscoveryProperties(); + } + + @Bean + @ConditionalOnMissingBean + public AwsCloudMapDiscoveryClient awsCloudMapReactiveDiscoveryClient(AWSServiceDiscovery awsServiceDiscovery) { + return new AwsCloudMapDiscoveryClient(awsServiceDiscovery); + } + +} diff --git a/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapDiscoveryProperties.java b/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapDiscoveryProperties.java new file mode 100644 index 000000000..cbc4cfdda --- /dev/null +++ b/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapDiscoveryProperties.java @@ -0,0 +1,34 @@ +/* + * Copyright 2013-2019 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.aws.cloudmap; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties("spring.cloud.aws.discovery.cloudmap") +public class AwsCloudMapDiscoveryProperties { + + private boolean enabled = true; + + boolean isEnabled() { + return enabled; + } + + void setEnabled(boolean enabled) { + this.enabled = enabled; + } + +} diff --git a/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/ConditionalOnAwsCloudMapDiscoveryEnabled.java b/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/ConditionalOnAwsCloudMapDiscoveryEnabled.java new file mode 100644 index 000000000..434eaf5ff --- /dev/null +++ b/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/ConditionalOnAwsCloudMapDiscoveryEnabled.java @@ -0,0 +1,35 @@ +/* + * Copyright 2013-2019 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.aws.cloudmap; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; + +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +@ConditionalOnProperty(value = "spring.cloud.aws.discovery.cloudmap.enabled", matchIfMissing = true) +public @interface ConditionalOnAwsCloudMapDiscoveryEnabled { + +} diff --git a/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapReactiveDiscoveryClient.java b/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/reactive/AwsCloudMapReactiveDiscoveryClient.java similarity index 75% rename from spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapReactiveDiscoveryClient.java rename to spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/reactive/AwsCloudMapReactiveDiscoveryClient.java index 66682d11b..ae985ac58 100644 --- a/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapReactiveDiscoveryClient.java +++ b/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/reactive/AwsCloudMapReactiveDiscoveryClient.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.cloud.aws.cloudmap; +package org.springframework.cloud.aws.cloudmap.reactive; import com.amazonaws.services.servicediscovery.AWSServiceDiscovery; import com.amazonaws.services.servicediscovery.model.GetInstanceRequest; @@ -26,6 +26,7 @@ import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; +import org.springframework.cloud.aws.cloudmap.AwsCloudMapServiceInstance; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient; @@ -44,21 +45,16 @@ public String description() { @Override public Flux getInstances(String serviceId) { - ListInstancesRequest request = new ListInstancesRequest() - .withServiceId(serviceId); + ListInstancesRequest request = new ListInstancesRequest().withServiceId(serviceId); - return Mono.fromSupplier(() -> aws.listInstances(request)) - .flatMapMany(resp -> Flux.fromIterable(resp.getInstances()) - .flatMap(summary -> getInstance(serviceId, summary.getId()))); + return Mono.fromSupplier(() -> aws.listInstances(request)).flatMapMany(resp -> Flux + .fromIterable(resp.getInstances()).flatMap(summary -> getInstance(serviceId, summary.getId()))); } - private Mono getInstance(String serviceId, - String instanceId) { - GetInstanceRequest request = new GetInstanceRequest().withServiceId(serviceId) - .withInstanceId(instanceId); + private Mono getInstance(String serviceId, String instanceId) { + GetInstanceRequest request = new GetInstanceRequest().withServiceId(serviceId).withInstanceId(instanceId); - return Mono.fromSupplier(() -> aws.getInstance(request)) - .map(GetInstanceResult::getInstance) + return Mono.fromSupplier(() -> aws.getInstance(request)).map(GetInstanceResult::getInstance) .map(instance -> new AwsCloudMapServiceInstance(serviceId, instance)); } @@ -66,8 +62,7 @@ private Mono getInstance(String serviceId, public Flux getServices() { ListServicesRequest request = new ListServicesRequest(); - return Mono.fromSupplier(() -> aws.listServices(request)) - .flatMapIterable(ListServicesResult::getServices) + return Mono.fromSupplier(() -> aws.listServices(request)).flatMapIterable(ListServicesResult::getServices) .map(ServiceSummary::getId); } diff --git a/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/reactive/AwsCloudMapReactiveDiscoveryClientConfiguration.java b/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/reactive/AwsCloudMapReactiveDiscoveryClientConfiguration.java new file mode 100644 index 000000000..fd479c81c --- /dev/null +++ b/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/reactive/AwsCloudMapReactiveDiscoveryClientConfiguration.java @@ -0,0 +1,57 @@ +/* + * Copyright 2013-2019 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.aws.cloudmap.reactive; + +import com.amazonaws.services.servicediscovery.AWSServiceDiscovery; +import com.amazonaws.services.servicediscovery.AWSServiceDiscoveryClientBuilder; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.cloud.aws.cloudmap.AwsCloudMapDiscoveryProperties; +import org.springframework.cloud.aws.cloudmap.ConditionalOnAwsCloudMapDiscoveryEnabled; +import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled; +import org.springframework.cloud.client.ConditionalOnReactiveDiscoveryEnabled; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration(proxyBeanMethods = false) +@ConditionalOnDiscoveryEnabled +@ConditionalOnReactiveDiscoveryEnabled +@ConditionalOnAwsCloudMapDiscoveryEnabled +@EnableConfigurationProperties +public class AwsCloudMapReactiveDiscoveryClientConfiguration { + + @Bean + @ConditionalOnMissingBean + public AWSServiceDiscovery awsServiceDiscovery() { + return AWSServiceDiscoveryClientBuilder.defaultClient(); + } + + @Bean + @ConditionalOnMissingBean + public AwsCloudMapDiscoveryProperties awsCloudMapDiscoveryProperties() { + return new AwsCloudMapDiscoveryProperties(); + } + + @Bean + @ConditionalOnMissingBean + public AwsCloudMapReactiveDiscoveryClient awsCloudMapReactiveDiscoveryClient( + AWSServiceDiscovery awsServiceDiscovery) { + return new AwsCloudMapReactiveDiscoveryClient(awsServiceDiscovery); + } + +} From e072a4e494d652563d34f3f115f9f3e247eb1648 Mon Sep 17 00:00:00 2001 From: Andrew Fitzgerald Date: Sun, 10 May 2020 00:10:19 -0400 Subject: [PATCH 7/7] Configure instance id + scheme for service instance --- .../aws/cloudmap/AwsCloudMapServiceInstance.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapServiceInstance.java b/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapServiceInstance.java index 790ada6b9..659e68bba 100644 --- a/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapServiceInstance.java +++ b/spring-cloud-aws-cloud-map-service-discovery/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapServiceInstance.java @@ -38,6 +38,11 @@ public AwsCloudMapServiceInstance(String serviceId, Instance instance) { this.instance = instance; } + @Override + public String getInstanceId() { + return instance.getId(); + } + @Override public String getServiceId() { return serviceId; @@ -64,8 +69,7 @@ public boolean isSecure() { @Override public URI getUri() { - String scheme = isSecure() ? "https" : "http"; - return URI.create(String.format("%s://%s/%s", scheme, getHost(), getPort())); + return URI.create(String.format("%s://%s/%s", getScheme(), getHost(), getPort())); } @Override @@ -73,4 +77,9 @@ public Map getMetadata() { return instance.getAttributes(); } + @Override + public String getScheme() { + return isSecure() ? "https" : "http"; + } + }