Giter Club home page Giter Club logo

dubboclub-qa's People

Stargazers

 avatar  avatar nobody avatar havejade avatar

Watchers

James Cloos avatar nobody avatar workwyz avatar  avatar 半夜郎中 avatar

dubboclub-qa's Issues

dubbo与Hystrix集成时,返回值类型转换异常问题

我调用dubbo服务返回值类型Map<String,String>

Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Map
at com.alibaba.dubbo.common.bytecode.proxy6.findAgentPayIsSuccess(proxy6.java) ~[na:2.5.3]

1:过滤器
package com.netease.hystrix.dubbo.rpc.filter;

import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.common.extension.Activate;
import com.alibaba.dubbo.rpc.Filter;
import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.Result;
import com.alibaba.dubbo.rpc.RpcException;

@activate(group = Constants.CONSUMER)
public class HystrixFilter implements Filter {

@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    DubboHystrixCommand command = new DubboHystrixCommand(invoker, invocation);
    return command.execute();
}

}

2:hystrix处理器

package com.netease.hystrix.dubbo.rpc.filter;

import org.apache.log4j.Logger;

import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.Result;
import com.alibaba.dubbo.rpc.RpcResult;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixCommandProperties;
import com.netflix.hystrix.HystrixThreadPoolProperties;

public class DubboHystrixCommand extends HystrixCommand {

private static Logger    logger                       = Logger.getLogger(DubboHystrixCommand.class);
private static final int DEFAULT_THREADPOOL_CORE_SIZE = 30;
private Invoker<?>       invoker;
private Invocation       invocation;

public DubboHystrixCommand(Invoker<?> invoker,Invocation invocation){
    super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(invoker.getInterface().getName()))
                .andCommandKey(HystrixCommandKey.Factory.asKey(String.format("%s_%d", invocation.getMethodName(),
                                                                             invocation.getArguments() == null ? 0 : invocation.getArguments().length)))
          .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                                        .withCircuitBreakerRequestVolumeThreshold(20)//10秒钟内至少19此请求失败,熔断器才发挥起作用
                                        .withCircuitBreakerSleepWindowInMilliseconds(30000)//熔断器中断请求30秒后会进入半打开状态,放部分流量过去重试
                                        .withCircuitBreakerErrorThresholdPercentage(50)//错误率达到50开启熔断保护
                                        .withExecutionTimeoutEnabled(true))//使用dubbo的超时,禁用这里的超时
          .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter().withCoreSize(getThreadPoolCoreSize(invoker.getUrl()))));//线程池为30
   
    
    this.invoker=invoker;
    this.invocation=invocation;
}

/**
 * 获取线程池大小
 * 
 * @param url
 * @return
 */
private static int getThreadPoolCoreSize(URL url) {
    if (url != null) {
        int size = url.getParameter("ThreadPoolCoreSize", DEFAULT_THREADPOOL_CORE_SIZE);
        if (logger.isDebugEnabled()) {
            logger.debug("ThreadPoolCoreSize:" + size);
        }
        return size;
    }

    return DEFAULT_THREADPOOL_CORE_SIZE;

}

@Override
protected Result run() throws Exception {
    return invoker.invoke(invocation);//
}

@Override
protected Result getFallback() {
  RpcResult rs = new  RpcResult();
  rs.setException(null);
  rs.setValue("降级了!");
return rs;
}

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.