Search in sources :

Example 1 with RoundRobinStrategy

use of com.predic8.membrane.core.interceptor.balancer.RoundRobinStrategy in project service-proxy by membrane.

the class LoadBalancingInterceptorTest method testFailOverOnConnectionRefused.

@Test
public void testFailOverOnConnectionRefused() throws Exception {
    balancingInterceptor.setDispatchingStrategy(roundRobinStrategy);
    HttpClient client = new HttpClient();
    client.getParams().setParameter(HttpProtocolParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    assertEquals(200, client.executeMethod(getPostMethod()));
    assertEquals(1, mockInterceptor1.getCount());
    assertEquals(0, mockInterceptor2.getCount());
    assertEquals(200, client.executeMethod(getPostMethod()));
    assertEquals(1, mockInterceptor1.getCount());
    assertEquals(1, mockInterceptor2.getCount());
    service1.shutdown();
    Thread.sleep(1000);
    assertEquals(200, client.executeMethod(getPostMethod()));
    assertEquals(1, mockInterceptor1.getCount());
    assertEquals(2, mockInterceptor2.getCount());
    assertEquals(200, client.executeMethod(getPostMethod()));
    assertEquals(3, mockInterceptor2.getCount());
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) Test(org.junit.Test) Http11Test(com.predic8.membrane.integration.Http11Test)

Example 2 with RoundRobinStrategy

use of com.predic8.membrane.core.interceptor.balancer.RoundRobinStrategy in project service-proxy by membrane.

the class LoadBalancingInterceptorTest method testFailOverOnStatus500.

@Test
public void testFailOverOnStatus500() throws Exception {
    balancingInterceptor.setDispatchingStrategy(roundRobinStrategy);
    HttpClient client = new HttpClient();
    client.getParams().setParameter(HttpProtocolParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    assertEquals(200, client.executeMethod(getPostMethod()));
    assertEquals(1, mockInterceptor1.getCount());
    assertEquals(0, mockInterceptor2.getCount());
    assertEquals(200, client.executeMethod(getPostMethod()));
    assertEquals(1, mockInterceptor1.getCount());
    assertEquals(1, mockInterceptor2.getCount());
    ((ServiceProxy) service1.getRuleManager().getRules().get(0)).getInterceptors().add(0, new AbstractInterceptor() {

        @Override
        public Outcome handleRequest(Exchange exc) throws Exception {
            exc.setResponse(Response.internalServerError().build());
            return Outcome.ABORT;
        }
    });
    assertEquals(200, client.executeMethod(getPostMethod()));
    assertEquals(1, mockInterceptor1.getCount());
    assertEquals(2, mockInterceptor2.getCount());
    assertEquals(200, client.executeMethod(getPostMethod()));
    assertEquals(3, mockInterceptor2.getCount());
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) Outcome(com.predic8.membrane.core.interceptor.Outcome) HttpClient(org.apache.commons.httpclient.HttpClient) AbstractInterceptor(com.predic8.membrane.core.interceptor.AbstractInterceptor) URISyntaxException(java.net.URISyntaxException) MalformedURLException(java.net.MalformedURLException) Test(org.junit.Test) Http11Test(com.predic8.membrane.integration.Http11Test)

Example 3 with RoundRobinStrategy

use of com.predic8.membrane.core.interceptor.balancer.RoundRobinStrategy in project service-proxy by membrane.

the class LoadBalancingInterceptorTest method setUp.

@Before
public void setUp() throws Exception {
    service1 = new HttpRouter();
    mockInterceptor1 = new DummyWebServiceInterceptor();
    ServiceProxy sp1 = new ServiceProxy(new ServiceProxyKey("localhost", "POST", ".*", 2000), "thomas-bayer.com", 80);
    sp1.getInterceptors().add(new AbstractInterceptor() {

        @Override
        public Outcome handleResponse(Exchange exc) throws Exception {
            exc.getResponse().getHeader().add("Connection", "close");
            return Outcome.CONTINUE;
        }
    });
    sp1.getInterceptors().add(mockInterceptor1);
    service1.getRuleManager().addProxyAndOpenPortIfNew(sp1);
    service1.init();
    service2 = new HttpRouter();
    mockInterceptor2 = new DummyWebServiceInterceptor();
    ServiceProxy sp2 = new ServiceProxy(new ServiceProxyKey("localhost", "POST", ".*", 3000), "thomas-bayer.com", 80);
    sp2.getInterceptors().add(new AbstractInterceptor() {

        @Override
        public Outcome handleResponse(Exchange exc) throws Exception {
            exc.getResponse().getHeader().add("Connection", "close");
            return Outcome.CONTINUE;
        }
    });
    sp2.getInterceptors().add(mockInterceptor2);
    service2.getRuleManager().addProxyAndOpenPortIfNew(sp2);
    service2.init();
    balancer = new HttpRouter();
    ServiceProxy sp3 = new ServiceProxy(new ServiceProxyKey("localhost", "POST", ".*", 7000), "thomas-bayer.com", 80);
    balancingInterceptor = new LoadBalancingInterceptor();
    balancingInterceptor.setName("Default");
    sp3.getInterceptors().add(balancingInterceptor);
    balancer.getRuleManager().addProxyAndOpenPortIfNew(sp3);
    enableFailOverOn5XX(balancer);
    balancer.init();
    BalancerUtil.lookupBalancer(balancer, "Default").up("Default", "localhost", 2000);
    BalancerUtil.lookupBalancer(balancer, "Default").up("Default", "localhost", 3000);
    roundRobinStrategy = new RoundRobinStrategy();
    byThreadStrategy = new ByThreadStrategy();
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) LoadBalancingInterceptor(com.predic8.membrane.core.interceptor.balancer.LoadBalancingInterceptor) ServiceProxyKey(com.predic8.membrane.core.rules.ServiceProxyKey) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) ByThreadStrategy(com.predic8.membrane.core.interceptor.balancer.ByThreadStrategy) Outcome(com.predic8.membrane.core.interceptor.Outcome) AbstractInterceptor(com.predic8.membrane.core.interceptor.AbstractInterceptor) HttpRouter(com.predic8.membrane.core.HttpRouter) RoundRobinStrategy(com.predic8.membrane.core.interceptor.balancer.RoundRobinStrategy) URISyntaxException(java.net.URISyntaxException) MalformedURLException(java.net.MalformedURLException) DummyWebServiceInterceptor(com.predic8.membrane.core.services.DummyWebServiceInterceptor) Before(org.junit.Before)

Example 4 with RoundRobinStrategy

use of com.predic8.membrane.core.interceptor.balancer.RoundRobinStrategy in project service-proxy by membrane.

the class LoadBalancingInterceptorTest method testExpect100Continue.

@Test
public void testExpect100Continue() throws Exception {
    balancingInterceptor.setDispatchingStrategy(roundRobinStrategy);
    HttpClient client = new HttpClient();
    Http11Test.initExpect100ContinueWithFastFail(client);
    PostMethod vari = getPostMethod();
    int status = client.executeMethod(vari);
    assertEquals(200, status);
    assertEquals(1, mockInterceptor1.getCount());
    assertEquals(0, mockInterceptor2.getCount());
    assertEquals(200, client.executeMethod(getPostMethod()));
    assertEquals(1, mockInterceptor1.getCount());
    assertEquals(1, mockInterceptor2.getCount());
    assertEquals(200, client.executeMethod(getPostMethod()));
    assertEquals(2, mockInterceptor1.getCount());
    assertEquals(1, mockInterceptor2.getCount());
    assertEquals(200, client.executeMethod(getPostMethod()));
    assertEquals(2, mockInterceptor1.getCount());
    assertEquals(2, mockInterceptor2.getCount());
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient) Test(org.junit.Test) Http11Test(com.predic8.membrane.integration.Http11Test)

Example 5 with RoundRobinStrategy

use of com.predic8.membrane.core.interceptor.balancer.RoundRobinStrategy in project service-proxy by membrane.

the class MultipleLoadBalancersTest method setUp.

@Before
public void setUp() throws Exception {
    service1 = new MockService(2001);
    service2 = new MockService(2002);
    service11 = new MockService(2011);
    service12 = new MockService(2012);
    balancer = new HttpRouter();
    balancingInterceptor1 = createBalancingInterceptor(7000, "Default");
    balancingInterceptor2 = createBalancingInterceptor(7001, "Balancer2");
    BalancerUtil.lookupBalancer(balancer, "Default").up("Default", "localhost", service1.port);
    BalancerUtil.lookupBalancer(balancer, "Default").up("Default", "localhost", service2.port);
    BalancerUtil.lookupBalancer(balancer, "Balancer2").up("Default", "localhost", service11.port);
    BalancerUtil.lookupBalancer(balancer, "Balancer2").up("Default", "localhost", service12.port);
    roundRobinStrategy1 = new RoundRobinStrategy();
    roundRobinStrategy2 = new RoundRobinStrategy();
}
Also used : HttpRouter(com.predic8.membrane.core.HttpRouter) RoundRobinStrategy(com.predic8.membrane.core.interceptor.balancer.RoundRobinStrategy) Before(org.junit.Before)

Aggregations

Http11Test (com.predic8.membrane.integration.Http11Test)4 HttpClient (org.apache.commons.httpclient.HttpClient)4 Test (org.junit.Test)4 HttpRouter (com.predic8.membrane.core.HttpRouter)2 Exchange (com.predic8.membrane.core.exchange.Exchange)2 AbstractInterceptor (com.predic8.membrane.core.interceptor.AbstractInterceptor)2 Outcome (com.predic8.membrane.core.interceptor.Outcome)2 RoundRobinStrategy (com.predic8.membrane.core.interceptor.balancer.RoundRobinStrategy)2 MalformedURLException (java.net.MalformedURLException)2 URISyntaxException (java.net.URISyntaxException)2 PostMethod (org.apache.commons.httpclient.methods.PostMethod)2 Before (org.junit.Before)2 ByThreadStrategy (com.predic8.membrane.core.interceptor.balancer.ByThreadStrategy)1 LoadBalancingInterceptor (com.predic8.membrane.core.interceptor.balancer.LoadBalancingInterceptor)1 ServiceProxy (com.predic8.membrane.core.rules.ServiceProxy)1 ServiceProxyKey (com.predic8.membrane.core.rules.ServiceProxyKey)1 DummyWebServiceInterceptor (com.predic8.membrane.core.services.DummyWebServiceInterceptor)1