From 9154b07dbba04eabe39fdf1931862b193a4a429d Mon Sep 17 00:00:00 2001 From: "wangle_r@163.com" Date: Mon, 15 Oct 2018 15:14:07 +0800 Subject: [PATCH] @SentinelResource value Not required, Use the current method name when not specified --- .../sentinel/annotation/SentinelResource.java | 2 +- .../aspectj/SentinelResourceAspect.java | 25 +++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/annotation/SentinelResource.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/annotation/SentinelResource.java index 98a73c871d..f69f5fe85e 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/annotation/SentinelResource.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/annotation/SentinelResource.java @@ -37,7 +37,7 @@ /** * @return name of the Sentinel resource */ - String value(); + String value() default "";; /** * @return the entry type (inbound or outbound), outbound by default diff --git a/sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/SentinelResourceAspect.java b/sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/SentinelResourceAspect.java index 4a387e8810..c955b09122 100644 --- a/sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/SentinelResourceAspect.java +++ b/sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/SentinelResourceAspect.java @@ -59,7 +59,7 @@ public Object invokeResourceWithSentinel(ProceedingJoinPoint pjp) throws Throwab // Should not go through here. throw new IllegalStateException("Wrong state for SentinelResource annotation"); } - String resourceName = annotation.value(); + String resourceName = getResourceName(annotation.value(), originMethod); EntryType entryType = annotation.entryType(); Entry entry = null; try { @@ -76,7 +76,28 @@ public Object invokeResourceWithSentinel(ProceedingJoinPoint pjp) throws Throwab ContextUtil.exit(); } } - + + private String getResourceName(String resourceName, Method method) { + if(StringUtil.isNotBlank(resourceName)){ + return resourceName; + } + StringBuilder buf = new StringBuilder(64); + buf.append(method.getDeclaringClass().getName()) + .append(":") + .append(method.getName()) + .append("("); + boolean isFirst = true; + for (Class clazz : method.getParameterTypes()) { + if (!isFirst) { + buf.append(","); + } + buf.append(clazz.getName()); + isFirst = false; + } + buf.append(")"); + return buf.toString(); + } + private Object handleBlockException(ProceedingJoinPoint pjp, SentinelResource annotation, BlockException ex) throws Exception { // Execute fallback for degrading if configured.