diff --git a/spring-cloud-alibaba-dependencies/pom.xml b/spring-cloud-alibaba-dependencies/pom.xml index a5ec3271..2d65d9d6 100644 --- a/spring-cloud-alibaba-dependencies/pom.xml +++ b/spring-cloud-alibaba-dependencies/pom.xml @@ -146,6 +146,16 @@ sentinel-dubbo-api ${project.version} + + com.alibaba.csp + sentinel-cluster-server-default + ${sentinel.version} + + + com.alibaba.csp + sentinel-cluster-client-default + ${sentinel.version} + diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/sentinel.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/sentinel.adoc index 0482e759..0d54a60f 100644 --- a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/sentinel.adoc +++ b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/sentinel.adoc @@ -165,6 +165,7 @@ public RestTemplate restTemplate() { 其中 `blockHandler` 或 `fallback` 对应的方法必须是对应 `blockHandlerClass` 或 `fallbackClass` 中的静态方法。 该注解对应的方法参数跟 `ClientHttpRequestInterceptor` 接口中的参数一致,并多出了一个 `BlockException` 参数,且返回值是 `ClientHttpResponse`。 +被限流后如果不继续执行后面的 Http 请求拦截器,则可以通过 `SentinelClientHttpResponse` 的一个实例来自定义被限流后的返回值结果。 比如上述 `ExceptionUtil` 的 `handleException` 方法对应的声明如下: diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc/sentinel.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc/sentinel.adoc index 982a5737..a604c3e7 100644 --- a/spring-cloud-alibaba-docs/src/main/asciidoc/sentinel.adoc +++ b/spring-cloud-alibaba-docs/src/main/asciidoc/sentinel.adoc @@ -167,6 +167,7 @@ The parameter of the `@SentinelRestTemplate` annotation support flow control(`bl The `blockHandler` or `fallback` is the static method of `blockHandlerClass` or `fallbackClass`. The parameter of method in `@SentinelRestTemplate` is same as `ClientHttpRequestInterceptor`, but it has one more parameter `BlockException` and its value of return type should be `ClientHttpResponse`. +If you do not continue to execute the following Http request interceptor after being restricted, you can use the instance of `SentinelClientHttpResponse` to customize the return value result after the current limit. The method signature of `handleException` in `ExceptionUtil` above should be like this: diff --git a/spring-cloud-alibaba-sentinel/pom.xml b/spring-cloud-alibaba-sentinel/pom.xml index f2068eb3..8b3afb91 100644 --- a/spring-cloud-alibaba-sentinel/pom.xml +++ b/spring-cloud-alibaba-sentinel/pom.xml @@ -47,6 +47,16 @@ sentinel-parameter-flow-control + + com.alibaba.csp + sentinel-cluster-server-default + + + + com.alibaba.csp + sentinel-cluster-client-default + + org.springframework.cloud spring-cloud-alibaba-sentinel-datasource diff --git a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/rest/SentinelClientHttpResponse.java b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/rest/SentinelClientHttpResponse.java index 76e4a3b8..03facf28 100644 --- a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/rest/SentinelClientHttpResponse.java +++ b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/rest/SentinelClientHttpResponse.java @@ -16,6 +16,13 @@ package org.springframework.cloud.alibaba.sentinel.rest; +import org.springframework.cloud.alibaba.sentinel.annotation.SentinelRestTemplate; +import org.springframework.cloud.alibaba.sentinel.custom.SentinelProtectInterceptor; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.client.AbstractClientHttpResponse; + import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; @@ -24,48 +31,49 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import org.springframework.cloud.alibaba.sentinel.annotation.SentinelRestTemplate; -import org.springframework.cloud.alibaba.sentinel.custom.SentinelProtectInterceptor; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; -import org.springframework.http.client.AbstractClientHttpResponse; - /** * Using by {@link SentinelRestTemplate} and {@link SentinelProtectInterceptor} + * * @author Jim */ public class SentinelClientHttpResponse extends AbstractClientHttpResponse { - private final String BLOCK_STR = "RestTemplate request block by sentinel"; + private String blockResponse = "RestTemplate request block by sentinel"; - @Override - public int getRawStatusCode() throws IOException { - return HttpStatus.OK.value(); - } + public SentinelClientHttpResponse() { + } - @Override - public String getStatusText() throws IOException { - return BLOCK_STR; - } + public SentinelClientHttpResponse(String blockResponse) { + this.blockResponse = blockResponse; + } - @Override - public void close() { - // nothing do - } + @Override + public int getRawStatusCode() throws IOException { + return HttpStatus.OK.value(); + } - @Override - public InputStream getBody() throws IOException { - return new ByteArrayInputStream(BLOCK_STR.getBytes()); - } + @Override + public String getStatusText() throws IOException { + return blockResponse; + } - @Override - public HttpHeaders getHeaders() { - Map> headers = new HashMap<>(); - headers.put(HttpHeaders.CONTENT_TYPE, - Arrays.asList(MediaType.APPLICATION_JSON_UTF8_VALUE)); - HttpHeaders httpHeaders = new HttpHeaders(); - httpHeaders.putAll(headers); - return httpHeaders; - } + @Override + public void close() { + // nothing do + } + + @Override + public InputStream getBody() throws IOException { + return new ByteArrayInputStream(blockResponse.getBytes()); + } + + @Override + public HttpHeaders getHeaders() { + Map> headers = new HashMap<>(); + headers.put(HttpHeaders.CONTENT_TYPE, + Arrays.asList(MediaType.APPLICATION_JSON_UTF8_VALUE)); + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.putAll(headers); + return httpHeaders; + } }