1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

replace code properties that have expired

This commit is contained in:
冷冷 2019-12-03 21:36:21 +08:00
parent cc053a520a
commit ffb246829f
5 changed files with 17 additions and 28 deletions

View File

@ -33,16 +33,16 @@ public class ConsumeMediaTypeExpressionTest
public void testMatch() { public void testMatch() {
ConsumeMediaTypeExpression expression = createExpression(MediaType.ALL_VALUE); ConsumeMediaTypeExpression expression = createExpression(MediaType.ALL_VALUE);
Assert.assertTrue(expression.match(MediaType.APPLICATION_JSON_UTF8)); Assert.assertTrue(expression.match(MediaType.APPLICATION_JSON));
expression = createExpression(MediaType.APPLICATION_JSON_VALUE); expression = createExpression(MediaType.APPLICATION_JSON_VALUE);
Assert.assertTrue(expression.match(MediaType.APPLICATION_JSON_UTF8)); Assert.assertTrue(expression.match(MediaType.APPLICATION_JSON));
expression = createExpression(MediaType.APPLICATION_JSON_VALUE + ";q=0.7"); expression = createExpression(MediaType.APPLICATION_JSON_VALUE + ";q=0.7");
Assert.assertTrue(expression.match(MediaType.APPLICATION_JSON_UTF8)); Assert.assertTrue(expression.match(MediaType.APPLICATION_JSON));
expression = createExpression(MediaType.TEXT_HTML_VALUE); expression = createExpression(MediaType.TEXT_HTML_VALUE);
Assert.assertFalse(expression.match(MediaType.APPLICATION_JSON_UTF8)); Assert.assertFalse(expression.match(MediaType.APPLICATION_JSON));
} }
} }

View File

@ -25,7 +25,7 @@ import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.ServerResponse; import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
import static org.springframework.web.reactive.function.BodyInserters.fromObject; import static org.springframework.web.reactive.function.BodyInserters.fromValue;
/** /**
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a> * @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
@ -39,9 +39,8 @@ public class MySCGConfiguration {
@Override @Override
public Mono<ServerResponse> handleRequest(ServerWebExchange exchange, public Mono<ServerResponse> handleRequest(ServerWebExchange exchange,
Throwable t) { Throwable t) {
return ServerResponse.status(444) return ServerResponse.status(444).contentType(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON_UTF8) .body(fromValue("SCS Sentinel block"));
.body(fromObject("SCS Sentinel block"));
} }
}; };
} }

View File

@ -26,7 +26,7 @@ import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.ServerResponse; import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
import static org.springframework.web.reactive.function.BodyInserters.fromObject; import static org.springframework.web.reactive.function.BodyInserters.fromValue;
/** /**
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a> * @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
@ -41,8 +41,7 @@ public class MyConfiguration {
public Mono<ServerResponse> handleRequest(ServerWebExchange exchange, public Mono<ServerResponse> handleRequest(ServerWebExchange exchange,
Throwable t) { Throwable t) {
return ServerResponse.status(HttpStatus.TOO_MANY_REQUESTS) return ServerResponse.status(HttpStatus.TOO_MANY_REQUESTS)
.contentType(MediaType.APPLICATION_JSON_UTF8) .contentType(MediaType.APPLICATION_JSON).body(fromValue("block"));
.body(fromObject("block"));
} }
}; };
} }

View File

@ -48,7 +48,7 @@ public class FallbackProperties {
/** /**
* Content-Type for `response` mode. * Content-Type for `response` mode.
*/ */
private String contentType = MediaType.APPLICATION_JSON_UTF8.toString(); private String contentType = MediaType.APPLICATION_JSON.toString();
public String getMode() { public String getMode() {
return mode; return mode;

View File

@ -33,7 +33,6 @@ import com.alibaba.csp.sentinel.config.SentinelConfig;
import com.alibaba.csp.sentinel.util.StringUtil; import com.alibaba.csp.sentinel.util.StringUtil;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -50,9 +49,8 @@ import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerCodecConfigurer; import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.web.reactive.function.server.ServerResponse; import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.reactive.result.view.ViewResolver; import org.springframework.web.reactive.result.view.ViewResolver;
import org.springframework.web.server.ServerWebExchange;
import static org.springframework.web.reactive.function.BodyInserters.fromObject; import static org.springframework.web.reactive.function.BodyInserters.fromValue;
/** /**
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a> * @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
@ -93,8 +91,7 @@ public class SentinelSCGAutoConfiguration {
} }
private void initAppType() { private void initAppType() {
System.setProperty(SentinelConfig.APP_TYPE, System.setProperty(SentinelConfig.APP_TYPE, ConfigConstants.APP_TYPE_SCG_GATEWAY);
String.valueOf(ConfigConstants.APP_TYPE_SCG_GATEWAY));
} }
private void initFallback() { private void initFallback() {
@ -105,17 +102,11 @@ public class SentinelSCGAutoConfiguration {
} }
if (ConfigConstants.FALLBACK_MSG_RESPONSE.equals(fallbackProperties.getMode())) { if (ConfigConstants.FALLBACK_MSG_RESPONSE.equals(fallbackProperties.getMode())) {
if (StringUtil.isNotBlank(fallbackProperties.getResponseBody())) { if (StringUtil.isNotBlank(fallbackProperties.getResponseBody())) {
GatewayCallbackManager.setBlockHandler(new BlockRequestHandler() { GatewayCallbackManager.setBlockHandler((exchange, t) -> ServerResponse
@Override
public Mono<ServerResponse> handleRequest(ServerWebExchange exchange,
Throwable t) {
return ServerResponse
.status(fallbackProperties.getResponseStatus()) .status(fallbackProperties.getResponseStatus())
.contentType(MediaType .contentType(
.valueOf(fallbackProperties.getContentType())) MediaType.valueOf(fallbackProperties.getContentType()))
.body(fromObject(fallbackProperties.getResponseBody())); .body(fromValue(fallbackProperties.getResponseBody())));
}
});
logger.info( logger.info(
"[Sentinel SpringCloudGateway] using AnonymousBlockRequestHandler, responseStatus: " "[Sentinel SpringCloudGateway] using AnonymousBlockRequestHandler, responseStatus: "
+ fallbackProperties.getResponseStatus() + fallbackProperties.getResponseStatus()