-
Notifications
You must be signed in to change notification settings - Fork 371
WIP Cloud Map Service Discovery #551
base: main
Are you sure you want to change the base?
Changes from 1 commit
872b93d
a6a848e
c64702b
5cbbff3
fdffdf1
d84564e
e072a4e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?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>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,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<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()); | ||
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. Currently making a bunch of sequential HTTP requests, any suggested alternatives? 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. potentially, there's also 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. Added a reactive client in fdffdf1. |
||
|
||
} | ||
|
||
private AwsCloudMapServiceInstance getInstance(String service, String id) { | ||
Instance instance = aws.getInstance(new GetInstanceRequest().withInstanceId(id)) | ||
.getInstance(); | ||
return new AwsCloudMapServiceInstance(service, 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,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; | ||
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() { | ||
String scheme = isSecure() ? "https" : "http"; | ||
return URI.create(String.format("%s:%s/%s", scheme, getHost(), getPort())); | ||
} | ||
|
||
@Override | ||
public Map<String, String> getMetadata() { | ||
return instance.getAttributes(); | ||
} | ||
|
||
} |
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.
Should we always be filtering for healthy instances only, or is that generally a configurable option?
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.
Could be an option or also could go in a filter for instance in ribbon or spring cloud loadbalancer
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.
ServiceInstance
doesn't appear to have ahealth
property, would that mean it would just need to look it up as a string viagetMetadata
?