com.alibaba.dubbo.rpc.cluster.LoadBalance Java Examples

The following examples show how to use com.alibaba.dubbo.rpc.cluster.LoadBalance. 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 dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
public Result invoke(final Invocation invocation) throws RpcException {

        checkWheatherDestoried();

        LoadBalance loadbalance;
        
        List<Invoker<T>> invokers = list(invocation);
        if (invokers != null && invokers.size() > 0) {
            loadbalance = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(invokers.get(0).getUrl()
                    .getMethodParameter(invocation.getMethodName(),Constants.LOADBALANCE_KEY, Constants.DEFAULT_LOADBALANCE));
        } else {
            loadbalance = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(Constants.DEFAULT_LOADBALANCE);
        }
        RpcUtils.attachInvocationIdIfAsync(getUrl(), invocation);
        return doInvoke(invocation, invokers, loadbalance);
    }
 
Example #2
Source File: AbstractClusterInvoker.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
/**
 * 使用loadbalance选择invoker.</br>
 * a)先lb选择,如果在selected列表中 或者 不可用且做检验时,进入下一步(重选),否则直接返回</br>
 * b)重选验证规则:selected > available .保证重选出的结果尽量不在select中,并且是可用的 
 * 
 * @param availablecheck 如果设置true,在选择的时候先选invoker.available == true
 * @param selected 已选过的invoker.注意:输入保证不重复
 * 
 */
protected Invoker<T> select(LoadBalance loadbalance, Invocation invocation, List<Invoker<T>> invokers, List<Invoker<T>> selected) throws RpcException {
    if (invokers == null || invokers.size() == 0)
        return null;
    String methodName = invocation == null ? "" : invocation.getMethodName();
    
    boolean sticky = invokers.get(0).getUrl().getMethodParameter(methodName,Constants.CLUSTER_STICKY_KEY, Constants.DEFAULT_CLUSTER_STICKY) ;
    {
        //ignore overloaded method
        if ( stickyInvoker != null && !invokers.contains(stickyInvoker) ){
            stickyInvoker = null;
        }
        //ignore cucurrent problem
        if (sticky && stickyInvoker != null && (selected == null || !selected.contains(stickyInvoker))){
            if (availablecheck && stickyInvoker.isAvailable()){
                return stickyInvoker;
            }
        }
    }
    Invoker<T> invoker = doselect(loadbalance, invocation, invokers, selected);
    
    if (sticky){
        stickyInvoker = invoker;
    }
    return invoker;
}
 
Example #3
Source File: AbstractClusterInvoker.java    From dubbox with Apache License 2.0 6 votes vote down vote up
/**
 * 使用loadbalance选择invoker.</br>
 * a)先lb选择,如果在selected列表中 或者 不可用且做检验时,进入下一步(重选),否则直接返回</br>
 * b)重选验证规则:selected > available .保证重选出的结果尽量不在select中,并且是可用的 
 * 
 * @param availablecheck 如果设置true,在选择的时候先选invoker.available == true
 * @param selected 已选过的invoker.注意:输入保证不重复
 * 
 */
protected Invoker<T> select(LoadBalance loadbalance, Invocation invocation, List<Invoker<T>> invokers, List<Invoker<T>> selected) throws RpcException {
    if (invokers == null || invokers.size() == 0)
        return null;
    String methodName = invocation == null ? "" : invocation.getMethodName();
    
    boolean sticky = invokers.get(0).getUrl().getMethodParameter(methodName,Constants.CLUSTER_STICKY_KEY, Constants.DEFAULT_CLUSTER_STICKY) ;
    {
        //ignore overloaded method
        if ( stickyInvoker != null && !invokers.contains(stickyInvoker) ){
            stickyInvoker = null;
        }
        //ignore cucurrent problem
        if (sticky && stickyInvoker != null && (selected == null || !selected.contains(stickyInvoker))){
            if (availablecheck && stickyInvoker.isAvailable()){
                return stickyInvoker;
            }
        }
    }
    Invoker<T> invoker = doselect(loadbalance, invocation, invokers, selected);
    
    if (sticky){
        stickyInvoker = invoker;
    }
    return invoker;
}
 
