mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
sentinel support custom block response by sentinelClientHttpResponse
This commit is contained in:
parent
61e7a02665
commit
92f728176a
@ -146,6 +146,16 @@
|
|||||||
<artifactId>sentinel-dubbo-api</artifactId>
|
<artifactId>sentinel-dubbo-api</artifactId>
|
||||||
<version>${project.version}</version>
|
<version>${project.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.csp</groupId>
|
||||||
|
<artifactId>sentinel-cluster-server-default</artifactId>
|
||||||
|
<version>${sentinel.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.csp</groupId>
|
||||||
|
<artifactId>sentinel-cluster-client-default</artifactId>
|
||||||
|
<version>${sentinel.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
<!-- Aliyun OSS dependencies -->
|
<!-- Aliyun OSS dependencies -->
|
||||||
|
@ -165,6 +165,7 @@ public RestTemplate restTemplate() {
|
|||||||
其中 `blockHandler` 或 `fallback` 对应的方法必须是对应 `blockHandlerClass` 或 `fallbackClass` 中的静态方法。
|
其中 `blockHandler` 或 `fallback` 对应的方法必须是对应 `blockHandlerClass` 或 `fallbackClass` 中的静态方法。
|
||||||
|
|
||||||
该注解对应的方法参数跟 `ClientHttpRequestInterceptor` 接口中的参数一致,并多出了一个 `BlockException` 参数,且返回值是 `ClientHttpResponse`。
|
该注解对应的方法参数跟 `ClientHttpRequestInterceptor` 接口中的参数一致,并多出了一个 `BlockException` 参数,且返回值是 `ClientHttpResponse`。
|
||||||
|
被限流后如果不继续执行后面的 Http 请求拦截器,则可以通过 `SentinelClientHttpResponse` 的一个实例来自定义被限流后的返回值结果。
|
||||||
|
|
||||||
比如上述 `ExceptionUtil` 的 `handleException` 方法对应的声明如下:
|
比如上述 `ExceptionUtil` 的 `handleException` 方法对应的声明如下:
|
||||||
|
|
||||||
|
@ -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 `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`.
|
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:
|
The method signature of `handleException` in `ExceptionUtil` above should be like this:
|
||||||
|
|
||||||
|
@ -47,6 +47,16 @@
|
|||||||
<artifactId>sentinel-parameter-flow-control</artifactId>
|
<artifactId>sentinel-parameter-flow-control</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.csp</groupId>
|
||||||
|
<artifactId>sentinel-cluster-server-default</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.csp</groupId>
|
||||||
|
<artifactId>sentinel-cluster-client-default</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.cloud</groupId>
|
<groupId>org.springframework.cloud</groupId>
|
||||||
<artifactId>spring-cloud-alibaba-sentinel-datasource</artifactId>
|
<artifactId>spring-cloud-alibaba-sentinel-datasource</artifactId>
|
||||||
|
@ -16,6 +16,13 @@
|
|||||||
|
|
||||||
package org.springframework.cloud.alibaba.sentinel.rest;
|
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.ByteArrayInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@ -24,20 +31,21 @@ import java.util.HashMap;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
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}
|
* Using by {@link SentinelRestTemplate} and {@link SentinelProtectInterceptor}
|
||||||
|
*
|
||||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||||
*/
|
*/
|
||||||
public class SentinelClientHttpResponse extends AbstractClientHttpResponse {
|
public class SentinelClientHttpResponse extends AbstractClientHttpResponse {
|
||||||
|
|
||||||
private final String BLOCK_STR = "RestTemplate request block by sentinel";
|
private String blockResponse = "RestTemplate request block by sentinel";
|
||||||
|
|
||||||
|
public SentinelClientHttpResponse() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public SentinelClientHttpResponse(String blockResponse) {
|
||||||
|
this.blockResponse = blockResponse;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getRawStatusCode() throws IOException {
|
public int getRawStatusCode() throws IOException {
|
||||||
@ -46,7 +54,7 @@ public class SentinelClientHttpResponse extends AbstractClientHttpResponse {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getStatusText() throws IOException {
|
public String getStatusText() throws IOException {
|
||||||
return BLOCK_STR;
|
return blockResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -56,7 +64,7 @@ public class SentinelClientHttpResponse extends AbstractClientHttpResponse {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public InputStream getBody() throws IOException {
|
public InputStream getBody() throws IOException {
|
||||||
return new ByteArrayInputStream(BLOCK_STR.getBytes());
|
return new ByteArrayInputStream(blockResponse.getBytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
x
Reference in New Issue
Block a user