开发者社区 > 云原生 > 正文

SentinelResource注释回退属性的错误

我使用@SentinelResource,并像下面这样设置回退。

@SentinelResource(value = "hello",fallback = "fallbackHandler")

在同一个类中定义了fallbackHandler方法。

public Response fallbackHandler(String name, String test, Throwable e){ return ResponseFactory.buildSuccessResponse(); }

但是并没有生效。

我发现AbastractStinelAspectSupport#findMethod确实不支持添加额外的Throwable参数。AbastractStinelAspectSupport#resolveFallbackInternal也没有处理回退方法参数。然后我删除了可抛出的论点,它有效。

private Method resolveFallbackInternal(ProceedingJoinPoint pjp, /*@NonNull*/ String name) {
    Method originMethod = resolveMethod(pjp);
    Class<?>[] parameterTypes = originMethod.getParameterTypes();
    return findMethod(false, pjp.getTarget().getClass(), name, originMethod.getReturnType(), parameterTypes);
}
private Method findMethod(boolean mustStatic, Class<?> clazz, String name, Class<?> returnType,
                          Class<?>... parameterTypes) {
    Method[] methods = clazz.getDeclaredMethods();
    for (Method method : methods) {
        if (name.equals(method.getName()) && checkStatic(mustStatic, method)
            && returnType.isAssignableFrom(method.getReturnType())
            && Arrays.equals(parameterTypes, method.getParameterTypes())) {

            RecordLog.info("Resolved method [{0}] in class [{1}]", name, clazz.getCanonicalName());
            return method;
        }
    }
    // Current class not found, find in the super classes recursively.
    Class<?> superClass = clazz.getSuperclass();
    if (superClass != null && !Object.class.equals(superClass)) {
        return findMethod(mustStatic, superClass, name, returnType, parameterTypes);
    } else {
        String methodType = mustStatic ? " static" : "";
        RecordLog.warn("Cannot find{0} method [{1}] in class [{2}] with parameters {3}",
            methodType, name, clazz.getCanonicalName(), Arrays.toString(parameterTypes));
        return null;
    }
}

原提问者GitHub用户tuser4198

展开
收起
码字王 2023-05-19 18:42:37 86 0
1 条回答
写回答
取消 提交回答
  • 1.6.0中引入了注释回退的改进,因此您必须升级到Sentinel1.6.x。

    原回答者GitHub用户sczyh30

    2023-05-19 22:44:14
    赞同 展开评论 打赏
问答地址:

阿里云拥有国内全面的云原生产品技术以及大规模的云原生应用实践,通过全面容器化、核心技术互联网化、应用 Serverless 化三大范式,助力制造业企业高效上云,实现系统稳定、应用敏捷智能。拥抱云原生,让创新无处不在。

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载