Example #4
Source File: AbstractClusterInvoker.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
public Result invoke(final Invocation invocation) throws RpcException {

        checkWheatherDestoried();

        LoadBalance loadbalance;
        
        List<Invoker<T>> invokers = list(invocation);
        if (invokers != null && invokers.size() > 0) {
            loadbalance = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(invokers.get(0).getUrl()
                    .getMethodParameter(invocation.getMethodName(),Constants.LOADBALANCE_KEY, Constants.DEFAULT_LOADBALANCE));
        } else {
            loadbalance = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(Constants.DEFAULT_LOADBALANCE);
        }
        RpcUtils.attachInvocationIdIfAsync(getUrl(), invocation);
        return doInvoke(invocation, invokers, loadbalance);
    }
 
Example #5
Source File: AbstractClusterInvoker.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public Result invoke(final Invocation invocation) throws RpcException {

        checkWheatherDestoried();

        LoadBalance loadbalance;
        
        List<Invoker<T>> invokers = list(invocation);
        if (invokers != null && invokers.size() > 0) {
            loadbalance = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(invokers.get(0).getUrl()
                    .getMethodParameter(invocation.getMethodName(),Constants.LOADBALANCE_KEY, Constants.DEFAULT_LOADBALANCE));
        } else {
            loadbalance = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(Constants.DEFAULT_LOADBALANCE);
        }
        RpcUtils.attachInvocationIdIfAsync(getUrl(), invocation);
        return doInvoke(invocation, invokers, loadbalance);
    }
 
Example #6
Source File: AbstractClusterInvokerTest.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Test
public void testBindingAttachment() {
    final String attachKey = "attach";
    final String attachValue = "value";

    // setup attachment
    RpcContext.getContext().setAttachment(attachKey, attachValue);
    Map<String, String> attachments = RpcContext.getContext().getAttachments();
    Assert.assertTrue("set attachment failed!", attachments != null && attachments.size() == 1);

    cluster = new AbstractClusterInvoker(dic) {
        @Override
        protected Result doInvoke(Invocation invocation, List invokers, LoadBalance loadbalance)
                throws RpcException {
            // attachment will be bind to invocation
            String value = invocation.getAttachment(attachKey);
            Assert.assertTrue("binding attachment failed!", value != null && value.equals(attachValue));
            return null;
        }
    };

    // invoke
    cluster.invoke(invocation);
}
 
Example #7
Source File: AbstractClusterInvoker.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
/**
 * 使用loadbalance选择invoker.</br>
 * a)先lb选择,如果在selected列表中 或者 不可用且做检验时,进入下一步(重选),否则直接返回</br>
 * b)重选验证规则:selected > available .保证重选出的结果尽量不在select中,并且是可用的 
 * 
 * @param availablecheck 如果设置true,在选择的时候先选invoker.available == true
 * @param selected 已选过的invoker.注意:输入保证不重复
 * 
 */
protected Invoker<T> select(LoadBalance loadbalance, Invocation invocation, List<Invoker<T>> invokers, List<Invoker<T>> selected) throws RpcException {
    if (invokers == null || invokers.size() == 0)
        return null;
    String methodName = invocation == null ? "" : invocation.getMethodName();
    
    boolean sticky = invokers.get(0).getUrl().getMethodParameter(methodName,Constants.CLUSTER_STICKY_KEY, Constants.DEFAULT_CLUSTER_STICKY) ;
    {
        //ignore overloaded method
        if ( stickyInvoker != null && !invokers.contains(stickyInvoker) ){
            stickyInvoker = null;
        }
        //ignore cucurrent problem
        if (sticky && stickyInvoker != null && (selected == null || !selected.contains(stickyInvoker))){
            if (availablecheck && stickyInvoker.isAvailable()){
                return stickyInvoker;
            }
        }
    }
    Invoker<T> invoker = doselect(loadbalance, invocation, invokers, selected);
    
    if (sticky){
        stickyInvoker = invoker;
    }
    return invoker;
}
 
Example #8
Source File: AbstractClusterInvoker.java    From dubbox with Apache License 2.0 6 votes vote down vote up
/**
 * 使用loadbalance选择invoker.</br>
 * a)先lb选择,如果在selected列表中 或者 不可用且做检验时,进入下一步(重选),否则直接返回</br>
 * b)重选验证规则:selected > available .保证重选出的结果尽量不在select中,并且是可用的 
 * 
 * @param availablecheck 如果设置true,在选择的时候先选invoker.available == true
 * @param selected 已选过的invoker.注意:输入保证不重复
 * 
 */
