Java Code Examples for com.alibaba.dubbo.rpc.cluster.LoadBalance#select()

The following examples show how to use com.alibaba.dubbo.rpc.cluster.LoadBalance#select() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractClusterInvoker.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
private Invoker<T> doSelect(LoadBalance loadbalance, Invocation invocation, List<Invoker<T>> invokers, List<Invoker<T>> selected) throws RpcException {
        if (invokers == null || invokers.isEmpty())
            return null;
        if (invokers.size() == 1)
            return invokers.get(0);
        if (loadbalance == null) {
//            默认负载均衡策略 random
            loadbalance = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(Constants.DEFAULT_LOADBALANCE);
        }
        Invoker<T> invoker = loadbalance.select(invokers, getUrl(), invocation);

        //If the `invoker` is in the  `selected` or invoker is unavailable && availablecheck is true, reselect.
        if ((selected != null && selected.contains(invoker))
                || (!invoker.isAvailable() && getUrl() != null && availablecheck)) {
            try {
//                如果选择的执行器不可用,重新选择=》
                Invoker<T> rinvoker = reselect(loadbalance, invocation, invokers, selected, availablecheck);
                if (rinvoker != null) {
                    invoker = rinvoker;
                } else {
                    //Check the index of current selected invoker, if it's not the last one, choose the one at index+1. 检查当前选定调用程序的索引,如果不是最后一个,则选择index+1处的索引。
                    int index = invokers.indexOf(invoker);
                    try {
                        //Avoid collision
                        invoker = index < invokers.size() - 1 ? invokers.get(index + 1) : invokers.get(0);
                    } catch (Exception e) {
                        logger.warn(e.getMessage() + " may because invokers list dynamic change, ignore.", e);
                    }
                }
            } catch (Throwable t) {
                logger.error("cluster reselect fail reason is :" + t.getMessage() + " if can not solve, you can set cluster.availablecheck=false in url", t);
            }
        }
        return invoker;
    }
 
Example 2
Source File: LoadBalanceTest.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
private Map<Invoker, AtomicLong> getInvokeCounter(int runs, String loadbalanceName) {
    Map<Invoker, AtomicLong> counter = new ConcurrentHashMap<Invoker, AtomicLong>();
    LoadBalance lb = getLoadBalance(loadbalanceName);
    for (Invoker invoker : invokers) {
        counter.put(invoker, new AtomicLong(0));
    }
    URL url = invokers.get(0).getUrl();
    for (int i = 0; i < runs; i++) {
        Invoker sinvoker = lb.select(invokers, url, invocation);
        counter.get(sinvoker).incrementAndGet();
    }
    return counter;
}
 
Example 3
Source File: AbstractClusterInvoker.java    From dubbox with Apache License 2.0 5 votes vote down vote up
private Invoker<T> doselect(LoadBalance loadbalance, Invocation invocation, List<Invoker<T>> invokers, List<Invoker<T>> selected) throws RpcException {
    if (invokers == null || invokers.size() == 0)
        return null;
    if (invokers.size() == 1)
        return invokers.get(0);
    // 如果只有两个invoker,退化成轮循
    if (invokers.size() == 2 && selected != null && selected.size() > 0) {
        return selected.get(0) == invokers.get(0) ? invokers.get(1) : invokers.get(0);
    }
    Invoker<T> invoker = loadbalance.select(invokers, getUrl(), invocation);
    
    //如果 selected中包含(优先判断) 或者 不可用&&availablecheck=true 则重试.
    if( (selected != null && selected.contains(invoker))
            ||(!invoker.isAvailable() && getUrl()!=null && availablecheck)){
        try{
            Invoker<T> rinvoker = reselect(loadbalance, invocation, invokers, selected, availablecheck);
            if(rinvoker != null){
                invoker =  rinvoker;
            }else{
                //看下第一次选的位置,如果不是最后,选+1位置.
                int index = invokers.indexOf(invoker);
                try{
                    //最后在避免碰撞
                    invoker = index <invokers.size()-1?invokers.get(index+1) :invoker;
                }catch (Exception e) {
                    logger.warn(e.getMessage()+" may because invokers list dynamic change, ignore.",e);
                }
            }
        }catch (Throwable t){
            logger.error("clustor relselect fail reason is :"+t.getMessage() +" if can not slove ,you can set cluster.availablecheck=false in url",t);
        }
    }
    return invoker;
}
 
Example 4
Source File: LoadBalanceTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public Map<Invoker,AtomicLong> getInvokeCounter(int runs,String loadbalanceName) {
    Map<Invoker,AtomicLong> counter = new ConcurrentHashMap<Invoker,AtomicLong>();
    LoadBalance lb = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(loadbalanceName);
    for(Invoker invoker :invokers){
        counter.put(invoker, new AtomicLong(0));
    }
    for(int i=0;i<runs;i++){
        Invoker sinvoker = lb.select(invokers, invokers.get(0).getUrl(), invocation);
        counter.get(sinvoker).incrementAndGet();
    }
    return counter;
}
 
