Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

WIP Cloud Map Service Discovery #551

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<module>spring-cloud-aws-actuator</module>
<module>spring-cloud-aws-parameter-store-config</module>
<module>spring-cloud-aws-secrets-manager-config</module>
<module>spring-cloud-aws-cloud-map-service-discovery</module>
<module>spring-cloud-starter-aws</module>
<module>spring-cloud-starter-aws-jdbc</module>
<module>spring-cloud-starter-aws-messaging</module>
Expand Down
62 changes: 62 additions & 0 deletions spring-cloud-aws-cloud-map-service-discovery/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-aws</artifactId>
<version>3.0.0-SNAPSHOT</version>
</parent>

<artifactId>spring-cloud-aws-cloud-map-service-discovery</artifactId>
<name>Spring Cloud AWS Cloud Map Service Discovery</name>
<description>Spring Cloud AWS Cloud Map Service Discovery</description>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
<version>${spring-cloud-commons.version}</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-servicediscovery</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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<ServiceInstance> 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());
Comment on lines +47 to +52

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend using the DiscoverInstances API that is built for this use-case.

  • No need for pagination
  • Filter on the health status is included out of the box

https://docs.aws.amazon.com/cloud-map/latest/api/API_DiscoverInstances.html


}

private AwsCloudMapServiceInstance getInstance(String serviceId, String instanceId) {
Instance instance = aws
.getInstance(new GetInstanceRequest().withServiceId(serviceId).withInstanceId(instanceId))
.getInstance();
return new AwsCloudMapServiceInstance(serviceId, instance);
}

@Override
public List<String> getServices() {
// TODO pagination
return aws.listServices(new ListServicesRequest()).getServices().stream().map(ServiceSummary::getId)
.collect(Collectors.toList());
}

}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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 getInstanceId() {
return instance.getId();
}

@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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this always the case?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's more of a best guess at the moment:

$ aws servicediscovery list-instances --service-id=srv-blah
{
    "Instances": [
        {
            "Id": "fitz-test-instance",
            "Attributes": {
                "AWS_INSTANCE_IPV4": "blah",
                "AWS_INSTANCE_IPV6": "blah blah blah",
                "AWS_INSTANCE_PORT": "8080"
            }
        }
    ]
}

}

@Override
public URI getUri() {
return URI.create(String.format("%s://%s/%s", getScheme(), getHost(), getPort()));
}

@Override
public Map<String, String> getMetadata() {
return instance.getAttributes();
}

@Override
public String getScheme() {
return isSecure() ? "https" : "http";
}

}
Original file line number Diff line number Diff line change
@@ -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 {

}
Loading