From 078df9db4faca7f61b16f0939f9bc061c3906b00 Mon Sep 17 00:00:00 2001 From: waveng Date: Wed, 24 Oct 2018 09:42:51 +0800 Subject: [PATCH] Sentinel annotation supports method name as default resource name (#187) --- .../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 98a73c87..f69f5fe8 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 @@ public @interface SentinelResource { /** * @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 4a387e88..c955b091 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 class SentinelResourceAspect { // 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 class SentinelResourceAspect { 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.