Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
cgitp opened this issue Sep 12, 2017 · 0 comments
Open

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

cgitp opened this issue Sep 12, 2017 · 0 comments

Comments

@cgitp
Copy link

cgitp commented Sep 12, 2017

我调用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;
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant