diff --git a/dubbo-cluster/src/main/java/com/alibaba/dubbo/rpc/cluster/support/SwitchCluster.java b/dubbo-cluster/src/main/java/com/alibaba/dubbo/rpc/cluster/support/SwitchCluster.java index c0063afdae571e82139eb0212c7466e7e2b9345f..4c19cae2dae2b64c2a29dab6aa08a83451f4eb0d 100644 --- a/dubbo-cluster/src/main/java/com/alibaba/dubbo/rpc/cluster/support/SwitchCluster.java +++ b/dubbo-cluster/src/main/java/com/alibaba/dubbo/rpc/cluster/support/SwitchCluster.java @@ -73,13 +73,7 @@ public class SwitchCluster implements Cluster { return effectiveInvokers; } - static final int WIN_FACTOR = 2; - - /** - * 优先返回前面的Inovker,除非后面的Invoker的invoker.count值 >= 2倍。 - * TODO Hard Code了因子 2! - */ - static Invoker getSuitableInvoker(List> invokers) { + static Invoker getSuitableInvoker(List> invokers, Directory directory) { if(invokers.isEmpty()) { throw new RpcException("No provider available in " + invokers); } @@ -87,13 +81,15 @@ public class SwitchCluster implements Cluster { return invokers.get(0); } + final double factor = directory.getUrl().getParameter( + Constants.CLUSTER_SWITCH_FACTOR, Constants.DEFAULT_CLUSTER_SWITCH_FACTOR); int i = 0; LOOP_BEFORE: for (; i < invokers.size(); i++) { Invoker before = invokers.get(i); for (int j = i + 1; j < invokers.size(); j++) { Invoker after = invokers.get(j); - if(WIN_FACTOR * getInvokerCount(before) <= getInvokerCount(after)) { + if(factor * getInvokerCount(before) <= getInvokerCount(after)) { // 被后面的打败了! 重找 continue LOOP_BEFORE; } @@ -108,7 +104,7 @@ public class SwitchCluster implements Cluster { return new AbstractClusterInvoker(directory) { public Result doInvoke(Invocation invocation, List> invokers, LoadBalance loadbalance) throws RpcException { List> effectiveInvokers = getEffectiveInvokers(invokers); - return getSuitableInvoker(effectiveInvokers).invoke(invocation); + return getSuitableInvoker(effectiveInvokers, directory).invoke(invocation); } }; } diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/Constants.java b/dubbo-common/src/main/java/com/alibaba/dubbo/common/Constants.java index fe5873f6d92d277648952b348188434682804bc4..ceebc0fd482354cac26433b0fe7f2b0e0f09e77c 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/Constants.java +++ b/dubbo-common/src/main/java/com/alibaba/dubbo/common/Constants.java @@ -408,6 +408,17 @@ public class Constants { */ public static final String INVOKER_INSIDE_INVOKER_COUNT_KEY = "inside.invoker.count"; + /** + * Switch cluster + * see SwitchCluster + */ + public static final String CLUSTER_SWITCH_FACTOR = "cluster.switch.factor"; + + /** + * default Switch cluster factor + */ + public static final double DEFAULT_CLUSTER_SWITCH_FACTOR = 2; + /** * 集群时是否排除非available的invoker */