|
@@ -1,16 +1,23 @@
|
|
|
package com.galaxis.wms.filter;
|
|
|
|
|
|
import com.galaxis.wms.GlobalConstant;
|
|
|
+import com.galaxis.wms.utils.XPathUtils;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
|
|
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
|
|
import org.springframework.cloud.gateway.route.Route;
|
|
|
+import org.springframework.cloud.gateway.support.NotFoundException;
|
|
|
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
|
|
|
import org.springframework.core.Ordered;
|
|
|
+import org.springframework.http.server.reactive.ServerHttpRequest;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.web.server.ServerWebExchange;
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
+import java.util.concurrent.ConcurrentMap;
|
|
|
+
|
|
|
/**
|
|
|
* 作者:lizw <br/>
|
|
|
* 创建时间:2022/03/17 10:39 <br/>
|
|
@@ -20,16 +27,56 @@ import reactor.core.publisher.Mono;
|
|
|
public class SelectRouteGlobalFilter implements GlobalFilter, Ordered {
|
|
|
private static final int ORDER = 0;
|
|
|
|
|
|
+ //// 山西
|
|
|
+ //private static final String SX = "http://127.0.0.1:8082";
|
|
|
+ // 重庆
|
|
|
private static final String CQ = "http://127.0.0.1:8182";
|
|
|
- private static final String GX = "http://127.0.0.1:3334";
|
|
|
- private static final String PZH = "http://127.0.0.1:3334";
|
|
|
+ // 广西
|
|
|
+ private static final String GX = "http://127.0.0.1:8282";
|
|
|
+ // 四川
|
|
|
+ private static final String SC = "http://127.0.0.1:8382";
|
|
|
+
|
|
|
+ private static final ConcurrentMap<String, String> wmsMapping = new ConcurrentHashMap<String, String>() {{
|
|
|
+ put("D104", ""); // 山西鸿翔一心堂药业有限公司
|
|
|
+ put("D103", SC); // 四川鸿翔一心堂药业有限公司
|
|
|
+ put("D105", CQ); // 重庆鸿翔一心堂药业有限公司
|
|
|
+ put("D907", ""); // 上海鸿翔一心堂药业有限公司
|
|
|
+ put("D101", GX); // 广西鸿翔一心堂药业有限公司
|
|
|
+ put("D110", ""); // 河南鸿翔一心堂药业有限公司
|
|
|
+ put("D115", ""); // 一心堂药业(山西)有限公司
|
|
|
+ put("D102", ""); // 贵州鸿翔-一心堂药业有限公司
|
|
|
+ put("D106", ""); // 成都鸿翔一心堂药业有限公司
|
|
|
+ put("D109", ""); // 海南鸿翔一心堂药业有限公司
|
|
|
+ put("D301", ""); // 海南一心堂医药有限公司
|
|
|
+ put("D100", ""); // 一心堂药业集团股份有限公司
|
|
|
+ put("D300", ""); // 云南鸿云药业有限公司
|
|
|
+ put("D108", ""); // 天津鸿翔一心堂药业有限公司
|
|
|
+ put("D900", ""); // 一心到家科技公司(电商仓)
|
|
|
+ put("D504", ""); // 医云医疗产业发展(云南)有限公司
|
|
|
+ }};
|
|
|
|
|
|
@Override
|
|
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
|
|
- String body = exchange.getAttribute(GlobalConstant.CACHED_REQUEST_BODY_STR_ATTR);
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+ final long startTime = System.currentTimeMillis();
|
|
|
+ ServerHttpRequest request = exchange.getRequest();
|
|
|
+ final String path = request.getPath().value();
|
|
|
+ final String method = StringUtils.upperCase(request.getMethodValue());
|
|
|
+ log.info("--> [{}] {}", method, path);
|
|
|
+ String expression = "//BWKEY[1]";
|
|
|
+ String body = null;
|
|
|
+ String bwkey = null;
|
|
|
+ String uri = null;
|
|
|
+ try {
|
|
|
+ body = exchange.getAttribute(GlobalConstant.CACHED_REQUEST_BODY_STR_ATTR);
|
|
|
+ bwkey = XPathUtils.evaluate(body, expression);
|
|
|
+ uri = wmsMapping.get(bwkey);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("获取物流中心编码失败 | bwkey={} | body=[\n{}\n]", bwkey, body, e);
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(uri)) {
|
|
|
+ log.warn("未配置物流中心编码[{}]的服务端映射", bwkey);
|
|
|
+ throw NotFoundException.create(true, "404 Not Found");
|
|
|
+ }
|
|
|
final Route route = exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR);
|
|
|
assert route != null;
|
|
|
Route newRoute = Route.async()
|
|
@@ -37,8 +84,10 @@ public class SelectRouteGlobalFilter implements GlobalFilter, Ordered {
|
|
|
.filters(route.getFilters())
|
|
|
.id(route.getId())
|
|
|
.order(route.getOrder())
|
|
|
- .uri("http://127.0.0.1:3334").build();
|
|
|
+ .uri(uri).build();
|
|
|
exchange.getAttributes().put(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR, newRoute);
|
|
|
+ final long endTime = System.currentTimeMillis();
|
|
|
+ log.info("<-- [{}] {} | {}ms", method, path, (endTime - startTime));
|
|
|
return chain.filter(exchange);
|
|
|
}
|
|
|
|