CAMEL-9683: Started on camel-ribbon
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/6da5b416 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/6da5b416 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/6da5b416 Branch: refs/heads/remoteServiceCall Commit: 6da5b416eada8ca58fde7796c3d83bca037f20b8 Parents: 671077a Author: Claus Ibsen <davscl...@apache.org> Authored: Wed Apr 27 13:57:53 2016 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Mon May 23 09:25:49 2016 +0200 ---------------------------------------------------------------------- components/camel-ribbon/pom.xml | 5 ++ ...onDelegateServiceCallServerListStrategy.java | 85 ++++++++++++++++++++ .../RibbonServiceCallServerListStrategy.java | 54 +++---------- .../component/ribbon/RibbonServerListTest.java | 52 ++++++++++++ .../RibbonServiceCallKubernetesRouteTest.java | 58 +++++++++++++ .../ribbon/RibbonServiceCallRouteTest.java | 38 ++++++--- 6 files changed, 239 insertions(+), 53 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/6da5b416/components/camel-ribbon/pom.xml ---------------------------------------------------------------------- diff --git a/components/camel-ribbon/pom.xml b/components/camel-ribbon/pom.xml index c76b414..f4be469 100644 --- a/components/camel-ribbon/pom.xml +++ b/components/camel-ribbon/pom.xml @@ -63,6 +63,11 @@ <artifactId>camel-kubernetes</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-jetty</artifactId> + <scope>test</scope> + </dependency> <!-- logging --> <dependency> http://git-wip-us.apache.org/repos/asf/camel/blob/6da5b416/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/RibbonDelegateServiceCallServerListStrategy.java ---------------------------------------------------------------------- diff --git a/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/RibbonDelegateServiceCallServerListStrategy.java b/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/RibbonDelegateServiceCallServerListStrategy.java new file mode 100644 index 0000000..46f512a --- /dev/null +++ b/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/RibbonDelegateServiceCallServerListStrategy.java @@ -0,0 +1,85 @@ +/** + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +import com.netflix.client.config.IClientConfig; +import com.netflix.loadbalancer.AbstractServerList; +import com.netflix.loadbalancer.ServerList; +import org.apache.camel.spi.ServiceCallServer; +import org.apache.camel.spi.ServiceCallServerListStrategy; + +/** + * To let an existing {@link ServiceCallServerListStrategy} by managed by Ribbon. + */ +public class RibbonDelegateServiceCallServerListStrategy extends AbstractServerList<RibbonServer> implements ServerList<RibbonServer> { + + // TODO: a plain ribbon server list without plugin for kube for now + // and some unit tests with static server list etc + // https://github.com/Netflix/ribbon/wiki/Working-with-load-balancers + // https://github.com/Netflix/ribbon/wiki/Getting-Started + // then later with a kube facade so the servers are looked up in k8s + + private IClientConfig clientConfig; + private final ServiceCallServerListStrategy delegate; + + /** + * Lets ribbon manage the {@link ServiceCallServerListStrategy}. + */ + public RibbonDelegateServiceCallServerListStrategy(ServiceCallServerListStrategy delegate) { + this.delegate = delegate; + } + + @Override + public void initWithNiwsConfig(IClientConfig clientConfig) { + this.clientConfig = clientConfig; + } + + @Override + @SuppressWarnings("unchecked") + public List<RibbonServer> getInitialListOfServers() { + Collection<ServiceCallServer> servers = delegate.getInitialListOfServers(); + if (servers == null || servers.isEmpty()) { + return Collections.EMPTY_LIST; + } + List<RibbonServer> answer = new ArrayList<>(servers.size()); + for (ServiceCallServer server : servers) { + RibbonServer ribbon = new RibbonServer(server.getIp(), server.getPort()); + answer.add(ribbon); + } + return answer; + } + + @Override + @SuppressWarnings("unchecked") + public List<RibbonServer> getUpdatedListOfServers() { + Collection<ServiceCallServer> servers = delegate.getUpdatedListOfServers(); + if (servers == null || servers.isEmpty()) { + return Collections.EMPTY_LIST; + } + List<RibbonServer> answer = new ArrayList<>(servers.size()); + for (ServiceCallServer server : servers) { + RibbonServer ribbon = new RibbonServer(server.getIp(), server.getPort()); + answer.add(ribbon); + } + return answer; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/6da5b416/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/RibbonServiceCallServerListStrategy.java ---------------------------------------------------------------------- diff --git a/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/RibbonServiceCallServerListStrategy.java b/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/RibbonServiceCallServerListStrategy.java index 06ebb16..848f9db 100644 --- a/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/RibbonServiceCallServerListStrategy.java +++ b/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/RibbonServiceCallServerListStrategy.java @@ -5,9 +5,9 @@ * 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 - * <p> + * <p/> * http://www.apache.org/licenses/LICENSE-2.0 - * <p> + * <p/> * 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. @@ -17,35 +17,23 @@ package org.apache.camel.component.ribbon; import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; import java.util.List; import com.netflix.client.config.IClientConfig; import com.netflix.loadbalancer.AbstractServerList; import com.netflix.loadbalancer.ServerList; -import org.apache.camel.spi.ServiceCallServer; import org.apache.camel.spi.ServiceCallServerListStrategy; -/** - * To let an existing {@link ServiceCallServerListStrategy} by managed by Ribbon. - */ -public class RibbonServiceCallServerListStrategy extends AbstractServerList<RibbonServer> implements ServerList<RibbonServer> { - - // TODO: a plain ribbon server list without plugin for kube for now - // and some unit tests with static server list etc - // https://github.com/Netflix/ribbon/wiki/Working-with-load-balancers - // https://github.com/Netflix/ribbon/wiki/Getting-Started - // then later with a kube facade so the servers are looked up in k8s +public class RibbonServiceCallServerListStrategy extends AbstractServerList<RibbonServer> implements ServerList<RibbonServer>, ServiceCallServerListStrategy<RibbonServer> { private IClientConfig clientConfig; - private final ServiceCallServerListStrategy delegate; + private final List<RibbonServer> servers = new ArrayList<>(); + + public RibbonServiceCallServerListStrategy() { + } - /** - * Lets ribbon manage the {@link ServiceCallServerListStrategy}. - */ - public RibbonServiceCallServerListStrategy(ServiceCallServerListStrategy delegate) { - this.delegate = delegate; + public RibbonServiceCallServerListStrategy(List<RibbonServer> servers) { + this.servers.addAll(servers); } @Override @@ -54,32 +42,12 @@ public class RibbonServiceCallServerListStrategy extends AbstractServerList<Ribb } @Override - @SuppressWarnings("unchecked") public List<RibbonServer> getInitialListOfServers() { - Collection<ServiceCallServer> servers = delegate.getInitialListOfServers(); - if (servers == null || servers.isEmpty()) { - return Collections.EMPTY_LIST; - } - List<RibbonServer> answer = new ArrayList<>(servers.size()); - for (ServiceCallServer server : servers) { - RibbonServer ribbon = new RibbonServer(server.getIp(), server.getPort()); - answer.add(ribbon); - } - return answer; + return servers; } @Override - @SuppressWarnings("unchecked") public List<RibbonServer> getUpdatedListOfServers() { - Collection<ServiceCallServer> servers = delegate.getUpdatedListOfServers(); - if (servers == null || servers.isEmpty()) { - return Collections.EMPTY_LIST; - } - List<RibbonServer> answer = new ArrayList<>(servers.size()); - for (ServiceCallServer server : servers) { - RibbonServer ribbon = new RibbonServer(server.getIp(), server.getPort()); - answer.add(ribbon); - } - return answer; + return servers; } } http://git-wip-us.apache.org/repos/asf/camel/blob/6da5b416/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/RibbonServerListTest.java ---------------------------------------------------------------------- diff --git a/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/RibbonServerListTest.java b/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/RibbonServerListTest.java new file mode 100644 index 0000000..8fb96ff --- /dev/null +++ b/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/RibbonServerListTest.java @@ -0,0 +1,52 @@ +/** + * 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 + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * 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; + +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.ServerList; +import com.netflix.loadbalancer.ZoneAwareLoadBalancer; +import junit.framework.TestCase; +import org.junit.Test; + +public class RibbonServerListTest extends TestCase { + + @Test + public void testFixedServerList() throws Exception { + List<RibbonServer> servers = new ArrayList(); + servers.add(new RibbonServer("localhost", 9090)); + servers.add(new RibbonServer("localhost", 9091)); + + ServerList<RibbonServer> list = new RibbonServiceCallServerListStrategy(servers); + + ZoneAwareLoadBalancer<RibbonServer> lb = LoadBalancerBuilder.<RibbonServer>newBuilder() + .withDynamicServerList(list) + .withRule(new RoundRobinRule()).buildDynamicServerListLoadBalancer(); + + Server server = lb.chooseServer(); + assertEquals("localhost", server.getHost()); + assertEquals(9091, server.getPort()); + + server = lb.chooseServer(); + assertEquals("localhost", server.getHost()); + assertEquals(9090, server.getPort()); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/6da5b416/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/RibbonServiceCallKubernetesRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/RibbonServiceCallKubernetesRouteTest.java b/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/RibbonServiceCallKubernetesRouteTest.java new file mode 100644 index 0000000..be0567f --- /dev/null +++ b/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/RibbonServiceCallKubernetesRouteTest.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 + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * 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; + +import org.apache.camel.RoutesBuilder; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.model.ServiceCallConfigurationDefinition; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Ignore; +import org.junit.Test; + +@Ignore("Manual test") +public class RibbonServiceCallKubernetesRouteTest extends CamelTestSupport { + + @Test + public void testServiceCall() throws Exception { + getMockEndpoint("mock:result").expectedMessageCount(1); + + template.sendBody("direct:start", "Hello World"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RoutesBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + ServiceCallConfigurationDefinition config = new ServiceCallConfigurationDefinition(); + config.setMasterUrl("https://fabric8-master.vagrant.f8:8443"); + config.setUsername("admin"); + config.setPassword("admin"); + config.setNamespace("default"); + // lets use the built-in round robin (random is default) + config.setLoadBalancerRef("roundrobin"); + + from("direct:start") + .serviceCall("cdi-camel-jetty", null, config) + .to("mock:result"); + } + }; + } +} + http://git-wip-us.apache.org/repos/asf/camel/blob/6da5b416/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/RibbonServiceCallRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/RibbonServiceCallRouteTest.java b/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/RibbonServiceCallRouteTest.java index 7321324..f8c5c06 100644 --- a/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/RibbonServiceCallRouteTest.java +++ b/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/RibbonServiceCallRouteTest.java @@ -16,21 +16,27 @@ */ package org.apache.camel.component.ribbon; +import java.util.ArrayList; +import java.util.List; + import org.apache.camel.RoutesBuilder; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.model.ServiceCallConfigurationDefinition; import org.apache.camel.test.junit4.CamelTestSupport; -import org.junit.Ignore; import org.junit.Test; -@Ignore("Manual test") public class RibbonServiceCallRouteTest extends CamelTestSupport { @Test public void testServiceCall() throws Exception { - getMockEndpoint("mock:result").expectedMessageCount(1); + getMockEndpoint("mock:9090").expectedMessageCount(1); + getMockEndpoint("mock:9091").expectedMessageCount(1); + getMockEndpoint("mock:result").expectedMessageCount(2); - template.sendBody("direct:start", "Hello World"); + String out = template.requestBody("direct:start", null, String.class); + String out2 = template.requestBody("direct:start", null, String.class); + assertEquals("9091", out); + assertEquals("9090", out2); assertMockEndpointsSatisfied(); } @@ -40,17 +46,29 @@ public class RibbonServiceCallRouteTest extends CamelTestSupport { return new RouteBuilder() { @Override public void configure() throws Exception { + // setup a static ribbon server list + List<RibbonServer> servers = new ArrayList<>(); + servers.add(new RibbonServer("localhost", 9090)); + servers.add(new RibbonServer("localhost", 9091)); + RibbonServiceCallServerListStrategy list = new RibbonServiceCallServerListStrategy(servers); + + + + // configure camel service call ServiceCallConfigurationDefinition config = new ServiceCallConfigurationDefinition(); - config.setMasterUrl("https://fabric8-master.vagrant.f8:8443"); - config.setUsername("admin"); - config.setPassword("admin"); - config.setNamespace("default"); - // lets use the built-in round robin (random is default) - config.setLoadBalancerRef("roundrobin"); + config.setServerListStrategy(list); from("direct:start") .serviceCall("cdi-camel-jetty", null, config) .to("mock:result"); + + from("jetty:http://localhost:9090") + .to("mock:9090") + .transform().constant("9090"); + + from("jetty:http://localhost:9091") + .to("mock:9091") + .transform().constant("9091"); } }; }