http://git-wip-us.apache.org/repos/asf/camel/blob/ecc81927/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallServiceLoadBalancerConfiguration.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallServiceLoadBalancerConfiguration.java b/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallServiceLoadBalancerConfiguration.java new file mode 100644 index 0000000..5c25f10 --- /dev/null +++ b/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallServiceLoadBalancerConfiguration.java @@ -0,0 +1,189 @@ +/** + * 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.model.cloud; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlTransient; + +import org.apache.camel.CamelContext; +import org.apache.camel.NoFactoryAvailableException; +import org.apache.camel.cloud.ServiceLoadBalancer; +import org.apache.camel.cloud.ServiceLoadBalancerFactory; +import org.apache.camel.model.IdentifiedType; +import org.apache.camel.model.ProcessorDefinition; +import org.apache.camel.model.PropertyDefinition; +import org.apache.camel.spi.Metadata; +import org.apache.camel.util.CamelContextHelper; +import org.apache.camel.util.IntrospectionSupport; +import org.apache.camel.util.ObjectHelper; + +@Metadata(label = "routing,cloud,load-balancing") +@XmlRootElement(name = "loadBalancerConfiguration") +@XmlAccessorType(XmlAccessType.FIELD) +public class ServiceCallServiceLoadBalancerConfiguration extends IdentifiedType implements ServiceLoadBalancerFactory { + private static final String RESOURCE_PATH = "META-INF/services/org/apache/camel/cloud/"; + + @XmlTransient + private final ServiceCallDefinition parent; + @XmlTransient + private final String factoryKey; + @XmlElement(name = "properties") @Metadata(label = "advanced") + private List<PropertyDefinition> properties; + + public ServiceCallServiceLoadBalancerConfiguration() { + this(null, null); + } + + public ServiceCallServiceLoadBalancerConfiguration(ServiceCallDefinition parent, String factoryKey) { + this.parent = parent; + this.factoryKey = factoryKey; + } + + public ServiceCallDefinition end() { + return this.parent; + } + + public ProcessorDefinition<?> endParent() { + return this.parent.end(); + } + + // ************************************************************************* + // + // ************************************************************************* + + public List<PropertyDefinition> getProperties() { + return properties; + } + + /** + * Set client properties to use. + * <p/> + * These properties are specific to what service call implementation are in + * use. For example if using ribbon, then the client properties are define + * in com.netflix.client.config.CommonClientConfigKey. + */ + public void setProperties(List<PropertyDefinition> properties) { + this.properties = properties; + } + + /** + * Adds a custom property to use. + * <p/> + * These properties are specific to what service call implementation are in + * use. For example if using ribbon, then the client properties are define + * in com.netflix.client.config.CommonClientConfigKey. + */ + public ServiceCallServiceLoadBalancerConfiguration property(String key, String value) { + if (properties == null) { + properties = new ArrayList<>(); + } + PropertyDefinition prop = new PropertyDefinition(); + prop.setKey(key); + prop.setValue(value); + properties.add(prop); + return this; + } + + protected Map<String, String> getPropertiesAsMap(CamelContext camelContext) throws Exception { + Map<String, String> answer; + + if (properties == null || properties.isEmpty()) { + answer = Collections.emptyMap(); + } else { + answer = new HashMap<>(); + for (PropertyDefinition prop : properties) { + // support property placeholders + String key = CamelContextHelper.parseText(camelContext, prop.getKey()); + String value = CamelContextHelper.parseText(camelContext, prop.getValue()); + answer.put(key, value); + } + } + + return answer; + } + + // ************************************************************************* + // Factory + // ************************************************************************* + + @Override + public ServiceLoadBalancer newInstance(CamelContext camelContext) throws Exception { + ObjectHelper.notNull(factoryKey, "LoadBalancer factoryKey"); + + ServiceLoadBalancer answer; + + // First try to find the factory from the registry. + ServiceLoadBalancerFactory factory = CamelContextHelper.lookup(camelContext, factoryKey, ServiceLoadBalancerFactory.class); + if (factory != null) { + // If a factory is found in the registry do not re-configure it as + // it should be pre-configured. + answer = factory.newInstance(camelContext); + } else { + + Class<?> type; + try { + // Then use Service factory. + type = camelContext.getFactoryFinder(RESOURCE_PATH).findClass(factoryKey); + } catch (Exception e) { + throw new NoFactoryAvailableException(RESOURCE_PATH + factoryKey, e); + } + + if (type != null) { + if (ServiceLoadBalancerFactory.class.isAssignableFrom(type)) { + factory = (ServiceLoadBalancerFactory) camelContext.getInjector().newInstance(type); + } else { + throw new IllegalArgumentException( + "Resolving LoadBalancer: " + factoryKey + " detected type conflict: Not a LoadBalancerFactory implementation. Found: " + type.getName()); + } + } + + try { + Map<String, Object> parameters = new HashMap<>(); + IntrospectionSupport.getProperties(this, parameters, null, false); + + // Convert properties to Map<String, String> + parameters.put("properties", getPropertiesAsMap(camelContext)); + + postProcessFactoryParameters(camelContext, parameters); + + IntrospectionSupport.setProperties(factory, parameters); + + answer = factory.newInstance(camelContext); + } catch (Exception e) { + throw new IllegalArgumentException(e); + } + } + + return answer; + } + + // ************************************************************************* + // Utilities + // ************************************************************************* + + protected void postProcessFactoryParameters(CamelContext camelContext, Map<String, Object> parameters) throws Exception { + } +} \ No newline at end of file
http://git-wip-us.apache.org/repos/asf/camel/blob/ecc81927/camel-core/src/main/resources/META-INF/services/org/apache/camel/cloud/default-load-balancer ---------------------------------------------------------------------- diff --git a/camel-core/src/main/resources/META-INF/services/org/apache/camel/cloud/default-load-balancer b/camel-core/src/main/resources/META-INF/services/org/apache/camel/cloud/default-load-balancer deleted file mode 100644 index f8c9ddb..0000000 --- a/camel-core/src/main/resources/META-INF/services/org/apache/camel/cloud/default-load-balancer +++ /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.DefaultLoadBalancerFactory \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/ecc81927/camel-core/src/main/resources/META-INF/services/org/apache/camel/cloud/default-service-load-balancer ---------------------------------------------------------------------- diff --git a/camel-core/src/main/resources/META-INF/services/org/apache/camel/cloud/default-service-load-balancer b/camel-core/src/main/resources/META-INF/services/org/apache/camel/cloud/default-service-load-balancer new file mode 100644 index 0000000..16644c8 --- /dev/null +++ b/camel-core/src/main/resources/META-INF/services/org/apache/camel/cloud/default-service-load-balancer @@ -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.DefaultServiceLoadBalancerFactory \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/ecc81927/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 5f5af59..fb41af7 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 @@ -19,7 +19,7 @@ ServiceCallConfigurationDefinition ServiceCallServiceDiscoveryConfiguration ServiceCallServiceFilterConfiguration ServiceCallServiceChooserConfiguration -ServiceCallLoadBalancerConfiguration +ServiceCallServiceLoadBalancerConfiguration BlacklistServiceCallServiceFilterConfiguration ChainedServiceCallServiceFilterConfiguration PassThroughServiceCallServiceFilterConfiguration @@ -31,4 +31,5 @@ HealthyServiceCallServiceFilterConfiguration KubernetesServiceCallServiceDiscoveryConfiguration AggregatingServiceCallServiceDiscoveryConfiguration StaticServiceCallServiceDiscoveryConfiguration -RibbonServiceCallLoadBalancerConfiguration +DefaultServiceCallServiceLoadBalancerConfiguration +RibbonServiceCallServiceLoadBalancerConfiguration http://git-wip-us.apache.org/repos/asf/camel/blob/ecc81927/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 index 5e17bfb..a78c291 100644 --- 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 @@ -30,7 +30,7 @@ public class LoadBalancerTest extends ContextTestSupport { serviceDiscovery.addServer("no-name", "127.0.0.1", 1001); serviceDiscovery.addServer("no-name", "127.0.0.1", 1002); - DefaultLoadBalancer loadBalancer = new DefaultLoadBalancer(); + DefaultServiceLoadBalancer loadBalancer = new DefaultServiceLoadBalancer(); loadBalancer.setCamelContext(context); loadBalancer.setServiceDiscovery(serviceDiscovery); loadBalancer.setServiceFilter(services -> services.stream().filter(s -> s.getPort() < 2000).collect(Collectors.toList())); http://git-wip-us.apache.org/repos/asf/camel/blob/ecc81927/camel-core/src/test/java/org/apache/camel/impl/cloud/ServiceCallConfigurationTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/impl/cloud/ServiceCallConfigurationTest.java b/camel-core/src/test/java/org/apache/camel/impl/cloud/ServiceCallConfigurationTest.java index e193c6e..155bcc0 100644 --- a/camel-core/src/test/java/org/apache/camel/impl/cloud/ServiceCallConfigurationTest.java +++ b/camel-core/src/test/java/org/apache/camel/impl/cloud/ServiceCallConfigurationTest.java @@ -68,9 +68,9 @@ public class ServiceCallConfigurationTest { DefaultServiceCallProcessor proc = findServiceCallProcessor(context.getRoute("default")); Assert.assertNotNull(proc); - Assert.assertTrue(proc.getLoadBalancer() instanceof DefaultLoadBalancer); + Assert.assertTrue(proc.getLoadBalancer() instanceof DefaultServiceLoadBalancer); - DefaultLoadBalancer loadBalancer = (DefaultLoadBalancer)proc.getLoadBalancer(); + DefaultServiceLoadBalancer loadBalancer = (DefaultServiceLoadBalancer)proc.getLoadBalancer(); Assert.assertEquals(sd, loadBalancer.getServiceDiscovery()); Assert.assertEquals(sf, loadBalancer.getServiceFilter()); @@ -111,9 +111,9 @@ public class ServiceCallConfigurationTest { DefaultServiceCallProcessor proc = findServiceCallProcessor(context.getRoute("default")); Assert.assertNotNull(proc); - Assert.assertTrue(proc.getLoadBalancer() instanceof DefaultLoadBalancer); + Assert.assertTrue(proc.getLoadBalancer() instanceof DefaultServiceLoadBalancer); - DefaultLoadBalancer loadBalancer = (DefaultLoadBalancer)proc.getLoadBalancer(); + DefaultServiceLoadBalancer loadBalancer = (DefaultServiceLoadBalancer)proc.getLoadBalancer(); Assert.assertEquals(sd, loadBalancer.getServiceDiscovery()); Assert.assertEquals(sf, loadBalancer.getServiceFilter()); @@ -155,9 +155,9 @@ public class ServiceCallConfigurationTest { DefaultServiceCallProcessor proc = findServiceCallProcessor(context.getRoute("default")); Assert.assertNotNull(proc); - Assert.assertTrue(proc.getLoadBalancer() instanceof DefaultLoadBalancer); + Assert.assertTrue(proc.getLoadBalancer() instanceof DefaultServiceLoadBalancer); - DefaultLoadBalancer loadBalancer = (DefaultLoadBalancer)proc.getLoadBalancer(); + DefaultServiceLoadBalancer loadBalancer = (DefaultServiceLoadBalancer)proc.getLoadBalancer(); Assert.assertEquals(sd, loadBalancer.getServiceDiscovery()); Assert.assertEquals(sf, loadBalancer.getServiceFilter()); @@ -236,9 +236,9 @@ public class ServiceCallConfigurationTest { DefaultServiceCallProcessor proc = findServiceCallProcessor(context.getRoute("default")); Assert.assertNotNull(proc); - Assert.assertTrue(proc.getLoadBalancer() instanceof DefaultLoadBalancer); + Assert.assertTrue(proc.getLoadBalancer() instanceof DefaultServiceLoadBalancer); - DefaultLoadBalancer loadBalancer = (DefaultLoadBalancer) proc.getLoadBalancer(); + DefaultServiceLoadBalancer loadBalancer = (DefaultServiceLoadBalancer) proc.getLoadBalancer(); Assert.assertEquals(defaultServiceDiscovery, loadBalancer.getServiceDiscovery()); Assert.assertEquals(defaultServiceFilter, loadBalancer.getServiceFilter()); } @@ -248,9 +248,9 @@ public class ServiceCallConfigurationTest { DefaultServiceCallProcessor proc = findServiceCallProcessor(context.getRoute("named")); Assert.assertNotNull(proc); - Assert.assertTrue(proc.getLoadBalancer() instanceof DefaultLoadBalancer); + Assert.assertTrue(proc.getLoadBalancer() instanceof DefaultServiceLoadBalancer); - DefaultLoadBalancer loadBalancer = (DefaultLoadBalancer) proc.getLoadBalancer(); + DefaultServiceLoadBalancer loadBalancer = (DefaultServiceLoadBalancer) proc.getLoadBalancer(); Assert.assertEquals(defaultServiceDiscovery, loadBalancer.getServiceDiscovery()); Assert.assertEquals(namedServiceFilter, loadBalancer.getServiceFilter()); } @@ -260,9 +260,9 @@ public class ServiceCallConfigurationTest { DefaultServiceCallProcessor proc = findServiceCallProcessor(context.getRoute("local")); Assert.assertNotNull(proc); - Assert.assertTrue(proc.getLoadBalancer() instanceof DefaultLoadBalancer); + Assert.assertTrue(proc.getLoadBalancer() instanceof DefaultServiceLoadBalancer); - DefaultLoadBalancer loadBalancer = (DefaultLoadBalancer) proc.getLoadBalancer(); + DefaultServiceLoadBalancer loadBalancer = (DefaultServiceLoadBalancer) proc.getLoadBalancer(); Assert.assertEquals(localServiceDiscovery, loadBalancer.getServiceDiscovery()); Assert.assertEquals(namedServiceFilter, loadBalancer.getServiceFilter()); } @@ -302,7 +302,7 @@ public class ServiceCallConfigurationTest { DefaultServiceCallProcessor proc = findServiceCallProcessor(context.getRoute("default")); Assert.assertNotNull(proc); - Assert.assertTrue(proc.getLoadBalancer() instanceof DefaultLoadBalancer); + Assert.assertTrue(proc.getLoadBalancer() instanceof DefaultServiceLoadBalancer); Assert.assertEquals("service-name", proc.getName()); Assert.assertEquals("file", proc.getScheme()); Assert.assertEquals("direct:service-name", proc.getUri()); http://git-wip-us.apache.org/repos/asf/camel/blob/ecc81927/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/SpringConsulDefaultServiceCallRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/SpringConsulDefaultServiceCallRouteTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/SpringConsulDefaultServiceCallRouteTest.java index 05b28d7..4627984 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/SpringConsulDefaultServiceCallRouteTest.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/SpringConsulDefaultServiceCallRouteTest.java @@ -17,6 +17,12 @@ package org.apache.camel.component.consul.cloud; +import java.util.List; + +import org.apache.camel.component.ribbon.cloud.RibbonServiceLoadBalancer; +import org.apache.camel.impl.cloud.DefaultServiceCallProcessor; +import org.junit.Assert; +import org.junit.Test; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; @@ -25,4 +31,14 @@ public class SpringConsulDefaultServiceCallRouteTest extends SpringConsulService protected AbstractApplicationContext createApplicationContext() { return new ClassPathXmlApplicationContext("org/apache/camel/component/consul/cloud/SpringConsulDefaultServiceCallRouteTest.xml"); } + + @Test + public void testServiceCallConfiguration() throws Exception { + List<DefaultServiceCallProcessor> processors = findServiceCallProcessors(); + + Assert.assertFalse(processors.isEmpty()); + Assert.assertEquals(2, processors.size()); + Assert.assertFalse(processors.get(0).getLoadBalancer() instanceof RibbonServiceLoadBalancer); + Assert.assertFalse(processors.get(1).getLoadBalancer() instanceof RibbonServiceLoadBalancer); + } } http://git-wip-us.apache.org/repos/asf/camel/blob/ecc81927/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/SpringConsulExpressionServiceCallRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/SpringConsulExpressionServiceCallRouteTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/SpringConsulExpressionServiceCallRouteTest.java index f7a4448..13cd719 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/SpringConsulExpressionServiceCallRouteTest.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/SpringConsulExpressionServiceCallRouteTest.java @@ -17,6 +17,12 @@ package org.apache.camel.component.consul.cloud; +import java.util.List; + +import org.apache.camel.component.ribbon.cloud.RibbonServiceLoadBalancer; +import org.apache.camel.impl.cloud.DefaultServiceCallProcessor; +import org.junit.Assert; +import org.junit.Test; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; @@ -25,4 +31,14 @@ public class SpringConsulExpressionServiceCallRouteTest extends SpringConsulServ protected AbstractApplicationContext createApplicationContext() { return new ClassPathXmlApplicationContext("org/apache/camel/component/consul/cloud/SpringConsulExpressionServiceCallRouteTest.xml"); } + + @Test + public void testServiceCallConfiguration() throws Exception { + List<DefaultServiceCallProcessor> processors = findServiceCallProcessors(); + + Assert.assertFalse(processors.isEmpty()); + Assert.assertEquals(2, processors.size()); + Assert.assertFalse(processors.get(0).getLoadBalancer() instanceof RibbonServiceLoadBalancer); + Assert.assertFalse(processors.get(1).getLoadBalancer() instanceof RibbonServiceLoadBalancer); + } } http://git-wip-us.apache.org/repos/asf/camel/blob/ecc81927/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/SpringConsulRibbonServiceCallRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/SpringConsulRibbonServiceCallRouteTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/SpringConsulRibbonServiceCallRouteTest.java index 70eebfb..f76e72e 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/SpringConsulRibbonServiceCallRouteTest.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/SpringConsulRibbonServiceCallRouteTest.java @@ -17,6 +17,12 @@ package org.apache.camel.component.consul.cloud; +import java.util.List; + +import org.apache.camel.component.ribbon.cloud.RibbonServiceLoadBalancer; +import org.apache.camel.impl.cloud.DefaultServiceCallProcessor; +import org.junit.Assert; +import org.junit.Test; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; @@ -25,4 +31,14 @@ public class SpringConsulRibbonServiceCallRouteTest extends SpringConsulServiceC protected AbstractApplicationContext createApplicationContext() { return new ClassPathXmlApplicationContext("org/apache/camel/component/consul/cloud/SpringConsulRibbonServiceCallRouteTest.xml"); } + + @Test + public void testServiceCallConfiguration() throws Exception { + List<DefaultServiceCallProcessor> processors = findServiceCallProcessors(); + + Assert.assertFalse(processors.isEmpty()); + Assert.assertEquals(2, processors.size()); + Assert.assertTrue(processors.get(0).getLoadBalancer() instanceof RibbonServiceLoadBalancer); + Assert.assertTrue(processors.get(1).getLoadBalancer() instanceof RibbonServiceLoadBalancer); + } } http://git-wip-us.apache.org/repos/asf/camel/blob/ecc81927/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/SpringConsulServiceCallRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/SpringConsulServiceCallRouteTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/SpringConsulServiceCallRouteTest.java index ac10b06..335c61e 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/SpringConsulServiceCallRouteTest.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/SpringConsulServiceCallRouteTest.java @@ -17,6 +17,7 @@ package org.apache.camel.component.consul.cloud; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -24,7 +25,14 @@ import com.orbitz.consul.AgentClient; import com.orbitz.consul.Consul; import com.orbitz.consul.model.agent.ImmutableRegistration; import com.orbitz.consul.model.agent.Registration; +import org.apache.camel.Navigate; +import org.apache.camel.Processor; +import org.apache.camel.Route; +import org.apache.camel.impl.cloud.DefaultServiceCallProcessor; +import org.apache.camel.processor.ChoiceProcessor; +import org.apache.camel.processor.FilterProcessor; import org.apache.camel.test.spring.CamelSpringTestSupport; +import org.junit.Assert; import org.junit.Test; public abstract class SpringConsulServiceCallRouteTest extends CamelSpringTestSupport { @@ -105,4 +113,33 @@ public abstract class SpringConsulServiceCallRouteTest extends CamelSpringTestSu assertMockEndpointsSatisfied(); } + + // ************************************ + // Helpers + // ************************************ + + protected List<DefaultServiceCallProcessor> findServiceCallProcessors() { + Route route = context().getRoute("scall"); + + Assert.assertNotNull("ServiceCall Route should be present", route); + + return findServiceCallProcessors(new ArrayList<>(), route.navigate()); + } + + protected List<DefaultServiceCallProcessor> findServiceCallProcessors(List<DefaultServiceCallProcessor> processors, Navigate<Processor> navigate) { + for (Processor processor : navigate.next()) { + if (processor instanceof DefaultServiceCallProcessor) { + processors.add((DefaultServiceCallProcessor)processor); + } + if (processor instanceof ChoiceProcessor) { + for (FilterProcessor filter : ((ChoiceProcessor) processor).getFilters()) { + findServiceCallProcessors(processors, filter); + } + } else if (processor instanceof Navigate) { + return findServiceCallProcessors(processors, (Navigate<Processor>)processor); + } + } + + return processors; + } } http://git-wip-us.apache.org/repos/asf/camel/blob/ecc81927/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulDefaultServiceCallRouteTest.xml ---------------------------------------------------------------------- diff --git a/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulDefaultServiceCallRouteTest.xml b/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulDefaultServiceCallRouteTest.xml index f050001..5e8731e 100644 --- a/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulDefaultServiceCallRouteTest.xml +++ b/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulDefaultServiceCallRouteTest.xml @@ -51,7 +51,7 @@ <!-- Routes --> <!-- ************************************* --> - <route> + <route id="scall"> <from uri="direct:start"/> <choice> <when> http://git-wip-us.apache.org/repos/asf/camel/blob/ecc81927/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulExpressionServiceCallRouteTest.xml ---------------------------------------------------------------------- diff --git a/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulExpressionServiceCallRouteTest.xml b/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulExpressionServiceCallRouteTest.xml index 9b463f8..a31e545 100644 --- a/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulExpressionServiceCallRouteTest.xml +++ b/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulExpressionServiceCallRouteTest.xml @@ -54,7 +54,7 @@ <!-- Routes --> <!-- ************************************* --> - <route> + <route id="scall"> <from uri="direct:start"/> <choice> <when> http://git-wip-us.apache.org/repos/asf/camel/blob/ecc81927/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulRibbonServiceCallRouteTest.xml ---------------------------------------------------------------------- diff --git a/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulRibbonServiceCallRouteTest.xml b/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulRibbonServiceCallRouteTest.xml index 582ccee..5af397c 100644 --- a/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulRibbonServiceCallRouteTest.xml +++ b/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulRibbonServiceCallRouteTest.xml @@ -51,7 +51,7 @@ <!-- Routes --> <!-- ************************************* --> - <route> + <route id="scall"> <from uri="direct:start"/> <choice> <when> http://git-wip-us.apache.org/repos/asf/camel/blob/ecc81927/components/camel-etcd/src/main/java/org/apache/camel/component/etcd/EtcdConfiguration.java ---------------------------------------------------------------------- diff --git a/components/camel-etcd/src/main/java/org/apache/camel/component/etcd/EtcdConfiguration.java b/components/camel-etcd/src/main/java/org/apache/camel/component/etcd/EtcdConfiguration.java index e784fa5..b8e7c81 100644 --- a/components/camel-etcd/src/main/java/org/apache/camel/component/etcd/EtcdConfiguration.java +++ b/components/camel-etcd/src/main/java/org/apache/camel/component/etcd/EtcdConfiguration.java @@ -16,6 +16,9 @@ */ package org.apache.camel.component.etcd; +import mousio.etcd4j.EtcdClient; +import mousio.etcd4j.EtcdSecurityContext; + import org.apache.camel.CamelContext; import org.apache.camel.CamelContextAware; import org.apache.camel.RuntimeCamelException; @@ -23,9 +26,6 @@ import org.apache.camel.spi.UriParam; import org.apache.camel.spi.UriParams; import org.apache.camel.util.jsse.SSLContextParameters; -import mousio.etcd4j.EtcdClient; -import mousio.etcd4j.EtcdSecurityContext; - @UriParams public class EtcdConfiguration implements CamelContextAware, Cloneable { http://git-wip-us.apache.org/repos/asf/camel/blob/ecc81927/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 deleted file mode 100644 index 759a602..0000000 --- a/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/cloud/RibbonLoadBalancer.java +++ /dev/null @@ -1,259 +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.component.ribbon.cloud; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; -import java.util.concurrent.RejectedExecutionException; - -import com.netflix.client.config.IClientConfig; -import com.netflix.client.config.IClientConfigKey; -import com.netflix.loadbalancer.DummyPing; -import com.netflix.loadbalancer.ILoadBalancer; -import com.netflix.loadbalancer.PollingServerListUpdater; -import com.netflix.loadbalancer.RoundRobinRule; -import com.netflix.loadbalancer.Server; -import com.netflix.loadbalancer.ServerList; -import com.netflix.loadbalancer.ZoneAwareLoadBalancer; -import org.apache.camel.CamelContext; -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.cloud.ServiceDiscovery; -import org.apache.camel.cloud.ServiceDiscoveryAware; -import org.apache.camel.cloud.ServiceFilter; -import org.apache.camel.cloud.ServiceFilterAware; -import org.apache.camel.component.ribbon.RibbonConfiguration; -import org.apache.camel.support.ServiceSupport; -import org.apache.camel.util.ObjectHelper; -import org.apache.camel.util.ServiceHelper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class RibbonLoadBalancer - extends ServiceSupport - implements CamelContextAware, ServiceDiscoveryAware, ServiceFilterAware, LoadBalancer { - - private static final Logger LOGGER = LoggerFactory.getLogger(RibbonLoadBalancer.class); - - private final RibbonConfiguration configuration; - private final ConcurrentMap<String, ZoneAwareLoadBalancer<RibbonServiceDefinition>> loadBalancers; - private CamelContext camelContext; - private ServiceDiscovery serviceDiscovery; - private ServiceFilter serviceFilter; - - public RibbonLoadBalancer(RibbonConfiguration configuration) { - this.configuration = configuration; - this.loadBalancers = new ConcurrentHashMap<>(); - } - - @Override - public CamelContext getCamelContext() { - return camelContext; - } - - @Override - public void setCamelContext(CamelContext camelContext) { - this.camelContext = camelContext; - } - - @Override - public ServiceDiscovery getServiceDiscovery() { - return serviceDiscovery; - } - - @Override - public void setServiceDiscovery(ServiceDiscovery serviceDiscovery) { - this.serviceDiscovery = serviceDiscovery; - } - - @Override - public ServiceFilter getServiceFilter() { - return serviceFilter; - } - - @Override - public void setServiceFilter(ServiceFilter serviceFilter) { - this.serviceFilter = serviceFilter; - } - - // ************************ - // lifecycle - // ************************ - - @Override - protected void doStart() throws Exception { - ObjectHelper.notNull(configuration, "configuration"); - ObjectHelper.notNull(camelContext, "camel context"); - - if (serviceDiscovery != null) { - LOGGER.info("ServiceCall is using ribbon load balancer with service discovery type: {} and service filter: {}", - serviceDiscovery.getClass(), - serviceDiscovery != null ? serviceFilter.getClass() : "none"); - } else { - LOGGER.info("ServiceCall is using ribbon load balancer"); - } - - ServiceHelper.startService(serviceDiscovery); - } - - @Override - protected void doStop() throws Exception { - loadBalancers.values().forEach(ZoneAwareLoadBalancer::stopServerListRefreshing); - loadBalancers.clear(); - - ServiceHelper.stopService(serviceDiscovery); - } - - // ************************ - // Processor - // ************************ - - @Override - public <T> T process(String serviceName, LoadBalancerFunction<T> request) throws Exception { - ILoadBalancer loadBalancer = loadBalancers.computeIfAbsent(serviceName, key -> createLoadBalancer(key)); - Server server = loadBalancer.chooseServer(serviceName); - - if (server == null) { - throw new RejectedExecutionException("No active services with name " + serviceName); - } - - ServiceDefinition definition; - - if (server instanceof ServiceDefinition) { - // If the service discovery is one of camel provides, the definition - // is already of the expected type. - definition = (ServiceDefinition)server; - } else { - // If ribbon server list is configured through client config properties - // i.e. with listOfServers property the instance provided by the load - // balancer is of type Server so a conversion is needed - definition = new RibbonServiceDefinition( - serviceName, - server.getHost(), - server.getPort() - ); - - String zone = server.getZone(); - if (zone != null) { - server.setZone(zone); - } - } - - return request.apply(definition); - } - - // ************************ - // Helpers - // ************************ - - private ZoneAwareLoadBalancer<RibbonServiceDefinition> createLoadBalancer(String serviceName) { - // setup client config - IClientConfig config = configuration.getClientName() != null - ? IClientConfig.Builder.newBuilder(configuration.getClientName()).build() - : IClientConfig.Builder.newBuilder().build(); - - if (configuration.getProperties() != null) { - for (Map.Entry<String, String> entry : configuration.getProperties().entrySet()) { - IClientConfigKey key = IClientConfigKey.Keys.valueOf(entry.getKey()); - String value = entry.getValue(); - - LOGGER.debug("RibbonClientConfig: {}={}", key.key(), value); - config.set(key, value); - } - } - - ZoneAwareLoadBalancer<RibbonServiceDefinition> loadBalancer; - - if (serviceDiscovery != null) { - loadBalancer = new ZoneAwareLoadBalancer<>( - config, - configuration.getRuleOrDefault(RoundRobinRule::new), - configuration.getPingOrDefault(DummyPing::new), - new RibbonServerList(serviceName, serviceDiscovery, serviceFilter), - null, - new PollingServerListUpdater(config)); - } else { - loadBalancer = new ZoneAwareLoadBalancer<>(config); - } - - return loadBalancer; - } - - static final class RibbonServerList implements ServerList<RibbonServiceDefinition> { - private final String serviceName; - private final ServiceDiscovery serviceDiscovery; - private final ServiceFilter serviceFilter; - - RibbonServerList(String serviceName, ServiceDiscovery serviceDiscovery, ServiceFilter serviceFilter) { - this.serviceName = serviceName; - this.serviceDiscovery = serviceDiscovery; - this.serviceFilter = serviceFilter; - } - - @Override - public List<RibbonServiceDefinition> getInitialListOfServers() { - List<ServiceDefinition> services = serviceDiscovery.getServices(serviceName); - if (serviceFilter != null) { - services = serviceFilter.apply(services); - } - - return asRibbonServerList(services); - } - - @Override - public List<RibbonServiceDefinition> getUpdatedListOfServers() { - List<ServiceDefinition> services = serviceDiscovery.getServices(serviceName); - if (serviceFilter != null) { - services = serviceFilter.apply(services); - } - - return asRibbonServerList(services); - } - - private List<RibbonServiceDefinition> asRibbonServerList(List<ServiceDefinition> services) { - List<RibbonServiceDefinition> ribbonServers = new ArrayList<>(); - - for (ServiceDefinition service : services) { - if (service instanceof RibbonServiceDefinition) { - ribbonServers.add((RibbonServiceDefinition)service); - } else { - RibbonServiceDefinition serviceDef = new RibbonServiceDefinition( - serviceName, - service.getHost(), - service.getPort(), - service.getHealth() - ); - - String zone = serviceDef.getMetadata().get("zone"); - if (zone != null) { - serviceDef.setZone(zone); - } - - ribbonServers.add(serviceDef); - } - } - - return ribbonServers; - } - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/ecc81927/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/cloud/RibbonLoadBalancerFactory.java ---------------------------------------------------------------------- diff --git a/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/cloud/RibbonLoadBalancerFactory.java b/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/cloud/RibbonLoadBalancerFactory.java deleted file mode 100644 index f166011..0000000 --- a/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/cloud/RibbonLoadBalancerFactory.java +++ /dev/null @@ -1,89 +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.component.ribbon.cloud; - -import java.util.Map; - -import org.apache.camel.CamelContext; -import org.apache.camel.cloud.LoadBalancer; -import org.apache.camel.cloud.LoadBalancerFactory; -import org.apache.camel.component.ribbon.RibbonConfiguration; - -public class RibbonLoadBalancerFactory implements LoadBalancerFactory { - private final RibbonConfiguration configuration; - - public RibbonLoadBalancerFactory() { - this(new RibbonConfiguration()); - } - - public RibbonLoadBalancerFactory(RibbonConfiguration configuration) { - this.configuration = configuration; - } - - // ************************************************************************* - // Properties - // ************************************************************************* - - public String getNamespace() { - return configuration.getNamespace(); - } - - public void setNamespace(String namespace) { - configuration.setNamespace(namespace); - } - - public String getUsername() { - return configuration.getUsername(); - } - - public void setUsername(String username) { - configuration.setUsername(username); - } - - public String getPassword() { - return configuration.getPassword(); - } - - public void setPassword(String password) { - configuration.setPassword(password); - } - - public String getClientName() { - return configuration.getClientName(); - } - - public void setClientName(String clientName) { - configuration.setClientName(clientName); - } - - public Map<String, String> getProperties() { - return configuration.getProperties(); - } - - public void setProperties(Map<String, String> clientConfig) { - configuration.setProperties(clientConfig); - } - - // ************************************************************************* - // Factory - // ************************************************************************* - - @Override - public LoadBalancer newInstance(CamelContext camelContext) throws Exception { - return new RibbonLoadBalancer(configuration); - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/ecc81927/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/cloud/RibbonServiceLoadBalancer.java ---------------------------------------------------------------------- diff --git a/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/cloud/RibbonServiceLoadBalancer.java b/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/cloud/RibbonServiceLoadBalancer.java new file mode 100644 index 0000000..b5415c3 --- /dev/null +++ b/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/cloud/RibbonServiceLoadBalancer.java @@ -0,0 +1,259 @@ +/** + * 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.ribbon.cloud; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.RejectedExecutionException; + +import com.netflix.client.config.IClientConfig; +import com.netflix.client.config.IClientConfigKey; +import com.netflix.loadbalancer.DummyPing; +import com.netflix.loadbalancer.ILoadBalancer; +import com.netflix.loadbalancer.PollingServerListUpdater; +import com.netflix.loadbalancer.RoundRobinRule; +import com.netflix.loadbalancer.Server; +import com.netflix.loadbalancer.ServerList; +import com.netflix.loadbalancer.ZoneAwareLoadBalancer; +import org.apache.camel.CamelContext; +import org.apache.camel.CamelContextAware; +import org.apache.camel.cloud.ServiceDefinition; +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.cloud.ServiceLoadBalancer; +import org.apache.camel.cloud.ServiceLoadBalancerFunction; +import org.apache.camel.component.ribbon.RibbonConfiguration; +import org.apache.camel.support.ServiceSupport; +import org.apache.camel.util.ObjectHelper; +import org.apache.camel.util.ServiceHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class RibbonServiceLoadBalancer + extends ServiceSupport + implements CamelContextAware, ServiceDiscoveryAware, ServiceFilterAware, ServiceLoadBalancer { + + private static final Logger LOGGER = LoggerFactory.getLogger(RibbonServiceLoadBalancer.class); + + private final RibbonConfiguration configuration; + private final ConcurrentMap<String, ZoneAwareLoadBalancer<RibbonServiceDefinition>> loadBalancers; + private CamelContext camelContext; + private ServiceDiscovery serviceDiscovery; + private ServiceFilter serviceFilter; + + public RibbonServiceLoadBalancer(RibbonConfiguration configuration) { + this.configuration = configuration; + this.loadBalancers = new ConcurrentHashMap<>(); + } + + @Override + public CamelContext getCamelContext() { + return camelContext; + } + + @Override + public void setCamelContext(CamelContext camelContext) { + this.camelContext = camelContext; + } + + @Override + public ServiceDiscovery getServiceDiscovery() { + return serviceDiscovery; + } + + @Override + public void setServiceDiscovery(ServiceDiscovery serviceDiscovery) { + this.serviceDiscovery = serviceDiscovery; + } + + @Override + public ServiceFilter getServiceFilter() { + return serviceFilter; + } + + @Override + public void setServiceFilter(ServiceFilter serviceFilter) { + this.serviceFilter = serviceFilter; + } + + // ************************ + // lifecycle + // ************************ + + @Override + protected void doStart() throws Exception { + ObjectHelper.notNull(configuration, "configuration"); + ObjectHelper.notNull(camelContext, "camel context"); + + if (serviceDiscovery != null) { + LOGGER.info("ServiceCall is using ribbon load balancer with service discovery type: {} and service filter: {}", + serviceDiscovery.getClass(), + serviceDiscovery != null ? serviceFilter.getClass() : "none"); + } else { + LOGGER.info("ServiceCall is using ribbon load balancer"); + } + + ServiceHelper.startService(serviceDiscovery); + } + + @Override + protected void doStop() throws Exception { + loadBalancers.values().forEach(ZoneAwareLoadBalancer::stopServerListRefreshing); + loadBalancers.clear(); + + ServiceHelper.stopService(serviceDiscovery); + } + + // ************************ + // Processor + // ************************ + + @Override + public <T> T process(String serviceName, ServiceLoadBalancerFunction<T> request) throws Exception { + ILoadBalancer loadBalancer = loadBalancers.computeIfAbsent(serviceName, key -> createLoadBalancer(key)); + Server server = loadBalancer.chooseServer(serviceName); + + if (server == null) { + throw new RejectedExecutionException("No active services with name " + serviceName); + } + + ServiceDefinition definition; + + if (server instanceof ServiceDefinition) { + // If the service discovery is one of camel provides, the definition + // is already of the expected type. + definition = (ServiceDefinition)server; + } else { + // If ribbon server list is configured through client config properties + // i.e. with listOfServers property the instance provided by the load + // balancer is of type Server so a conversion is needed + definition = new RibbonServiceDefinition( + serviceName, + server.getHost(), + server.getPort() + ); + + String zone = server.getZone(); + if (zone != null) { + server.setZone(zone); + } + } + + return request.apply(definition); + } + + // ************************ + // Helpers + // ************************ + + private ZoneAwareLoadBalancer<RibbonServiceDefinition> createLoadBalancer(String serviceName) { + // setup client config + IClientConfig config = configuration.getClientName() != null + ? IClientConfig.Builder.newBuilder(configuration.getClientName()).build() + : IClientConfig.Builder.newBuilder().build(); + + if (configuration.getProperties() != null) { + for (Map.Entry<String, String> entry : configuration.getProperties().entrySet()) { + IClientConfigKey key = IClientConfigKey.Keys.valueOf(entry.getKey()); + String value = entry.getValue(); + + LOGGER.debug("RibbonClientConfig: {}={}", key.key(), value); + config.set(key, value); + } + } + + ZoneAwareLoadBalancer<RibbonServiceDefinition> loadBalancer; + + if (serviceDiscovery != null) { + loadBalancer = new ZoneAwareLoadBalancer<>( + config, + configuration.getRuleOrDefault(RoundRobinRule::new), + configuration.getPingOrDefault(DummyPing::new), + new RibbonServerList(serviceName, serviceDiscovery, serviceFilter), + null, + new PollingServerListUpdater(config)); + } else { + loadBalancer = new ZoneAwareLoadBalancer<>(config); + } + + return loadBalancer; + } + + static final class RibbonServerList implements ServerList<RibbonServiceDefinition> { + private final String serviceName; + private final ServiceDiscovery serviceDiscovery; + private final ServiceFilter serviceFilter; + + RibbonServerList(String serviceName, ServiceDiscovery serviceDiscovery, ServiceFilter serviceFilter) { + this.serviceName = serviceName; + this.serviceDiscovery = serviceDiscovery; + this.serviceFilter = serviceFilter; + } + + @Override + public List<RibbonServiceDefinition> getInitialListOfServers() { + List<ServiceDefinition> services = serviceDiscovery.getServices(serviceName); + if (serviceFilter != null) { + services = serviceFilter.apply(services); + } + + return asRibbonServerList(services); + } + + @Override + public List<RibbonServiceDefinition> getUpdatedListOfServers() { + List<ServiceDefinition> services = serviceDiscovery.getServices(serviceName); + if (serviceFilter != null) { + services = serviceFilter.apply(services); + } + + return asRibbonServerList(services); + } + + private List<RibbonServiceDefinition> asRibbonServerList(List<ServiceDefinition> services) { + List<RibbonServiceDefinition> ribbonServers = new ArrayList<>(); + + for (ServiceDefinition service : services) { + if (service instanceof RibbonServiceDefinition) { + ribbonServers.add((RibbonServiceDefinition)service); + } else { + RibbonServiceDefinition serviceDef = new RibbonServiceDefinition( + serviceName, + service.getHost(), + service.getPort(), + service.getHealth() + ); + + String zone = serviceDef.getMetadata().get("zone"); + if (zone != null) { + serviceDef.setZone(zone); + } + + ribbonServers.add(serviceDef); + } + } + + return ribbonServers; + } + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/ecc81927/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/cloud/RibbonServiceLoadBalancerFactory.java ---------------------------------------------------------------------- diff --git a/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/cloud/RibbonServiceLoadBalancerFactory.java b/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/cloud/RibbonServiceLoadBalancerFactory.java new file mode 100644 index 0000000..0e328bf --- /dev/null +++ b/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/cloud/RibbonServiceLoadBalancerFactory.java @@ -0,0 +1,89 @@ +/** + * 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.ribbon.cloud; + +import java.util.Map; + +import org.apache.camel.CamelContext; +import org.apache.camel.cloud.ServiceLoadBalancer; +import org.apache.camel.cloud.ServiceLoadBalancerFactory; +import org.apache.camel.component.ribbon.RibbonConfiguration; + +public class RibbonServiceLoadBalancerFactory implements ServiceLoadBalancerFactory { + private final RibbonConfiguration configuration; + + public RibbonServiceLoadBalancerFactory() { + this(new RibbonConfiguration()); + } + + public RibbonServiceLoadBalancerFactory(RibbonConfiguration configuration) { + this.configuration = configuration; + } + + // ************************************************************************* + // Properties + // ************************************************************************* + + public String getNamespace() { + return configuration.getNamespace(); + } + + public void setNamespace(String namespace) { + configuration.setNamespace(namespace); + } + + public String getUsername() { + return configuration.getUsername(); + } + + public void setUsername(String username) { + configuration.setUsername(username); + } + + public String getPassword() { + return configuration.getPassword(); + } + + public void setPassword(String password) { + configuration.setPassword(password); + } + + public String getClientName() { + return configuration.getClientName(); + } + + public void setClientName(String clientName) { + configuration.setClientName(clientName); + } + + public Map<String, String> getProperties() { + return configuration.getProperties(); + } + + public void setProperties(Map<String, String> clientConfig) { + configuration.setProperties(clientConfig); + } + + // ************************************************************************* + // Factory + // ************************************************************************* + + @Override + public ServiceLoadBalancer newInstance(CamelContext camelContext) throws Exception { + return new RibbonServiceLoadBalancer(configuration); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/ecc81927/components/camel-ribbon/src/main/resources/META-INF/services/org/apache/camel/cloud/ribbon-load-balancer ---------------------------------------------------------------------- diff --git a/components/camel-ribbon/src/main/resources/META-INF/services/org/apache/camel/cloud/ribbon-load-balancer b/components/camel-ribbon/src/main/resources/META-INF/services/org/apache/camel/cloud/ribbon-load-balancer deleted file mode 100644 index 161ac23..0000000 --- a/components/camel-ribbon/src/main/resources/META-INF/services/org/apache/camel/cloud/ribbon-load-balancer +++ /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.component.ribbon.cloud.RibbonLoadBalancerFactory http://git-wip-us.apache.org/repos/asf/camel/blob/ecc81927/components/camel-ribbon/src/main/resources/META-INF/services/org/apache/camel/cloud/ribbon-service-load-balancer ---------------------------------------------------------------------- diff --git a/components/camel-ribbon/src/main/resources/META-INF/services/org/apache/camel/cloud/ribbon-service-load-balancer b/components/camel-ribbon/src/main/resources/META-INF/services/org/apache/camel/cloud/ribbon-service-load-balancer new file mode 100644 index 0000000..65c38dd --- /dev/null +++ b/components/camel-ribbon/src/main/resources/META-INF/services/org/apache/camel/cloud/ribbon-service-load-balancer @@ -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.component.ribbon.cloud.RibbonServiceLoadBalancerFactory http://git-wip-us.apache.org/repos/asf/camel/blob/ecc81927/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 f0d5dc3..a1f9c54 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 @@ -31,7 +31,7 @@ public class RibbonServerListTest { @Test public void testFixedServerList() throws Exception { ZoneAwareLoadBalancer<RibbonServiceDefinition> lb = LoadBalancerBuilder.<RibbonServiceDefinition>newBuilder() - .withDynamicServerList(new RibbonLoadBalancer.RibbonServerList( + .withDynamicServerList(new RibbonServiceLoadBalancer.RibbonServerList( "unknown", StaticServiceDiscovery.forServices( new RibbonServiceDefinition("unknown", "localhost", 9090), http://git-wip-us.apache.org/repos/asf/camel/blob/ecc81927/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/RibbonServiceCallRegistryRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/RibbonServiceCallRegistryRouteTest.java b/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/RibbonServiceCallRegistryRouteTest.java index 7f95639..5be0344 100644 --- a/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/RibbonServiceCallRegistryRouteTest.java +++ b/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/RibbonServiceCallRegistryRouteTest.java @@ -35,7 +35,7 @@ public class RibbonServiceCallRegistryRouteTest extends RibbonServiceCallRouteTe servers.addServer("localhost", 9091); RibbonConfiguration configuration = new RibbonConfiguration(); - RibbonLoadBalancer loadBalancer = new RibbonLoadBalancer(configuration); + RibbonServiceLoadBalancer loadBalancer = new RibbonServiceLoadBalancer(configuration); // configure camel service call ServiceCallConfigurationDefinition config = new ServiceCallConfigurationDefinition(); http://git-wip-us.apache.org/repos/asf/camel/blob/ecc81927/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/RibbonServiceCallRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/RibbonServiceCallRouteTest.java b/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/RibbonServiceCallRouteTest.java index 6bda099..1b87c7a 100644 --- a/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/RibbonServiceCallRouteTest.java +++ b/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/RibbonServiceCallRouteTest.java @@ -50,7 +50,7 @@ public class RibbonServiceCallRouteTest extends CamelTestSupport { servers.addServer("localhost", 9091); RibbonConfiguration configuration = new RibbonConfiguration(); - RibbonLoadBalancer loadBalancer = new RibbonLoadBalancer(configuration); + RibbonServiceLoadBalancer loadBalancer = new RibbonServiceLoadBalancer(configuration); from("direct:start") .serviceCall() http://git-wip-us.apache.org/repos/asf/camel/blob/ecc81927/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/RibbonServiceCallUpdateRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/RibbonServiceCallUpdateRouteTest.java b/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/RibbonServiceCallUpdateRouteTest.java index 27aba3e..38efa72 100644 --- a/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/RibbonServiceCallUpdateRouteTest.java +++ b/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/RibbonServiceCallUpdateRouteTest.java @@ -75,7 +75,7 @@ public class RibbonServiceCallUpdateRouteTest extends CamelTestSupport { RibbonConfiguration configuration = new RibbonConfiguration(); // lets update quick so we do not have to sleep so much in the tests configuration.addProperty("ServerListRefreshInterval", "250"); - RibbonLoadBalancer loadBalancer = new RibbonLoadBalancer(configuration); + RibbonServiceLoadBalancer loadBalancer = new RibbonServiceLoadBalancer(configuration); from("direct:start") .serviceCall() http://git-wip-us.apache.org/repos/asf/camel/blob/ecc81927/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/SpringBeanServiceCallRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/SpringBeanServiceCallRouteTest.java b/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/SpringBeanServiceCallRouteTest.java index 9c99aec..5cbf8c4 100644 --- a/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/SpringBeanServiceCallRouteTest.java +++ b/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/SpringBeanServiceCallRouteTest.java @@ -17,6 +17,10 @@ package org.apache.camel.component.ribbon.cloud; +import org.apache.camel.impl.cloud.DefaultServiceCallProcessor; +import org.apache.camel.impl.cloud.StaticServiceDiscovery; +import org.junit.Assert; +import org.junit.Test; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.test.annotation.DirtiesContext; @@ -27,5 +31,16 @@ public class SpringBeanServiceCallRouteTest extends SpringRibbonServiceCallRoute protected AbstractApplicationContext createApplicationContext() { return new ClassPathXmlApplicationContext("org/apache/camel/component/ribbon/cloud/SpringBeanRibbonServiceCallRouteTest.xml"); } + + @Test + public void testServiceCallConfiguration() throws Exception { + DefaultServiceCallProcessor processor = findServiceCallProcessor(); + + Assert.assertNotNull(processor.getLoadBalancer()); + Assert.assertTrue(processor.getLoadBalancer() instanceof RibbonServiceLoadBalancer); + + RibbonServiceLoadBalancer loadBalancer = (RibbonServiceLoadBalancer)processor.getLoadBalancer(); + Assert.assertTrue(loadBalancer.getServiceDiscovery() instanceof StaticServiceDiscovery); + } } http://git-wip-us.apache.org/repos/asf/camel/blob/ecc81927/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/SpringDslRibbonPropertiesServiceCallRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/SpringDslRibbonPropertiesServiceCallRouteTest.java b/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/SpringDslRibbonPropertiesServiceCallRouteTest.java index 9456b7a..56d734f 100644 --- a/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/SpringDslRibbonPropertiesServiceCallRouteTest.java +++ b/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/SpringDslRibbonPropertiesServiceCallRouteTest.java @@ -17,6 +17,9 @@ package org.apache.camel.component.ribbon.cloud; +import org.apache.camel.impl.cloud.DefaultServiceCallProcessor; +import org.junit.Assert; +import org.junit.Test; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.test.annotation.DirtiesContext; @@ -27,5 +30,16 @@ public class SpringDslRibbonPropertiesServiceCallRouteTest extends SpringRibbonS protected AbstractApplicationContext createApplicationContext() { return new ClassPathXmlApplicationContext("org/apache/camel/component/ribbon/cloud/SpringDslRibbonPropertiesServiceCallRouteTest.xml"); } + + @Test + public void testServiceCallConfiguration() throws Exception { + DefaultServiceCallProcessor processor = findServiceCallProcessor(); + + Assert.assertNotNull(processor.getLoadBalancer()); + Assert.assertTrue(processor.getLoadBalancer() instanceof RibbonServiceLoadBalancer); + + RibbonServiceLoadBalancer loadBalancer = (RibbonServiceLoadBalancer)processor.getLoadBalancer(); + Assert.assertNull(loadBalancer.getServiceDiscovery()); + } } http://git-wip-us.apache.org/repos/asf/camel/blob/ecc81927/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/SpringDslRibbonServiceCallRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/SpringDslRibbonServiceCallRouteTest.java b/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/SpringDslRibbonServiceCallRouteTest.java index 34269b3..bb5304e 100644 --- a/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/SpringDslRibbonServiceCallRouteTest.java +++ b/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/SpringDslRibbonServiceCallRouteTest.java @@ -17,6 +17,10 @@ package org.apache.camel.component.ribbon.cloud; +import org.apache.camel.impl.cloud.DefaultServiceCallProcessor; +import org.apache.camel.impl.cloud.StaticServiceDiscovery; +import org.junit.Assert; +import org.junit.Test; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.test.annotation.DirtiesContext; @@ -27,5 +31,16 @@ public class SpringDslRibbonServiceCallRouteTest extends SpringRibbonServiceCall protected AbstractApplicationContext createApplicationContext() { return new ClassPathXmlApplicationContext("org/apache/camel/component/ribbon/cloud/SpringDslRibbonServiceCallRouteTest.xml"); } + + @Test + public void testServiceCallConfiguration() throws Exception { + DefaultServiceCallProcessor processor = findServiceCallProcessor(); + + Assert.assertNotNull(processor.getLoadBalancer()); + Assert.assertTrue(processor.getLoadBalancer() instanceof RibbonServiceLoadBalancer); + + RibbonServiceLoadBalancer loadBalancer = (RibbonServiceLoadBalancer)processor.getLoadBalancer(); + Assert.assertTrue(loadBalancer.getServiceDiscovery() instanceof StaticServiceDiscovery); + } } http://git-wip-us.apache.org/repos/asf/camel/blob/ecc81927/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/SpringRibbonServiceCallRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/SpringRibbonServiceCallRouteTest.java b/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/SpringRibbonServiceCallRouteTest.java index 3c9e8b2..0ca03f8 100644 --- a/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/SpringRibbonServiceCallRouteTest.java +++ b/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/cloud/SpringRibbonServiceCallRouteTest.java @@ -17,7 +17,14 @@ package org.apache.camel.component.ribbon.cloud; +import java.util.Optional; + +import org.apache.camel.Navigate; +import org.apache.camel.Processor; +import org.apache.camel.Route; +import org.apache.camel.impl.cloud.DefaultServiceCallProcessor; import org.apache.camel.test.spring.CamelSpringTestSupport; +import org.junit.Assert; import org.junit.Test; import org.springframework.test.annotation.DirtiesContext; @@ -36,5 +43,32 @@ public abstract class SpringRibbonServiceCallRouteTest extends CamelSpringTestSu assertMockEndpointsSatisfied(); } + + // ************************************ + // Helpers + // ************************************ + + protected DefaultServiceCallProcessor findServiceCallProcessor() { + Route route = context().getRoute("scall"); + + Assert.assertNotNull("ServiceCall Route should be present", route); + + return findServiceCallProcessor(route.navigate()) + .orElseThrow(() -> new IllegalStateException("Unable to find a ServiceCallProcessor")); + } + + protected Optional<DefaultServiceCallProcessor> findServiceCallProcessor(Navigate<Processor> navigate) { + for (Processor processor : navigate.next()) { + if (processor instanceof DefaultServiceCallProcessor) { + return Optional.ofNullable((DefaultServiceCallProcessor)processor); + } + + if (processor instanceof Navigate) { + return findServiceCallProcessor((Navigate<Processor>)processor); + } + } + + return Optional.empty(); + } } http://git-wip-us.apache.org/repos/asf/camel/blob/ecc81927/components/camel-ribbon/src/test/resources/org/apache/camel/component/ribbon/cloud/SpringBeanRibbonServiceCallRouteTest.xml ---------------------------------------------------------------------- diff --git a/components/camel-ribbon/src/test/resources/org/apache/camel/component/ribbon/cloud/SpringBeanRibbonServiceCallRouteTest.xml b/components/camel-ribbon/src/test/resources/org/apache/camel/component/ribbon/cloud/SpringBeanRibbonServiceCallRouteTest.xml index 01e59d9..22ad55c 100644 --- a/components/camel-ribbon/src/test/resources/org/apache/camel/component/ribbon/cloud/SpringBeanRibbonServiceCallRouteTest.xml +++ b/components/camel-ribbon/src/test/resources/org/apache/camel/component/ribbon/cloud/SpringBeanRibbonServiceCallRouteTest.xml @@ -28,7 +28,7 @@ <property name="servers" value="localhost:9090,localhost:9091"/> </bean> - <bean id="balancer" class="org.apache.camel.component.ribbon.cloud.RibbonLoadBalancer"> + <bean id="balancer" class="org.apache.camel.component.ribbon.cloud.RibbonServiceLoadBalancer"> <constructor-arg index="0"> <bean class="org.apache.camel.component.ribbon.RibbonConfiguration"/> </constructor-arg> @@ -39,7 +39,7 @@ <!-- service call configuration to use ribbon --> <serviceCallConfiguration id="ribbon" loadBalancerRef="balancer" serviceDiscoveryRef="discovery"/> - <route> + <route id="scall"> <from uri="direct:start"/> <serviceCall name="myService"/> <to uri="mock:result"/> http://git-wip-us.apache.org/repos/asf/camel/blob/ecc81927/components/camel-ribbon/src/test/resources/org/apache/camel/component/ribbon/cloud/SpringDslRibbonPropertiesServiceCallRouteTest.xml ---------------------------------------------------------------------- diff --git a/components/camel-ribbon/src/test/resources/org/apache/camel/component/ribbon/cloud/SpringDslRibbonPropertiesServiceCallRouteTest.xml b/components/camel-ribbon/src/test/resources/org/apache/camel/component/ribbon/cloud/SpringDslRibbonPropertiesServiceCallRouteTest.xml index b182bc2..3c92e91 100644 --- a/components/camel-ribbon/src/test/resources/org/apache/camel/component/ribbon/cloud/SpringDslRibbonPropertiesServiceCallRouteTest.xml +++ b/components/camel-ribbon/src/test/resources/org/apache/camel/component/ribbon/cloud/SpringDslRibbonPropertiesServiceCallRouteTest.xml @@ -24,7 +24,7 @@ http://camel.apache.org/schema/spring/camel-spring.xsd"> <camelContext xmlns="http://camel.apache.org/schema/spring"> - <route> + <route id="scall"> <from uri="direct:start"/> <serviceCall name="myService"> <!-- enable ribbon load balancer --> http://git-wip-us.apache.org/repos/asf/camel/blob/ecc81927/components/camel-ribbon/src/test/resources/org/apache/camel/component/ribbon/cloud/SpringDslRibbonServiceCallRouteTest.xml ---------------------------------------------------------------------- diff --git a/components/camel-ribbon/src/test/resources/org/apache/camel/component/ribbon/cloud/SpringDslRibbonServiceCallRouteTest.xml b/components/camel-ribbon/src/test/resources/org/apache/camel/component/ribbon/cloud/SpringDslRibbonServiceCallRouteTest.xml index 48ce72a..8a40924 100644 --- a/components/camel-ribbon/src/test/resources/org/apache/camel/component/ribbon/cloud/SpringDslRibbonServiceCallRouteTest.xml +++ b/components/camel-ribbon/src/test/resources/org/apache/camel/component/ribbon/cloud/SpringDslRibbonServiceCallRouteTest.xml @@ -24,7 +24,7 @@ http://camel.apache.org/schema/spring/camel-spring.xsd"> <camelContext xmlns="http://camel.apache.org/schema/spring"> - <route> + <route id="scall"> <from uri="direct:start"/> <serviceCall name="myService"> <!-- static list of servers --> http://git-wip-us.apache.org/repos/asf/camel/blob/ecc81927/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/cloud/CamelCloudConfigurationProperties.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/cloud/CamelCloudConfigurationProperties.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/cloud/CamelCloudConfigurationProperties.java index 937e042..31ebf09 100644 --- a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/cloud/CamelCloudConfigurationProperties.java +++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/cloud/CamelCloudConfigurationProperties.java @@ -21,6 +21,7 @@ import java.util.List; import java.util.Map; import org.apache.camel.Expression; +import org.apache.camel.cloud.ServiceLoadBalancer; import org.apache.camel.model.cloud.ServiceCallConstants; import org.springframework.boot.context.properties.ConfigurationProperties; @@ -93,7 +94,7 @@ public class CamelCloudConfigurationProperties { private String serviceChooser; /** - * A reference to the {@link org.apache.camel.cloud.LoadBalancer} to use. + * A reference to the {@link ServiceLoadBalancer} to use. */ private String loadBalancer; http://git-wip-us.apache.org/repos/asf/camel/blob/ecc81927/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/cloud/CamelCloudServiceCallConfigurationTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/cloud/CamelCloudServiceCallConfigurationTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/cloud/CamelCloudServiceCallConfigurationTest.java index 282af31..3f3415a 100644 --- a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/cloud/CamelCloudServiceCallConfigurationTest.java +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/cloud/CamelCloudServiceCallConfigurationTest.java @@ -17,7 +17,7 @@ package org.apache.camel.spring.boot.cloud; -import org.apache.camel.cloud.LoadBalancer; +import org.apache.camel.cloud.ServiceLoadBalancer; import org.apache.camel.cloud.ServiceChooser; import org.apache.camel.cloud.ServiceDiscovery; import org.apache.camel.cloud.ServiceFilter; @@ -70,7 +70,7 @@ public class CamelCloudServiceCallConfigurationTest { assertTrue(ctx.getBeansOfType(ServiceDiscovery.class).isEmpty()); assertTrue(ctx.getBeansOfType(ServiceFilter.class).isEmpty()); assertTrue(ctx.getBeansOfType(ServiceChooser.class).isEmpty()); - assertTrue(ctx.getBeansOfType(LoadBalancer.class).isEmpty()); + assertTrue(ctx.getBeansOfType(ServiceLoadBalancer.class).isEmpty()); } } http://git-wip-us.apache.org/repos/asf/camel/blob/ecc81927/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/CamelSpringCloudLoadBalancer.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/CamelSpringCloudLoadBalancer.java b/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/CamelSpringCloudLoadBalancer.java deleted file mode 100644 index bfd6ab8..0000000 --- a/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/CamelSpringCloudLoadBalancer.java +++ /dev/null @@ -1,81 +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 org.apache.camel.CamelContext; -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 CamelSpringCloudLoadBalancer extends ServiceSupport implements CamelContextAware, LoadBalancer { - private static final Logger LOGGER = LoggerFactory.getLogger(CamelSpringCloudLoadBalancer.class); - - private final LoadBalancerClient loadBalancerClient; - private CamelContext camelContext; - - public CamelSpringCloudLoadBalancer(LoadBalancerClient loadBalancerClient) { - this.loadBalancerClient = loadBalancerClient; - } - - @Override - public CamelContext getCamelContext() { - return camelContext; - } - - @Override - public void setCamelContext(CamelContext camelContext) { - this.camelContext = camelContext; - } - - @Override - 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()); - } - - @Override - protected void doStop() throws Exception { - } - - @Override - public <T> T process(String serviceName, LoadBalancerFunction<T> function) throws Exception { - return loadBalancerClient.execute(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