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

Commit fdffdf1

Browse files
committed
Add Reactive client
1 parent 5cbbff3 commit fdffdf1

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

spring-cloud-aws-cloud-map-service-discovery/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
<groupId>org.springframework</groupId>
3535
<artifactId>spring-core</artifactId>
3636
</dependency>
37+
<dependency>
38+
<groupId>io.projectreactor</groupId>
39+
<artifactId>reactor-core</artifactId>
40+
<optional>true</optional>
41+
</dependency>
3742
<dependency>
3843
<groupId>org.springframework.cloud</groupId>
3944
<artifactId>spring-cloud-commons</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2013-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.aws.cloudmap;
18+
19+
import com.amazonaws.services.servicediscovery.AWSServiceDiscovery;
20+
import com.amazonaws.services.servicediscovery.model.GetInstanceRequest;
21+
import com.amazonaws.services.servicediscovery.model.GetInstanceResult;
22+
import com.amazonaws.services.servicediscovery.model.ListInstancesRequest;
23+
import com.amazonaws.services.servicediscovery.model.ListServicesRequest;
24+
import com.amazonaws.services.servicediscovery.model.ListServicesResult;
25+
import com.amazonaws.services.servicediscovery.model.ServiceSummary;
26+
import reactor.core.publisher.Flux;
27+
import reactor.core.publisher.Mono;
28+
29+
import org.springframework.cloud.client.ServiceInstance;
30+
import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient;
31+
32+
public class AwsCloudMapReactiveDiscoveryClient implements ReactiveDiscoveryClient {
33+
34+
private final AWSServiceDiscovery aws;
35+
36+
public AwsCloudMapReactiveDiscoveryClient(AWSServiceDiscovery aws) {
37+
this.aws = aws;
38+
}
39+
40+
@Override
41+
public String description() {
42+
return "AWS Cloud Map Reactive Discovery Client";
43+
}
44+
45+
@Override
46+
public Flux<ServiceInstance> getInstances(String serviceId) {
47+
ListInstancesRequest request = new ListInstancesRequest()
48+
.withServiceId(serviceId);
49+
50+
return Mono.fromSupplier(() -> aws.listInstances(request))
51+
.flatMapMany(resp -> Flux.fromIterable(resp.getInstances())
52+
.flatMap(summary -> getInstance(serviceId, summary.getId())));
53+
}
54+
55+
private Mono<AwsCloudMapServiceInstance> getInstance(String serviceId,
56+
String instanceId) {
57+
GetInstanceRequest request = new GetInstanceRequest().withServiceId(serviceId)
58+
.withInstanceId(instanceId);
59+
60+
return Mono.fromSupplier(() -> aws.getInstance(request))
61+
.map(GetInstanceResult::getInstance)
62+
.map(instance -> new AwsCloudMapServiceInstance(serviceId, instance));
63+
}
64+
65+
@Override
66+
public Flux<String> getServices() {
67+
ListServicesRequest request = new ListServicesRequest();
68+
69+
return Mono.fromSupplier(() -> aws.listServices(request))
70+
.flatMapIterable(ListServicesResult::getServices)
71+
.map(ServiceSummary::getId);
72+
}
73+
74+
}

0 commit comments

Comments
 (0)