From 8a30ac6d2788001f027bb3e84b1e9a33cb1a2fa1 Mon Sep 17 00:00:00 2001 From: hongqiaowei Date: Mon, 11 Oct 2021 15:38:12 +0800 Subject: [PATCH] Code optimization --- .../java/we/FizzBootstrapApplication.java | 3 +- .../src/main/java/we/util/ReactorUtils.java | 7 +- fizz-core/src/main/java/we/Fizz.java | 29 ++++++ .../src/main/java/we/FizzAppContext.java | 6 ++ .../filter/FilterExceptionHandlerConfig.java | 18 ++-- .../main/java/we/filter/PreprocessFilter.java | 90 ++++++++----------- .../java/we/plugin/FizzPluginFilterChain.java | 28 +++--- .../java/we/plugin/stat/StatPluginFilter.java | 44 ++++----- .../stat/StatPluginFilterProperties.java | 3 + .../src/main/java/we/proxy/FizzWebClient.java | 8 +- 10 files changed, 130 insertions(+), 106 deletions(-) create mode 100644 fizz-core/src/main/java/we/Fizz.java diff --git a/fizz-bootstrap/src/main/java/we/FizzBootstrapApplication.java b/fizz-bootstrap/src/main/java/we/FizzBootstrapApplication.java index 8564f9c..ef0e335 100644 --- a/fizz-bootstrap/src/main/java/we/FizzBootstrapApplication.java +++ b/fizz-bootstrap/src/main/java/we/FizzBootstrapApplication.java @@ -190,7 +190,8 @@ public class FizzBootstrapApplication { SpringApplication springApplication = new SpringApplication(FizzBootstrapApplication.class); springApplication.setApplicationContextClass(CustomReactiveWebServerApplicationContext.class); - FizzAppContext.appContext = springApplication.run(args); + Fizz.context = springApplication.run(args); + FizzAppContext.appContext = Fizz.context; } private static class CustomReactiveWebServerApplicationContext extends AnnotationConfigReactiveWebServerApplicationContext { diff --git a/fizz-common/src/main/java/we/util/ReactorUtils.java b/fizz-common/src/main/java/we/util/ReactorUtils.java index d91fbb3..b9c3e2b 100644 --- a/fizz-common/src/main/java/we/util/ReactorUtils.java +++ b/fizz-common/src/main/java/we/util/ReactorUtils.java @@ -30,16 +30,19 @@ public abstract class ReactorUtils { public static final Object NULL = OBJ; + public static final Object Void = OBJ; + + @Deprecated public static final Throwable EMPTY_THROWABLE = Utils.throwableWithoutStack(null); private ReactorUtils() { } - public static Mono getInitiateMono() { + public static Mono getInitiateMono() { return Mono.just(OBJ); } - public static Flux getInitiateFlux() { + public static Flux getInitiateFlux() { return Flux.just(OBJ); } } diff --git a/fizz-core/src/main/java/we/Fizz.java b/fizz-core/src/main/java/we/Fizz.java new file mode 100644 index 0000000..0b8d0da --- /dev/null +++ b/fizz-core/src/main/java/we/Fizz.java @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2020 the original author or authors. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package we; + +import org.springframework.context.ConfigurableApplicationContext; + +/** + * @author hongqiaowei + */ + +public class Fizz { + + public static ConfigurableApplicationContext context; +} diff --git a/fizz-core/src/main/java/we/FizzAppContext.java b/fizz-core/src/main/java/we/FizzAppContext.java index 7e3705c..e011348 100644 --- a/fizz-core/src/main/java/we/FizzAppContext.java +++ b/fizz-core/src/main/java/we/FizzAppContext.java @@ -14,10 +14,16 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ + package we; import org.springframework.context.ConfigurableApplicationContext; +/** + * @deprecated and use {@link we.Fizz} instead + */ + +@Deprecated public class FizzAppContext { public static ConfigurableApplicationContext appContext; diff --git a/fizz-core/src/main/java/we/filter/FilterExceptionHandlerConfig.java b/fizz-core/src/main/java/we/filter/FilterExceptionHandlerConfig.java index e00395e..bdc8414 100644 --- a/fizz-core/src/main/java/we/filter/FilterExceptionHandlerConfig.java +++ b/fizz-core/src/main/java/we/filter/FilterExceptionHandlerConfig.java @@ -82,29 +82,33 @@ public class FilterExceptionHandlerConfig { } } + String tMsg = t.getMessage(); + if (tMsg == null) { + tMsg = t.toString(); + } if (t instanceof ExecuteScriptException) { ExecuteScriptException ex = (ExecuteScriptException) t; resp.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); RespEntity rs = null; if (ex.getStepContext() != null && ex.getStepContext().returnContext()) { - rs = new RespEntity(HttpStatus.INTERNAL_SERVER_ERROR.value(), t.getMessage(), traceId, ex.getStepContext()); + rs = new RespEntity(HttpStatus.INTERNAL_SERVER_ERROR.value(), tMsg, traceId, ex.getStepContext()); return resp.writeWith(Mono.just(resp.bufferFactory().wrap(JacksonUtils.writeValueAsString(rs).getBytes()))); } else { - rs = new RespEntity(HttpStatus.INTERNAL_SERVER_ERROR.value(), t.getMessage(), traceId); + rs = new RespEntity(HttpStatus.INTERNAL_SERVER_ERROR.value(), tMsg, traceId); return resp.writeWith(Mono.just(resp.bufferFactory().wrap(rs.toString().getBytes()))); } } if (t instanceof FizzRuntimeException) { FizzRuntimeException ex = (FizzRuntimeException) t; - log.error(ex.getMessage(), LogService.BIZ_ID, traceId, ex); + log.error(tMsg, LogService.BIZ_ID, traceId, ex); resp.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); RespEntity rs = null; if (ex.getStepContext() != null && ex.getStepContext().returnContext()) { - rs = new RespEntity(HttpStatus.INTERNAL_SERVER_ERROR.value(), t.getMessage(), traceId, ex.getStepContext()); + rs = new RespEntity(HttpStatus.INTERNAL_SERVER_ERROR.value(), tMsg, traceId, ex.getStepContext()); return resp.writeWith(Mono.just(resp.bufferFactory().wrap(JacksonUtils.writeValueAsString(rs).getBytes()))); } else { - rs = new RespEntity(HttpStatus.INTERNAL_SERVER_ERROR.value(), t.getMessage(), traceId); + rs = new RespEntity(HttpStatus.INTERNAL_SERVER_ERROR.value(), tMsg, traceId); return resp.writeWith(Mono.just(resp.bufferFactory().wrap(rs.toString().getBytes()))); } } @@ -115,11 +119,11 @@ public class FilterExceptionHandlerConfig { StringBuilder b = ThreadContext.getStringBuilder(); WebUtils.request2stringBuilder(exchange, b); log.error(b.toString(), LogService.BIZ_ID, traceId, t); - String s = WebUtils.jsonRespBody(HttpStatus.INTERNAL_SERVER_ERROR.value(), t.getMessage(), traceId); + String s = WebUtils.jsonRespBody(HttpStatus.INTERNAL_SERVER_ERROR.value(), tMsg, traceId); resp.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); vm = resp.writeWith(Mono.just(resp.bufferFactory().wrap(s.getBytes()))); } else { - vm = WebUtils.responseError(exchange, filterExceptionHandler, HttpStatus.INTERNAL_SERVER_ERROR.value(), t.getMessage(), t); + vm = WebUtils.responseError(exchange, filterExceptionHandler, HttpStatus.INTERNAL_SERVER_ERROR.value(), tMsg, t); } return vm; } diff --git a/fizz-core/src/main/java/we/filter/PreprocessFilter.java b/fizz-core/src/main/java/we/filter/PreprocessFilter.java index f7ed2c8..2faa9c8 100644 --- a/fizz-core/src/main/java/we/filter/PreprocessFilter.java +++ b/fizz-core/src/main/java/we/filter/PreprocessFilter.java @@ -28,9 +28,7 @@ import org.springframework.web.server.WebFilterChain; import reactor.core.publisher.Mono; import we.plugin.FixedPluginFilter; import we.plugin.FizzPluginFilterChain; -import we.plugin.PluginFilter; import we.plugin.auth.ApiConfig; -import we.plugin.auth.ApiConfigService; import we.plugin.auth.AuthPluginFilter; import we.plugin.stat.StatPluginFilter; import we.proxy.Route; @@ -39,7 +37,6 @@ import we.util.Result; import we.util.WebUtils; import javax.annotation.Resource; -import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -73,42 +70,41 @@ public class PreprocessFilter extends FizzWebFilter { Map eas = exchange.getAttributes(); eas.put(WebUtils.FILTER_CONTEXT, fc); eas.put(WebUtils.APPEND_HEADERS, appendHdrs); - Mono vm = statPluginFilter.filter(exchange, null, null); - return process(exchange, chain, eas, vm); - } + return + statPluginFilter.filter(exchange, null, null) + .then( + authPluginFilter.filter(exchange, null, null) + ) + .thenReturn(ReactorUtils.Void) + .flatMap( + v -> { + Result authRes = (Result) WebUtils.getFilterResultDataItem(exchange, AuthPluginFilter.AUTH_PLUGIN_FILTER, AuthPluginFilter.RESULT); + if (authRes.code == Result.FAIL) { + return WebUtils.responseError(exchange, HttpStatus.FORBIDDEN.value(), authRes.msg); + } + ApiConfig ac = authRes.data; + if (ac == null) { + afterAuth(exchange, null, null); + return executeFixedPluginFilters(exchange).thenReturn(ReactorUtils.Void).flatMap(checkDirectRespOrChainFilter(exchange, chain)); + } + Route route = ac.getRoute(exchange); + eas.put(WebUtils.ROUTE, route); + afterAuth(exchange, ac, route); - private Mono process(ServerWebExchange exchange, WebFilterChain chain, Map eas, Mono vm) { - return chain(exchange, vm, authPluginFilter).defaultIfEmpty(ReactorUtils.NULL) - .flatMap( - v -> { - Result authRes = (Result) WebUtils.getFilterResultDataItem(exchange, AuthPluginFilter.AUTH_PLUGIN_FILTER, AuthPluginFilter.RESULT); - if (authRes.code == Result.FAIL) { - return WebUtils.responseError(exchange, HttpStatus.FORBIDDEN.value(), authRes.msg); - } - Mono m = ReactorUtils.getInitiateMono(); - ApiConfig ac = authRes.data; - if (ac == null) { - afterAuth(exchange, null, null); - m = executeFixedPluginFilters(exchange); - return m.defaultIfEmpty(ReactorUtils.NULL).flatMap(func(exchange, chain)); - } - Route route = ac.getRoute(exchange); - eas.put(WebUtils.ROUTE, route); - afterAuth(exchange, ac, route); - m = executeFixedPluginFilters(exchange); - m = m.defaultIfEmpty(ReactorUtils.NULL); - if (CollectionUtils.isEmpty(route.pluginConfigs)) { - return m.flatMap(func(exchange, chain)); - } else { - return m.flatMap( - nil -> { - eas.put(FizzPluginFilterChain.WEB_FILTER_CHAIN, chain); - return FizzPluginFilterChain.next(exchange); - } - ); - } - } - ); + return + executeFixedPluginFilters(exchange).thenReturn(ReactorUtils.Void) + .flatMap( + e -> { + if (CollectionUtils.isEmpty(route.pluginConfigs)) { + return checkDirectRespOrChainFilter(exchange, chain).apply(ReactorUtils.NULL); + } else { + eas.put(FizzPluginFilterChain.WEB_FILTER_CHAIN, chain); + return FizzPluginFilterChain.next(exchange); + } + } + ); + } + ); } private void afterAuth(ServerWebExchange exchange, ApiConfig ac, Route route) { @@ -135,15 +131,7 @@ public class PreprocessFilter extends FizzWebFilter { } } - private Mono chain(ServerWebExchange exchange, Mono m, PluginFilter pf) { - return m.defaultIfEmpty(ReactorUtils.NULL).flatMap( - v -> { - return pf.filter(exchange, null, null); - } - ); - } - - private Function func(ServerWebExchange exchange, WebFilterChain chain) { + private Function> checkDirectRespOrChainFilter(ServerWebExchange exchange, WebFilterChain chain) { return v -> { Mono dr = WebUtils.getDirectResponse(exchange); if (dr != null) { @@ -154,15 +142,11 @@ public class PreprocessFilter extends FizzWebFilter { } private Mono executeFixedPluginFilters(ServerWebExchange exchange) { - Mono vm = Mono.empty(); + Mono vm = Mono.empty(); List fixedPluginFilters = FixedPluginFilter.getPluginFilters(); for (byte i = 0; i < fixedPluginFilters.size(); i++) { FixedPluginFilter fpf = fixedPluginFilters.get(i); - vm = vm.defaultIfEmpty(ReactorUtils.NULL).flatMap( - v -> { - return fpf.filter(exchange, null, null); - } - ); + vm = vm.then(fpf.filter(exchange, null, null)); } return vm; } diff --git a/fizz-core/src/main/java/we/plugin/FizzPluginFilterChain.java b/fizz-core/src/main/java/we/plugin/FizzPluginFilterChain.java index dac2689..f3fed7d 100644 --- a/fizz-core/src/main/java/we/plugin/FizzPluginFilterChain.java +++ b/fizz-core/src/main/java/we/plugin/FizzPluginFilterChain.java @@ -20,6 +20,7 @@ package we.plugin; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.WebFilterChain; import reactor.core.publisher.Mono; +import we.Fizz; import we.FizzAppContext; import we.util.ReactorUtils; import we.util.WebUtils; @@ -51,18 +52,18 @@ public final class FizzPluginFilterChain { } if (it.hasNext()) { PluginConfig pc = it.next(); - FizzPluginFilter pf = FizzAppContext.appContext.getBean(pc.plugin, FizzPluginFilter.class); - Mono m = pf.filter(exchange, pc.config); + FizzPluginFilter pf = Fizz.context.getBean(pc.plugin, FizzPluginFilter.class); + Mono m = pf.filter(exchange, pc.config); if (pf instanceof PluginFilter) { boolean f = false; while (it.hasNext()) { PluginConfig pc0 = it.next(); - FizzPluginFilter pf0 = FizzAppContext.appContext.getBean(pc0.plugin, FizzPluginFilter.class); - m = m.defaultIfEmpty(ReactorUtils.NULL).flatMap( - v -> { - return pf0.filter(exchange, pc0.config); - } - ); + FizzPluginFilter pf0 = Fizz.context.getBean(pc0.plugin, FizzPluginFilter.class); + m = m.thenReturn(ReactorUtils.Void).flatMap( + v -> { + return pf0.filter(exchange, pc0.config); + } + ); if (pf0 instanceof PluginFilter) { } else { f = true; @@ -71,11 +72,11 @@ public final class FizzPluginFilterChain { } if (!f && !it.hasNext()) { WebFilterChain chain = exchange.getAttribute(WEB_FILTER_CHAIN); - m = m.defaultIfEmpty(ReactorUtils.NULL).flatMap( - v -> { - return chain.filter(exchange); - } - ); + m = m.thenReturn(ReactorUtils.Void).flatMap( + v -> { + return chain.filter(exchange); + } + ); } } return m; @@ -85,6 +86,7 @@ public final class FizzPluginFilterChain { } } + @Deprecated public static Mono next(ServerWebExchange exchange, List pcs) { Iterator it = pcs.iterator(); Map attris = exchange.getAttributes(); diff --git a/fizz-core/src/main/java/we/plugin/stat/StatPluginFilter.java b/fizz-core/src/main/java/we/plugin/stat/StatPluginFilter.java index 893fe49..5101323 100644 --- a/fizz-core/src/main/java/we/plugin/stat/StatPluginFilter.java +++ b/fizz-core/src/main/java/we/plugin/stat/StatPluginFilter.java @@ -47,19 +47,19 @@ public class StatPluginFilter extends PluginFilter { public static final String STAT_PLUGIN_FILTER = "statPlugin"; - private static final String ip = "\"ip\":"; + private static final String ip = "\"ip\":"; private static final String gatewayGroup = "\"gatewayGroup\":"; - private static final String service = "\"service\":"; + private static final String service = "\"service\":"; - private static final String appid = "\"appid\":"; + private static final String appid = "\"appid\":"; - private static final String apiMethod = "\"apiMethod\":"; + private static final String apiMethod = "\"apiMethod\":"; - private static final String apiPath = "\"apiPath\":"; + private static final String apiPath = "\"apiPath\":"; - private static final String reqTime = "\"reqTime\":"; + private static final String reqTime = "\"reqTime\":"; @Resource private StatPluginFilterProperties statPluginFilterProperties; @@ -76,30 +76,18 @@ public class StatPluginFilter extends PluginFilter { if (statPluginFilterProperties.isStatOpen()) { StringBuilder b = ThreadContext.getStringBuilder(); b.append(Consts.S.LEFT_BRACE); - b.append(ip); - toJsonStringValue(b, WebUtils.getOriginIp(exchange)); - b.append(Consts.S.COMMA); - b.append(gatewayGroup); - toJsonStringValue(b, currentGatewayGroups()); - b.append(Consts.S.COMMA); - b.append(service); - toJsonStringValue(b, WebUtils.getClientService(exchange)); - b.append(Consts.S.COMMA); + b.append(ip); toJsonStringValue(b, WebUtils.getOriginIp(exchange)); b.append(Consts.S.COMMA); + b.append(gatewayGroup); toJsonStringValue(b, currentGatewayGroups()); b.append(Consts.S.COMMA); + b.append(service); toJsonStringValue(b, WebUtils.getClientService(exchange)); b.append(Consts.S.COMMA); - String appId = WebUtils.getAppId(exchange); - if (appId != null) { - b.append(appid); - toJsonStringValue(b, appId); - b.append(Consts.S.COMMA); - } + String appId = WebUtils.getAppId(exchange); + if (appId != null) { + b.append(appid); toJsonStringValue(b, appId); b.append(Consts.S.COMMA); + } - b.append(apiMethod); - toJsonStringValue(b, exchange.getRequest().getMethodValue()); - b.append(Consts.S.COMMA); - b.append(apiPath); - toJsonStringValue(b, WebUtils.getClientReqPath(exchange)); - b.append(Consts.S.COMMA); - b.append(reqTime).append(System.currentTimeMillis()); + b.append(apiMethod); toJsonStringValue(b, exchange.getRequest().getMethodValue()); b.append(Consts.S.COMMA); + b.append(apiPath); toJsonStringValue(b, WebUtils.getClientReqPath(exchange)); b.append(Consts.S.COMMA); + b.append(reqTime) .append(System.currentTimeMillis()); b.append(Consts.S.RIGHT_BRACE); if (StringUtils.isBlank(statPluginFilterProperties.getFizzAccessStatTopic())) { diff --git a/fizz-core/src/main/java/we/plugin/stat/StatPluginFilterProperties.java b/fizz-core/src/main/java/we/plugin/stat/StatPluginFilterProperties.java index f976867..e09820c 100644 --- a/fizz-core/src/main/java/we/plugin/stat/StatPluginFilterProperties.java +++ b/fizz-core/src/main/java/we/plugin/stat/StatPluginFilterProperties.java @@ -14,6 +14,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ + package we.plugin.stat; import lombok.Data; @@ -26,10 +27,12 @@ import org.springframework.stereotype.Component; * * @author zhongjie */ + @RefreshScope @Component @Data public class StatPluginFilterProperties { + @Value("${stat.open:false}") private boolean statOpen = false; diff --git a/fizz-core/src/main/java/we/proxy/FizzWebClient.java b/fizz-core/src/main/java/we/proxy/FizzWebClient.java index a7d0c15..f310ec1 100644 --- a/fizz-core/src/main/java/we/proxy/FizzWebClient.java +++ b/fizz-core/src/main/java/we/proxy/FizzWebClient.java @@ -146,9 +146,13 @@ public class FizzWebClient { return cr; } - private Mono send2uri(@Nullable String traceId, + public Mono send2uri(@Nullable String traceId, HttpMethod method, String uri, @Nullable HttpHeaders headers, @Nullable Object body) { + return send2uri(traceId, method, uri, headers, body, 0); + } + + public Mono send2uri(@Nullable String traceId, HttpMethod method, String uri, @Nullable HttpHeaders headers, @Nullable Object body, - long timeout) { + long timeout) { if (log.isDebugEnabled()) { StringBuilder b = ThreadContext.getStringBuilder();