This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 372
WIP Cloud Map Service Discovery #551
Draft
fitzoh
wants to merge
7
commits into
spring-attic:main
Choose a base branch
from
fitzoh:master
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
872b93d
WIP Cloud Map Service Discovery
fitzoh a6a848e
include service id with getInstance request
fitzoh c64702b
formatting
fitzoh 5cbbff3
fix scheme
fitzoh fdffdf1
Add Reactive client
fitzoh d84564e
Add properties + configuration boilerplate and add reactive client to…
fitzoh e072a4e
Configure instance id + scheme for service instance
fitzoh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
70 changes: 70 additions & 0 deletions
70
...very/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapDiscoveryClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
|
||
} | ||
|
||
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()); | ||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
.../java/org/springframework/cloud/aws/cloudmap/AwsCloudMapDiscoveryClientConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
.../src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapDiscoveryProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
85 changes: 85 additions & 0 deletions
85
...very/src/main/java/org/springframework/cloud/aws/cloudmap/AwsCloudMapServiceInstance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this always the case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's more of a best guess at the moment:
|
||
} | ||
|
||
@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"; | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
...java/org/springframework/cloud/aws/cloudmap/ConditionalOnAwsCloudMapDiscoveryEnabled.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.https://docs.aws.amazon.com/cloud-map/latest/api/API_DiscoverInstances.html