Skip to content

Commit

Permalink
Factory for Service Discovery Client and Builder (#1086)
Browse files Browse the repository at this point in the history
Co-authored-by: Denis Stepanov <[email protected]>
  • Loading branch information
sdelamo and dstepanov authored Jun 1, 2021
1 parent bf2d319 commit 510a796
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 0 deletions.
2 changes: 2 additions & 0 deletions aws-sdk-v2/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ dependencies {
compileOnly "software.amazon.awssdk:sqs"
compileOnly "software.amazon.awssdk:ssm"
compileOnly 'software.amazon.awssdk:secretsmanager'
compileOnly 'software.amazon.awssdk:servicediscovery'

// Tests
testImplementation 'software.amazon.awssdk:servicediscovery'
testImplementation "software.amazon.awssdk:url-connection-client"
testImplementation "software.amazon.awssdk:netty-nio-client"
testImplementation "software.amazon.awssdk:apache-client"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright 2017-2020 original 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 io.micronaut.aws.sdk.v2.service.servicediscovery;

import io.micronaut.aws.sdk.v2.service.AwsClientFactory;
import io.micronaut.context.annotation.Bean;
import io.micronaut.context.annotation.Factory;
import io.micronaut.context.annotation.Requires;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProviderChain;
import software.amazon.awssdk.http.SdkHttpClient;
import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
import software.amazon.awssdk.regions.providers.AwsRegionProviderChain;
import software.amazon.awssdk.services.servicediscovery.ServiceDiscoveryAsyncClient;
import software.amazon.awssdk.services.servicediscovery.ServiceDiscoveryAsyncClientBuilder;
import software.amazon.awssdk.services.servicediscovery.ServiceDiscoveryClient;
import software.amazon.awssdk.services.servicediscovery.ServiceDiscoveryClientBuilder;

import javax.inject.Singleton;

/**
* Factory that creates service discovery clients.
*
* @author Denis Stepanov
*/
@Factory
public class ServiceDiscoveryAsyncClientFactory extends AwsClientFactory<ServiceDiscoveryClientBuilder,
ServiceDiscoveryAsyncClientBuilder, ServiceDiscoveryClient, ServiceDiscoveryAsyncClient> {

/**
* Constructor.
*
* @param credentialsProvider The credentials provider
* @param regionProvider The region provider
*/
public ServiceDiscoveryAsyncClientFactory(AwsCredentialsProviderChain credentialsProvider, AwsRegionProviderChain regionProvider) {
super(credentialsProvider, regionProvider);
}

@Override
protected ServiceDiscoveryClientBuilder createSyncBuilder() {
return ServiceDiscoveryClient.builder();
}

@Override
protected ServiceDiscoveryAsyncClientBuilder createAsyncBuilder() {
return ServiceDiscoveryAsyncClient.builder();
}

@Requires(missingBeans = ServiceDiscoveryClientBuilder.class)
@Override
@Singleton
public ServiceDiscoveryClientBuilder syncBuilder(SdkHttpClient httpClient) {
return super.syncBuilder(httpClient);
}

@Requires(missingBeans = ServiceDiscoveryClient.class)
@Override
@Bean(preDestroy = "close")
@Singleton
public ServiceDiscoveryClient syncClient(ServiceDiscoveryClientBuilder builder) {
return super.syncClient(builder);
}

@Requires(missingBeans = ServiceDiscoveryAsyncClientBuilder.class)
@Override
@Singleton
@Requires(beans = SdkAsyncHttpClient.class)
public ServiceDiscoveryAsyncClientBuilder asyncBuilder(SdkAsyncHttpClient httpClient) {
return super.asyncBuilder(httpClient);
}

@Requires(missingBeans = ServiceDiscoveryAsyncClient.class)
@Override
@Bean(preDestroy = "close")
@Singleton
@Requires(beans = SdkAsyncHttpClient.class)
public ServiceDiscoveryAsyncClient asyncClient(ServiceDiscoveryAsyncClientBuilder builder) {
return super.asyncClient(builder);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2017-2020 original 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.
*/
/**
* Service discovery client configuration and factory.
*
* @author Denis Stepanov
*/
@Requires(classes = {ServiceDiscoveryClient.class, ServiceDiscoveryAsyncClient.class})
@Configuration
package io.micronaut.aws.sdk.v2.service.servicediscovery;

import io.micronaut.context.annotation.Configuration;
import io.micronaut.context.annotation.Requires;
import software.amazon.awssdk.services.servicediscovery.ServiceDiscoveryAsyncClient;
import software.amazon.awssdk.services.servicediscovery.ServiceDiscoveryClient;
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.micronaut.aws.sdk.v2.service;

import io.micronaut.aws.sdk.v2.ApplicationContextSpecification;
import software.amazon.awssdk.services.servicediscovery.ServiceDiscoveryAsyncClient
import software.amazon.awssdk.services.servicediscovery.ServiceDiscoveryAsyncClientBuilder;
import software.amazon.awssdk.services.servicediscovery.ServiceDiscoveryClient
import software.amazon.awssdk.services.servicediscovery.ServiceDiscoveryClientBuilder;

class ServiceDiscoveryClientSpec extends ApplicationContextSpecification {

void "it can configure a service discovery client"() {
when:
ServiceDiscoveryClient client = applicationContext.getBean(ServiceDiscoveryClient)

then:
client.serviceName() == ServiceDiscoveryClient.SERVICE_NAME
}

void "it can configure an async service discovery client"() {
when:
ServiceDiscoveryAsyncClient client = applicationContext.getBean(ServiceDiscoveryAsyncClient)

then:
client.serviceName() == ServiceDiscoveryClient.SERVICE_NAME
}

void "it can configure an async service discovery client Builder"() {
expect:
applicationContext.getBean(ServiceDiscoveryAsyncClientBuilder)
}

void "it can configure a service discovery client Builder"() {
expect:
applicationContext.getBean(ServiceDiscoveryClientBuilder)
}
}
15 changes: 15 additions & 0 deletions src/main/docs/guide/sdkv2/servicediscovery.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
To use Service discovery, the AWS Java SDK for Amazon Route 53 Auto Naming module, add the following dependency:

dependency:servicediscovery[groupId="software.amazon.awssdk"]

Then, the following beans will be created:

* `software.amazon.awssdk.services.servicediscovery.ServiceDiscoveryClientBuilder`
* `software.amazon.awssdk.services.servicediscovery.ServiceDiscoveryClient`
And:

* `software.amazon.awssdk.services.servicediscovery.ServiceDiscoveryAsyncClient`
* `software.amazon.awssdk.services.servicediscovery.ServiceDiscoveryAsyncClientBuilder`.
The HTTP client, credentials and region will be configured as per described in the <<sdkv2, SDK v2 documentation>>.
1 change: 1 addition & 0 deletions src/main/docs/guide/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ sdkv2:
sqs: SQS
ssm: SSM
secretsmanager: Secrets Manager
servicediscovery: Service discovery
advancedConfiguration: Advanced configuration
otherServices: Other services
lambda:
Expand Down

0 comments on commit 510a796

Please sign in to comment.