Repository: camel Updated Branches: refs/heads/master 9c6263d8a -> 0aae1257b
http://git-wip-us.apache.org/repos/asf/camel/blob/0aae1257/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallConfigurationDefinition.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallConfigurationDefinition.java b/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallConfigurationDefinition.java index ef9e3a4..aec0291 100644 --- a/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallConfigurationDefinition.java +++ b/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallConfigurationDefinition.java @@ -68,16 +68,25 @@ public class ServiceCallConfigurationDefinition extends IdentifiedType { private Expression expression; @XmlElements({ @XmlElement(name = "cachingServiceDiscovery", type = CachingServiceCallServiceDiscoveryConfiguration.class), + @XmlElement(name = "chainedServiceDiscovery", type = ChainedServiceCallServiceDiscoveryConfiguration.class), @XmlElement(name = "consulServiceDiscovery", type = ConsulServiceCallServiceDiscoveryConfiguration.class), @XmlElement(name = "dnsServiceDiscovery", type = DnsServiceCallServiceDiscoveryConfiguration.class), @XmlElement(name = "etcdServiceDiscovery", type = EtcdServiceCallServiceDiscoveryConfiguration.class), @XmlElement(name = "kubernetesServiceDiscovery", type = KubernetesServiceCallServiceDiscoveryConfiguration.class), - @XmlElement(name = "multiServiceDiscovery", type = MultiServiceCallServiceDiscoveryConfiguration.class), @XmlElement(name = "staticServiceDiscovery", type = StaticServiceCallServiceDiscoveryConfiguration.class)} ) private ServiceCallServiceDiscoveryConfiguration serviceDiscoveryConfiguration; @XmlElements({ + @XmlElement(name = "blacklistServiceFilter", type = BlacklistServiceCallServiceFilterConfiguration.class), + @XmlElement(name = "chainedServiceFilter", type = ChainedServiceCallServiceFilterConfiguration.class), + @XmlElement(name = "customServiceFilter", type = CustomServiceCallServiceFilterConfiguration.class), + @XmlElement(name = "healthyServiceFilter", type = HealthyServiceCallServiceFilterConfiguration.class), + @XmlElement(name = "passThroughServiceFilter", type = PassThroughServiceCallServiceFilterConfiguration.class)} + ) + private ServiceCallServiceFilterConfiguration serviceFilterConfiguration; + + @XmlElements({ @XmlElement(name = "ribbonLoadBalancer", type = RibbonServiceCallLoadBalancerConfiguration.class)} ) private ServiceCallLoadBalancerConfiguration loadBalancerConfiguration; @@ -246,6 +255,17 @@ public class ServiceCallConfigurationDefinition extends IdentifiedType { this.serviceDiscoveryConfiguration = serviceDiscoveryConfiguration; } + public ServiceCallServiceFilterConfiguration getServiceFilterConfiguration() { + return serviceFilterConfiguration; + } + + /** + * Configures the ServiceFilter using the given configuration. + */ + public void setServiceFilterConfiguration(ServiceCallServiceFilterConfiguration serviceFilterConfiguration) { + this.serviceFilterConfiguration = serviceFilterConfiguration; + } + public ServiceCallLoadBalancerConfiguration getLoadBalancerConfiguration() { return loadBalancerConfiguration; } @@ -392,6 +412,14 @@ public class ServiceCallConfigurationDefinition extends IdentifiedType { } /** + * Configures the ServiceFilter using the given configuration. + */ + public ServiceCallConfigurationDefinition serviceFilterConfiguration(ServiceCallServiceFilterConfiguration serviceFilterConfiguration) { + setServiceFilterConfiguration(serviceFilterConfiguration); + return this; + } + + /** * Configures the LoadBalancer using the given configuration. */ public ServiceCallConfigurationDefinition loadBalancerConfiguration(ServiceCallLoadBalancerConfiguration loadBalancerConfiguration) { @@ -494,8 +522,8 @@ public class ServiceCallConfigurationDefinition extends IdentifiedType { return this; } - public MultiServiceCallServiceDiscoveryConfiguration multiServiceDiscovery() { - MultiServiceCallServiceDiscoveryConfiguration conf = new MultiServiceCallServiceDiscoveryConfiguration(); + public ChainedServiceCallServiceDiscoveryConfiguration multiServiceDiscovery() { + ChainedServiceCallServiceDiscoveryConfiguration conf = new ChainedServiceCallServiceDiscoveryConfiguration(); setServiceDiscoveryConfiguration(conf); return conf; @@ -509,6 +537,49 @@ public class ServiceCallConfigurationDefinition extends IdentifiedType { } // ***************************** + // Shortcuts - ServiceFilter + // ***************************** + + public ServiceCallConfigurationDefinition healthyFilter() { + HealthyServiceCallServiceFilterConfiguration conf = new HealthyServiceCallServiceFilterConfiguration(); + setServiceFilterConfiguration(conf); + + return this; + } + + public ServiceCallConfigurationDefinition passThroughFilter() { + PassThroughServiceCallServiceFilterConfiguration conf = new PassThroughServiceCallServiceFilterConfiguration(); + setServiceFilterConfiguration(conf); + + return this; + } + + public ChainedServiceCallServiceFilterConfiguration multiFilter() { + ChainedServiceCallServiceFilterConfiguration conf = new ChainedServiceCallServiceFilterConfiguration(); + setServiceFilterConfiguration(conf); + + return conf; + } + + public ServiceCallConfigurationDefinition customFilter(String serviceFilter) { + CustomServiceCallServiceFilterConfiguration conf = new CustomServiceCallServiceFilterConfiguration(); + conf.setServiceFilterRef(serviceFilter); + + setServiceFilterConfiguration(conf); + + return this; + } + + public ServiceCallConfigurationDefinition customFilter(ServiceFilter serviceFilter) { + CustomServiceCallServiceFilterConfiguration conf = new CustomServiceCallServiceFilterConfiguration(); + conf.setServiceFilter(serviceFilter); + + setServiceFilterConfiguration(conf); + + return this; + } + + // ***************************** // Shortcuts - LoadBalancer // ***************************** http://git-wip-us.apache.org/repos/asf/camel/blob/0aae1257/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallConstants.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallConstants.java b/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallConstants.java index 33f7011..1c535f2 100644 --- a/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallConstants.java +++ b/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallConstants.java @@ -22,11 +22,19 @@ import java.util.List; final class ServiceCallConstants { public static final List<ServiceCallServiceDiscoveryConfiguration> SERVICE_DISCOVERY_CONFIGURATIONS = Arrays.asList( new CachingServiceCallServiceDiscoveryConfiguration(), + new ChainedServiceCallServiceDiscoveryConfiguration(), new ConsulServiceCallServiceDiscoveryConfiguration(), new DnsServiceCallServiceDiscoveryConfiguration(), new EtcdServiceCallServiceDiscoveryConfiguration(), - new KubernetesServiceCallServiceDiscoveryConfiguration(), - new MultiServiceCallServiceDiscoveryConfiguration() + new KubernetesServiceCallServiceDiscoveryConfiguration() + ); + + public static final List<ServiceCallServiceFilterConfiguration> SERVICE_FILTER_CONFIGURATIONS = Arrays.asList( + new BlacklistServiceCallServiceFilterConfiguration(), + new ChainedServiceCallServiceFilterConfiguration(), + new CustomServiceCallServiceFilterConfiguration(), + new HealthyServiceCallServiceFilterConfiguration(), + new PassThroughServiceCallServiceFilterConfiguration() ); public static final List<ServiceCallLoadBalancerConfiguration> LOAD_BALANCER_CONFIGURATIONS = Arrays.asList( http://git-wip-us.apache.org/repos/asf/camel/blob/0aae1257/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallDefinition.java ---------------------------------------------------------------------- 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 6c5cdc8..f51bbc4 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 @@ -39,11 +39,11 @@ import org.apache.camel.cloud.ServiceDiscovery; import org.apache.camel.cloud.ServiceDiscoveryAware; import org.apache.camel.cloud.ServiceFilter; import org.apache.camel.cloud.ServiceFilterAware; -import org.apache.camel.impl.cloud.AllServiceFilter; import org.apache.camel.impl.cloud.DefaultLoadBalancer; import org.apache.camel.impl.cloud.DefaultServiceCallExpression; import org.apache.camel.impl.cloud.DefaultServiceCallProcessor; import org.apache.camel.impl.cloud.HealthyServiceFilter; +import org.apache.camel.impl.cloud.PassThroughServiceFilter; import org.apache.camel.impl.cloud.RandomServiceChooser; import org.apache.camel.impl.cloud.RoundRobinServiceChooser; import org.apache.camel.model.NoOutputDefinition; @@ -92,22 +92,31 @@ public class ServiceCallDefinition extends NoOutputDefinition<ServiceCallDefinit @XmlElements({ @XmlElement(name = "cachingServiceDiscovery", type = CachingServiceCallServiceDiscoveryConfiguration.class), + @XmlElement(name = "chainedServiceDiscovery", type = ChainedServiceCallServiceDiscoveryConfiguration.class), @XmlElement(name = "consulServiceDiscovery", type = ConsulServiceCallServiceDiscoveryConfiguration.class), @XmlElement(name = "dnsServiceDiscovery", type = DnsServiceCallServiceDiscoveryConfiguration.class), @XmlElement(name = "etcdServiceDiscovery", type = EtcdServiceCallServiceDiscoveryConfiguration.class), @XmlElement(name = "kubernetesServiceDiscovery", type = KubernetesServiceCallServiceDiscoveryConfiguration.class), - @XmlElement(name = "multiServiceDiscovery", type = MultiServiceCallServiceDiscoveryConfiguration.class), @XmlElement(name = "staticServiceDiscovery", type = StaticServiceCallServiceDiscoveryConfiguration.class)} ) private ServiceCallServiceDiscoveryConfiguration serviceDiscoveryConfiguration; @XmlElements({ + @XmlElement(name = "blacklistServiceFilter", type = BlacklistServiceCallServiceFilterConfiguration.class), + @XmlElement(name = "chainedServiceFilter", type = ChainedServiceCallServiceFilterConfiguration.class), + @XmlElement(name = "customServiceFilter", type = CustomServiceCallServiceFilterConfiguration.class), + @XmlElement(name = "healthyServiceFilter", type = HealthyServiceCallServiceFilterConfiguration.class), + @XmlElement(name = "passThroughServiceFilter", type = PassThroughServiceCallServiceFilterConfiguration.class)} + ) + private ServiceCallServiceFilterConfiguration serviceFilterConfiguration; + + @XmlElements({ @XmlElement(name = "ribbonLoadBalancer", type = RibbonServiceCallLoadBalancerConfiguration.class)} ) private ServiceCallLoadBalancerConfiguration loadBalancerConfiguration; @XmlElements({ - @XmlElement(name = "expressionCOnfiguration", type = ServiceCallExpressionConfiguration.class)} + @XmlElement(name = "expressionConfiguration", type = ServiceCallExpressionConfiguration.class)} ) private ServiceCallExpressionConfiguration expressionConfiguration; @@ -305,6 +314,17 @@ public class ServiceCallDefinition extends NoOutputDefinition<ServiceCallDefinit this.serviceDiscoveryConfiguration = serviceDiscoveryConfiguration; } + public ServiceCallServiceFilterConfiguration getServiceFilterConfiguration() { + return serviceFilterConfiguration; + } + + /** + * Configures the ServiceFilter using the given configuration. + */ + public void setServiceFilterConfiguration(ServiceCallServiceFilterConfiguration serviceFilterConfiguration) { + this.serviceFilterConfiguration = serviceFilterConfiguration; + } + public ServiceCallLoadBalancerConfiguration getLoadBalancerConfiguration() { return loadBalancerConfiguration; } @@ -460,6 +480,14 @@ public class ServiceCallDefinition extends NoOutputDefinition<ServiceCallDefinit } /** + * Configures the ServiceFilter using the given configuration. + */ + public ServiceCallDefinition serviceFilterConfiguration(ServiceCallServiceFilterConfiguration serviceFilterConfiguration) { + setServiceFilterConfiguration(serviceFilterConfiguration); + return this; + } + + /** * Configures the LoadBalancer using the given configuration. */ public ServiceCallDefinition loadBalancerConfiguration(ServiceCallLoadBalancerConfiguration loadBalancerConfiguration) { @@ -562,8 +590,8 @@ public class ServiceCallDefinition extends NoOutputDefinition<ServiceCallDefinit return this; } - public MultiServiceCallServiceDiscoveryConfiguration multiServiceDiscovery() { - MultiServiceCallServiceDiscoveryConfiguration conf = new MultiServiceCallServiceDiscoveryConfiguration(this); + public ChainedServiceCallServiceDiscoveryConfiguration multiServiceDiscovery() { + ChainedServiceCallServiceDiscoveryConfiguration conf = new ChainedServiceCallServiceDiscoveryConfiguration(this); setServiceDiscoveryConfiguration(conf); return conf; @@ -577,6 +605,49 @@ public class ServiceCallDefinition extends NoOutputDefinition<ServiceCallDefinit } // ***************************** + // Shortcuts - ServiceFilter + // ***************************** + + public ServiceCallDefinition healthyFilter() { + HealthyServiceCallServiceFilterConfiguration conf = new HealthyServiceCallServiceFilterConfiguration(this); + setServiceFilterConfiguration(conf); + + return this; + } + + public ServiceCallDefinition passThroughFilter() { + PassThroughServiceCallServiceFilterConfiguration conf = new PassThroughServiceCallServiceFilterConfiguration(this); + setServiceFilterConfiguration(conf); + + return this; + } + + public ChainedServiceCallServiceFilterConfiguration multiFilter() { + ChainedServiceCallServiceFilterConfiguration conf = new ChainedServiceCallServiceFilterConfiguration(this); + setServiceFilterConfiguration(conf); + + return conf; + } + + public ServiceCallDefinition customFilter(String serviceFilter) { + CustomServiceCallServiceFilterConfiguration conf = new CustomServiceCallServiceFilterConfiguration(); + conf.setServiceFilterRef(serviceFilter); + + setServiceFilterConfiguration(conf); + + return this; + } + + public ServiceCallDefinition customFilter(ServiceFilter serviceFilter) { + CustomServiceCallServiceFilterConfiguration conf = new CustomServiceCallServiceFilterConfiguration(); + conf.setServiceFilter(serviceFilter); + + setServiceFilterConfiguration(conf); + + return this; + } + + // ***************************** // Shortcuts - LoadBalancer // ***************************** @@ -697,22 +768,33 @@ public class ServiceCallDefinition extends NoOutputDefinition<ServiceCallDefinit return answer; } - private ServiceFilter retrieveServiceFilter(CamelContext camelContext, ServiceCallConfigurationDefinition config) { - ServiceFilter answer = retrieve(ServiceFilter.class, camelContext, this::getServiceFilter, this::getServiceFilterRef); - if (answer == null && config != null) { - answer = retrieve(ServiceFilter.class, camelContext, config::getServiceFilter, config::getServiceFilterRef); + private ServiceFilter retrieveServiceFilter(CamelContext camelContext, ServiceCallConfigurationDefinition config) throws Exception { + ServiceFilter answer; - // If the ServiceFilter is not found but a ref is set, try to determine - // the implementation according to the ref name. - if (answer == null) { - String ref = config.getServiceFilterRef(); - if (ObjectHelper.equal("healthy", ref, true)) { - answer = new HealthyServiceFilter(); - } else if (ObjectHelper.equal("all", ref, true)) { - answer = new AllServiceFilter(); + if (serviceFilterConfiguration != null) { + answer = serviceFilterConfiguration.newInstance(camelContext); + } else if (config != null && config.getLoadBalancerConfiguration() != null) { + answer = config.getServiceFilterConfiguration().newInstance(camelContext); + } else { + answer = retrieve(ServiceFilter.class, camelContext, this::getServiceFilter, this::getServiceFilterRef); + if (answer == null && config != null) { + answer = retrieve(ServiceFilter.class, camelContext, config::getServiceFilter, config::getServiceFilterRef); + + // If the ServiceFilter is not found but a ref is set, try to determine + // the implementation according to the ref name. + if (answer == null) { + String ref = config.getServiceFilterRef(); + if (ObjectHelper.equal("healthy", ref, true)) { + answer = new HealthyServiceFilter(); + } else if (ObjectHelper.equal("pass-through", ref, true)) { + answer = new PassThroughServiceFilter(); + } else if (ObjectHelper.equal("passthrough", ref, true)) { + answer = new PassThroughServiceFilter(); + } } } } + if (answer == null) { answer = findByType(camelContext, ServiceFilter.class); } http://git-wip-us.apache.org/repos/asf/camel/blob/0aae1257/camel-core/src/main/resources/META-INF/services/org/apache/camel/cloud/blacklist-service-filter ---------------------------------------------------------------------- diff --git a/camel-core/src/main/resources/META-INF/services/org/apache/camel/cloud/blacklist-service-filter b/camel-core/src/main/resources/META-INF/services/org/apache/camel/cloud/blacklist-service-filter new file mode 100644 index 0000000..29ee4e6 --- /dev/null +++ b/camel-core/src/main/resources/META-INF/services/org/apache/camel/cloud/blacklist-service-filter @@ -0,0 +1,17 @@ +# +# 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. +# +class=org.apache.camel.impl.cloud.BlacklistServiceFilterFactory \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/0aae1257/camel-core/src/main/resources/META-INF/services/org/apache/camel/cloud/chained-service-discovery ---------------------------------------------------------------------- diff --git a/camel-core/src/main/resources/META-INF/services/org/apache/camel/cloud/chained-service-discovery b/camel-core/src/main/resources/META-INF/services/org/apache/camel/cloud/chained-service-discovery new file mode 100644 index 0000000..e183367 --- /dev/null +++ b/camel-core/src/main/resources/META-INF/services/org/apache/camel/cloud/chained-service-discovery @@ -0,0 +1,17 @@ +# +# 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. +# +class=org.apache.camel.impl.cloud.ChainedServiceDiscoveryFactory \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/0aae1257/camel-core/src/main/resources/META-INF/services/org/apache/camel/cloud/chained-service-filter ---------------------------------------------------------------------- diff --git a/camel-core/src/main/resources/META-INF/services/org/apache/camel/cloud/chained-service-filter b/camel-core/src/main/resources/META-INF/services/org/apache/camel/cloud/chained-service-filter new file mode 100644 index 0000000..6eb18b5 --- /dev/null +++ b/camel-core/src/main/resources/META-INF/services/org/apache/camel/cloud/chained-service-filter @@ -0,0 +1,17 @@ +# +# 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. +# +class=org.apache.camel.impl.cloud.ChainedServiceFilterFactory \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/0aae1257/camel-core/src/main/resources/META-INF/services/org/apache/camel/cloud/healthy-service-filter ---------------------------------------------------------------------- diff --git a/camel-core/src/main/resources/META-INF/services/org/apache/camel/cloud/healthy-service-filter b/camel-core/src/main/resources/META-INF/services/org/apache/camel/cloud/healthy-service-filter new file mode 100644 index 0000000..b1cdd65 --- /dev/null +++ b/camel-core/src/main/resources/META-INF/services/org/apache/camel/cloud/healthy-service-filter @@ -0,0 +1,17 @@ +# +# 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. +# +class=org.apache.camel.impl.cloud.HealthyServiceFilterFactory \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/0aae1257/camel-core/src/main/resources/META-INF/services/org/apache/camel/cloud/multi-service-discovery ---------------------------------------------------------------------- diff --git a/camel-core/src/main/resources/META-INF/services/org/apache/camel/cloud/multi-service-discovery b/camel-core/src/main/resources/META-INF/services/org/apache/camel/cloud/multi-service-discovery deleted file mode 100644 index eacee81..0000000 --- a/camel-core/src/main/resources/META-INF/services/org/apache/camel/cloud/multi-service-discovery +++ /dev/null @@ -1,17 +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. -# -class=org.apache.camel.impl.cloud.MultiServiceDiscoveryFactory \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/0aae1257/camel-core/src/main/resources/META-INF/services/org/apache/camel/cloud/pass-through-service-filter ---------------------------------------------------------------------- diff --git a/camel-core/src/main/resources/META-INF/services/org/apache/camel/cloud/pass-through-service-filter b/camel-core/src/main/resources/META-INF/services/org/apache/camel/cloud/pass-through-service-filter new file mode 100644 index 0000000..e1e1912 --- /dev/null +++ b/camel-core/src/main/resources/META-INF/services/org/apache/camel/cloud/pass-through-service-filter @@ -0,0 +1,17 @@ +# +# 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. +# +class=org.apache.camel.impl.cloud.PassThroughServiceFilterFactory \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/0aae1257/camel-core/src/main/resources/org/apache/camel/model/cloud/jaxb.index ---------------------------------------------------------------------- diff --git a/camel-core/src/main/resources/org/apache/camel/model/cloud/jaxb.index b/camel-core/src/main/resources/org/apache/camel/model/cloud/jaxb.index index c275033..7d5156b 100644 --- a/camel-core/src/main/resources/org/apache/camel/model/cloud/jaxb.index +++ b/camel-core/src/main/resources/org/apache/camel/model/cloud/jaxb.index @@ -20,11 +20,15 @@ ServiceCallServiceDiscoveryConfiguration ServiceCallServiceFilterConfiguration ServiceCallServiceChooserConfiguration ServiceCallLoadBalancerConfiguration +BlacklistServiceCallServiceFilterConfiguration +ChainedServiceCallServiceFilterConfiguration +PassThroughServiceCallServiceFilterConfiguration CachingServiceCallServiceDiscoveryConfiguration ConsulServiceCallServiceDiscoveryConfiguration DnsServiceCallServiceDiscoveryConfiguration EtcdServiceCallServiceDiscoveryConfiguration +HealthyServiceCallServiceFilterConfiguration KubernetesServiceCallServiceDiscoveryConfiguration -MultiServiceCallServiceDiscoveryConfiguration +ChainedServiceCallServiceDiscoveryConfiguration StaticServiceCallServiceDiscoveryConfiguration RibbonServiceCallLoadBalancerConfiguration http://git-wip-us.apache.org/repos/asf/camel/blob/0aae1257/camel-core/src/test/java/org/apache/camel/impl/cloud/ChainedServiceDiscoveryTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/impl/cloud/ChainedServiceDiscoveryTest.java b/camel-core/src/test/java/org/apache/camel/impl/cloud/ChainedServiceDiscoveryTest.java new file mode 100644 index 0000000..91377aa --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/impl/cloud/ChainedServiceDiscoveryTest.java @@ -0,0 +1,71 @@ +/** + * 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.impl.cloud; + +import java.util.Arrays; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.model.cloud.ChainedServiceCallServiceDiscoveryConfiguration; +import org.apache.camel.model.cloud.StaticServiceCallServiceDiscoveryConfiguration; +import org.junit.Assert; +import org.junit.Test; + +public class ChainedServiceDiscoveryTest extends ContextTestSupport { + @Test + public void testMultiServiceDiscovery() throws Exception { + StaticServiceDiscovery discovery1 = new StaticServiceDiscovery(); + discovery1.addServer(new DefaultServiceDefinition("discovery1", "localhost", 1111)); + discovery1.addServer(new DefaultServiceDefinition("discovery1", "localhost", 1112)); + + StaticServiceDiscovery discovery2 = new StaticServiceDiscovery(); + discovery2.addServer(new DefaultServiceDefinition("discovery1", "localhost", 1113)); + discovery2.addServer(new DefaultServiceDefinition("discovery2", "localhost", 1114)); + + ChainedServiceDiscovery discovery = ChainedServiceDiscovery.wrap(discovery1, discovery2); + Assert.assertEquals(3, discovery.getUpdatedListOfServices("discovery1").size()); + Assert.assertEquals(1, discovery.getUpdatedListOfServices("discovery2").size()); + } + + @Test + public void testMultiServiceDiscoveryConfiguration() throws Exception { + StaticServiceCallServiceDiscoveryConfiguration staticConf1 = new StaticServiceCallServiceDiscoveryConfiguration(); + staticConf1.setServers(Arrays.asList("discovery1@localhost:1111", "discovery1@localhost:1112")); + + StaticServiceCallServiceDiscoveryConfiguration staticConf2 = new StaticServiceCallServiceDiscoveryConfiguration(); + staticConf2.setServers(Arrays.asList("discovery1@localhost:1113", "discovery2@localhost:1114")); + + ChainedServiceCallServiceDiscoveryConfiguration multiConf = new ChainedServiceCallServiceDiscoveryConfiguration(); + multiConf.setServiceDiscoveryConfigurations(Arrays.asList(staticConf1, staticConf2)); + + ChainedServiceDiscovery discovery = (ChainedServiceDiscovery)multiConf.newInstance(context); + Assert.assertEquals(2, discovery.getDelegates().size()); + Assert.assertEquals(3, discovery.getUpdatedListOfServices("discovery1").size()); + Assert.assertEquals(1, discovery.getUpdatedListOfServices("discovery2").size()); + } + + @Test + public void testMultiServiceDiscoveryConfigurationDsl() throws Exception { + ChainedServiceCallServiceDiscoveryConfiguration multiConf = new ChainedServiceCallServiceDiscoveryConfiguration(); + multiConf.staticServiceDiscovery().setServers(Arrays.asList("discovery1@localhost:1111", "discovery1@localhost:1112")); + multiConf.staticServiceDiscovery().setServers(Arrays.asList("discovery1@localhost:1113", "discovery2@localhost:1114")); + + ChainedServiceDiscovery discovery = (ChainedServiceDiscovery)multiConf.newInstance(context); + Assert.assertEquals(2, discovery.getDelegates().size()); + Assert.assertEquals(3, discovery.getUpdatedListOfServices("discovery1").size()); + Assert.assertEquals(1, discovery.getUpdatedListOfServices("discovery2").size()); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/0aae1257/camel-core/src/test/java/org/apache/camel/impl/cloud/ChainedServiceFilterTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/impl/cloud/ChainedServiceFilterTest.java b/camel-core/src/test/java/org/apache/camel/impl/cloud/ChainedServiceFilterTest.java new file mode 100644 index 0000000..dc99d11 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/impl/cloud/ChainedServiceFilterTest.java @@ -0,0 +1,66 @@ +/** + * 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.impl.cloud; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.cloud.ServiceDefinition; +import org.apache.camel.model.cloud.ChainedServiceCallServiceFilterConfiguration; +import org.junit.Assert; +import org.junit.Test; + +public class ChainedServiceFilterTest extends ContextTestSupport { + @Test + public void testMultiServiceFilterConfiguration() throws Exception { + ChainedServiceCallServiceFilterConfiguration conf = + new ChainedServiceCallServiceFilterConfiguration() + .healthy() + .passThrough(); + + ChainedServiceFilter filter = (ChainedServiceFilter)conf.newInstance(context); + Assert.assertEquals(2, filter.getDelegates().size()); + Assert.assertTrue(filter.getDelegates().get(0) instanceof HealthyServiceFilter); + Assert.assertTrue(filter.getDelegates().get(1) instanceof PassThroughServiceFilter); + } + + + @Test + public void testMultiServiceFilter() throws Exception { + ChainedServiceCallServiceFilterConfiguration conf = + new ChainedServiceCallServiceFilterConfiguration() + .healthy() + .custom(services -> services.stream().filter(s -> s.getPort() < 2000).collect(Collectors.toList()) + ); + + List<ServiceDefinition> services = conf.newInstance(context).apply(Arrays.asList( + new DefaultServiceDefinition("no-name", "127.0.0.1", 1000), + new DefaultServiceDefinition("no-name", "127.0.0.1", 1001, new DefaultServiceHealth(false)), + new DefaultServiceDefinition("no-name", "127.0.0.1", 1002, new DefaultServiceHealth(true)), + new DefaultServiceDefinition("no-name", "127.0.0.1", 2001, new DefaultServiceHealth(true)), + new DefaultServiceDefinition("no-name", "127.0.0.1", 2001, new DefaultServiceHealth(false)) + )); + + Assert.assertEquals(2, services.size()); + Assert.assertFalse(services.stream().anyMatch(s -> s.getHealth().isHealthy() == false)); + Assert.assertFalse(services.stream().anyMatch(s -> s.getPort() > 2000)); + Assert.assertTrue(services.stream().anyMatch(s -> s.getPort() == 1000)); + Assert.assertTrue(services.stream().anyMatch(s -> s.getPort() == 1002)); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/0aae1257/camel-core/src/test/java/org/apache/camel/impl/cloud/LoadBalancerTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/impl/cloud/LoadBalancerTest.java b/camel-core/src/test/java/org/apache/camel/impl/cloud/LoadBalancerTest.java new file mode 100644 index 0000000..56cf63f --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/impl/cloud/LoadBalancerTest.java @@ -0,0 +1,42 @@ +/** + * 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.impl.cloud; + +import java.util.stream.Collectors; + +import org.apache.camel.ContextTestSupport; +import org.junit.Test; + +public class LoadBalancerTest extends ContextTestSupport { + @Test + public void testLoadBalancer() throws Exception { + StaticServiceDiscovery serviceDiscovery = new StaticServiceDiscovery(); + serviceDiscovery.addServer("no-name", "127.0.0.1", 2001); + serviceDiscovery.addServer("no-name", "127.0.0.1", 2002); + serviceDiscovery.addServer("no-name", "127.0.0.1", 1001); + serviceDiscovery.addServer("no-name", "127.0.0.1", 1002); + + DefaultLoadBalancer loadBalancer = new DefaultLoadBalancer(); + loadBalancer.setCamelContext(context); + loadBalancer.setServiceDiscovery(serviceDiscovery); + loadBalancer.setServiceFilter(services -> services.stream().filter(s -> s.getPort() < 2000).collect(Collectors.toList())); + loadBalancer.setServiceChooser(new RoundRobinServiceChooser()); + loadBalancer.process("no-name", service -> { assertEquals(1001, service.getPort()); return false; }); + loadBalancer.process("no-name", service -> { assertEquals(1002, service.getPort()); return false; }); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/0aae1257/camel-core/src/test/java/org/apache/camel/impl/cloud/MultiServiceDiscoveryTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/impl/cloud/MultiServiceDiscoveryTest.java b/camel-core/src/test/java/org/apache/camel/impl/cloud/MultiServiceDiscoveryTest.java deleted file mode 100644 index 108c5cd..0000000 --- a/camel-core/src/test/java/org/apache/camel/impl/cloud/MultiServiceDiscoveryTest.java +++ /dev/null @@ -1,71 +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.impl.cloud; - -import java.util.Arrays; - -import org.apache.camel.ContextTestSupport; -import org.apache.camel.model.cloud.MultiServiceCallServiceDiscoveryConfiguration; -import org.apache.camel.model.cloud.StaticServiceCallServiceDiscoveryConfiguration; -import org.junit.Assert; -import org.junit.Test; - -public class MultiServiceDiscoveryTest extends ContextTestSupport { - @Test - public void testCachingServiceDiscovery() throws Exception { - StaticServiceDiscovery discovery1 = new StaticServiceDiscovery(); - discovery1.addServer(new DefaultServiceDefinition("discovery1", "localhost", 1111)); - discovery1.addServer(new DefaultServiceDefinition("discovery1", "localhost", 1112)); - - StaticServiceDiscovery discovery2 = new StaticServiceDiscovery(); - discovery2.addServer(new DefaultServiceDefinition("discovery1", "localhost", 1113)); - discovery2.addServer(new DefaultServiceDefinition("discovery2", "localhost", 1114)); - - MultiServiceDiscovery discovery = MultiServiceDiscovery.wrap(discovery1, discovery2); - Assert.assertEquals(3, discovery.getUpdatedListOfServices("discovery1").size()); - Assert.assertEquals(1, discovery.getUpdatedListOfServices("discovery2").size()); - } - - @Test - public void testCachingServiceDiscoveryConfiguration() throws Exception { - StaticServiceCallServiceDiscoveryConfiguration staticConf1 = new StaticServiceCallServiceDiscoveryConfiguration(); - staticConf1.setServers(Arrays.asList("discovery1@localhost:1111", "discovery1@localhost:1112")); - - StaticServiceCallServiceDiscoveryConfiguration staticConf2 = new StaticServiceCallServiceDiscoveryConfiguration(); - staticConf2.setServers(Arrays.asList("discovery1@localhost:1113", "discovery2@localhost:1114")); - - MultiServiceCallServiceDiscoveryConfiguration multiConf = new MultiServiceCallServiceDiscoveryConfiguration(); - multiConf.setServiceDiscoveryConfigurations(Arrays.asList(staticConf1, staticConf2)); - - MultiServiceDiscovery discovery = (MultiServiceDiscovery)multiConf.newInstance(context); - Assert.assertEquals(2, discovery.getDelegates().size()); - Assert.assertEquals(3, discovery.getUpdatedListOfServices("discovery1").size()); - Assert.assertEquals(1, discovery.getUpdatedListOfServices("discovery2").size()); - } - - @Test - public void testCachingServiceDiscoveryConfigurationDsl() throws Exception { - MultiServiceCallServiceDiscoveryConfiguration multiConf = new MultiServiceCallServiceDiscoveryConfiguration(); - multiConf.staticServiceDiscovery().setServers(Arrays.asList("discovery1@localhost:1111", "discovery1@localhost:1112")); - multiConf.staticServiceDiscovery().setServers(Arrays.asList("discovery1@localhost:1113", "discovery2@localhost:1114")); - - MultiServiceDiscovery discovery = (MultiServiceDiscovery)multiConf.newInstance(context); - Assert.assertEquals(2, discovery.getDelegates().size()); - Assert.assertEquals(3, discovery.getUpdatedListOfServices("discovery1").size()); - Assert.assertEquals(1, discovery.getUpdatedListOfServices("discovery2").size()); - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/0aae1257/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/cloud/RibbonLoadBalancer.java ---------------------------------------------------------------------- diff --git a/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/cloud/RibbonLoadBalancer.java b/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/cloud/RibbonLoadBalancer.java index eb23c1b..b08901d 100644 --- a/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/cloud/RibbonLoadBalancer.java +++ b/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/cloud/RibbonLoadBalancer.java @@ -31,7 +31,6 @@ import com.netflix.loadbalancer.ILoadBalancer; import com.netflix.loadbalancer.PollingServerListUpdater; import com.netflix.loadbalancer.RoundRobinRule; import com.netflix.loadbalancer.ServerList; -import com.netflix.loadbalancer.ServerListFilter; import com.netflix.loadbalancer.ZoneAwareLoadBalancer; import org.apache.camel.CamelContext; import org.apache.camel.CamelContextAware; @@ -162,32 +161,40 @@ public class RibbonLoadBalancer config, configuration.getRuleOrDefault(RoundRobinRule::new), configuration.getPingOrDefault(DummyPing::new), - new RibbonServerList(serviceName, serviceDiscovery), - new RibbonServerFilter(serviceFilter), + new RibbonServerList(serviceName, serviceDiscovery, serviceFilter), + null, new PollingServerListUpdater(config)); } static final class RibbonServerList implements ServerList<RibbonServiceDefinition> { private final String serviceName; private final ServiceDiscovery serviceDiscovery; + private final ServiceFilter serviceFilter; - RibbonServerList(String serviceName, ServiceDiscovery serviceDiscovery) { + RibbonServerList(String serviceName, ServiceDiscovery serviceDiscovery, ServiceFilter serviceFilter) { this.serviceName = serviceName; this.serviceDiscovery = serviceDiscovery; + this.serviceFilter = serviceFilter; } @Override public List<RibbonServiceDefinition> getInitialListOfServers() { - return asRibbonServerList( - serviceDiscovery.getInitialListOfServices(serviceName) - ); + List<ServiceDefinition> services = serviceDiscovery.getInitialListOfServices(serviceName); + if (serviceFilter != null) { + services = serviceFilter.apply(services); + } + + return asRibbonServerList(services); } @Override public List<RibbonServiceDefinition> getUpdatedListOfServers() { - return asRibbonServerList( - serviceDiscovery.getUpdatedListOfServices(serviceName) - ); + List<ServiceDefinition> services = serviceDiscovery.getUpdatedListOfServices(serviceName); + if (serviceFilter != null) { + services = serviceFilter.apply(services); + } + + return asRibbonServerList(services); } private List<RibbonServiceDefinition> asRibbonServerList(List<ServiceDefinition> services) { @@ -216,16 +223,4 @@ public class RibbonLoadBalancer return ribbonServers; } } - - static final class RibbonServerFilter implements ServerListFilter<RibbonServiceDefinition> { - private final ServiceFilter serviceFilter; - - RibbonServerFilter(ServiceFilter serviceFilter) { - this.serviceFilter = serviceFilter; - } - - public List<RibbonServiceDefinition> getFilteredListOfServers(List<RibbonServiceDefinition> servers) { - return serviceFilter.apply(servers); - } - } } http://git-wip-us.apache.org/repos/asf/camel/blob/0aae1257/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/RibbonServerListTest.java ---------------------------------------------------------------------- diff --git a/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/RibbonServerListTest.java b/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/RibbonServerListTest.java index fcc4f6b..f0d5dc3 100644 --- a/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/RibbonServerListTest.java +++ b/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/RibbonServerListTest.java @@ -17,15 +17,11 @@ package org.apache.camel.component.ribbon.cloud; -import java.util.ArrayList; -import java.util.List; - import com.netflix.loadbalancer.LoadBalancerBuilder; import com.netflix.loadbalancer.RoundRobinRule; import com.netflix.loadbalancer.Server; import com.netflix.loadbalancer.ZoneAwareLoadBalancer; -import org.apache.camel.cloud.ServiceDefinition; -import org.apache.camel.cloud.ServiceDiscovery; +import org.apache.camel.impl.cloud.PassThroughServiceFilter; import org.apache.camel.impl.cloud.StaticServiceDiscovery; import org.junit.Test; @@ -34,14 +30,14 @@ import static org.junit.Assert.assertEquals; public class RibbonServerListTest { @Test public void testFixedServerList() throws Exception { - List<ServiceDefinition> servers = new ArrayList<>(); - servers.add(new RibbonServiceDefinition("unknown", "localhost", 9090)); - servers.add(new RibbonServiceDefinition("unknown", "localhost", 9091)); - - ServiceDiscovery list = new StaticServiceDiscovery(servers); - ZoneAwareLoadBalancer<RibbonServiceDefinition> lb = LoadBalancerBuilder.<RibbonServiceDefinition>newBuilder() - .withDynamicServerList(new RibbonLoadBalancer.RibbonServerList("unknown", list)) + .withDynamicServerList(new RibbonLoadBalancer.RibbonServerList( + "unknown", + StaticServiceDiscovery.forServices( + new RibbonServiceDefinition("unknown", "localhost", 9090), + new RibbonServiceDefinition("unknown", "localhost", 9091) + ), + PassThroughServiceFilter.INSTANCE)) .withRule(new RoundRobinRule()) .buildDynamicServerListLoadBalancer(); http://git-wip-us.apache.org/repos/asf/camel/blob/0aae1257/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/ServiceCallConfigurationProperties.java ---------------------------------------------------------------------- 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 index dd1124e..feeeb0d 100644 --- 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 @@ -89,6 +89,7 @@ public class ServiceCallConfigurationProperties { public static class ServiceFilter { private boolean enabled = true; + private Map<String, String> blacklist = new HashMap<>(); public boolean isEnabled() { return enabled; @@ -97,6 +98,10 @@ public class ServiceCallConfigurationProperties { public void setEnabled(boolean enabled) { this.enabled = enabled; } + + public Map<String, String> getBlacklist() { + return blacklist; + } } public static class ServiceChooser { http://git-wip-us.apache.org/repos/asf/camel/blob/0aae1257/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/ServiceCallServiceFilterAutoConfiguration.java ---------------------------------------------------------------------- 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/ServiceCallServiceFilterAutoConfiguration.java index d97c7d1..cdcffc6 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/ServiceCallServiceFilterAutoConfiguration.java @@ -17,10 +17,14 @@ package org.apache.camel.spring.cloud; +import java.util.Map; + import org.apache.camel.cloud.ServiceFilter; -import org.apache.camel.impl.cloud.AllServiceFilter; +import org.apache.camel.impl.cloud.BlacklistServiceFilter; +import org.apache.camel.impl.cloud.ChainedServiceFilter; import org.apache.camel.impl.cloud.HealthyServiceFilter; import org.apache.camel.spring.boot.util.GroupCondition; +import org.apache.camel.util.StringHelper; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; @@ -33,18 +37,24 @@ import org.springframework.context.annotation.Scope; public class ServiceCallServiceFilterAutoConfiguration { @Lazy @Scope("prototype") - @Bean(name = "service-filter-healthy") + @Bean(name = "service-filter-chained") @Conditional(ServiceCallServiceFilterAutoConfiguration.ServiceFilterCondition.class) - public ServiceFilter healthyServiceFilter() { - return new HealthyServiceFilter(); - } + public ServiceFilter chainedServiceFilter(ServiceCallConfigurationProperties properties) { + BlacklistServiceFilter blacklist = new BlacklistServiceFilter(); - @Lazy - @Scope("prototype") - @Bean(name = "service-filter-all") - @Conditional(ServiceCallServiceFilterAutoConfiguration.ServiceFilterCondition.class) - public ServiceFilter allServiceFilter() { - return new AllServiceFilter(); + Map<String, String> services = properties.getServiceFilter().getBlacklist(); + for (Map.Entry<String, String> entry : services.entrySet()) { + + String[] parts = entry.getValue().split(","); + for (String part : parts) { + String host = StringHelper.before(part, ":"); + String port = StringHelper.after(part, ":"); + + blacklist.addServer(entry.getKey(), host, Integer.parseInt(port)); + } + } + + return ChainedServiceFilter.wrap(new HealthyServiceFilter(), blacklist); } public static class ServiceFilterCondition extends GroupCondition { http://git-wip-us.apache.org/repos/asf/camel/blob/0aae1257/components/camel-spring-cloud/src/test/java/org/apache/camel/spring/cloud/CamelCloudServiceCallTest.java ---------------------------------------------------------------------- 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 5e26e74..c22b029 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 @@ -42,7 +42,8 @@ import org.springframework.test.context.junit4.SpringRunner; }, properties = { "camel.cloud.servicecall.load-balancer.enabled=false", - "camel.cloud.servicecall.service-discovery.services[custom-svc-list]=localhost:9090,localhost:9091", + "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", "ribbon.enabled=false", "debug=false" } @@ -54,7 +55,7 @@ public class CamelCloudServiceCallTest { @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)); } // ************************** @@ -71,12 +72,16 @@ public class CamelCloudServiceCallTest { 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"); } }; } http://git-wip-us.apache.org/repos/asf/camel/blob/0aae1257/components/camel-spring/src/test/java/org/apache/camel/spring/cloud/ServiceCallConfigurationTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/test/java/org/apache/camel/spring/cloud/ServiceCallConfigurationTest.java b/components/camel-spring/src/test/java/org/apache/camel/spring/cloud/ServiceCallConfigurationTest.java index 97cac01..b1adfee 100644 --- a/components/camel-spring/src/test/java/org/apache/camel/spring/cloud/ServiceCallConfigurationTest.java +++ b/components/camel-spring/src/test/java/org/apache/camel/spring/cloud/ServiceCallConfigurationTest.java @@ -16,7 +16,10 @@ */ package org.apache.camel.spring.cloud; -import org.apache.camel.model.cloud.MultiServiceCallServiceDiscoveryConfiguration; +import org.apache.camel.model.cloud.BlacklistServiceCallServiceFilterConfiguration; +import org.apache.camel.model.cloud.ChainedServiceCallServiceDiscoveryConfiguration; +import org.apache.camel.model.cloud.ChainedServiceCallServiceFilterConfiguration; +import org.apache.camel.model.cloud.HealthyServiceCallServiceFilterConfiguration; import org.apache.camel.model.cloud.ServiceCallConfigurationDefinition; import org.apache.camel.model.cloud.StaticServiceCallServiceDiscoveryConfiguration; import org.apache.camel.spring.SpringCamelContext; @@ -44,7 +47,7 @@ public class ServiceCallConfigurationTest { assertNotNull("No ServiceCallConfiguration (2)", conf2); assertNotNull("No ServiceDiscoveryConfiguration (2)", conf2.getServiceDiscoveryConfiguration()); - MultiServiceCallServiceDiscoveryConfiguration discovery2 = (MultiServiceCallServiceDiscoveryConfiguration)conf2.getServiceDiscoveryConfiguration(); + ChainedServiceCallServiceDiscoveryConfiguration discovery2 = (ChainedServiceCallServiceDiscoveryConfiguration)conf2.getServiceDiscoveryConfiguration(); assertEquals(2, discovery2.getServiceDiscoveryConfigurations().size()); assertTrue(discovery2.getServiceDiscoveryConfigurations().get(0) instanceof StaticServiceCallServiceDiscoveryConfiguration); assertTrue(discovery2.getServiceDiscoveryConfigurations().get(1) instanceof StaticServiceCallServiceDiscoveryConfiguration); @@ -55,7 +58,12 @@ public class ServiceCallConfigurationTest { StaticServiceCallServiceDiscoveryConfiguration sconf2 = (StaticServiceCallServiceDiscoveryConfiguration)discovery2.getServiceDiscoveryConfigurations().get(1); assertEquals(1, sconf2.getServers().size()); - assertEquals("localhost:9093,localhost:9094", sconf2.getServers().get(0)); + assertEquals("localhost:9093,localhost:9094,localhost:9095,localhost:9096", sconf2.getServers().get(0)); + + ChainedServiceCallServiceFilterConfiguration filter = (ChainedServiceCallServiceFilterConfiguration)conf2.getServiceFilterConfiguration(); + assertEquals(2, filter.getServiceFilterConfigurations().size()); + assertTrue(filter.getServiceFilterConfigurations().get(0) instanceof HealthyServiceCallServiceFilterConfiguration); + assertTrue(filter.getServiceFilterConfigurations().get(1) instanceof BlacklistServiceCallServiceFilterConfiguration); } protected SpringCamelContext createContext(String classpathConfigFile) { http://git-wip-us.apache.org/repos/asf/camel/blob/0aae1257/components/camel-spring/src/test/java/org/apache/camel/spring/cloud/ServiceCallFilterTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/test/java/org/apache/camel/spring/cloud/ServiceCallFilterTest.java b/components/camel-spring/src/test/java/org/apache/camel/spring/cloud/ServiceCallFilterTest.java new file mode 100644 index 0000000..442868b --- /dev/null +++ b/components/camel-spring/src/test/java/org/apache/camel/spring/cloud/ServiceCallFilterTest.java @@ -0,0 +1,69 @@ +/** + * 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.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.impl.cloud.ServiceCallConstants; +import org.apache.camel.spring.SpringTestSupport; +import org.junit.Assert; +import org.junit.Test; +import org.springframework.context.support.AbstractXmlApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.test.annotation.DirtiesContext; + +@DirtiesContext +public class ServiceCallFilterTest extends SpringTestSupport { + @Test + public void testServiceFilter() throws Exception { + Exchange result; + + result = template.request("direct:start", emptyProcessor()); + assertHeader(result, ServiceCallConstants.SERVICE_HOST, "host1"); + assertHeader(result, ServiceCallConstants.SERVICE_PORT, 9093); + + result = template.request("direct:start", emptyProcessor()); + assertHeader(result, ServiceCallConstants.SERVICE_HOST, "host4"); + assertHeader(result, ServiceCallConstants.SERVICE_PORT, 9094); + } + + // ********************* + // Helpers + // ********************* + + private void assertHeader(Exchange exchange, String header, Object expectedValue) { + Assert.assertNotNull(exchange); + Assert.assertTrue(exchange.getIn().getHeaders().containsKey(header)); + Assert.assertEquals(expectedValue, exchange.getIn().getHeader(header)); + } + + private Processor emptyProcessor() { + return e -> { + return; + }; + } + + // ********************* + // Application Context + // ********************* + + @Override + protected AbstractXmlApplicationContext createApplicationContext() { + return new ClassPathXmlApplicationContext("org/apache/camel/spring/cloud/ServiceCallFilterTest.xml"); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/0aae1257/components/camel-spring/src/test/resources/org/apache/camel/spring/cloud/ServiceCallConfigurationTest.xml ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/test/resources/org/apache/camel/spring/cloud/ServiceCallConfigurationTest.xml b/components/camel-spring/src/test/resources/org/apache/camel/spring/cloud/ServiceCallConfigurationTest.xml index d54348c..2ad4e54 100644 --- a/components/camel-spring/src/test/resources/org/apache/camel/spring/cloud/ServiceCallConfigurationTest.xml +++ b/components/camel-spring/src/test/resources/org/apache/camel/spring/cloud/ServiceCallConfigurationTest.xml @@ -32,14 +32,20 @@ </serviceCallConfiguration> <serviceCallConfiguration id="conf2"> - <multiServiceDiscovery> + <chainedServiceDiscovery> <staticServiceDiscovery> <servers>localhost:9092</servers> </staticServiceDiscovery> <staticServiceDiscovery> - <servers>localhost:9093,localhost:9094</servers> + <servers>localhost:9093,localhost:9094,localhost:9095,localhost:9096</servers> </staticServiceDiscovery> - </multiServiceDiscovery> + </chainedServiceDiscovery> + <chainedServiceFilter> + <healthyServiceFilter/> + <blacklistServiceFilter> + <servers>localhost:9095,localhost:9096</servers> + </blacklistServiceFilter> + </chainedServiceFilter> </serviceCallConfiguration> <route id="test1"> http://git-wip-us.apache.org/repos/asf/camel/blob/0aae1257/components/camel-spring/src/test/resources/org/apache/camel/spring/cloud/ServiceCallFilterTest.xml ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/test/resources/org/apache/camel/spring/cloud/ServiceCallFilterTest.xml b/components/camel-spring/src/test/resources/org/apache/camel/spring/cloud/ServiceCallFilterTest.xml new file mode 100644 index 0000000..1e4fe80 --- /dev/null +++ b/components/camel-spring/src/test/resources/org/apache/camel/spring/cloud/ServiceCallFilterTest.xml @@ -0,0 +1,53 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ 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. + --> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans + http://www.springframework.org/schema/beans/spring-beans.xsd + http://camel.apache.org/schema/spring + http://camel.apache.org/schema/spring/camel-spring.xsd"> + + <camelContext xmlns="http://camel.apache.org/schema/spring"> + + <interceptSendToEndpoint uri="file:*" skipSendToOriginalEndpoint="true"> + <to uri="mock:result"/> + </interceptSendToEndpoint> + + <route id="test"> + <from uri="direct:start"/> + <serviceCall name="test" component="file"> + + <staticServiceDiscovery> + <servers>test@host1:9093</servers> + <servers>test@host2:9093</servers> + <servers>unknown@host1:9094</servers> + <servers>test@host3:9093</servers> + <servers>test@host4:9094</servers> + </staticServiceDiscovery> + + <blacklistServiceFilter> + <servers>test@host2:9093</servers> + <servers>test@host3:9093</servers> + </blacklistServiceFilter> + </serviceCall> + + </route> + </camelContext> + +</beans> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/0aae1257/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/cloud/ServiceCallFilterTest.java ---------------------------------------------------------------------- diff --git a/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/cloud/ServiceCallFilterTest.java b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/cloud/ServiceCallFilterTest.java new file mode 100644 index 0000000..5c2a505 --- /dev/null +++ b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/cloud/ServiceCallFilterTest.java @@ -0,0 +1,58 @@ +/** + * 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.test.blueprint.cloud; + +import org.apache.camel.Exchange; +import org.apache.camel.impl.cloud.ServiceCallConstants; +import org.apache.camel.test.blueprint.CamelBlueprintTestSupport; +import org.junit.Assert; +import org.junit.Test; + +public class ServiceCallFilterTest extends CamelBlueprintTestSupport { + @Test + public void testServiceFilter() throws Exception { + Exchange result; + + result = template.request("direct:start", e -> { return; } ); + assertHeader(result, ServiceCallConstants.SERVICE_HOST, "host1"); + assertHeader(result, ServiceCallConstants.SERVICE_PORT, 9093); + + result = template.request("direct:start", e -> { return; } ); + assertHeader(result, ServiceCallConstants.SERVICE_HOST, "host4"); + assertHeader(result, ServiceCallConstants.SERVICE_PORT, 9094); + } + + // ********************* + // Helpers + // ********************* + + private void assertHeader(Exchange exchange, String header, Object expectedValue) { + Assert.assertNotNull(exchange); + Assert.assertTrue(exchange.getIn().getHeaders().containsKey(header)); + Assert.assertEquals(expectedValue, exchange.getIn().getHeader(header)); + } + + // ********************* + // Blueprint + // ********************* + + @Override + protected String getBlueprintDescriptor() { + return "org/apache/camel/test/blueprint/cloud/ServiceCallFilterTest.xml"; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/0aae1257/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/cloud/ServiceCallFilterTest.xml ---------------------------------------------------------------------- diff --git a/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/cloud/ServiceCallFilterTest.xml b/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/cloud/ServiceCallFilterTest.xml new file mode 100644 index 0000000..d3eb9b5 --- /dev/null +++ b/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/cloud/ServiceCallFilterTest.xml @@ -0,0 +1,52 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ 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. + --> +<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://www.osgi.org/xmlns/blueprint/v1.0.0 + https://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"> + + <camelContext xmlns="http://camel.apache.org/schema/blueprint"> + + <interceptSendToEndpoint uri="file:*" skipSendToOriginalEndpoint="true"> + <to uri="mock:result"/> + </interceptSendToEndpoint> + + <route id="test"> + <from uri="direct:start"/> + <serviceCall name="test" component="file"> + + <staticServiceDiscovery> + <servers>test@host1:9093</servers> + <servers>test@host2:9093</servers> + <servers>unknown@host1:9094</servers> + <servers>test@host3:9093</servers> + <servers>test@host4:9094</servers> + </staticServiceDiscovery> + + <blacklistServiceFilter> + <servers>test@host2:9093</servers> + <servers>test@host3:9093</servers> + </blacklistServiceFilter> + </serviceCall> + + </route> + + </camelContext> + +</blueprint> \ No newline at end of file