Example 5
Source File: AbstractClusterInvoker.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
private Invoker<T> doselect(LoadBalance loadbalance, Invocation invocation, List<Invoker<T>> invokers, List<Invoker<T>> selected) throws RpcException {
    if (invokers == null || invokers.size() == 0)
        return null;
    if (invokers.size() == 1)
        return invokers.get(0);
    // 如果只有两个invoker,退化成轮循
    if (invokers.size() == 2 && selected != null && selected.size() > 0) {
        return selected.get(0) == invokers.get(0) ? invokers.get(1) : invokers.get(0);
    }
    Invoker<T> invoker = loadbalance.select(invokers, getUrl(), invocation);
    
    //如果 selected中包含(优先判断) 或者 不可用&&availablecheck=true 则重试.
    if( (selected != null && selected.contains(invoker))
            ||(!invoker.isAvailable() && getUrl()!=null && availablecheck)){
        try{
            Invoker<T> rinvoker = reselect(loadbalance, invocation, invokers, selected, availablecheck);
            if(rinvoker != null){
                invoker =  rinvoker;
            }else{
                //看下第一次选的位置,如果不是最后,选+1位置.
                int index = invokers.indexOf(invoker);
                try{
                    //最后在避免碰撞
                    invoker = index <invokers.size()-1?invokers.get(index+1) :invoker;
                }catch (Exception e) {
                    logger.warn(e.getMessage()+" may because invokers list dynamic change, ignore.",e);
                }
            }
        }catch (Throwable t){
            logger.error("clustor relselect fail reason is :"+t.getMessage() +" if can not slove ,you can set cluster.availablecheck=false in url",t);
        }
    }
    return invoker;
}
 
Example 6
Source File: LoadBalanceTest.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
public Map<Invoker,AtomicLong> getInvokeCounter(int runs,String loadbalanceName) {
    Map<Invoker,AtomicLong> counter = new ConcurrentHashMap<Invoker,AtomicLong>();
    LoadBalance lb = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(loadbalanceName);
    for(Invoker invoker :invokers){
        counter.put(invoker, new AtomicLong(0));
    }
    for(int i=0;i<runs;i++){
        Invoker sinvoker = lb.select(invokers, invokers.get(0).getUrl(), invocation);
        counter.get(sinvoker).incrementAndGet();
    }
    return counter;
}
 
Example 7
Source File: AbstractClusterInvoker.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
private Invoker<T> doselect(LoadBalance loadbalance, Invocation invocation, List<Invoker<T>> invokers, List<Invoker<T>> selected) throws RpcException {
    if (invokers == null || invokers.size() == 0)
        return null;
    if (invokers.size() == 1)
        return invokers.get(0);
    // 如果只有两个invoker,退化成轮循
    if (invokers.size() == 2 && selected != null && selected.size() > 0) {
        return selected.get(0) == invokers.get(0) ? invokers.get(1) : invokers.get(0);
    }
    Invoker<T> invoker = loadbalance.select(invokers, getUrl(), invocation);
    
    //如果 selected中包含(优先判断) 或者 不可用&&availablecheck=true 则重试.
    if( (selected != null && selected.contains(invoker))
            ||(!invoker.isAvailable() && getUrl()!=null && availablecheck)){
        try{
            Invoker<T> rinvoker = reselect(loadbalance, invocation, invokers, selected, availablecheck);
            if(rinvoker != null){
                invoker =  rinvoker;
            }else{
                //看下第一次选的位置,如果不是最后,选+1位置.
                int index = invokers.indexOf(invoker);
                try{
                    //最后在避免碰撞
                    invoker = index <invokers.size()-1?invokers.get(index+1) :invoker;
                }catch (Exception e) {
                    logger.warn(e.getMessage()+" may because invokers list dynamic change, ignore.",e);
                }
            }
        }catch (Throwable t){
            logger.error("clustor relselect fail reason is :"+t.getMessage() +" if can not slove ,you can set cluster.availablecheck=false in url",t);
        }
    }
    return invoker;
}
 
Example 8
Source File: LoadBalanceTest.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
public Map<Invoker,AtomicLong> getInvokeCounter(int runs,String loadbalanceName) {
    Map<Invoker,AtomicLong> counter = new ConcurrentHashMap<Invoker,AtomicLong>();
    LoadBalance lb = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(loadbalanceName);
    for(Invoker invoker :invokers){
        counter.put(invoker, new AtomicLong(0));
    }
    for(int i=0;i<runs;i++){
        Invoker sinvoker = lb.select(invokers, invokers.get(0).getUrl(), invocation);
        counter.get(sinvoker).incrementAndGet();
    }
    return counter;
}
 