protected Invoker<T> select(LoadBalance loadbalance, Invocation invocation, List<Invoker<T>> invokers, List<Invoker<T>> selected) throws RpcException {
    if (invokers == null || invokers.size() == 0)
        return null;
    String methodName = invocation == null ? "" : invocation.getMethodName();
    
    boolean sticky = invokers.get(0).getUrl().getMethodParameter(methodName,Constants.CLUSTER_STICKY_KEY, Constants.DEFAULT_CLUSTER_STICKY) ;
    {
        //ignore overloaded method
        if ( stickyInvoker != null && !invokers.contains(stickyInvoker) ){
            stickyInvoker = null;
        }
        //ignore cucurrent problem
        if (sticky && stickyInvoker != null && (selected == null || !selected.contains(stickyInvoker))){
            if (availablecheck && stickyInvoker.isAvailable()){
                return stickyInvoker;
            }
        }
    }
    Invoker<T> invoker = doselect(loadbalance, invocation, invokers, selected);
    
    if (sticky){
        stickyInvoker = invoker;
    }
    return invoker;
}
 
Example #9
Source File: AbstractClusterInvoker.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public Result invoke(final Invocation invocation) throws RpcException {

        checkWheatherDestoried();

        LoadBalance loadbalance;
        
        List<Invoker<T>> invokers = list(invocation);
        if (invokers != null && invokers.size() > 0) {
            loadbalance = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(invokers.get(0).getUrl()
                    .getMethodParameter(invocation.getMethodName(),Constants.LOADBALANCE_KEY, Constants.DEFAULT_LOADBALANCE));
        } else {
            loadbalance = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(Constants.DEFAULT_LOADBALANCE);
        }
        RpcUtils.attachInvocationIdIfAsync(getUrl(), invocation);
        return doInvoke(invocation, invokers, loadbalance);
    }
 
Example #10
Source File: AbstractClusterInvoker.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public Result invoke(final Invocation invocation) throws RpcException {

    	checkWhetherDestroyed();

        LoadBalance loadbalance;
        
        List<Invoker<T>> invokers = list(invocation);
        if (invokers != null && invokers.size() > 0) {
            loadbalance = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(invokers.get(0).getUrl()
                    .getMethodParameter(invocation.getMethodName(),Constants.LOADBALANCE_KEY, Constants.DEFAULT_LOADBALANCE));
        } else {
            loadbalance = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(Constants.DEFAULT_LOADBALANCE);
        }
        RpcUtils.attachInvocationIdIfAsync(getUrl(), invocation);
        return doInvoke(invocation, invokers, loadbalance);
    }
 
