|
| 1 | +/* |
| 2 | + * Copyright 2013-2021 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 | +package io.awspring.cloud.autoconfigure.cloudmap; |
| 17 | + |
| 18 | +import io.awspring.cloud.autoconfigure.cloudmap.discovery.CloudMapDiscoveryClient; |
| 19 | +import io.awspring.cloud.autoconfigure.cloudmap.properties.CloudMapProperties; |
| 20 | +import io.awspring.cloud.autoconfigure.cloudmap.properties.registration.ServiceRegistration; |
| 21 | +import io.awspring.cloud.autoconfigure.cloudmap.registration.CloudMapAutoRegistration; |
| 22 | +import io.awspring.cloud.autoconfigure.core.AwsClientBuilderConfigurer; |
| 23 | +import io.awspring.cloud.autoconfigure.core.AwsClientCustomizer; |
| 24 | +import software.amazon.awssdk.services.servicediscovery.ServiceDiscoveryClient; |
| 25 | +import software.amazon.awssdk.services.servicediscovery.ServiceDiscoveryClientBuilder; |
| 26 | + |
| 27 | +import org.springframework.beans.factory.ObjectProvider; |
| 28 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 29 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 30 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 31 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 32 | +import org.springframework.context.ApplicationContext; |
| 33 | +import org.springframework.context.annotation.Bean; |
| 34 | +import org.springframework.context.annotation.Configuration; |
| 35 | + |
| 36 | +/** |
| 37 | + * Cloudmap BootstrapConfiguration configuration class to create the required beans. |
| 38 | + * |
| 39 | + * @author Hari Ohm Prasath |
| 40 | + * @since 3.0 |
| 41 | + */ |
| 42 | +@Configuration(proxyBeanMethods = false) |
| 43 | +@EnableConfigurationProperties(CloudMapProperties.class) |
| 44 | +@ConditionalOnClass({ ServiceDiscoveryClient.class, ServiceRegistration.class, CloudMapAutoRegistration.class }) |
| 45 | +@ConditionalOnProperty(prefix = CloudMapProperties.CONFIG_PREFIX, name = "enabled", matchIfMissing = true) |
| 46 | +public class CloudMapBootstrapConfiguration { |
| 47 | + |
| 48 | + private final ApplicationContext context; |
| 49 | + private final CloudMapProperties properties; |
| 50 | + |
| 51 | + public CloudMapBootstrapConfiguration(CloudMapProperties properties, ApplicationContext context) { |
| 52 | + this.properties = properties; |
| 53 | + this.context = context; |
| 54 | + } |
| 55 | + |
| 56 | + @ConditionalOnMissingBean |
| 57 | + @Bean |
| 58 | + public ServiceDiscoveryClient discoveryClient(AwsClientBuilderConfigurer awsClientBuilderConfigurer, |
| 59 | + ObjectProvider<AwsClientCustomizer<ServiceDiscoveryClientBuilder>> configurer) { |
| 60 | + return awsClientBuilderConfigurer |
| 61 | + .configure(ServiceDiscoveryClient.builder(), this.properties, configurer.getIfAvailable()).build(); |
| 62 | + } |
| 63 | + |
| 64 | + @Bean |
| 65 | + @ConditionalOnMissingBean |
| 66 | + CloudMapAutoRegistration createAutoRegistration(ServiceDiscoveryClient serviceDiscovery) { |
| 67 | + return new CloudMapAutoRegistration(context, serviceDiscovery, properties.getRegistry()); |
| 68 | + } |
| 69 | + |
| 70 | + @Bean |
| 71 | + @ConditionalOnMissingBean |
| 72 | + CloudMapDiscoveryClient createDiscoveryClient(ServiceDiscoveryClient serviceDiscovery){ |
| 73 | + return new CloudMapDiscoveryClient(serviceDiscovery, properties); |
| 74 | + } |
| 75 | + |
| 76 | + @Bean |
| 77 | + @ConditionalOnMissingBean |
| 78 | + ServiceRegistration serviceRegistration() { |
| 79 | + return new ServiceRegistration(properties.getRegistry()); |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments