Code optimization
This commit is contained in:
29
fizz-core/src/main/java/we/Fizz.java
Normal file
29
fizz-core/src/main/java/we/Fizz.java
Normal file
@@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package we;
|
||||
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
|
||||
/**
|
||||
* @author hongqiaowei
|
||||
*/
|
||||
|
||||
public class Fizz {
|
||||
|
||||
public static ConfigurableApplicationContext context;
|
||||
}
|
||||
@@ -14,10 +14,16 @@
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package we;
|
||||
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
|
||||
/**
|
||||
* @deprecated and use {@link we.Fizz} instead
|
||||
*/
|
||||
|
||||
@Deprecated
|
||||
public class FizzAppContext {
|
||||
|
||||
public static ConfigurableApplicationContext appContext;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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<String, Object> 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<ApiConfig> authRes = (Result<ApiConfig>) 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<Void> process(ServerWebExchange exchange, WebFilterChain chain, Map<String, Object> eas, Mono vm) {
|
||||
return chain(exchange, vm, authPluginFilter).defaultIfEmpty(ReactorUtils.NULL)
|
||||
.flatMap(
|
||||
v -> {
|
||||
Result<ApiConfig> authRes = (Result<ApiConfig>) 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<Object, Mono<Void>> checkDirectRespOrChainFilter(ServerWebExchange exchange, WebFilterChain chain) {
|
||||
return v -> {
|
||||
Mono<Void> dr = WebUtils.getDirectResponse(exchange);
|
||||
if (dr != null) {
|
||||
@@ -154,15 +142,11 @@ public class PreprocessFilter extends FizzWebFilter {
|
||||
}
|
||||
|
||||
private Mono<Void> executeFixedPluginFilters(ServerWebExchange exchange) {
|
||||
Mono vm = Mono.empty();
|
||||
Mono<Void> vm = Mono.empty();
|
||||
List<FixedPluginFilter> 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;
|
||||
}
|
||||
|
||||
@@ -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<Void> 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<Void> next(ServerWebExchange exchange, List<PluginConfig> pcs) {
|
||||
Iterator<PluginConfig> it = pcs.iterator();
|
||||
Map<String, Object> attris = exchange.getAttributes();
|
||||
|
||||
@@ -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())) {
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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;
|
||||
|
||||
|
||||
@@ -146,9 +146,13 @@ public class FizzWebClient {
|
||||
return cr;
|
||||
}
|
||||
|
||||
private Mono<ClientResponse> send2uri(@Nullable String traceId,
|
||||
public Mono<ClientResponse> send2uri(@Nullable String traceId, HttpMethod method, String uri, @Nullable HttpHeaders headers, @Nullable Object body) {
|
||||
return send2uri(traceId, method, uri, headers, body, 0);
|
||||
}
|
||||
|
||||
public Mono<ClientResponse> 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();
|
||||
|
||||
Reference in New Issue
Block a user