diff --git a/camel-core/src/main/docs/eips/filter-eip.adoc b/camel-core/src/main/docs/eips/filter-eip.adoc index ba5d7b80b736f..fafc14c0aa449 100644 --- a/camel-core/src/main/docs/eips/filter-eip.adoc +++ b/camel-core/src/main/docs/eips/filter-eip.adoc @@ -1,6 +1,5 @@ -[[MessageFilter-MessageFilter]] -Message Filter -^^^^^^^^^^^^^^ +## Filter EIP +### Message Filter The http://www.enterpriseintegrationpatterns.com/Filter.html[Message Filter] from the link:enterprise-integration-patterns.html[EIP patterns] @@ -84,9 +83,7 @@ For further examples of this pattern in use you could look at the http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/FilterTest.java?view=markup[junit test case] -[[MessageFilter-Usingstop]] -*Using stop* -++++++++++++ +#### *Using stop* Stop is a bit different than a message filter as it will filter out all messages and end the route entirely (filter only applies to its child @@ -98,9 +95,7 @@ In the example below we do not want to route messages any further that has the word `Bye` in the message body. Notice how we prevent this in the when predicate by using the `.stop()`. -[[MessageFilter-Knowingifwasfilteredornot]] -Knowing if link:exchange.html[Exchange] was filtered or not -+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +#### Knowing if link:exchange.html[Exchange] was filtered or not *Available as of Camel 2.5* @@ -118,13 +113,11 @@ with the value set based on the results of the last link:message-filter.html[Message Filter] link:predicate.html[Predicate] evaluated. -[[MessageFilter-UsingThisPattern]] -Using This Pattern -++++++++++++++++++ +#### Using This Pattern If you would like to use this EIP Pattern then please read the link:getting-started.html[Getting Started], you may also find the link:architecture.html[Architecture] useful particularly the description of link:endpoint.html[Endpoint] and link:uris.html[URIs]. Then you could try out some of the link:examples.html[Examples] first before trying -this pattern out. +this pattern out. \ No newline at end of file diff --git a/camel-core/src/main/java/org/apache/camel/impl/cloud/StaticServiceDiscovery.java b/camel-core/src/main/java/org/apache/camel/impl/cloud/StaticServiceDiscovery.java index 1507b243d29a1..d017a112c0bab 100644 --- a/camel-core/src/main/java/org/apache/camel/impl/cloud/StaticServiceDiscovery.java +++ b/camel-core/src/main/java/org/apache/camel/impl/cloud/StaticServiceDiscovery.java @@ -26,6 +26,7 @@ import java.util.stream.Collectors; import org.apache.camel.cloud.ServiceDefinition; +import org.apache.camel.util.ObjectHelper; import org.apache.camel.util.StringHelper; /** @@ -52,6 +53,17 @@ public void setServers(List servers) { servers.forEach(this::addServer); } + public void addServers(String serviceName, List servers) { + for (String server : servers) { + String host = StringHelper.before(server, ":"); + String port = StringHelper.after(server, ":"); + + if (ObjectHelper.isNotEmpty(host) && ObjectHelper.isNotEmpty(port)) { + addServer(serviceName, host, Integer.valueOf(port)); + } + } + } + /** * Set the servers. * @@ -83,7 +95,9 @@ public void addServer(String serverString) { String host = StringHelper.before(part, ":"); String port = StringHelper.after(part, ":"); - addServer(service, host, Integer.valueOf(port)); + if (ObjectHelper.isNotEmpty(host) && ObjectHelper.isNotEmpty(port)) { + addServer(service, host, Integer.valueOf(port)); + } } } diff --git a/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallDefinition.java b/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallDefinition.java index 5d8fe91ef4d21..b6f7ab043113a 100644 --- a/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallDefinition.java +++ b/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallDefinition.java @@ -753,6 +753,10 @@ private ServiceDiscovery retrieveServiceDiscovery(CamelContext camelContext, Ser if (answer == null && config != null) { answer = retrieve(ServiceDiscovery.class, camelContext, config::getServiceDiscovery, config::getServiceDiscoveryRef); } + + if (answer == null) { + answer = camelContext.getRegistry().lookupByNameAndType("service-discovery", ServiceDiscovery.class); + } if (answer == null) { answer = findByType(camelContext, ServiceDiscovery.class); } @@ -803,6 +807,9 @@ private ServiceFilter retrieveServiceFilter(CamelContext camelContext, ServiceCa } } + if (answer == null) { + answer = camelContext.getRegistry().lookupByNameAndType("service-filter", ServiceFilter.class); + } if (answer == null) { answer = findByType(camelContext, ServiceFilter.class); } @@ -833,6 +840,10 @@ private ServiceChooser retrieveServiceChooser(CamelContext camelContext, Service } } } + + if (answer == null) { + answer = camelContext.getRegistry().lookupByNameAndType("service-chooser", ServiceChooser.class); + } if (answer == null) { answer = findByType(camelContext, ServiceChooser.class); } @@ -856,6 +867,10 @@ private LoadBalancer retrieveLoadBalancer(CamelContext camelContext, ServiceCall if (answer == null && config != null) { answer = retrieve(LoadBalancer.class, camelContext, config::getLoadBalancer, config::getLoadBalancerRef); } + + if (answer == null) { + answer = camelContext.getRegistry().lookupByNameAndType("load-balancer", LoadBalancer.class); + } if (answer == null) { answer = findByType(camelContext, LoadBalancer.class); } diff --git a/components/camel-consul/src/main/java/org/apache/camel/component/consul/cloud/ConsulServiceDiscoveryFactory.java b/components/camel-consul/src/main/java/org/apache/camel/component/consul/cloud/ConsulServiceDiscoveryFactory.java index 9207d3ed01992..04215efa84ec3 100644 --- a/components/camel-consul/src/main/java/org/apache/camel/component/consul/cloud/ConsulServiceDiscoveryFactory.java +++ b/components/camel-consul/src/main/java/org/apache/camel/component/consul/cloud/ConsulServiceDiscoveryFactory.java @@ -26,7 +26,11 @@ public class ConsulServiceDiscoveryFactory implements ServiceDiscoveryFactory { private final ConsulConfiguration configuration; public ConsulServiceDiscoveryFactory() { - this.configuration = new ConsulConfiguration(); + this(new ConsulConfiguration()); + } + + public ConsulServiceDiscoveryFactory(ConsulConfiguration configuration) { + this.configuration = configuration; } // ************************************************************************* diff --git a/components/camel-dns/src/main/java/org/apache/camel/component/dns/cloud/DnsServiceDiscoveryFactory.java b/components/camel-dns/src/main/java/org/apache/camel/component/dns/cloud/DnsServiceDiscoveryFactory.java index 18568d72d2dcb..5c70532c7fe15 100644 --- a/components/camel-dns/src/main/java/org/apache/camel/component/dns/cloud/DnsServiceDiscoveryFactory.java +++ b/components/camel-dns/src/main/java/org/apache/camel/component/dns/cloud/DnsServiceDiscoveryFactory.java @@ -28,6 +28,10 @@ public DnsServiceDiscoveryFactory() { this.configuration = new DnsConfiguration(); } + public DnsServiceDiscoveryFactory(DnsConfiguration configuration) { + this.configuration = configuration; + } + // ************************************************************************* // Properties // ************************************************************************* diff --git a/components/camel-etcd/src/main/java/org/apache/camel/component/etcd/cloud/EtcdServiceDiscoveryFactory.java b/components/camel-etcd/src/main/java/org/apache/camel/component/etcd/cloud/EtcdServiceDiscoveryFactory.java index 5bf89e01cf617..357684335d2f2 100644 --- a/components/camel-etcd/src/main/java/org/apache/camel/component/etcd/cloud/EtcdServiceDiscoveryFactory.java +++ b/components/camel-etcd/src/main/java/org/apache/camel/component/etcd/cloud/EtcdServiceDiscoveryFactory.java @@ -28,7 +28,11 @@ public class EtcdServiceDiscoveryFactory implements ServiceDiscoveryFactory { private String type; public EtcdServiceDiscoveryFactory() { - this.configuration = new EtcdConfiguration(); + this(new EtcdConfiguration()); + } + + public EtcdServiceDiscoveryFactory(EtcdConfiguration configuration) { + this.configuration = configuration; } // ************************************************************************* diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/cloud/KubernetesServiceDiscoveryFactory.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/cloud/KubernetesServiceDiscoveryFactory.java index 37bdff1332f9c..d93caa3152157 100644 --- a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/cloud/KubernetesServiceDiscoveryFactory.java +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/cloud/KubernetesServiceDiscoveryFactory.java @@ -27,7 +27,11 @@ public class KubernetesServiceDiscoveryFactory implements ServiceDiscoveryFactor private String lookup; public KubernetesServiceDiscoveryFactory() { - this.configuration = new KubernetesConfiguration(); + this(new KubernetesConfiguration()); + } + + public KubernetesServiceDiscoveryFactory(KubernetesConfiguration configuration) { + this.configuration = configuration; } // ************************************************************************* diff --git a/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/CamelCloudConfigurationProperties.java b/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/CamelCloudConfigurationProperties.java index 00f91d8b33246..97c1234be5567 100644 --- a/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/CamelCloudConfigurationProperties.java +++ b/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/CamelCloudConfigurationProperties.java @@ -16,11 +16,19 @@ */ package org.apache.camel.spring.cloud; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "camel.cloud") public class CamelCloudConfigurationProperties { private boolean enabled = true; + private LoadBalancer loadBalancer = new LoadBalancer(); + private ServiceDiscovery serviceDiscovery = new ServiceDiscovery(); + private ServiceFilter serviceFilter = new ServiceFilter(); + private ServiceChooser serviceChooser = new ServiceChooser(); public boolean isEnabled() { return enabled; @@ -29,4 +37,91 @@ public boolean isEnabled() { public void setEnabled(boolean enabled) { this.enabled = enabled; } + + public LoadBalancer getLoadBalancer() { + return loadBalancer; + } + + public ServiceDiscovery getServiceDiscovery() { + return serviceDiscovery; + } + + public ServiceFilter getServiceFilter() { + return serviceFilter; + } + + public ServiceChooser getServiceChooser() { + return serviceChooser; + } + + // ***************************************** + // Nested configurations + // ***************************************** + + public static class LoadBalancer { + private boolean enabled = true; + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + } + + public static class ServiceDiscovery { + private boolean enabled = true; + private Map> services = new HashMap<>(); + private String cacheTimeout; + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + public Map> getServices() { + return services; + } + + public String getCacheTimeout() { + return cacheTimeout; + } + + public void setCacheTimeout(String cacheTimeout) { + this.cacheTimeout = cacheTimeout; + } + } + + public static class ServiceFilter { + private boolean enabled = true; + private Map> blacklist = new HashMap<>(); + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + public Map> getBlacklist() { + return blacklist; + } + } + + public static class ServiceChooser { + private boolean enabled = true; + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + } } diff --git a/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/CamelCloudDiscoveryClient.java b/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/CamelCloudDiscoveryClient.java index 194f518601fc5..5ffd6c16dcbe8 100644 --- a/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/CamelCloudDiscoveryClient.java +++ b/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/CamelCloudDiscoveryClient.java @@ -17,30 +17,28 @@ package org.apache.camel.spring.cloud; -import java.util.ArrayList; import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.LinkedList; import java.util.List; -import java.util.Map; +import java.util.stream.Collectors; +import org.apache.camel.cloud.ServiceDiscovery; import org.springframework.cloud.client.DefaultServiceInstance; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; public class CamelCloudDiscoveryClient implements DiscoveryClient { private final String description; - private final Map> services; + private final ServiceDiscovery serviceDiscovery; private ServiceInstance localInstance; - public CamelCloudDiscoveryClient(String description) { - this(description, Collections.emptyMap()); + public CamelCloudDiscoveryClient(String description, ServiceDiscovery serviceDiscovery) { + this(description, null, serviceDiscovery); } - public CamelCloudDiscoveryClient(String description, Map> services) { + public CamelCloudDiscoveryClient(String description, ServiceInstance localServiceDiscovery,ServiceDiscovery serviceDiscovery) { this.description = description; - this.services = new LinkedHashMap<>(services); - this.localInstance = null; + this.serviceDiscovery = serviceDiscovery; + this.localInstance = localServiceDiscovery; } @Override @@ -53,31 +51,20 @@ public ServiceInstance getLocalServiceInstance() { return this.localInstance; } + public CamelCloudDiscoveryClient setLocalServiceInstance(ServiceInstance instance) { + this.localInstance = instance; + return this; + } + @Override public List getInstances(String serviceId) { - return services.get(serviceId); + return serviceDiscovery.getUpdatedListOfServices(serviceId).stream() + .map(s -> new DefaultServiceInstance(s.getName(), s.getHost(), s.getPort(), false, s.getMetadata())) + .collect(Collectors.toList()); } @Override public List getServices() { - return new ArrayList<>(services.keySet()); - } - - public CamelCloudDiscoveryClient addServiceInstance(ServiceInstance instance) { - services.computeIfAbsent(instance.getServiceId(), key -> new LinkedList<>()).add(instance); - return this; - } - - public CamelCloudDiscoveryClient addServiceInstance(String serviceId, String host, int port) { - return addServiceInstance(new DefaultServiceInstance(serviceId, host, port, false)); - } - - public CamelCloudDiscoveryClient setLocalServiceInstance(ServiceInstance instance) { - this.localInstance = instance; - return this; - } - - public CamelCloudDiscoveryClient setLocalServiceInstance(String serviceId, String host, int port) { - return setLocalServiceInstance(new DefaultServiceInstance(serviceId, host, port, false)); + return Collections.emptyList(); } } diff --git a/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/CamelCloudLoadBalancer.java b/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/CamelCloudLoadBalancer.java index 08a73b8a6bc75..c37a1eea1cc53 100644 --- a/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/CamelCloudLoadBalancer.java +++ b/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/CamelCloudLoadBalancer.java @@ -21,11 +21,13 @@ import org.apache.camel.CamelContextAware; import org.apache.camel.cloud.LoadBalancer; import org.apache.camel.cloud.LoadBalancerFunction; +import org.apache.camel.cloud.ServiceDefinition; import org.apache.camel.impl.cloud.DefaultServiceDefinition; import org.apache.camel.support.ServiceSupport; import org.apache.camel.util.ObjectHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; public class CamelCloudLoadBalancer extends ServiceSupport implements CamelContextAware, LoadBalancer { @@ -53,8 +55,7 @@ protected void doStart() throws Exception { ObjectHelper.notNull(camelContext, "camelContext"); ObjectHelper.notNull(loadBalancerClient, "loadBalancerClient"); - LOGGER.info("ServiceCall is using cloud load balancer of type: {}", - loadBalancerClient.getClass()); + LOGGER.info("ServiceCall is using cloud load balancer of type: {}", loadBalancerClient.getClass()); } @Override @@ -63,9 +64,22 @@ protected void doStop() throws Exception { @Override public T process(String serviceName, LoadBalancerFunction function) throws Exception { + + return loadBalancerClient.execute( - serviceName, - i -> function.apply(new DefaultServiceDefinition(i.getServiceId(), i.getHost(), i.getPort(), i.getMetadata())) + serviceName, i -> function.apply(instanceToDefinition(i))); + } + + // ******************************* + // Helpers + // ******************************* + + private ServiceDefinition instanceToDefinition(ServiceInstance instance) { + return new DefaultServiceDefinition( + instance.getServiceId(), + instance.getHost(), + instance.getPort(), + instance.getMetadata() ); } } \ No newline at end of file diff --git a/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/ServiceCallLoadBalancerAutoConfiguration.java b/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/CamelCloudLoadBalancerAutoConfiguration.java similarity index 70% rename from components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/ServiceCallLoadBalancerAutoConfiguration.java rename to components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/CamelCloudLoadBalancerAutoConfiguration.java index 32b66d2782b95..fb3e72829a5d8 100644 --- a/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/ServiceCallLoadBalancerAutoConfiguration.java +++ b/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/CamelCloudLoadBalancerAutoConfiguration.java @@ -14,14 +14,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.apache.camel.spring.cloud; + import org.apache.camel.cloud.LoadBalancer; import org.apache.camel.spring.boot.util.GroupCondition; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalancerAutoConfiguration; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.context.annotation.Bean; @@ -32,22 +33,29 @@ @Configuration @ConditionalOnBean({ CamelCloudAutoConfiguration.class, LoadBalancerClient.class }) -@AutoConfigureAfter(LoadBalancerAutoConfiguration.class) -@EnableConfigurationProperties(ServiceCallConfigurationProperties.class) -public class ServiceCallLoadBalancerAutoConfiguration { - @Lazy - @Scope("prototype") - @Bean(name = "cloud-load-balancer") - @Conditional(ServiceCallLoadBalancerAutoConfiguration.LoadBalancerCondition.class) +@AutoConfigureAfter({ LoadBalancerAutoConfiguration.class, CamelCloudServiceDiscoveryAutoConfiguration.class }) +@EnableConfigurationProperties(CamelCloudConfigurationProperties.class) +@Conditional(CamelCloudLoadBalancerAutoConfiguration.LoadBalancerCondition.class) +public class CamelCloudLoadBalancerAutoConfiguration { + @Bean(name = "load-balancer") public LoadBalancer cloudLoadBalancer(LoadBalancerClient loadBalancerClient) { return new CamelCloudLoadBalancer(loadBalancerClient); } + @Bean(name = "load-balancer-discovery-client") + public DiscoveryClient serviceDiscoveryClient(CamelCloudServiceDiscovery serviceDiscovery) { + return new CamelCloudDiscoveryClient("service-discovery-client", serviceDiscovery); + } + + // ******************************* + // Condition + // ******************************* + public static class LoadBalancerCondition extends GroupCondition { public LoadBalancerCondition() { super( - "camel.cloud.servicecall", - "camel.cloud.servicecall.load-balancer" + "camel.cloud", + "camel.cloud.load-balancer" ); } } diff --git a/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/ServiceCallServiceChooserAutoConfiguration.java b/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/CamelCloudServiceChooserAutoConfiguration.java similarity index 54% rename from components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/ServiceCallServiceChooserAutoConfiguration.java rename to components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/CamelCloudServiceChooserAutoConfiguration.java index dae38a07b6c44..81838ef25d054 100644 --- a/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/ServiceCallServiceChooserAutoConfiguration.java +++ b/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/CamelCloudServiceChooserAutoConfiguration.java @@ -17,47 +17,27 @@ package org.apache.camel.spring.cloud; -import org.apache.camel.cloud.ServiceChooser; -import org.apache.camel.impl.cloud.RandomServiceChooser; -import org.apache.camel.impl.cloud.RoundRobinServiceChooser; import org.apache.camel.spring.boot.util.GroupCondition; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Lazy; -import org.springframework.context.annotation.Scope; -import org.springframework.core.annotation.Order; @Configuration @ConditionalOnBean(CamelCloudAutoConfiguration.class) -@EnableConfigurationProperties(ServiceCallConfigurationProperties.class) -public class ServiceCallServiceChooserAutoConfiguration { +@EnableConfigurationProperties(CamelCloudConfigurationProperties.class) +@Conditional(CamelCloudServiceChooserAutoConfiguration.ServiceChooserCondition.class) +public class CamelCloudServiceChooserAutoConfiguration { - @Lazy - @Scope("prototype") - @Order(1) - @Bean(name = "round-robin-service-chooser") - @Conditional(ServiceCallServiceChooserAutoConfiguration.ServiceChooserCondition.class) - public ServiceChooser roundRobinLoadBalancer() { - return new RoundRobinServiceChooser(); - } - - @Lazy - @Scope("prototype") - @Order(2) - @Bean(name = "random-service-chooser") - @Conditional(ServiceCallServiceChooserAutoConfiguration.ServiceChooserCondition.class) - public ServiceChooser randomLoadBalancer() { - return new RandomServiceChooser(); - } + // ******************************* + // Condition + // ******************************* public static class ServiceChooserCondition extends GroupCondition { public ServiceChooserCondition() { super( - "camel.cloud.servicecall", - "camel.cloud.servicecall.service-chooser" + "camel.cloud", + "camel.cloud.service-chooser" ); } } diff --git a/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/CamelCloudServiceDiscovery.java b/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/CamelCloudServiceDiscovery.java index 389649490c935..8029894973cfe 100644 --- a/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/CamelCloudServiceDiscovery.java +++ b/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/CamelCloudServiceDiscovery.java @@ -17,36 +17,37 @@ package org.apache.camel.spring.cloud; -import java.util.ArrayList; import java.util.List; -import java.util.stream.Collectors; +import java.util.concurrent.TimeUnit; import org.apache.camel.cloud.ServiceDefinition; -import org.apache.camel.impl.cloud.DefaultServiceDefinition; -import org.apache.camel.impl.cloud.DefaultServiceDiscovery; -import org.springframework.cloud.client.discovery.DiscoveryClient; - -public class CamelCloudServiceDiscovery extends DefaultServiceDiscovery { - private final List clients; - - public CamelCloudServiceDiscovery(List clients) { - this.clients = new ArrayList<>(clients); +import org.apache.camel.cloud.ServiceDiscovery; +import org.apache.camel.impl.cloud.CachingServiceDiscovery; +import org.apache.camel.impl.cloud.ChainedServiceDiscovery; + +public class CamelCloudServiceDiscovery implements ServiceDiscovery { + private ServiceDiscovery delegate; + + public CamelCloudServiceDiscovery(Long timeout, List serviceDiscoveryList) { + // Created a chained service discovery that collects services from multiple + // ServiceDiscovery + this.delegate = new ChainedServiceDiscovery(serviceDiscoveryList); + + // If a timeout is provided, wrap the serviceDiscovery with a caching + // strategy so the discovery implementations are not queried for each + // discovery request + if (timeout != null && timeout > 0) { + this.delegate = CachingServiceDiscovery.wrap(this.delegate, timeout, TimeUnit.MILLISECONDS); + } } @Override public List getInitialListOfServices(String name) { - return getServers(name); + return delegate.getInitialListOfServices(name); } @Override public List getUpdatedListOfServices(String name) { - return getServers(name); - } - - private List getServers(String name) { - return clients.stream() - .flatMap(c -> c.getInstances(name).stream()) - .map(s -> new DefaultServiceDefinition(s.getServiceId(), s.getHost(), s.getPort(), s.getMetadata())) - .collect(Collectors.toList()); + return delegate.getUpdatedListOfServices(name); } } diff --git a/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/ServiceCallServiceDiscoveryAutoConfiguration.java b/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/CamelCloudServiceDiscoveryAutoConfiguration.java similarity index 55% rename from components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/ServiceCallServiceDiscoveryAutoConfiguration.java rename to components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/CamelCloudServiceDiscoveryAutoConfiguration.java index 055aa28f8d343..3581b5bbaefdf 100644 --- a/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/ServiceCallServiceDiscoveryAutoConfiguration.java +++ b/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/CamelCloudServiceDiscoveryAutoConfiguration.java @@ -20,56 +20,59 @@ import java.util.List; import java.util.Map; +import org.apache.camel.CamelContext; +import org.apache.camel.NoTypeConversionAvailableException; import org.apache.camel.cloud.ServiceDiscovery; +import org.apache.camel.impl.cloud.StaticServiceDiscovery; import org.apache.camel.spring.boot.util.GroupCondition; -import org.apache.camel.util.ObjectHelper; -import org.apache.camel.util.StringHelper; import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Lazy; -import org.springframework.context.annotation.Scope; @Configuration -@EnableConfigurationProperties(ServiceCallConfigurationProperties.class) -public class ServiceCallServiceDiscoveryAutoConfiguration { +@EnableConfigurationProperties(CamelCloudConfigurationProperties.class) +@Conditional(CamelCloudServiceDiscoveryAutoConfiguration.ServiceDiscoveryCondition.class) +public class CamelCloudServiceDiscoveryAutoConfiguration { + @Lazy - @Scope("prototype") - @Bean(name = "service-discovery-client") - @Conditional(ServiceCallServiceDiscoveryAutoConfiguration.ServiceDiscoveryCondition.class) - public DiscoveryClient serviceDiscoveryClient(ServiceCallConfigurationProperties properties) { - CamelCloudDiscoveryClient client = new CamelCloudDiscoveryClient("service-discovery-client"); + @Bean(name = "static-service-discovery") + public ServiceDiscovery staticServiceDiscovery(CamelCloudConfigurationProperties properties) { + StaticServiceDiscovery staticServiceDiscovery = new StaticServiceDiscovery(); Map> services = properties.getServiceDiscovery().getServices(); for (Map.Entry> entry : services.entrySet()) { - for (String part : entry.getValue()) { - String host = StringHelper.before(part, ":"); - String port = StringHelper.after(part, ":"); - - if (ObjectHelper.isNotEmpty(host) && ObjectHelper.isNotEmpty(port)) { - client.addServiceInstance(entry.getKey(), host, Integer.parseInt(port)); - } - } + staticServiceDiscovery.addServers(entry.getKey(), entry.getValue()); } - return client; + return staticServiceDiscovery; } @Lazy - @Scope("prototype") @Bean(name = "service-discovery") - @Conditional(ServiceCallServiceDiscoveryAutoConfiguration.ServiceDiscoveryCondition.class) - public ServiceDiscovery serviceDiscovery(List clients) { - return new CamelCloudServiceDiscovery(clients); + public CamelCloudServiceDiscovery serviceDiscovery( + CamelContext camelContext, CamelCloudConfigurationProperties properties, List serviceDiscoveryList) throws NoTypeConversionAvailableException{ + + String cacheTimeout = properties.getServiceDiscovery().getCacheTimeout(); + Long timeout = null; + + if (cacheTimeout != null) { + timeout = camelContext.getTypeConverter().mandatoryConvertTo(Long.class, timeout); + } + + return new CamelCloudServiceDiscovery(timeout, serviceDiscoveryList); } + // ******************************* + // Condition + // ******************************* + public static class ServiceDiscoveryCondition extends GroupCondition { public ServiceDiscoveryCondition() { super( - "camel.cloud.servicecall", - "camel.cloud.servicecall.service-discovery" + "camel.cloud", + "camel.cloud.service-discovery" ); } } diff --git a/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/ServiceCallServiceFilterAutoConfiguration.java b/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/CamelCloudServiceFilterAutoConfiguration.java similarity index 82% rename from components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/ServiceCallServiceFilterAutoConfiguration.java rename to components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/CamelCloudServiceFilterAutoConfiguration.java index 5f9267823b7b7..bb687bd2b1257 100644 --- a/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/ServiceCallServiceFilterAutoConfiguration.java +++ b/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/CamelCloudServiceFilterAutoConfiguration.java @@ -32,16 +32,14 @@ import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Lazy; -import org.springframework.context.annotation.Scope; @Configuration -@EnableConfigurationProperties(ServiceCallConfigurationProperties.class) -public class ServiceCallServiceFilterAutoConfiguration { +@EnableConfigurationProperties(CamelCloudConfigurationProperties.class) +@Conditional(CamelCloudServiceFilterAutoConfiguration.ServiceFilterCondition.class) +public class CamelCloudServiceFilterAutoConfiguration { @Lazy - @Scope("prototype") - @Bean(name = "service-filter-chained") - @Conditional(ServiceCallServiceFilterAutoConfiguration.ServiceFilterCondition.class) - public ServiceFilter chainedServiceFilter(ServiceCallConfigurationProperties properties) { + @Bean(name = "service-filter") + public ServiceFilter serviceFilter(CamelCloudConfigurationProperties properties) { BlacklistServiceFilter blacklist = new BlacklistServiceFilter(); Map> services = properties.getServiceFilter().getBlacklist(); @@ -59,11 +57,15 @@ public ServiceFilter chainedServiceFilter(ServiceCallConfigurationProperties pro return ChainedServiceFilter.wrap(new HealthyServiceFilter(), blacklist); } + // ******************************* + // Condition + // ******************************* + public static class ServiceFilterCondition extends GroupCondition { public ServiceFilterCondition() { super( - "camel.cloud.servicecall", - "camel.cloud.servicecall.service-filter" + "camel.cloud", + "camel.cloud.service-filter" ); } } diff --git a/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/ServiceCallConfigurationProperties.java b/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/ServiceCallConfigurationProperties.java deleted file mode 100644 index d316dcbccc5ad..0000000000000 --- a/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/ServiceCallConfigurationProperties.java +++ /dev/null @@ -1,119 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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 - * - * http://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.apache.camel.spring.cloud; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.springframework.boot.context.properties.ConfigurationProperties; - -@ConfigurationProperties(prefix = "camel.cloud.servicecall") -public class ServiceCallConfigurationProperties { - private boolean enabled = true; - private LoadBalancer loadBalancer = new LoadBalancer(); - private ServiceDiscovery serviceDiscovery = new ServiceDiscovery(); - private ServiceFilter serviceFilter = new ServiceFilter(); - private ServiceChooser serviceChooser = new ServiceChooser(); - - public boolean isEnabled() { - return enabled; - } - - public void setEnabled(boolean enabled) { - this.enabled = enabled; - } - - public LoadBalancer getLoadBalancer() { - return loadBalancer; - } - - public ServiceDiscovery getServiceDiscovery() { - return serviceDiscovery; - } - - public ServiceFilter getServiceFilter() { - return serviceFilter; - } - - public ServiceChooser getServiceChooser() { - return serviceChooser; - } - - // ***************************************** - // Nested configurations - // ***************************************** - - public static class LoadBalancer { - private boolean enabled = true; - - public boolean isEnabled() { - return enabled; - } - - public void setEnabled(boolean enabled) { - this.enabled = enabled; - } - } - - public static class ServiceDiscovery { - private boolean enabled = true; - private Map> services = new HashMap<>(); - - public boolean isEnabled() { - return enabled; - } - - public void setEnabled(boolean enabled) { - this.enabled = enabled; - } - - public Map> getServices() { - return services; - } - } - - public static class ServiceFilter { - private boolean enabled = true; - private Map> blacklist = new HashMap<>(); - - public boolean isEnabled() { - return enabled; - } - - public void setEnabled(boolean enabled) { - this.enabled = enabled; - } - - public Map> getBlacklist() { - return blacklist; - } - } - - public static class ServiceChooser { - private boolean enabled = true; - - public boolean isEnabled() { - return enabled; - } - - public void setEnabled(boolean enabled) { - this.enabled = enabled; - } - } -} diff --git a/components/camel-spring-cloud/src/main/resources/META-INF/spring.factories b/components/camel-spring-cloud/src/main/resources/META-INF/spring.factories index d92509416a3a3..d9c7e2128c388 100644 --- a/components/camel-spring-cloud/src/main/resources/META-INF/spring.factories +++ b/components/camel-spring-cloud/src/main/resources/META-INF/spring.factories @@ -17,6 +17,6 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.apache.camel.spring.cloud.CamelCloudAutoConfiguration,\ -org.apache.camel.spring.cloud.ServiceCallServiceChooserAutoConfiguration,\ -org.apache.camel.spring.cloud.ServiceCallServiceDiscoveryAutoConfiguration,\ -org.apache.camel.spring.cloud.ServiceCallLoadBalancerAutoConfiguration +org.apache.camel.spring.cloud.CamelCloudServiceChooserAutoConfiguration,\ +org.apache.camel.spring.cloud.CamelCloudServiceDiscoveryAutoConfiguration,\ +org.apache.camel.spring.cloud.CamelCloudLoadBalancerAutoConfiguration diff --git a/components/camel-spring-cloud/src/test/java/org/apache/camel/spring/cloud/CamelCloudServiceCallConfiguration.java b/components/camel-spring-cloud/src/test/java/org/apache/camel/spring/cloud/CamelCloudServiceCallConfiguration.java new file mode 100644 index 0000000000000..e45e25c3107f6 --- /dev/null +++ b/components/camel-spring-cloud/src/test/java/org/apache/camel/spring/cloud/CamelCloudServiceCallConfiguration.java @@ -0,0 +1,49 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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.apache.camel.spring.cloud; + +import org.apache.camel.builder.RouteBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class CamelCloudServiceCallConfiguration { + @Bean + public RouteBuilder myRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .serviceCall() + .name("custom-svc-list/hello"); + + from("jetty:http://localhost:9090/hello") + .transform() + .constant("9090"); + from("jetty:http://localhost:9091/hello") + .transform() + .constant("9091"); + from("jetty:http://localhost:9092/hello") + .transform() + .constant("9092"); + } + }; + } +} + + diff --git a/components/camel-spring-cloud/src/test/java/org/apache/camel/spring/cloud/CamelCloudServiceCallConfigurationTest.java b/components/camel-spring-cloud/src/test/java/org/apache/camel/spring/cloud/CamelCloudServiceCallConfigurationTest.java index cb992a4940fd8..a5d9a79a3f2dc 100644 --- a/components/camel-spring-cloud/src/test/java/org/apache/camel/spring/cloud/CamelCloudServiceCallConfigurationTest.java +++ b/components/camel-spring-cloud/src/test/java/org/apache/camel/spring/cloud/CamelCloudServiceCallConfigurationTest.java @@ -42,14 +42,14 @@ classes = { CamelAutoConfiguration.class, CamelCloudAutoConfiguration.class, - ServiceCallServiceChooserAutoConfiguration.class + CamelCloudServiceChooserAutoConfiguration.class }, properties = { - "camel.cloud.servicecall.enabled=false", - "camel.cloud.servicecall.service-discovery.enabled=false", - "camel.cloud.servicecall.service-filter.enabled=false", - "camel.cloud.servicecall.service-chooser.enabled=true", - "camel.cloud.servicecall.load-balancer.enabled=false", + "camel.cloud.enabled=false", + "camel.cloud.service-discovery.enabled=false", + "camel.cloud.service-filter.enabled=false", + "camel.cloud.service-chooser.enabled=true", + "camel.cloud.load-balancer.enabled=false", "debug=false" } ) @@ -61,15 +61,15 @@ public class CamelCloudServiceCallConfigurationTest { public void doTest() throws Exception { Environment env = ctx.getEnvironment(); - assertFalse(env.getProperty("camel.cloud.servicecall.enabled", Boolean.class)); - assertFalse(env.getProperty("camel.cloud.servicecall.service-discovery.enabled", Boolean.class)); - assertFalse(env.getProperty("camel.cloud.servicecall.service-filter.enabled", Boolean.class)); - assertTrue(env.getProperty("camel.cloud.servicecall.service-chooser.enabled", Boolean.class)); - assertFalse(env.getProperty("camel.cloud.servicecall.load-balancer.enabled", Boolean.class)); + assertFalse(env.getProperty("camel.cloud.enabled", Boolean.class)); + assertFalse(env.getProperty("camel.cloud.service-discovery.enabled", Boolean.class)); + assertFalse(env.getProperty("camel.cloud.service-filter.enabled", Boolean.class)); + assertTrue(env.getProperty("camel.cloud.service-chooser.enabled", Boolean.class)); + assertFalse(env.getProperty("camel.cloud.load-balancer.enabled", Boolean.class)); assertTrue(ctx.getBeansOfType(ServiceDiscovery.class).isEmpty()); assertTrue(ctx.getBeansOfType(ServiceFilter.class).isEmpty()); - assertFalse(ctx.getBeansOfType(ServiceChooser.class).isEmpty()); + assertTrue(ctx.getBeansOfType(ServiceChooser.class).isEmpty()); assertTrue(ctx.getBeansOfType(LoadBalancer.class).isEmpty()); } } diff --git a/components/camel-spring-cloud/src/test/java/org/apache/camel/spring/cloud/CamelCloudServiceCallRibbonTest.java b/components/camel-spring-cloud/src/test/java/org/apache/camel/spring/cloud/CamelCloudServiceCallRibbonTest.java index b038a19a50d50..c80489e86e6f5 100644 --- a/components/camel-spring-cloud/src/test/java/org/apache/camel/spring/cloud/CamelCloudServiceCallRibbonTest.java +++ b/components/camel-spring-cloud/src/test/java/org/apache/camel/spring/cloud/CamelCloudServiceCallRibbonTest.java @@ -18,7 +18,6 @@ package org.apache.camel.spring.cloud; import org.apache.camel.ProducerTemplate; -import org.apache.camel.builder.RouteBuilder; import org.apache.camel.spring.boot.CamelAutoConfiguration; import org.junit.Assert; import org.junit.Test; @@ -26,8 +25,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit4.SpringRunner; @@ -38,11 +35,11 @@ classes = { CamelAutoConfiguration.class, CamelCloudAutoConfiguration.class, - CamelCloudServiceCallRibbonTest.TestConfiguration.class + CamelCloudServiceCallConfiguration.class }, properties = { "ribbon.eureka.enabled=false", - "ribbon.listOfServers=localhost:9090,localhost:9091", + "ribbon.listOfServers=localhost:9090,localhost:9092", "ribbon.ServerListRefreshInterval=15000", "debug=false" } @@ -54,32 +51,7 @@ public class CamelCloudServiceCallRibbonTest { @Test public void testServiceCall() throws Exception { Assert.assertEquals("9090", template.requestBody("direct:start", null, String.class)); - Assert.assertEquals("9091", template.requestBody("direct:start", null, String.class)); + Assert.assertEquals("9092", template.requestBody("direct:start", null, String.class)); } - - // ************************** - // Configuration - // ************************** - - @Configuration - public static class TestConfiguration { - @Bean - public RouteBuilder myRouteBuilder() { - return new RouteBuilder() { - @Override - public void configure() throws Exception { - from("direct:start") - .serviceCall() - .name("service-call"); - - from("jetty:http://localhost:9090") - .transform().constant("9090"); - from("jetty:http://localhost:9091") - .transform().constant("9091"); - } - }; - } - } - } diff --git a/components/camel-spring-cloud/src/test/java/org/apache/camel/spring/cloud/CamelCloudServiceCallTest.java b/components/camel-spring-cloud/src/test/java/org/apache/camel/spring/cloud/CamelCloudServiceCallTest.java index c22b029e1ed7f..333561c0ad1a8 100644 --- a/components/camel-spring-cloud/src/test/java/org/apache/camel/spring/cloud/CamelCloudServiceCallTest.java +++ b/components/camel-spring-cloud/src/test/java/org/apache/camel/spring/cloud/CamelCloudServiceCallTest.java @@ -18,7 +18,6 @@ package org.apache.camel.spring.cloud; import org.apache.camel.ProducerTemplate; -import org.apache.camel.builder.RouteBuilder; import org.apache.camel.spring.boot.CamelAutoConfiguration; import org.junit.Assert; import org.junit.Test; @@ -26,8 +25,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit4.SpringRunner; @@ -38,12 +35,12 @@ classes = { CamelAutoConfiguration.class, CamelCloudAutoConfiguration.class, - CamelCloudServiceCallTest.TestConfiguration.class + CamelCloudServiceCallConfiguration.class }, properties = { - "camel.cloud.servicecall.load-balancer.enabled=false", - "camel.cloud.servicecall.service-discovery.services[custom-svc-list]=localhost:9090,localhost:9091,localhost:9092", - "camel.cloud.servicecall.service-filter.blacklist[custom-svc-list]=localhost:9091", + "camel.cloud.load-balancer.enabled=false", + "camel.cloud.service-discovery.services[custom-svc-list]=localhost:9090,localhost:9091,localhost:9092", + "camel.cloud.service-filter.blacklist[custom-svc-list]=localhost:9091", "ribbon.enabled=false", "debug=false" } @@ -57,35 +54,5 @@ public void testServiceCall() throws Exception { Assert.assertEquals("9090", template.requestBody("direct:start", null, String.class)); Assert.assertEquals("9092", template.requestBody("direct:start", null, String.class)); } - - // ************************** - // Configuration - // ************************** - - @Configuration - public static class TestConfiguration { - @Bean - public RouteBuilder myRouteBuilder() { - return new RouteBuilder() { - @Override - public void configure() throws Exception { - from("direct:start") - .serviceCall() - .name("custom-svc-list/hello"); - - from("jetty:http://localhost:9090/hello") - .transform() - .constant("9090"); - from("jetty:http://localhost:9091/hello") - .transform() - .constant("9091"); - from("jetty:http://localhost:9092/hello") - .transform() - .constant("9092"); - } - }; - } - } - } diff --git a/platforms/spring-boot/components-starter/camel-consul-starter/src/main/java/org/apache/camel/component/consul/springboot/cloud/ConsulCloudAutoConfiguration.java b/platforms/spring-boot/components-starter/camel-consul-starter/src/main/java/org/apache/camel/component/consul/springboot/cloud/ConsulCloudAutoConfiguration.java new file mode 100644 index 0000000000000..db5cf32c353a9 --- /dev/null +++ b/platforms/spring-boot/components-starter/camel-consul-starter/src/main/java/org/apache/camel/component/consul/springboot/cloud/ConsulCloudAutoConfiguration.java @@ -0,0 +1,79 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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.apache.camel.component.consul.springboot.cloud; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.camel.CamelContext; +import org.apache.camel.cloud.ServiceDiscovery; +import org.apache.camel.component.consul.cloud.ConsulServiceDiscoveryFactory; +import org.apache.camel.spring.boot.CamelAutoConfiguration; +import org.apache.camel.util.IntrospectionSupport; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.condition.ConditionMessage; +import org.springframework.boot.autoconfigure.condition.ConditionOutcome; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.SpringBootCondition; +import org.springframework.boot.bind.RelaxedPropertyResolver; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ConditionContext; +import org.springframework.context.annotation.Conditional; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Lazy; +import org.springframework.core.type.AnnotatedTypeMetadata; + +@Configuration +@ConditionalOnBean(CamelAutoConfiguration.class) +@Conditional(ConsulCloudAutoConfiguration.Condition.class) +@AutoConfigureAfter(CamelAutoConfiguration.class) +@EnableConfigurationProperties(ConsulCloudConfiguration.class) +public class ConsulCloudAutoConfiguration { + @Lazy + @Bean(name = "consul-service-discovery") + @ConditionalOnClass(CamelContext.class) + public ServiceDiscovery configureServiceDiscoveryFactory(CamelContext camelContext, ConsulCloudConfiguration configuration) throws Exception { + ConsulServiceDiscoveryFactory factory = new ConsulServiceDiscoveryFactory(); + + Map parameters = new HashMap<>(); + IntrospectionSupport.getProperties(configuration, parameters, null, false); + IntrospectionSupport.setProperties(camelContext, camelContext.getTypeConverter(), factory, parameters); + + return factory.newInstance(camelContext); + } + + public static class Condition extends SpringBootCondition { + @Override + public ConditionOutcome getMatchOutcome(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) { + boolean groupEnabled = isEnabled(conditionContext, "camel.cloud.", true); + + ConditionMessage.Builder message = ConditionMessage.forCondition("camel.cloud.consul"); + if (isEnabled(conditionContext, "camel.cloud.consul.", groupEnabled)) { + return ConditionOutcome.match(message.because("enabled")); + } + + return ConditionOutcome.noMatch(message.because("not enabled")); + } + + private boolean isEnabled(ConditionContext context, String prefix, boolean defaultValue) { + RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(context.getEnvironment(), prefix); + return resolver.getProperty("enabled", Boolean.class, defaultValue); + } + } +} diff --git a/platforms/spring-boot/components-starter/camel-consul-starter/src/main/java/org/apache/camel/component/consul/springboot/cloud/ConsulCloudConfiguration.java b/platforms/spring-boot/components-starter/camel-consul-starter/src/main/java/org/apache/camel/component/consul/springboot/cloud/ConsulCloudConfiguration.java new file mode 100644 index 0000000000000..23398595a97b1 --- /dev/null +++ b/platforms/spring-boot/components-starter/camel-consul-starter/src/main/java/org/apache/camel/component/consul/springboot/cloud/ConsulCloudConfiguration.java @@ -0,0 +1,45 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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.apache.camel.component.consul.springboot.cloud; + +import org.apache.camel.component.consul.ConsulConfiguration; +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties(prefix = "camel.cloud.consul") +public class ConsulCloudConfiguration { + private boolean enabled = true; + private ServiceDiscoveryConfiguration serviceDiscovery = new ServiceDiscoveryConfiguration(); + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + public ServiceDiscoveryConfiguration getServiceDiscovery() { + return serviceDiscovery; + } + + // ************************************************************************* + // + // ************************************************************************* + + public class ServiceDiscoveryConfiguration extends ConsulConfiguration { + } +} diff --git a/platforms/spring-boot/components-starter/camel-consul-starter/src/main/resources/META-INF/spring.factories b/platforms/spring-boot/components-starter/camel-consul-starter/src/main/resources/META-INF/spring.factories index 85286f6cc59f3..4517ac372a0f2 100644 --- a/platforms/spring-boot/components-starter/camel-consul-starter/src/main/resources/META-INF/spring.factories +++ b/platforms/spring-boot/components-starter/camel-consul-starter/src/main/resources/META-INF/spring.factories @@ -16,4 +16,5 @@ # org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ -org.apache.camel.component.consul.springboot.ConsulComponentAutoConfiguration +org.apache.camel.component.consul.springboot.ConsulComponentAutoConfiguration,\ +org.apache.camel.component.consul.springboot.cloud.ConsulCloudAutoConfiguration diff --git a/platforms/spring-boot/components-starter/camel-dns-starter/src/main/java/org/apache/camel/component/dns/springboot/cloud/DnsCloudAutoConfiguration.java b/platforms/spring-boot/components-starter/camel-dns-starter/src/main/java/org/apache/camel/component/dns/springboot/cloud/DnsCloudAutoConfiguration.java new file mode 100644 index 0000000000000..a3e9b097de731 --- /dev/null +++ b/platforms/spring-boot/components-starter/camel-dns-starter/src/main/java/org/apache/camel/component/dns/springboot/cloud/DnsCloudAutoConfiguration.java @@ -0,0 +1,79 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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.apache.camel.component.dns.springboot.cloud; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.camel.CamelContext; +import org.apache.camel.cloud.ServiceDiscovery; +import org.apache.camel.component.dns.cloud.DnsServiceDiscoveryFactory; +import org.apache.camel.spring.boot.CamelAutoConfiguration; +import org.apache.camel.util.IntrospectionSupport; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.condition.ConditionMessage; +import org.springframework.boot.autoconfigure.condition.ConditionOutcome; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.SpringBootCondition; +import org.springframework.boot.bind.RelaxedPropertyResolver; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ConditionContext; +import org.springframework.context.annotation.Conditional; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Lazy; +import org.springframework.core.type.AnnotatedTypeMetadata; + +@Configuration +@ConditionalOnBean(CamelAutoConfiguration.class) +@Conditional(DnsCloudAutoConfiguration.Condition.class) +@AutoConfigureAfter(CamelAutoConfiguration.class) +@EnableConfigurationProperties(DnsCloudConfiguration.class) +public class DnsCloudAutoConfiguration { + @Lazy + @Bean(name = "dns-service-discovery") + @ConditionalOnClass(CamelContext.class) + public ServiceDiscovery configureServiceDiscoveryFactory(CamelContext camelContext, DnsCloudConfiguration configuration) throws Exception { + DnsServiceDiscoveryFactory factory = new DnsServiceDiscoveryFactory(); + + Map parameters = new HashMap<>(); + IntrospectionSupport.getProperties(configuration, parameters, null, false); + IntrospectionSupport.setProperties(camelContext, camelContext.getTypeConverter(), factory, parameters); + + return factory.newInstance(camelContext); + } + + public static class Condition extends SpringBootCondition { + @Override + public ConditionOutcome getMatchOutcome(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) { + boolean groupEnabled = isEnabled(conditionContext, "camel.cloud.", true); + + ConditionMessage.Builder message = ConditionMessage.forCondition("camel.cloud.dns"); + if (isEnabled(conditionContext, "camel.cloud.dns.", groupEnabled)) { + return ConditionOutcome.match(message.because("enabled")); + } + + return ConditionOutcome.noMatch(message.because("not enabled")); + } + + private boolean isEnabled(ConditionContext context, String prefix, boolean defaultValue) { + RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(context.getEnvironment(), prefix); + return resolver.getProperty("enabled", Boolean.class, defaultValue); + } + } +} diff --git a/platforms/spring-boot/components-starter/camel-dns-starter/src/main/java/org/apache/camel/component/dns/springboot/cloud/DnsCloudConfiguration.java b/platforms/spring-boot/components-starter/camel-dns-starter/src/main/java/org/apache/camel/component/dns/springboot/cloud/DnsCloudConfiguration.java new file mode 100644 index 0000000000000..3d2d7b78739e8 --- /dev/null +++ b/platforms/spring-boot/components-starter/camel-dns-starter/src/main/java/org/apache/camel/component/dns/springboot/cloud/DnsCloudConfiguration.java @@ -0,0 +1,45 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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.apache.camel.component.dns.springboot.cloud; + +import org.apache.camel.component.dns.DnsConfiguration; +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties(prefix = "camel.cloud.dns") +public class DnsCloudConfiguration { + private boolean enabled = true; + private ServiceDiscoveryConfiguration serviceDiscovery = new ServiceDiscoveryConfiguration(); + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + public ServiceDiscoveryConfiguration getServiceDiscovery() { + return serviceDiscovery; + } + + // ************************************************************************* + // + // ************************************************************************* + + public class ServiceDiscoveryConfiguration extends DnsConfiguration { + } +} diff --git a/platforms/spring-boot/components-starter/camel-dns-starter/src/main/resources/META-INF/spring.factories b/platforms/spring-boot/components-starter/camel-dns-starter/src/main/resources/META-INF/spring.factories index fef73275cb099..f078ff9ddf57b 100644 --- a/platforms/spring-boot/components-starter/camel-dns-starter/src/main/resources/META-INF/spring.factories +++ b/platforms/spring-boot/components-starter/camel-dns-starter/src/main/resources/META-INF/spring.factories @@ -16,4 +16,5 @@ # org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ -org.apache.camel.component.dns.springboot.DnsComponentAutoConfiguration +org.apache.camel.component.dns.springboot.DnsComponentAutoConfiguration,\ +org.apache.camel.component.dns.springboot.cloud.DnsCloudAutoConfiguration \ No newline at end of file diff --git a/platforms/spring-boot/components-starter/camel-etcd-starter/src/main/java/org/apache/camel/component/etcd/springboot/cloud/EtcdCloudAutoConfiguration.java b/platforms/spring-boot/components-starter/camel-etcd-starter/src/main/java/org/apache/camel/component/etcd/springboot/cloud/EtcdCloudAutoConfiguration.java new file mode 100644 index 0000000000000..9af32185165bb --- /dev/null +++ b/platforms/spring-boot/components-starter/camel-etcd-starter/src/main/java/org/apache/camel/component/etcd/springboot/cloud/EtcdCloudAutoConfiguration.java @@ -0,0 +1,83 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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.apache.camel.component.etcd.springboot.cloud; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.camel.CamelContext; +import org.apache.camel.cloud.ServiceDiscovery; +import org.apache.camel.component.etcd.cloud.EtcdOnDemandServiceDiscovery; +import org.apache.camel.component.etcd.cloud.EtcdServiceDiscoveryFactory; +import org.apache.camel.component.etcd.cloud.EtcdWatchServiceDiscovery; +import org.apache.camel.spring.boot.CamelAutoConfiguration; +import org.apache.camel.util.IntrospectionSupport; +import org.apache.camel.util.ObjectHelper; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.condition.ConditionMessage; +import org.springframework.boot.autoconfigure.condition.ConditionOutcome; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.SpringBootCondition; +import org.springframework.boot.bind.RelaxedPropertyResolver; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ConditionContext; +import org.springframework.context.annotation.Conditional; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Lazy; +import org.springframework.core.type.AnnotatedTypeMetadata; + +@Configuration +@ConditionalOnBean(CamelAutoConfiguration.class) +@Conditional(EtcdCloudAutoConfiguration.Condition.class) +@AutoConfigureAfter(CamelAutoConfiguration.class) +@EnableConfigurationProperties(EtcdCloudConfiguration.class) +public class EtcdCloudAutoConfiguration { + @Lazy + @Bean(name = "etcd-service-discovery") + @ConditionalOnClass(CamelContext.class) + public ServiceDiscovery configureServiceDiscoveryFactory(CamelContext camelContext, EtcdCloudConfiguration configuration) throws Exception { + EtcdServiceDiscoveryFactory factory = new EtcdServiceDiscoveryFactory(); + + Map parameters = new HashMap<>(); + IntrospectionSupport.getProperties(configuration, parameters, null, false); + IntrospectionSupport.setProperties(camelContext, camelContext.getTypeConverter(), factory, parameters); + + return factory.newInstance(camelContext); + } + + public static class Condition extends SpringBootCondition { + @Override + public ConditionOutcome getMatchOutcome(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) { + boolean groupEnabled = isEnabled(conditionContext, "camel.cloud.", true); + + ConditionMessage.Builder message = ConditionMessage.forCondition("camel.cloud.etcd"); + if (isEnabled(conditionContext, "camel.cloud.etcd.", groupEnabled)) { + return ConditionOutcome.match(message.because("enabled")); + } + + return ConditionOutcome.noMatch(message.because("not enabled")); + } + + private boolean isEnabled(ConditionContext context, String prefix, boolean defaultValue) { + RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(context.getEnvironment(), prefix); + return resolver.getProperty("enabled", Boolean.class, defaultValue); + } + } +} diff --git a/platforms/spring-boot/components-starter/camel-etcd-starter/src/main/java/org/apache/camel/component/etcd/springboot/cloud/EtcdCloudConfiguration.java b/platforms/spring-boot/components-starter/camel-etcd-starter/src/main/java/org/apache/camel/component/etcd/springboot/cloud/EtcdCloudConfiguration.java new file mode 100644 index 0000000000000..42d4e9254dc5c --- /dev/null +++ b/platforms/spring-boot/components-starter/camel-etcd-starter/src/main/java/org/apache/camel/component/etcd/springboot/cloud/EtcdCloudConfiguration.java @@ -0,0 +1,54 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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.apache.camel.component.etcd.springboot.cloud; + +import org.apache.camel.component.etcd.EtcdConfiguration; +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties(prefix = "camel.cloud.etcd") +public class EtcdCloudConfiguration { + private boolean enabled = true; + private ServiceDiscoveryConfiguration serviceDiscovery = new ServiceDiscoveryConfiguration(); + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + public ServiceDiscoveryConfiguration getServiceDiscovery() { + return serviceDiscovery; + } + + // ************************************************************************* + // + // ************************************************************************* + + public class ServiceDiscoveryConfiguration extends EtcdConfiguration { + private String type; + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + } +} diff --git a/platforms/spring-boot/components-starter/camel-etcd-starter/src/main/resources/META-INF/spring.factories b/platforms/spring-boot/components-starter/camel-etcd-starter/src/main/resources/META-INF/spring.factories index d3e9b9acefb25..1358d3e1ea154 100644 --- a/platforms/spring-boot/components-starter/camel-etcd-starter/src/main/resources/META-INF/spring.factories +++ b/platforms/spring-boot/components-starter/camel-etcd-starter/src/main/resources/META-INF/spring.factories @@ -16,4 +16,5 @@ # org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ -org.apache.camel.component.etcd.springboot.EtcdComponentAutoConfiguration +org.apache.camel.component.etcd.springboot.EtcdComponentAutoConfiguration,\ +org.apache.camel.component.etcd.springboot.cloud.EtcdCloudAutoConfiguration diff --git a/platforms/spring-boot/components-starter/camel-kubernetes-starter/src/main/java/org/apache/camel/component/kubernetes/springboot/cloud/KubernetesCloudAutoConfiguration.java b/platforms/spring-boot/components-starter/camel-kubernetes-starter/src/main/java/org/apache/camel/component/kubernetes/springboot/cloud/KubernetesCloudAutoConfiguration.java new file mode 100644 index 0000000000000..6527ba45378bc --- /dev/null +++ b/platforms/spring-boot/components-starter/camel-kubernetes-starter/src/main/java/org/apache/camel/component/kubernetes/springboot/cloud/KubernetesCloudAutoConfiguration.java @@ -0,0 +1,79 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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.apache.camel.component.kubernetes.springboot.cloud; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.camel.CamelContext; +import org.apache.camel.cloud.ServiceDiscovery; +import org.apache.camel.component.kubernetes.cloud.KubernetesServiceDiscoveryFactory; +import org.apache.camel.spring.boot.CamelAutoConfiguration; +import org.apache.camel.util.IntrospectionSupport; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.condition.ConditionMessage; +import org.springframework.boot.autoconfigure.condition.ConditionOutcome; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.SpringBootCondition; +import org.springframework.boot.bind.RelaxedPropertyResolver; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ConditionContext; +import org.springframework.context.annotation.Conditional; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Lazy; +import org.springframework.core.type.AnnotatedTypeMetadata; + +@Configuration +@ConditionalOnBean(CamelAutoConfiguration.class) +@Conditional(KubernetesCloudAutoConfiguration.Condition.class) +@AutoConfigureAfter(CamelAutoConfiguration.class) +@EnableConfigurationProperties(KubernetesCloudConfiguration.class) +public class KubernetesCloudAutoConfiguration { + @Lazy + @Bean(name = "kubernetes-service-discovery") + @ConditionalOnClass(CamelContext.class) + public ServiceDiscovery configureServiceDiscoveryFactory(CamelContext camelContext, KubernetesCloudConfiguration configuration) throws Exception { + KubernetesServiceDiscoveryFactory factory = new KubernetesServiceDiscoveryFactory(); + + Map parameters = new HashMap<>(); + IntrospectionSupport.getProperties(configuration, parameters, null, false); + IntrospectionSupport.setProperties(camelContext, camelContext.getTypeConverter(), factory, parameters); + + return factory.newInstance(camelContext); + } + + public static class Condition extends SpringBootCondition { + @Override + public ConditionOutcome getMatchOutcome(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) { + boolean groupEnabled = isEnabled(conditionContext, "camel.cloud.", true); + + ConditionMessage.Builder message = ConditionMessage.forCondition("camel.cloud.kubernetes"); + if (isEnabled(conditionContext, "camel.cloud.kubernetes.", groupEnabled)) { + return ConditionOutcome.match(message.because("enabled")); + } + + return ConditionOutcome.noMatch(message.because("not enabled")); + } + + private boolean isEnabled(ConditionContext context, String prefix, boolean defaultValue) { + RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(context.getEnvironment(), prefix); + return resolver.getProperty("enabled", Boolean.class, defaultValue); + } + } +} diff --git a/platforms/spring-boot/components-starter/camel-kubernetes-starter/src/main/java/org/apache/camel/component/kubernetes/springboot/cloud/KubernetesCloudConfiguration.java b/platforms/spring-boot/components-starter/camel-kubernetes-starter/src/main/java/org/apache/camel/component/kubernetes/springboot/cloud/KubernetesCloudConfiguration.java new file mode 100644 index 0000000000000..5dd6902f59211 --- /dev/null +++ b/platforms/spring-boot/components-starter/camel-kubernetes-starter/src/main/java/org/apache/camel/component/kubernetes/springboot/cloud/KubernetesCloudConfiguration.java @@ -0,0 +1,54 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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.apache.camel.component.kubernetes.springboot.cloud; + +import org.apache.camel.component.kubernetes.KubernetesConfiguration; +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties(prefix = "camel.cloud.kubernetes") +public class KubernetesCloudConfiguration { + private boolean enabled = true; + private ServiceDiscoveryConfiguration serviceDiscovery = new ServiceDiscoveryConfiguration(); + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + public ServiceDiscoveryConfiguration getServiceDiscovery() { + return serviceDiscovery; + } + + // ************************************************************************* + // + // ************************************************************************* + + public class ServiceDiscoveryConfiguration extends KubernetesConfiguration { + private String lookup; + + public String getLookup() { + return lookup; + } + + public void setLookup(String lookup) { + this.lookup = lookup; + } + } +} diff --git a/platforms/spring-boot/components-starter/camel-kubernetes-starter/src/main/resources/META-INF/spring.factories b/platforms/spring-boot/components-starter/camel-kubernetes-starter/src/main/resources/META-INF/spring.factories index 5ef225844fc4a..310e1fefe5898 100644 --- a/platforms/spring-boot/components-starter/camel-kubernetes-starter/src/main/resources/META-INF/spring.factories +++ b/platforms/spring-boot/components-starter/camel-kubernetes-starter/src/main/resources/META-INF/spring.factories @@ -16,4 +16,5 @@ # org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ -org.apache.camel.component.kubernetes.springboot.KubernetesComponentAutoConfiguration +org.apache.camel.component.kubernetes.springboot.KubernetesComponentAutoConfiguration,\ +org.apache.camel.component.kubernetes.springboot.cloud.KubernetesCloudAutoConfiguration