轮询 策略
<route>
<from uri="jetty:http://{{ip}}:{{port}}/roundRobin" />
<removeHeaders pattern="CamelHttpUri" />
<camel:loadBalance>
<camel:roundRobin />
<to uri="bean:test?method=testA" />
<to uri="bean:test?method=testB" />
<to uri="bean:test?method=testC" />
</camel:loadBalance>
</route>
轮询调用 testA 、testB、testC
随机 策略
<route>
<from uri="jetty:http://{{ip}}:{{port}}/random" />
<removeHeaders pattern="CamelHttpUri" />
<camel:loadBalance>
<camel:random/>
<to uri="bean:test?method=testA" />
<to uri="bean:test?method=testB" />
<to uri="bean:test?method=testC" />
</camel:loadBalance>
</route>
随机调用 testA 、testB、testC
轮询+权重 策略
<route>
<from uri="jetty:http://{{ip}}:{{port}}/weighted" />
<removeHeaders pattern="CamelHttpUri" />
<camel:loadBalance>
<camel:weighted distributionRatio="4-2-1" distributionRatioDelimiter="-" roundRobin="true"/>
<to uri="bean:test?method=testA" />
<to uri="bean:test?method=testB" />
<to uri="bean:test?method=testC" />
</camel:loadBalance>
</route>
轮询调用 testA 、testB、testC,并按照权重 4、2、1进行调用。
自定义负载均衡策略
import java.util.List;
import java.util.Random;
import org.apache.camel.AsyncCallback;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.processor.loadbalancer.SimpleLoadBalancerSupport;
public class CustomLoadBalanceTest extends SimpleLoadBalancerSupport {
public boolean process(Exchange exchange, AsyncCallback callback) {
System.out.println("异步处理,负载均衡");
try {
List<Processor> processorsList = getProcessors();
Random random = new Random();
int index = random.nextInt(processorsList.size());
System.out.println("调用路由:" + (index + 1));
//选取其中一个调用
getProcessors().get(index).process(exchange);
} catch (Exception e) {
e.printStackTrace();
}
callback.done(true);
return true;
}
@Override
public void process(Exchange exchange) throws Exception {
System.out.println("同步处理,负载均衡");
}
}
<bean id='myBalancer' class='com.xiaoka.camel.spring.load.balance.CustomLoadBalanceTest' />
在spring中配置使用
<route>
<from uri="jetty:http://{{ip}}:{{port}}/custom" />
<removeHeaders pattern="CamelHttpUri" />
<camel:loadBalance>
<camel:custom ref="myBalancer"/>
<to uri="bean:test?method=testA" />
<to uri="bean:test?method=testB" />
<to uri="bean:test?method=testC" />
</camel:loadBalance>
</route>
会按照 用户自定义的策略 来 调用 testA 、testB、testC