Example #11
Source File: AvailableClusterInvoker.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public Result doInvoke(Invocation invocation, List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {
    for (Invoker<T> invoker : invokers) {
        if (invoker.isAvailable()) {
            return invoker.invoke(invocation);
        }
    }
    throw new RpcException("No provider available in " + invokers);
}
 
Example #12
Source File: FailfastClusterInvoker.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public Result doInvoke(Invocation invocation, List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {
    checkInvokers(invokers, invocation);
    Invoker<T> invoker = select(loadbalance, invocation, invokers, null);
    try {
        return invoker.invoke(invocation);
    } catch (Throwable e) {
        if (e instanceof RpcException && ((RpcException)e).isBiz()) { // biz exception.
            throw (RpcException) e;
        }
        throw new RpcException(e instanceof RpcException ? ((RpcException)e).getCode() : 0, "Failfast invoke providers " + invoker.getUrl() + " " + loadbalance.getClass().getSimpleName() + " select from all providers " + invokers + " for service " + getInterface().getName() + " method " + invocation.getMethodName() + " on consumer " + NetUtils.getLocalHost() + " use dubbo version " + Version.getVersion() + ", but no luck to perform the invocation. Last error is: " + e.getMessage(), e.getCause() != null ? e.getCause() : e);
    }
}
 
Example #13
Source File: FailbackClusterInvoker.java    From dubbox with Apache License 2.0 5 votes vote down vote up
protected Result doInvoke(Invocation invocation, List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {
    try {
        checkInvokers(invokers, invocation);
        Invoker<T> invoker = select(loadbalance, invocation, invokers, null);
        return invoker.invoke(invocation);
    } catch (Throwable e) {
        logger.error("Failback to invoke method " + invocation.getMethodName() + ", wait for retry in background. Ignored exception: "
                             + e.getMessage() + ", ", e);
        addFailed(invocation, this);
        return new RpcResult(); // ignore
    }
}
 
Example #14
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 #15
Source File: AvailableCluster.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public <T> Invoker<T> join(Directory<T> directory) throws RpcException {
    
    return new AbstractClusterInvoker<T>(directory) {
        public Result doInvoke(Invocation invocation, List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {
            for (Invoker<T> invoker : invokers) {
                if (invoker.isAvailable()) {
                    return invoker.invoke(invocation);
                }
            }
            throw new RpcException("No provider available in " + invokers);
        }
    };
    
}
 
Example #16
Source File: FailsafeClusterInvoker.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public Result doInvoke(Invocation invocation, List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {
    try {
        checkInvokers(invokers, invocation);
        Invoker<T> invoker = select(loadbalance, invocation, invokers, null);
        return invoker.invoke(invocation);
    } catch (Throwable e) {
        logger.error("Failsafe ignore exception: " + e.getMessage(), e);
        return new RpcResult(); // ignore
    }
}
 
Example #17
Source File: FailfastClusterInvoker.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
@Override
    public Result doInvoke(Invocation invocation, List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {
//        检查invoker是否可用
        checkInvokers(invokers, invocation);
//        执行负载均衡策略
        Invoker<T> invoker = select(loadbalance, invocation, invokers, null);
        try {
            return invoker.invoke(invocation);
        } catch (Throwable e) {
            if (e instanceof RpcException && ((RpcException) e).isBiz()) { // biz exception.
                throw (RpcException) e;
            }
            throw new RpcException(e instanceof RpcException ? ((RpcException) e).getCode() : 0, "Failfast invoke providers " + invoker.getUrl() + " " + loadbalance.getClass().getSimpleName() + " select from all providers " + invokers + " for service " + getInterface().getName() + " method " + invocation.getMethodName() + " on consumer " + NetUtils.getLocalHost() + " use dubbo version " + Version.getVersion() + ", but no luck to perform the invocation. Last error is: " + e.getMessage(), e.getCause() != null ? e.getCause() : e);
        }
    }
 
Example #18
Source File: AbstractClusterInvokerTest.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
/**
     * 测试均衡.
     */
    @Test
    public void testSelectBalance(){
        
        LoadBalance lb = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(RoundRobinLoadBalance.NAME);
        initlistsize5();
        
        Map<Invoker,AtomicLong> counter = new ConcurrentHashMap<Invoker,AtomicLong>();
        for(Invoker invoker :invokers){
            counter.put(invoker, new AtomicLong(0));
        }
        int runs = 1000;
        for(int i=0;i<runs;i++){
            selectedInvokers.clear();
            Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
            counter.get(sinvoker).incrementAndGet();
        }
        
        for (Invoker minvoker :counter.keySet() ){
            Long count = counter.get(minvoker).get();
//            System.out.println(count);
            if(minvoker.isAvailable())
                Assert.assertTrue("count should > avg", count>runs/invokers.size());
        }
        
        Assert.assertEquals(runs, counter.get(invoker2).get()+counter.get(invoker4).get());;
        
    }
 
Example #19
Source File: FileRouterEngineTest.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
@Override
protected Result doInvoke(Invocation invocation, List<Invoker<T>> invokers,
                          LoadBalance loadbalance) throws RpcException {
    Invoker<T> invoker = select(loadbalance, invocation, invokers, null);
    selectedInvoker = invoker;
    return null;
}
 
Example #20
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 #21
Source File: AvailableClusterInvoker.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
public Result doInvoke(Invocation invocation, List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {
    for (Invoker<T> invoker : invokers) {
        if (invoker.isAvailable()) {
            return invoker.invoke(invocation);
        }
    }
    throw new RpcException("No provider available in " + invokers);
}
 
Example #22
Source File: FailfastClusterInvoker.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
public Result doInvoke(Invocation invocation, List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {
    checkInvokers(invokers, invocation);
    Invoker<T> invoker = select(loadbalance, invocation, invokers, null);
    try {
        return invoker.invoke(invocation);
    } catch (Throwable e) {
        if (e instanceof RpcException && ((RpcException)e).isBiz()) { // biz exception.
            throw (RpcException) e;
        }
        throw new RpcException(e instanceof RpcException ? ((RpcException)e).getCode() : 0, "Failfast invoke providers " + invoker.getUrl() + " " + loadbalance.getClass().getSimpleName() + " select from all providers " + invokers + " for service " + getInterface().getName() + " method " + invocation.getMethodName() + " on consumer " + NetUtils.getLocalHost() + " use dubbo version " + Version.getVersion() + ", but no luck to perform the invocation. Last error is: " + e.getMessage(), e.getCause() != null ? e.getCause() : e);
    }
}
 
Example #23
Source File: AbstractClusterInvokerTest.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
/**
     * 测试均衡.
     */
    @Test
    public void testSelectBalance(){
        
        LoadBalance lb = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(RoundRobinLoadBalance.NAME);
        initlistsize5();
        
        Map<Invoker,AtomicLong> counter = new ConcurrentHashMap<Invoker,AtomicLong>();
        for(Invoker invoker :invokers){
            counter.put(invoker, new AtomicLong(0));
        }
        int runs = 1000;
        for(int i=0;i<runs;i++){
            selectedInvokers.clear();
            Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
            counter.get(sinvoker).incrementAndGet();
        }
        
        for (Invoker minvoker :counter.keySet() ){
            Long count = counter.get(minvoker).get();
//            System.out.println(count);
            if(minvoker.isAvailable())
                Assert.assertTrue("count should > avg", count>runs/invokers.size());
        }
        
        Assert.assertEquals(runs, counter.get(invoker2).get()+counter.get(invoker4).get());;
        
    }
 
Example #24
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 #25
Source File: FailsafeClusterInvoker.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
public Result doInvoke(Invocation invocation, List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {
    try {
        checkInvokers(invokers, invocation);
        Invoker<T> invoker = select(loadbalance, invocation, invokers, null);
        return invoker.invoke(invocation);
    } catch (Throwable e) {
        logger.error("Failsafe ignore exception: " + e.getMessage(), e);
        return new RpcResult(); // ignore
    }
}
 
Example #26
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 #27
Source File: AvailableCluster.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
public <T> Invoker<T> join(Directory<T> directory) throws RpcException {
    
    return new AbstractClusterInvoker<T>(directory) {
        public Result doInvoke(Invocation invocation, List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {
            for (Invoker<T> invoker : invokers) {
                if (invoker.isAvailable()) {
                    return invoker.invoke(invocation);
                }
            }
            throw new RpcException("No provider available in " + invokers);
        }
    };
    
}
 
Example #28
Source File: FailbackClusterInvoker.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
protected Result doInvoke(Invocation invocation, List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {
    try {
        checkInvokers(invokers, invocation);
        Invoker<T> invoker = select(loadbalance, invocation, invokers, null);
        return invoker.invoke(invocation);
    } catch (Throwable e) {
        logger.error("Failback to invoke method " + invocation.getMethodName() + ", wait for retry in background. Ignored exception: "
                             + e.getMessage() + ", ", e);
        addFailed(invocation, this);
        return new RpcResult(); // ignore
    }
}
 
Example #29
Source File: AvailableClusterInvoker.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
public Result doInvoke(Invocation invocation, List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {
    for (Invoker<T> invoker : invokers) {
        if (invoker.isAvailable()) {
            return invoker.invoke(invocation);
        }
    }
    throw new RpcException("No provider available in " + invokers);
}
 
Example #30
Source File: FailfastClusterInvoker.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
public Result doInvoke(Invocation invocation, List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {
    checkInvokers(invokers, invocation);
    Invoker<T> invoker = select(loadbalance, invocation, invokers, null);
    try {
        return invoker.invoke(invocation);
    } catch (Throwable e) {
        if (e instanceof RpcException && ((RpcException)e).isBiz()) { // biz exception.
            throw (RpcException) e;
        }
        throw new RpcException(e instanceof RpcException ? ((RpcException)e).getCode() : 0, "Failfast invoke providers " + invoker.getUrl() + " " + loadbalance.getClass().getSimpleName() + " select from all providers " + invokers + " for service " + getInterface().getName() + " method " + invocation.getMethodName() + " on consumer " + NetUtils.getLocalHost() + " use dubbo version " + Version.getVersion() + ", but no luck to perform the invocation. Last error is: " + e.getMessage(), e.getCause() != null ? e.getCause() : e);
    }
}