From ba101f479ed6330371f11e04807a96c22786ce1c Mon Sep 17 00:00:00 2001 From: fangjian0423 Date: Tue, 18 Dec 2018 22:20:52 +0800 Subject: [PATCH 1/3] calculate exception ratio in Sentinel RestTemplate --- .../custom/SentinelProtectInterceptor.java | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/custom/SentinelProtectInterceptor.java b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/custom/SentinelProtectInterceptor.java index f21da49b5..2bfb39e3a 100644 --- a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/custom/SentinelProtectInterceptor.java +++ b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/custom/SentinelProtectInterceptor.java @@ -33,6 +33,7 @@ import org.springframework.util.ClassUtils; import com.alibaba.csp.sentinel.Entry; import com.alibaba.csp.sentinel.SphU; +import com.alibaba.csp.sentinel.Tracer; import com.alibaba.csp.sentinel.context.ContextUtil; import com.alibaba.csp.sentinel.slots.block.BlockException; import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException; @@ -69,16 +70,24 @@ public class SentinelProtectInterceptor implements ClientHttpRequestInterceptor hostEntry = SphU.entry(hostResource); response = execution.execute(request, body); } - catch (BlockException e) { - try { - return handleBlockException(request, body, execution, e); + catch (Throwable e) { + if (!BlockException.isBlockException(e)) { + Tracer.trace(e); + throw new IllegalStateException(e); } - catch (Exception ex) { - if (ex instanceof IllegalStateException) { - throw (IllegalStateException) ex; + else { + try { + return handleBlockException(request, body, execution, + (BlockException) e); + } + catch (Exception ex) { + if (ex instanceof IllegalStateException) { + throw (IllegalStateException) ex; + } + throw new IllegalStateException( + "sentinel handle BlockException error: " + ex.getMessage(), + ex); } - throw new IllegalStateException( - "sentinel handle BlockException error: " + ex.getMessage(), ex); } } finally { From 46e4d71b22c2e6baf3613fc0fd977faeea8baf7c Mon Sep 17 00:00:00 2001 From: fangjian0423 Date: Tue, 18 Dec 2018 23:00:55 +0800 Subject: [PATCH 2/3] remove duplicate entry when request path is empty --- .../sentinel/custom/SentinelProtectInterceptor.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/custom/SentinelProtectInterceptor.java b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/custom/SentinelProtectInterceptor.java index 2bfb39e3a..18a895de7 100644 --- a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/custom/SentinelProtectInterceptor.java +++ b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/custom/SentinelProtectInterceptor.java @@ -62,11 +62,17 @@ public class SentinelProtectInterceptor implements ClientHttpRequestInterceptor String hostResource = uri.getScheme() + "://" + uri.getHost() + (uri.getPort() == -1 ? "" : ":" + uri.getPort()); String hostWithPathResource = hostResource + uri.getPath(); + boolean entryWithPath = true; + if (hostResource.equals(hostWithPathResource)) { + entryWithPath = false; + } Entry hostEntry = null, hostWithPathEntry = null; ClientHttpResponse response; try { - ContextUtil.enter(hostWithPathResource); - hostWithPathEntry = SphU.entry(hostWithPathResource); + ContextUtil.enter(hostResource); + if (entryWithPath) { + hostWithPathEntry = SphU.entry(hostWithPathResource); + } hostEntry = SphU.entry(hostResource); response = execution.execute(request, body); } From 179a517e2d00b14ff79c5d07cf426cc37b47ee6b Mon Sep 17 00:00:00 2001 From: fangjian0423 Date: Tue, 18 Dec 2018 23:27:31 +0800 Subject: [PATCH 3/3] using resource with path in Context --- .../alibaba/sentinel/custom/SentinelProtectInterceptor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/custom/SentinelProtectInterceptor.java b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/custom/SentinelProtectInterceptor.java index 18a895de7..a1ab9e22d 100644 --- a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/custom/SentinelProtectInterceptor.java +++ b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/custom/SentinelProtectInterceptor.java @@ -69,7 +69,7 @@ public class SentinelProtectInterceptor implements ClientHttpRequestInterceptor Entry hostEntry = null, hostWithPathEntry = null; ClientHttpResponse response; try { - ContextUtil.enter(hostResource); + ContextUtil.enter(hostWithPathResource); if (entryWithPath) { hostWithPathEntry = SphU.entry(hostWithPathResource); }