Example 9
Source File: AbstractClusterInvoker.java    From dubbox with Apache License 2.0 5 votes vote down vote up
private Invoker<T> doselect(LoadBalance loadbalance, Invocation invocation, List<Invoker<T>> invokers, List<Invoker<T>> selected) throws RpcException {
    if (invokers == null || invokers.size() == 0)
        return null;
    if (invokers.size() == 1)
        return invokers.get(0);
    // 如果只有两个invoker,退化成轮循
    if (invokers.size() == 2 && selected != null && selected.size() > 0) {
        return selected.get(0) == invokers.get(0) ? invokers.get(1) : invokers.get(0);
    }
    Invoker<T> invoker = loadbalance.select(invokers, getUrl(), invocation);
    
    //如果 selected中包含(优先判断) 或者 不可用&&availablecheck=true 则重试.
    if( (selected != null && selected.contains(invoker))
            ||(!invoker.isAvailable() && getUrl()!=null && availablecheck)){
        try{
            Invoker<T> rinvoker = reselect(loadbalance, invocation, invokers, selected, availablecheck);
            if(rinvoker != null){
                invoker =  rinvoker;
            }else{
                //看下第一次选的位置,如果不是最后,选+1位置.
                int index = invokers.indexOf(invoker);
                try{
                    //最后在避免碰撞
                    invoker = index <invokers.size()-1?invokers.get(index+1) :invoker;
                }catch (Exception e) {
                    logger.warn(e.getMessage()+" may because invokers list dynamic change, ignore.",e);
                }
            }
        }catch (Throwable t){
            logger.error("clustor relselect fail reason is :"+t.getMessage() +" if can not slove ,you can set cluster.availablecheck=false in url",t);
        }
    }
    return invoker;
}
 
Example 10
Source File: LoadBalanceTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public Map<Invoker,AtomicLong> getInvokeCounter(int runs,String loadbalanceName) {
    Map<Invoker,AtomicLong> counter = new ConcurrentHashMap<Invoker,AtomicLong>();
    LoadBalance lb = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(loadbalanceName);
    for(Invoker invoker :invokers){
        counter.put(invoker, new AtomicLong(0));
    }
    for(int i=0;i<runs;i++){
        Invoker sinvoker = lb.select(invokers, invokers.get(0).getUrl(), invocation);
        counter.get(sinvoker).incrementAndGet();
    }
    return counter;
}
 
Example 11
Source File: AbstractClusterInvoker.java    From dubbox with Apache License 2.0 5 votes vote down vote up
private Invoker<T> doselect(LoadBalance loadbalance, Invocation invocation, List<Invoker<T>> invokers, List<Invoker<T>> selected) throws RpcException {
    if (invokers == null || invokers.size() == 0)
        return null;
    if (invokers.size() == 1)
        return invokers.get(0);
    // 如果只有两个invoker,退化成轮循
    if (invokers.size() == 2 && selected != null && selected.size() > 0) {
        return selected.get(0) == invokers.get(0) ? invokers.get(1) : invokers.get(0);
    }
    Invoker<T> invoker = loadbalance.select(invokers, getUrl(), invocation);
    
    //如果 selected中包含(优先判断) 或者 不可用&&availablecheck=true 则重试.
    if( (selected != null && selected.contains(invoker))
            ||(!invoker.isAvailable() && getUrl()!=null && availablecheck)){
        try{
            Invoker<T> rinvoker = reselect(loadbalance, invocation, invokers, selected, availablecheck);
            if(rinvoker != null){
                invoker =  rinvoker;
            }else{
                //看下第一次选的位置,如果不是最后,选+1位置.
                int index = invokers.indexOf(invoker);
                try{
                    //最后在避免碰撞
                    invoker = index <invokers.size()-1?invokers.get(index+1) :invoker;
                }catch (Exception e) {
                    logger.warn(e.getMessage()+" may because invokers list dynamic change, ignore.",e);
                }
            }
        }catch (Throwable t){
            logger.error("clustor relselect fail reason is :"+t.getMessage() +" if can not slove ,you can set cluster.availablecheck=false in url",t);
        }
    }
    return invoker;
}
 
Example 12
Source File: LoadBalanceTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public Map<Invoker,AtomicLong> getInvokeCounter(int runs,String loadbalanceName) {
    Map<Invoker,AtomicLong> counter = new ConcurrentHashMap<Invoker,AtomicLong>();
    LoadBalance lb = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(loadbalanceName);
    for(Invoker invoker :invokers){
        counter.put(invoker, new AtomicLong(0));
    }
    for(int i=0;i<runs;i++){
        Invoker sinvoker = lb.select(invokers, invokers.get(0).getUrl(), invocation);
        counter.get(sinvoker).incrementAndGet();
    }
    return counter;
}