This commit is contained in:
hongqiaowei
2020-12-01 18:07:21 +08:00
parent 1778f01aba
commit 3c74848e1d
6 changed files with 34 additions and 5 deletions

View File

@@ -110,7 +110,11 @@ public class RouteFilter extends ProxyAggrFilter {
String rid = clientReq.getId();
ApiConfig ac = WebUtils.getApiConfig(exchange);
if (ac.type == ApiConfig.Type.SERVICE_DISCOVERY) {
if (ac == null) {
String relativeUri = WebUtils.getRelativeUri(exchange);
return send(exchange, WebUtils.getServiceId(exchange), relativeUri, hdrs);
} else if (ac.type == ApiConfig.Type.SERVICE_DISCOVERY) {
String relativeUri = WebUtils.appendQuery(ac.transform(reqPath), exchange);
return send(exchange, ac.backendService, relativeUri, hdrs);

View File

@@ -176,6 +176,9 @@ public class ApiConfig {
}
public String transform(String reqPath) {
if (exactMatch) {
return backendPath;
}
return UrlTransformUtils.transform(path, backendPath, reqPath);
}

View File

@@ -105,7 +105,12 @@ public class StatPluginFilter extends PluginFilter {
b.append(ip); toJsonStringValue(b, WebUtils.getOriginIp(exchange)); b.append(Constants.Symbol.COMMA);
b.append(gatewayGroup); toJsonStringValue(b, currentGatewayGroups); b.append(Constants.Symbol.COMMA);
b.append(service); toJsonStringValue(b, WebUtils.getServiceId(exchange)); b.append(Constants.Symbol.COMMA);
b.append(appid); toJsonStringValue(b, WebUtils.getAppId(exchange)); b.append(Constants.Symbol.COMMA);
String appId = WebUtils.getAppId(exchange);
if (appId != null) {
b.append(appid); toJsonStringValue(b, appId); b.append(Constants.Symbol.COMMA);
}
b.append(apiMethod); toJsonStringValue(b, exchange.getRequest().getMethodValue()); b.append(Constants.Symbol.COMMA);
b.append(apiPath); toJsonStringValue(b, WebUtils.getReqPath(exchange)); b.append(Constants.Symbol.COMMA);
b.append(reqTime) .append(System.currentTimeMillis());

View File

@@ -7,7 +7,9 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
/**
* The Eureka implementation of {@code DiscoveryClientUriSelector}
@@ -30,11 +32,11 @@ public class EurekaUriSelector extends AbstractDiscoveryClientUriSelector {
// private static List<InstanceInfo> aggrMemberInsts = new ArrayList<>();
// static {
// InstanceInfo i0 = InstanceInfo.Builder.newBuilder().setAppName("TRIP-MINI").setIPAddr("xxx.25.63.192").setPort(7094).build();
// InstanceInfo i0 = InstanceInfo.Builder.newBuilder().setAppName("MINITRIP").setIPAddr("xxx.xxx.63.192").setPort(7094).build();
// aggrMemberInsts.add(i0);
// }
// private static AtomicLong counter = new AtomicLong(0);
// private static final String aggrMember = "trip-mini";
// private static final String aggrMember = "minitrip";
private InstanceInfo roundRobinChoose1instFrom(String service) {

View File

@@ -1,8 +1,11 @@
package we.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.lang.Nullable;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.Assert;
import we.filter.RouteFilter;
import java.util.LinkedList;
import java.util.List;
@@ -17,6 +20,9 @@ import java.util.regex.Pattern;
* @author zhongjie
*/
public class UrlTransformUtils {
private static final Logger log = LoggerFactory.getLogger(UrlTransformUtils.class);
private UrlTransformUtils() {}
private static final FizzGatewayUrlAntPathMatcher ANT_PATH_MATCHER = new FizzGatewayUrlAntPathMatcher();
@@ -34,6 +40,7 @@ public class UrlTransformUtils {
Assert.hasText(frontendPath, "frontend path cannot be null");
Assert.hasText(backendPath, "backend path cannot be null");
Assert.hasText(reqPath, "req path cannot be null");
String bp = backendPath;
Map<String, String> variables = ANT_PATH_MATCHER.extractUriTemplateVariables(frontendPath, reqPath);
for (Map.Entry<String, String> entry : variables.entrySet()) {
backendPath = backendPath.replaceAll("\\{" + Matcher.quoteReplacement(entry.getKey()) + "}", entry.getValue());
@@ -43,6 +50,10 @@ public class UrlTransformUtils {
backendPath = backendPath.replaceAll("\\{[^/]*}", "");
}
if (log.isDebugEnabled()) {
log.debug("req: " + reqPath + ", frontend: " + frontendPath + ", backend: " + bp + ", target: " + backendPath);
}
return backendPath;
}

View File

@@ -269,7 +269,11 @@ public abstract class WebUtils {
}
public static String appendQuery(String path, ServerWebExchange exchange) {
return path + Constants.Symbol.QUESTION + getQuery(exchange);
String qry = getQuery(exchange);
if (qry != null) {
return path + Constants.Symbol.QUESTION + qry;
}
return path;
}
public static Map<String, String> getAppendHeaders(ServerWebExchange exchange) {