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

@SentinelResource注释在同一类内的方法调用中无效

部分场景SentinelResource注解将无法生效的问题

@Service public class AsyncServiceImpl implements AsyncService {

@Override
@SentinelResource(value = "Async:doEcho", blockHandler = "doBlock")

public EchoResult echoV1(String mStr) {
    this.echoProviderAsync(mStr);
    return this.doEcho(mStr);
}

@Override
@SentinelResource(value = "Async:ZZZZ", blockHandler = "doBlock")
public EchoResult echoProviderAsync( String mStr) {
    EchoResult rs = new EchoResult();
    rs.setEcho("Hello Sync "+ mStr);
    return rs;
}

@SentinelResource(value = "Async:YYYY", blockHandler = "doBlock")
private EchoResult doEcho(String mStr) {
    EchoResult rs = new EchoResult();
    rs.setEcho("Hello "+ mStr);
    return rs;
}

private EchoResult doBlock(BlockException be) {
    EchoResult rs = new EchoResult();
    rs.setEcho("我被block了");
    return rs;
}

}

如代码块中的代码,echoV1和echoProviderAsync从外部调用的时候,可以进入切面,但是通过echoV1调用的this.echoProviderAsync以及this.doEcho方法,无法进入切面,与预期不符。

@SentinelResource annotation does not take effect in method calls within the same class: e.g. call this.echoProviderAsync method via echoV1 method.

原提问者GitHub用户fingthinking

展开
收起
码字王 2023-05-19 19:15:23 145 0
1 条回答
写回答
取消 提交回答
  • 同一类中的方法调用将不满足SpringAOP中的方面。您可以在此处参考解决方法:https://stackoverflow.com/questions/13564627/spring-aop-not-working-for-method-call-inside-another-method

    原回答者GitHub用户sczyh30

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

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

相关电子书

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