lizw 3 years ago
parent
commit
fe4a3bb105

+ 3 - 2
gateway-server/build.gradle

@@ -8,8 +8,9 @@ dependencies {
     // api 'org.springframework.boot:spring-boot-starter-data-redis'
     // api 'com.zaxxer:HikariCP'
     api 'org.apache.commons:commons-lang3'
-    api 'org.apache.commons:commons-pool2'
-    api 'com.google.guava:guava'
+    api 'commons-io:commons-io'
+    // api 'org.apache.commons:commons-pool2'
+    // api 'com.google.guava:guava'
 }
 
 bootJar {

+ 0 - 82
gateway-server/src/main/java/com/galaxis/wms/filter/AuthGlobalFilter.java

@@ -1,82 +0,0 @@
-//package com.galaxis.wms.filter;
-//
-//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.route.builder.GatewayFilterSpec;
-//import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
-//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 org.springframework.web.util.UriComponentsBuilder;
-//import reactor.core.publisher.Mono;
-//
-//import java.net.URI;
-//
-///**
-// * 全局认证请求
-// * <p>
-// * 作者:lizw <br/>
-// * 创建时间:2021/01/26 11:36 <br/>
-// */
-//@Component
-//@Slf4j
-//public class AuthGlobalFilter implements GlobalFilter, Ordered {
-//    private static final int ORDER = Ordered.LOWEST_PRECEDENCE - 1;
-//
-//    @Override
-//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
-//        ServerHttpRequest request = exchange.getRequest();
-//        final String path = request.getPath().value();
-//        final String method = StringUtils.upperCase(request.getMethodValue());
-//        log.debug(" -> [{}] | {}", method, path);
-//        // 当前Route
-//        final Route route = exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR);
-//        final String routeId = route != null ? route.getId() : null;
-//        if (route == null || StringUtils.isBlank(routeId)) {
-//            throw NotFoundException.create(true, "404 Not Found");
-//        }
-//
-//        Route newRoute = Route.async()
-//                .asyncPredicate(route.getPredicate())
-//                .filters(route.getFilters())
-//                .id(route.getId())
-//                .order(route.getOrder())
-//                .uri("http://127.0.0.1:6666").build();
-////        ServerWebExchange mutateExchange = exchange.mutate().request(builder -> builder.uri(uri).build()).build();
-//        exchange.getAttributes().put(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR, newRoute);
-//        URI uri = UriComponentsBuilder.
-//                newInstance().scheme(exchange.getRequest().getURI().getScheme()).
-//                host("127.0.0.1").port("6666")
-//                .path("/wms/api").query(exchange.getRequest().getURI().getRawQuery()).build(true)
-//                .toUri();
-//
-//        exchange.getAttributes().put(ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR, uri);
-//        log.debug(" -> routeId={} | [{}] | {}", routeId, method, path);
-//        return chain.filter(exchange);
-//    }
-//
-//    @Override
-//    public int getOrder() {
-//        return ORDER;
-//    }
-//
-//    public void test() {
-//        RouteLocatorBuilder builder = new RouteLocatorBuilder(null);
-//        builder.routes()
-//                .route(r -> r.path("/aaa")
-//                        .and()
-//                        .path("")
-//                        .filters(GatewayFilterSpec::preserveHostHeader).uri("http://127.0.0.1:3333")
-//                        ///.predicate(serverWebExchange -> {})
-//                        .id("")
-//                )
-//                // .route()
-//                .build();
-//    }
-//}

+ 56 - 7
gateway-server/src/main/java/com/galaxis/wms/filter/SelectRouteGlobalFilter.java

@@ -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);
     }
 

+ 39 - 0
gateway-server/src/main/java/com/galaxis/wms/utils/XPathUtils.java

@@ -0,0 +1,39 @@
+package com.galaxis.wms.utils;
+
+import org.w3c.dom.Document;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathFactory;
+import java.io.ByteArrayInputStream;
+import java.nio.charset.StandardCharsets;
+
+/**
+ * 作者:lizw <br/>
+ * 创建时间:2022/03/17 11:08 <br/>
+ */
+public class XPathUtils {
+    private static final DocumentBuilderFactory DOCUMENT_BUILDER_FACTORY = DocumentBuilderFactory.newInstance();
+    private static final XPathFactory X_PATH_FACTORY = XPathFactory.newInstance();
+
+    static {
+        DOCUMENT_BUILDER_FACTORY.setValidating(false);
+        DOCUMENT_BUILDER_FACTORY.setIgnoringComments(true);
+        DOCUMENT_BUILDER_FACTORY.setIgnoringElementContentWhitespace(true);
+    }
+
+    public static String evaluate(String xml, String expression) throws Exception {
+        DocumentBuilder documentBuilder = DOCUMENT_BUILDER_FACTORY.newDocumentBuilder();
+        XPath xPath = X_PATH_FACTORY.newXPath();
+        try (ByteArrayInputStream inputStream = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8))) {
+            Document document = documentBuilder.parse(inputStream);
+            Object res = xPath.evaluate(expression, document, XPathConstants.STRING);
+            if (res == null) {
+                return null;
+            }
+            return String.valueOf(res);
+        }
+    }
+}

+ 51 - 0
gateway-server/src/test/java/com/galaxis/wms/utils/XPathUtilsTest.java

