Load Balancer
The Load Balancer Pattern allows you to delegate to one of a number of endpoints using a variety of different load balancing policies.
Built-in load balancing policies
Camel provides the following policies out-of-the-box:
Policy
|
Description
|
---|---|
The exchanges are selected from in a round robin fashion. This is a well known and classic policy, which spreads the load evenly.
| |
A random endpoint is selected for each exchange.
| |
Sticky load balancing using an Expression to calculate a correlation key to perform the sticky load balancing; rather like jsessionid in the web or JMSXGroupID in JMS.
| |
Topic which sends to all destinations (rather like JMS Topics)
| |
In case of failures the exchange will be tried on the next endpoint.
| |
Weighted Round-Robin
|
Camel 2.5: The weighted load balancing policy allows you to specify a processing load distribution ratio for each server with respect to the others. In addition to the weight, endpoint selection is then further refined using round-robin distribution based on weight.
|
Weighted Random
|
Camel 2.5: The weighted load balancing policy allows you to specify a processing load distribution ratio for each server with respect to others.In addition to the weight, endpoint selection is then further refined using random distribution based on weight.
|
Custom
|
Camel 2.8: From Camel 2.8 onwards the preferred way of using a custom Load Balancer is to use this policy, instead of using the @deprecated
ref attribute. |
Round Robin
The round robin load balancer is not meant to work with failover, for that you should use the dedicated failover load balancer. The round robin load balancer will only change to next endpoint per message.
The round robin load balancer is stateful as it keeps state of which endpoint to use next time.
Using the Fluent Builders
from("direct:start").loadBalance(). roundRobin().to("mock:x", "mock:y", "mock:z"); |
Using the Spring configuration
< route > < from uri="direct:start"/> < loadBalance > < roundRobin /> < to uri="mock:x"/> < to uri="mock:y"/> < to uri="mock:z"/> </ loadBalance > </ route > </ camelContext > |
The above example loads balance requests from direct:start to one of the available mock endpoint instances, in this case using a round robin policy.
For further examples of this pattern look at this junit test case
For further examples of this pattern look at this junit test case
Failover
The
You can constrain the
failover
load balancer is capable of trying the next processor in case an Exchange failed with an exception
during processing.You can constrain the
failover
to activate only when one exception of a list you specify occurs. If you do not specify a list any exception will cause fail over to occur. This balancer uses the same strategy for matching exceptions as the Exception Clause does for the onException.
Failover offers the following options:
Option
|
Type
|
Default
|
Description
|
---|---|---|---|
inheritErrorHandler
|
boolean
|
true
|
Camel 2.3: Whether or not the Error Handler configured on the route should be used. Disable this if you want failover to transfer immediately to the next endpoint. On the other hand, if you have this option enabled, then Camel will first let the Error Handler try to process the message. The Error Handler may have been configured to redeliver and use delays between attempts. If you have enabled a number of redeliveries then Camel will try to redeliver to the sameendpoint, and only fail over to the next endpoint, when the Error Handler is exhausted.
|
maximumFailoverAttempts
|
int
|
-1
|
Camel 2.3: A value to indicate after X failover attempts we should exhaust (give up). Use -1 to indicate never give up and continuously try to failover. Use 0 to never failover. And use e.g. 3 to failover at most 3 times before giving up. This option can be used whether or not roundRobin is enabled or not.
|
roundRobin
|
boolean
|
false
|
Camel 2.3: Whether or not the
failover load balancer should operate in round robin mode or not. If not, then it will always start from the first endpoint when a new message is to be processed. In other words it restart from the top for every message. If round robin is enabled, then it keeps state and will continue with the next endpoint in a round robin fashion. When using round robin it will not stick to last known good endpoint, it will always pick the next endpoint to use. |
Camel 2.2 or older behavior
The current implementation of failover load balancer uses simple logic which always tries the first endpoint, and in case of an exception being thrown it tries the next in the list, and so forth. It has no state, and the next message will thus always start with the first endpoint.
The current implementation of failover load balancer uses simple logic which always tries the first endpoint, and in case of an exception being thrown it tries the next in the list, and so forth. It has no state, and the next message will thus always start with the first endpoint.
Camel 2.3 onwards behavior
The
The
failover
load balancer now supports round robin mode, which allows you to failover in a round robin fashion. See the roundRobin
option.
Here is a sample to failover only if a
IOException
related exception was thrown:from("direct:start") // here we will load balance if IOException was thrown // any other kind of exception will result in the Exchange as failed // to failover over any kind of exception we can just omit the exception // in the failOver DSL .loadBalance().failover(IOException. class ) .to("direct:x", "direct:y", "direct:z"); |
You can specify multiple exceptions to failover as the option is varargs, for instance:
// enable redelivery so failover can react errorHandler(defaultErrorHandler().maximumRedeliveries( 5 )); from("direct:foo"). loadBalance().failover(IOException. class , MyOtherException. class ) .to("direct:a", "direct:b"); |
Using failover in Spring DSL
Failover can also be used from Spring DSL and you configure it as:
< route errorHandlerRef="myErrorHandler"> < from uri="direct:foo"/> < loadBalance > < failover > < exception >java.io.IOException</ exception > < exception >com.mycompany.MyOtherException</ exception > </ failover > < to uri="direct:a"/> < to uri="direct:b"/> </ loadBalance > </ route > |
Using failover in round robin mode
An example using Java DSL:
from("direct:start") // Use failover load balancer in stateful round robin mode // which mean it will failover immediately in case of an exception // as it does NOT inherit error handler. It will also keep retrying as // its configured to newer exhaust. .loadBalance().failover(- 1 , false , true ). to("direct:bad", "direct:bad2", "direct:good", "direct:good2"); |
And the same example using Spring XML:
< route > < from uri="direct:start"/> < loadBalance >
< failover roundRobin="true"/> < to uri="direct:bad"/> < to uri="direct:bad2"/> < to uri="direct:good"/> < to uri="direct:good2"/> </ loadBalance > </ route > |
Weighted Round-Robin and Random Load Balancing
Available as of Camel 2.5
In many enterprise environments where server nodes of unequal processing power & performance characteristics are utilized to host services and processing endpoints, it is frequently necessary to distribute processing load based on their individual server capabilities so that some endpoints are not unfairly burdened with requests. Obviously simple round-robin or random load balancing do not alleviate problems of this nature. A Weighted Round-Robin and/or Weighted Random load balancer can be used to address this problem.
The weighted load balancing policy allows you to specify a processing load distribution ratio for each server with respect to others. You can specify this as a positive processing weight for each server. A larger number indicates that the server can handle a larger load. The weight is utilized to determine the payload distribution ratio to different processing endpoints with respect to others.
The parameters that can be used are
In Camel 2.5
Option
|
Type
|
Default
|
Description
|
---|---|---|---|
roundRobin
|
boolean
|
false
|
The default value for round-robin is false. In the absence of this setting or parameter the load balancing algorithm used is random.
|
distributionRatio
|
List
|
none
|
The distributionRatio is a list consisting on integer weights passed in as a parameter. The distributionRatio must match the number of endpoints and/or processors specified in the load balancer list. In Camel 2.5 if endpoints do not match ratios, then a best effort distribution is attempted.
|
Available In Camel 2.6
Option
|
Type
|
Default
|
Description
|
---|---|---|---|
roundRobin
|
boolean
|
false
|
The default value for round-robin is false. In the absence of this setting or parameter the load balancing algorithm used is random.
|
distributionRatio
|
String
|
none
|
The distributionRatio is a delimited String consisting on integer weights separated by delimiters for example "2,3,5". The distributionRatio must match the number of endpoints and/or processors specified in the load balancer list.
|
distributionRatioDelimiter
|
String
|
,
|
The distributionRatioDelimiter is the delimiter used to specify the distributionRatio. If this attribute is not specified a default delimiter "," is expected as the delimiter used for specifying the distributionRatio.
|
Using Weighted round-robin & random load balancing
In Camel 2.5
An example using Java DSL:
ArrayList new ArrayList distributionRatio.add( 4 ); distributionRatio.add( 2 ); distributionRatio.add( 1 ); // round-robin from("direct:start") .loadBalance().weighted( true , distributionRatio) .to("mock:x", "mock:y", "mock:z"); //random from("direct:start") .loadBalance().weighted( false , distributionRatio) .to("mock:x", "mock:y", "mock:z"); |
And the same example using Spring XML:
< route > < from uri="direct:start"/> < loadBalance > < weighted roundRobin="false" distributionRatio="4 2 1"/> < to uri="mock:x"/> < to uri="mock:y"/> < to uri="mock:z"/> </ loadBalance > </ route > |
Available In Camel 2.6
An example using Java DSL:
// round-robin from("direct:start") .loadBalance().weighted( true , " 4 : 2 : 1 " distributionRatioDelimiter=":") .to("mock:x", "mock:y", "mock:z"); //random from("direct:start") .loadBalance().weighted( false , " 4 , 2 , 1 ") .to("mock:x", "mock:y", "mock:z"); |
And the same example using Spring XML:
< route > < from uri="direct:start"/> < loadBalance > < weighted roundRobin="false" distributionRatio="4-2-1" distributionRatioDelimiter="-" /> < to uri="mock:x"/> < to uri="mock:y"/> < to uri="mock:z"/> </ loadBalance > </ route > |
Custom Load Balancer
You can use a custom load balancer (eg your own implementation) also.
An example using Java DSL:
from("direct:start") // using our custom load balancer .loadBalance( new MyLoadBalancer()) .to("mock:x", "mock:y", "mock:z"); |
And the same example using XML DSL:
< bean id="myBalancer" class="org.apache.camel.processor.CustomLoadBalanceTest$MyLoadBalancer"/> < route > < from uri="direct:start"/> < loadBalance >
< custom ref="myBalancer"/>
< to uri="mock:x"/> < to uri="mock:y"/> < to uri="mock:z"/> </ loadBalance > </ route > </ camelContext > |
Notice in the XML DSL above we use which is only available in Camel 2.8 onwards. In older releases you would have to do as follows instead:
< loadBalance ref="myBalancer">
< to uri="mock:x"/> < to uri="mock:y"/> < to uri="mock:z"/> </ loadBalance > |
To implement a custom load balancer you can extend some support classes such as
LoadBalancerSupport
and SimpleLoadBalancerSupport
. The former supports the asynchronous routing engine, and the latter does not. Here is an example:
Custom load balancer implementation
public static class MyLoadBalancer extends LoadBalancerSupport { public boolean process(Exchange exchange, AsyncCallback callback) { String body = exchange.getIn().getBody(String. class ); try { if ("x".equals(body)) { getProcessors().get( 0 ).process(exchange); } else if ("y".equals(body)) { getProcessors().get( 1 ).process(exchange); } else { getProcessors().get( 2 ).process(exchange); } } catch (Throwable e) { exchange.setException(e); } callback.done( true ); return true ; } } |
Using This Pattern
If you would like to use this EIP Pattern then please read the Getting Started, you may also find the Architecture useful particularly the description of Endpoint and URIs. Then you could try out some of the Examples first before trying this pattern out.
No comments:
Post a Comment