1
0
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:
pengbingting
2018-12-18 15:14:35 +08:00
parent 61e7a02665
commit 92f728176a
5 changed files with 63 additions and 33 deletions

View File

@@ -47,6 +47,16 @@
<artifactId>sentinel-parameter-flow-control</artifactId>
</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>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-datasource</artifactId>

View File

@@ -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 <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/
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<String, List<String>> 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<String, List<String>> headers = new HashMap<>();
headers.put(HttpHeaders.CONTENT_TYPE,
Arrays.asList(MediaType.APPLICATION_JSON_UTF8_VALUE));
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.putAll(headers);
return httpHeaders;
}
}