@@ -0,0 +1,51 @@
+package com.galaxis.wms.utils;
+
+import lombok.SneakyThrows;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.io.FileUtils;
+import org.junit.jupiter.api.Test;
+import org.w3c.dom.Document;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathFactory;
+import java.io.ByteArrayInputStream;
+import java.io.File;
+
+/**
+ * 作者:lizw <br/>
+ * 创建时间:2022/03/17 11:12 <br/>
+ */
+@Slf4j
+public class XPathUtilsTest {
+
+    @SneakyThrows
+    @Test
+    public void t01() {
+        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
+        documentBuilderFactory.setValidating(false);
+        documentBuilderFactory.setIgnoringComments(true);
+        documentBuilderFactory.setIgnoringElementContentWhitespace(true);
+
+        XPathFactory xPathFactory = XPathFactory.newInstance();
+
+        ByteArrayInputStream inputStream = new ByteArrayInputStream(FileUtils.readFileToByteArray(new File("./src/test/resources/test.xml")));
+        long start = System.currentTimeMillis();
+        for (int i = 0; i < 100000; i++) {
+            DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
+            XPath xPath = xPathFactory.newXPath();
+            Document doc = builder.parse(inputStream);
+            inputStream.reset();
+            // String expression = "/Envelope/Body/MT_KUNNR_IN_WMS01/arg0/BWKEY";
+            String expression = "//MT_KUNNR_IN_WMS01//BWKEY[1]";
+            Object res = xPath.evaluate(expression, doc, XPathConstants.STRING);
+            if (i == 0) {
+                log.info("-> {}", res);
+            }
+        }
+        long end = System.currentTimeMillis();
+        log.info("耗时 -> {}ms", end - start);
+    }
+}

+ 98 - 0
gateway-server/src/test/resources/test.xml

@@ -0,0 +1,98 @@
+<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.jztd.com">
+    <soapenv:Header/>
+    <soapenv:Body>
+        <ser:MT_KUNNR_IN_WMS01>
+            <!--Zero or more repetitions:-->
+            <arg0>
+                <!--Optional:-->
+                <BEZEI>123</BEZEI>
+                <!--Optional:-->
+                <BREIT>?</BREIT>
+                <!--Optional:-->
+                <BRGEW>?</BRGEW>
+                <!--Optional:-->
+                <BUKRS>?</BUKRS>
+                <!--Optional:-->
+                <BWKEY>abc123</BWKEY>
+                <!--Optional:-->
+                <CONFD>?</CONFD>
+                <!--Optional:-->
+                <EAN11_CAR>?</EAN11_CAR>
+                <!--Optional:-->
+                <EAN11_EA>?</EAN11_EA>
+                <!--Optional:-->
+                <EAN11_PCS>?</EAN11_PCS>
+                <!--Optional:-->
+                <EKGRP>?</EKGRP>
+                <!--Optional:-->
+                <GROES>?</GROES>
+                <!--Optional:-->
+                <HOEHE>?</HOEHE>
+                <!--Optional:-->
+                <LAENG>?</LAENG>
+                <!--Optional:-->
+                <LANDX>?</LANDX>
+                <!--Optional:-->
+                <MAGRV>?</MAGRV>
+                <!--Optional:-->
+                <MAKTX>?</MAKTX>
+                <!--Optional:-->
+                <MATNR>?</MATNR>
+                <!--Optional:-->
+                <MEINS>?</MEINS>
+                <!--Optional:-->
+                <MHDHB>?</MHDHB>
+                <!--Optional:-->
+                <MMSTA>?</MMSTA>
+                <!--Optional:-->
+                <MSEHT_CAR>?</MSEHT_CAR>
+                <!--Optional:-->
+                <MSEHT_PCS>?</MSEHT_PCS>
+                <!--Optional:-->
+                <MTART>?</MTART>
+                <!--Optional:-->
+                <NAME1>?</NAME1>
+                <!--Optional:-->
+                <NAME1_CYR>?</NAME1_CYR>
+                <!--Optional:-->
+                <NORMT>?</NORMT>
+                <!--Optional:-->
+                <RAUBE>?</RAUBE>
+                <!--Optional:-->
+                <UMREZ_CAR>?</UMREZ_CAR>
+                <!--Optional:-->
+                <UMREZ_PCS>?</UMREZ_PCS>
+                <!--Optional:-->
+                <ZIS_FC>?</ZIS_FC>
+                <!--Optional:-->
+                <ZIS_GZYP>?</ZIS_GZYP>
+                <!--Optional:-->
+                <ZIS_JKYP>?</ZIS_JKYP>
+                <!--Optional:-->
+                <ZIS_YP>?</ZIS_YP>
+                <!--Optional:-->
+                <ZIS_YS>?</ZIS_YS>
+                <!--Optional:-->
+                <ZIS_ZDYH>?</ZIS_ZDYH>
+                <!--Optional:-->
+                <ZNEWS>?</ZNEWS>
+                <!--Optional:-->
+                <ZOUNIT_MIN>?</ZOUNIT_MIN>
+                <!--Optional:-->
+                <ZPLI>?</ZPLI>
+                <!--Optional:-->
+                <ZPRESX>?</ZPRESX>
+                <!--Optional:-->
+                <ZPROCESS>?</ZPROCESS>
+                <!--Optional:-->
+                <ZSUPCODE>?</ZSUPCODE>
+                <!--Optional:-->
+                <ZUNIT_GTAX>?</ZUNIT_GTAX>
+                <!--Optional:-->
+                <ZZISDWPZ>?</ZZISDWPZ>
+                <!--Optional:-->
+                <ZZSRYS>?</ZZSRYS>
+            </arg0>
+        </ser:MT_KUNNR_IN_WMS01>
+    </soapenv:Body>
+</soapenv:Envelope>