From 89178bc913d03b92a03561dee18ff03b07b8edb0 Mon Sep 17 00:00:00 2001 From: fangjian0423 Date: Tue, 18 Dec 2018 21:36:14 +0800 Subject: [PATCH] update docs --- .../sentinel-core-example/readme-zh.md | 28 ++++++++++--------- .../sentinel-core-example/readme.md | 8 ++++-- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme-zh.md b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme-zh.md index af535e6d..bfdc599b 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme-zh.md +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme-zh.md @@ -122,28 +122,30 @@ ## 自定义限流处理逻辑 -1. URL 限流触发后默认处理逻辑是,直接返回 "Blocked by Sentinel (flow limiting)"。 +* 默认限流异常处理 + +URL 限流触发后默认处理逻辑是,直接返回 "Blocked by Sentinel (flow limiting)"。 如果需要自定义处理逻辑,实现的方式如下: - ```java - public class CustomUrlBlockHandler implements UrlBlockHandler { - @Override - public void blocked(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException { - // todo add your logic - } - } +```java +public class CustomUrlBlockHandler implements UrlBlockHandler { + @Override + public void blocked(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException { + // todo add your logic + } +} - WebCallbackManager.setUrlBlockHandler(new CustomUrlBlockHandler()); - ``` +WebCallbackManager.setUrlBlockHandler(new CustomUrlBlockHandler()); +``` -2. 自定义限流触发后,默认的处理逻辑是抛出异常。 +* 使用 `@SentinelResource` 注解下的限流异常处理 如果需要自定义处理逻辑,填写 `@SentinelResource` 注解的 `blockHandler` 属性(针对所有类型的 `BlockException`,需自行判断)或 `fallback` 属性(针对熔断降级异常),注意**对应方法的签名和位置有限制**,详情见 [Sentinel 注解支持文档](https://github.com/alibaba/Sentinel/wiki/%E6%B3%A8%E8%A7%A3%E6%94%AF%E6%8C%81#sentinelresource-%E6%B3%A8%E8%A7%A3)。示例实现如下: ```java public class TestService { - // blockHandler 是位于 ExceptionUtil 类下的 handleException 静态方法,需符合对应的类型限制. + // blockHandler 是位于 ExceptionUtil 类下的 handleException 静态方法,需符合对应的类型限制. @SentinelResource(value = "test", blockHandler = "handleException", blockHandlerClass = {ExceptionUtil.class}) public void test() { System.out.println("Test"); @@ -172,7 +174,7 @@ public final class ExceptionUtil { } ``` -一个简单的示例可以见 [sentinel-demo-annotation-spring-aop](https://github.com/alibaba/Sentinel/tree/master/sentinel-demo/sentinel-demo-annotation-spring-aop)。 +一个简单的 `@SentinelResource` 示例可以见 [sentinel-demo-annotation-spring-aop](https://github.com/alibaba/Sentinel/tree/master/sentinel-demo/sentinel-demo-annotation-spring-aop)。 ## Endpoint 信息查看 diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme.md b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme.md index e3135729..eb9ca2e0 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme.md +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme.md @@ -108,7 +108,9 @@ The screenshot belows shows invoke success: ## Customize Flow Control Logic -1. When a URL resource is blocked by Sentinel, the default logic is return HTTP response "Blocked by Sentinel (flow limiting)". +* Flow control exception handle by default + +When a URL resource is blocked by Sentinel, the default logic is return HTTP response "Blocked by Sentinel (flow limiting)". If you want to customize your flow control logic, see the code below: @@ -123,7 +125,9 @@ The screenshot belows shows invoke success: WebCallbackManager.setUrlBlockHandler(new CustomUrlBlockHandler()); -2. When a custom resource is blocked by Sentinel, the default logic is throw BlockException. +* Flow control exception handle by using `@SentinelResource` + +When a custom resource is blocked by Sentinel, the default logic is throw BlockException. If you want to customize your flow control logic, implement interface `SentinelExceptionHandler`, set @SentinelResource's blockHandler() and blockHandlerClass(). See the code below: