diff --git a/dubbo-cluster/src/main/java/com/alibaba/dubbo/rpc/cluster/loadbalance/RoundRobinLoadBalance.java b/dubbo-cluster/src/main/java/com/alibaba/dubbo/rpc/cluster/loadbalance/RoundRobinLoadBalance.java index ff65499be76..19443b8dd3f 100644 --- a/dubbo-cluster/src/main/java/com/alibaba/dubbo/rpc/cluster/loadbalance/RoundRobinLoadBalance.java +++ b/dubbo-cluster/src/main/java/com/alibaba/dubbo/rpc/cluster/loadbalance/RoundRobinLoadBalance.java @@ -13,10 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.dubbo.rpc.cluster.loadbalance; - -import java.util.ArrayList; +package com.alibaba.dubbo.rpc.cluster.loadbalance; + +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -24,59 +25,79 @@ import com.alibaba.dubbo.common.utils.AtomicPositiveInteger; import com.alibaba.dubbo.rpc.Invocation; import com.alibaba.dubbo.rpc.Invoker; - -/** - * Round robin load balance. - * - * @author qian.lei - * @author william.liangf - */ + +/** + * Round robin load balance. + * + * @author qian.lei + * @author william.liangf + */ public class RoundRobinLoadBalance extends AbstractLoadBalance { - - public static final String NAME = "roundrobin"; - - private final ConcurrentMap sequences = new ConcurrentHashMap(); - private final ConcurrentMap weightSequences = new ConcurrentHashMap(); - - protected Invoker doSelect(List> invokers, URL url, Invocation invocation) { - String key = invokers.get(0).getUrl().getServiceKey() + "." + invocation.getMethodName(); - int length = invokers.size(); // 总个数 - int maxWeight = 0; // 最大权重 - int minWeight = Integer.MAX_VALUE; // 最小权重 - for (int i = 0; i < length; i++) { - int weight = getWeight(invokers.get(i), invocation); - maxWeight = Math.max(maxWeight, weight); // 累计最大权重 - minWeight = Math.min(minWeight, weight); // 累计最小权重 - } - if (maxWeight > 0 && minWeight < maxWeight) { // 权重不一样 - AtomicPositiveInteger weightSequence = weightSequences.get(key); - if (weightSequence == null) { - weightSequences.putIfAbsent(key, new AtomicPositiveInteger()); - weightSequence = weightSequences.get(key); - } - int currentWeight = weightSequence.getAndIncrement() % maxWeight; - List> weightInvokers = new ArrayList>(); - for (Invoker invoker : invokers) { // 筛选权重大于当前权重基数的Invoker - if (getWeight(invoker, invocation) > currentWeight) { - weightInvokers.add(invoker); - } - } - int weightLength = weightInvokers.size(); - if (weightLength == 1) { - return weightInvokers.get(0); - } else if (weightLength > 1) { - invokers = weightInvokers; - length = invokers.size(); - } - } - AtomicPositiveInteger sequence = sequences.get(key); - if (sequence == null) { - sequences.putIfAbsent(key, new AtomicPositiveInteger()); - sequence = sequences.get(key); - } - // 取模轮循 - return invokers.get(sequence.getAndIncrement() % length); - } - + public static final String NAME = "roundrobin"; + + private final ConcurrentMap sequences = new ConcurrentHashMap(); + + private static final class IntegerWrapper { + public IntegerWrapper(int value) { + this.value = value; + } + + private int value; + + public int getValue() { + return value; + } + + public void setValue(int value) { + this.value = value; + } + + public void decrement() { + this.value--; + } + } + + protected Invoker doSelect(List> invokers, URL url, Invocation invocation) { + String key = invokers.get(0).getUrl().getServiceKey() + "." + invocation.getMethodName(); + int length = invokers.size(); // 总个数 + int maxWeight = 0; // 最大权重 + int minWeight = Integer.MAX_VALUE; // 最小权重 + final LinkedHashMap, IntegerWrapper> invokerToWeightMap = new LinkedHashMap, IntegerWrapper>(); + int weightSum = 0; + for (int i = 0; i < length; i++) { + int weight = getWeight(invokers.get(i), invocation); + maxWeight = Math.max(maxWeight, weight); // 累计最大权重 + minWeight = Math.min(minWeight, weight); // 累计最小权重 + if (weight > 0) { + invokerToWeightMap.put(invokers.get(i), new IntegerWrapper(weight)); + weightSum += weight; + } + } + AtomicPositiveInteger sequence = sequences.get(key); + if (sequence == null) { + sequences.putIfAbsent(key, new AtomicPositiveInteger()); + sequence = sequences.get(key); + } + int currentSequence = sequence.getAndIncrement(); + if (maxWeight > 0 && minWeight < maxWeight) { // 权重不一样 + int mod = currentSequence % weightSum; + for (int i = 0; i < maxWeight; i++) { + for (Map.Entry, IntegerWrapper> each : invokerToWeightMap.entrySet()) { + final Invoker k = each.getKey(); + final IntegerWrapper v = each.getValue(); + if (mod == 0 && v.getValue() > 0) { + return k; + } + if (v.getValue() > 0) { + v.decrement(); + mod--; + } + } + } + } + // 取模轮循 + return invokers.get(currentSequence % length); + } + } \ No newline at end of file diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/ReflectUtils.java b/dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/ReflectUtils.java index ccd6aa7b965..da886593950 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/ReflectUtils.java +++ b/dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/ReflectUtils.java @@ -784,9 +784,9 @@ private static Class[] desc2classArray(ClassLoader cl, String desc) throws Cl */ public static Method findMethodByMethodSignature(Class clazz, String methodName, String[] parameterTypes) throws NoSuchMethodException, ClassNotFoundException { - String signature = methodName; + String signature = clazz.getName() + "." + methodName; if(parameterTypes != null && parameterTypes.length > 0){ - signature = methodName + StringUtils.join(parameterTypes); + signature += StringUtils.join(parameterTypes); } Method method = Signature_METHODS_CACHE.get(signature); if(method != null){ diff --git a/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/ServiceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/ServiceConfig.java index 2d420fdd60d..8ac6e61964c 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/ServiceConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/ServiceConfig.java @@ -207,7 +207,7 @@ protected synchronized void doExport() { generic = Boolean.FALSE.toString(); } if(local !=null){ - if(local=="true"){ + if("true".equals(local)){ local=interfaceName+"Local"; } Class localClass; @@ -221,7 +221,7 @@ protected synchronized void doExport() { } } if(stub !=null){ - if(stub=="true"){ + if("true".equals(stub)){ stub=interfaceName+"Stub"; } Class stubClass;