spring-cloud-practice icon indicating copy to clipboard operation
spring-cloud-practice copied to clipboard

HystrixRequestVariableDefault 获取不到存入的数据

Open web-xiaxia opened this issue 8 years ago • 0 comments

把您的CoreHeaderInterceptor 搬过去就改了 存入类型 在 LabelAndWeightMetadataRule里面获取为空 ,是我哪没有 弄好么? ` public class DevInterceptor extends HandlerInterceptorAdapter { public static final String DEV_EUREKA_HEADER="DEV_EUREKA_HEADER"; public static final HystrixRequestVariableDefault<DevConfig> THREAD_LOCAL = new HystrixRequestVariableDefault<>();

public static void initHystrixRequestContext(DevConfig labels) {
    if (!HystrixRequestContext.isCurrentThreadInitialized()) {
        HystrixRequestContext.initializeContext();
    }
    labels=labels!=null?labels:new DevConfig();
    DevInterceptor.THREAD_LOCAL.set(labels);
}

public static void shutdownHystrixRequestContext() {
    if (HystrixRequestContext.isCurrentThreadInitialized()) {
        HystrixRequestContext.getContextForCurrentThread().shutdown();
    }
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    DevConfig devConfig=null;
    try {
        String header = request.getHeader(DEV_EUREKA_HEADER);
        if(StringUtils.isNotEmpty(header)){
            try {
                devConfig = JSON.parseObject(Base64.getDecoder().decode(header),DevConfig.class);
            }catch (Exception e){
                e.printStackTrace();
            }
        }

    }catch (Exception e){
        e.printStackTrace();
    }

    DevInterceptor.initHystrixRequestContext(devConfig);
    return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    DevInterceptor.shutdownHystrixRequestContext();
}

} `

` public class DevMetadataRule extends ZoneAvoidanceRule {

private Random random = new Random();

@Override
public Server choose(Object key) {
    DevConfig config = DevInterceptor.THREAD_LOCAL.get();
    //config  为null
}

} `

web-xiaxia avatar Dec 22 '17 07:12 web-xiaxia