Code optimization
This commit is contained in:
@@ -19,6 +19,7 @@ package we.api.pairing;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
@@ -49,6 +50,7 @@ import java.util.stream.Collectors;
|
||||
* @author hongqiaowei
|
||||
*/
|
||||
|
||||
@ConditionalOnBean({ApiPairingDocSetService.class})
|
||||
@RestController
|
||||
@RequestMapping(SystemConfig.DEFAULT_GATEWAY_PREFIX + "/_fizz-pairing")
|
||||
public class ApiPairingController {
|
||||
@@ -75,16 +77,16 @@ public class ApiPairingController {
|
||||
|
||||
String appId = WebUtils.getAppId(exchange);
|
||||
if (appId == null) {
|
||||
return WebUtils.buildDirectResponse(response, null, null, "请求无应用信息");
|
||||
return WebUtils.buildDirectResponse(response, null, null, "no app info in request");
|
||||
}
|
||||
App app = appService.getApp(appId);
|
||||
if (app == null) {
|
||||
return WebUtils.buildDirectResponse(response, null, null, "系统无" + appId + "应用信息");
|
||||
return WebUtils.buildDirectResponse(response, null, null, appId + " not exists");
|
||||
}
|
||||
|
||||
String timestamp = getTimestamp(headers);
|
||||
if (timestamp == null) {
|
||||
return WebUtils.buildDirectResponse(response, null, null, "请求无时间戳");
|
||||
return WebUtils.buildDirectResponse(response, null, null, "no timestamp in request");
|
||||
}
|
||||
try {
|
||||
long ts = Long.parseLong(timestamp);
|
||||
@@ -95,15 +97,15 @@ public class ApiPairingController {
|
||||
if (start <= ts && ts <= end) {
|
||||
// valid
|
||||
} else {
|
||||
return WebUtils.buildDirectResponse(response, null, null, "请求时间戳无效");
|
||||
return WebUtils.buildDirectResponse(response, null, null, "request timestamp invalid");
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
return WebUtils.buildDirectResponse(response, null, null, "请求时间戳无效");
|
||||
return WebUtils.buildDirectResponse(response, null, null, "request timestamp invalid");
|
||||
}
|
||||
|
||||
String sign = getSign(headers);
|
||||
if (sign == null) {
|
||||
return WebUtils.buildDirectResponse(response, null, null, "请求未签名");
|
||||
return WebUtils.buildDirectResponse(response, null, null, "no sign in request");
|
||||
}
|
||||
|
||||
boolean equals = ApiPairingUtils.checkSign(appId, timestamp, app.secretkey, sign);
|
||||
@@ -114,7 +116,7 @@ public class ApiPairingController {
|
||||
} else {
|
||||
log.warn("{}request authority: app {}, timestamp {}, sign {} invalid",
|
||||
exchange.getLogPrefix(), appId, timestamp, sign, LogService.BIZ_ID, WebUtils.getTraceId(exchange));
|
||||
return WebUtils.buildDirectResponse(response, null, null, "请求签名无效");
|
||||
return WebUtils.buildDirectResponse(response, null, null, "request sign invalid");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ package we.api.pairing;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.data.redis.core.ReactiveStringRedisTemplate;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -39,6 +40,7 @@ import java.util.*;
|
||||
* @author hongqiaowei
|
||||
*/
|
||||
|
||||
@ConditionalOnProperty(name = "fizz.api.pairing.enable", havingValue = "true")
|
||||
@Service
|
||||
public class ApiPairingDocSetService {
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ package we.api.pairing;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.data.redis.core.ReactiveStringRedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import reactor.core.publisher.Flux;
|
||||
@@ -39,6 +40,7 @@ import java.util.Map;
|
||||
* @author hongqiaowei
|
||||
*/
|
||||
|
||||
@ConditionalOnProperty(name = "fizz.api.pairing.enable", havingValue = "true")
|
||||
@Service
|
||||
public class ApiPairingInfoService {
|
||||
|
||||
|
||||
@@ -107,8 +107,8 @@ class FizzApiPairingHttpHandler implements HttpHandler {
|
||||
String service = path.substring(1, secFS);
|
||||
ApiPairingInfo apiPairingInfo = apiPairingInfoService.get(service);
|
||||
if (apiPairingInfo == null) {
|
||||
log.warn("{}{} 服务无配对信息", logPrefix, service);
|
||||
return WebUtils.buildDirectResponse(response, HttpStatus.FORBIDDEN, null, service + " 服务无配对信息").then(response.setComplete());
|
||||
log.warn("{}{} service no api pairing info", logPrefix, service);
|
||||
return WebUtils.buildDirectResponse(response, HttpStatus.FORBIDDEN, null, service + " service no api pairing info").then(response.setComplete());
|
||||
}
|
||||
|
||||
StringBuilder b = ThreadContext.getStringBuilder();
|
||||
|
||||
@@ -21,7 +21,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.web.reactive.HttpHandlerAutoConfiguration;
|
||||
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
|
||||
import org.springframework.boot.web.server.WebServer;
|
||||
@@ -39,7 +39,7 @@ import javax.annotation.Resource;
|
||||
* @author hongqiaowei
|
||||
*/
|
||||
|
||||
@ConditionalOnProperty(name = "fizz.api.pairing.enable", havingValue = "true")
|
||||
@ConditionalOnBean({ApiPairingInfoService.class})
|
||||
@Configuration
|
||||
@AutoConfigureAfter({HttpHandlerAutoConfiguration.class})
|
||||
public class FizzApiPairingWebServer {
|
||||
|
||||
@@ -48,7 +48,8 @@ public abstract class ManualApiConfig {
|
||||
|
||||
@PostConstruct
|
||||
public void iniApiConfigs() {
|
||||
gatewayGroupService.currentGatewayGroupSet = Stream.of(GatewayGroup.DEFAULT).collect(Collectors.toSet());
|
||||
// gatewayGroupService.currentGatewayGroupSet = Stream.of(GatewayGroup.DEFAULT).collect(Collectors.toSet());
|
||||
gatewayGroupService.currentGatewayGroupSet.add(GatewayGroup.DEFAULT);
|
||||
List<ApiConfig> apiConfigs = setApiConfigs();
|
||||
for (ApiConfig ac : apiConfigs) {
|
||||
ServiceConfig sc = apiConfigService.serviceConfigMap.get(ac.service);
|
||||
|
||||
@@ -24,7 +24,6 @@ import org.springframework.data.redis.core.ReactiveStringRedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import we.FizzAppContext;
|
||||
import we.config.AggregateRedisConfig;
|
||||
import we.flume.clients.log4j2appender.LogService;
|
||||
import we.util.Consts;
|
||||
@@ -201,15 +200,6 @@ public class GatewayGroupService {
|
||||
// publish event
|
||||
}
|
||||
|
||||
public boolean currentGatewayGroupIn(Set<String> gatewayGroups) {
|
||||
for (String cgg : currentGatewayGroupSet) {
|
||||
if (gatewayGroups.contains(cgg)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public GatewayGroup get(String gg) {
|
||||
return gatewayGroupMap.get(gg);
|
||||
}
|
||||
|
||||
@@ -82,16 +82,14 @@ public abstract class WebUtils {
|
||||
|
||||
private static String gatewayPrefix = SystemConfig.DEFAULT_GATEWAY_PREFIX;
|
||||
|
||||
private static List<String> appHeaders = Stream.of("fizz-appid").collect(Collectors.toList());
|
||||
private static List<String> appHeaders = Stream.of(SystemConfig.FIZZ_APP_ID) .collect(Collectors.toList());
|
||||
|
||||
private static List<String> signHeaders = Stream.of("fizz-sign").collect(Collectors.toList());
|
||||
private static List<String> signHeaders = Stream.of(SystemConfig.FIZZ_SIGN) .collect(Collectors.toList());
|
||||
|
||||
private static List<String> timestampHeaders = Stream.of("fizz-ts").collect(Collectors.toList());
|
||||
private static List<String> timestampHeaders = Stream.of(SystemConfig.FIZZ_TIMESTAMP).collect(Collectors.toList());
|
||||
|
||||
private static final String app = "app";
|
||||
|
||||
// private static final String respbT = "respbT";
|
||||
|
||||
public static final String TRACE_ID = "traid@";
|
||||
|
||||
public static final String BACKEND_SERVICE = "bs@";
|
||||
@@ -151,26 +149,22 @@ public abstract class WebUtils {
|
||||
}
|
||||
|
||||
public static String getAppId(ServerWebExchange exchange) {
|
||||
String a = exchange.getAttribute(app);
|
||||
if (a == null) {
|
||||
HttpHeaders headers = exchange.getRequest().getHeaders();
|
||||
for (int i = 0; i < appHeaders.size(); i++) {
|
||||
a = headers.getFirst(appHeaders.get(i));
|
||||
if (a != null) {
|
||||
exchange.getAttributes().put(app, a);
|
||||
break;
|
||||
}
|
||||
HttpHeaders headers = exchange.getRequest().getHeaders();
|
||||
for (int i = 0; i < appHeaders.size(); i++) {
|
||||
String v = headers.getFirst(appHeaders.get(i));
|
||||
if (v != null) {
|
||||
return v;
|
||||
}
|
||||
}
|
||||
return a;
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String getTimestamp(ServerWebExchange exchange) {
|
||||
HttpHeaders headers = exchange.getRequest().getHeaders();
|
||||
for (int i = 0; i < timestampHeaders.size(); i++) {
|
||||
String a = headers.getFirst(timestampHeaders.get(i));
|
||||
if (a != null) {
|
||||
return a;
|
||||
String v = headers.getFirst(timestampHeaders.get(i));
|
||||
if (v != null) {
|
||||
return v;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
@@ -179,15 +173,14 @@ public abstract class WebUtils {
|
||||
public static String getSign(ServerWebExchange exchange) {
|
||||
HttpHeaders headers = exchange.getRequest().getHeaders();
|
||||
for (int i = 0; i < signHeaders.size(); i++) {
|
||||
String a = headers.getFirst(signHeaders.get(i));
|
||||
if (a != null) {
|
||||
return a;
|
||||
String v = headers.getFirst(signHeaders.get(i));
|
||||
if (v != null) {
|
||||
return v;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public static String getClientService(ServerWebExchange exchange) {
|
||||
String svc = exchange.getAttribute(clientService);
|
||||
if (svc == null) {
|
||||
@@ -497,8 +490,8 @@ public abstract class WebUtils {
|
||||
// body to b
|
||||
}
|
||||
|
||||
public static void response2stringBuilder(String rid, ClientResponse clientResponse, StringBuilder b) {
|
||||
b.append(rid).append(response).append(clientResponse.statusCode());
|
||||
public static void response2stringBuilder(String traceId, ClientResponse clientResponse, StringBuilder b) {
|
||||
b.append(traceId).append(response).append(clientResponse.statusCode());
|
||||
HttpHeaders headers = clientResponse.headers().asHttpHeaders();
|
||||
final boolean[] f = {false};
|
||||
LOG_HEADER_SET.forEach(
|
||||
@@ -669,7 +662,6 @@ public abstract class WebUtils {
|
||||
}
|
||||
|
||||
public static String jsonRespBody(int code, @Nullable String msg, @Nullable String traceId, @Nullable Object context) {
|
||||
// StringBuilder b = ThreadContext.getStringBuilder(respbT);
|
||||
StringBuilder b = ThreadContext.getStringBuilder(ThreadContext.sb0);
|
||||
b.append(s0).append(SystemConfig.FIZZ_ERR_RESP_CODE_FIELD).append(s1).append(code);
|
||||
if (StringUtils.isNotBlank(msg)) {
|
||||
|
||||
Reference in New Issue
Block a user