Browse Source

抽离web模块

zhanghaijun 2 years ago
parent
commit
933e2ae435

+ 5 - 0
train-common/train-common-bom/pom.xml

@@ -23,6 +23,11 @@
                 <artifactId>train-common-mybatis</artifactId>
                 <version>${revision}</version>
             </dependency>
+            <dependency>
+                <groupId>top.haijunit</groupId>
+                <artifactId>train-common-web</artifactId>
+                <version>${revision}</version>
+            </dependency>
             <dependency>
                 <groupId>top.haijunit</groupId>
                 <artifactId>train-common-websocket</artifactId>

+ 8 - 0
train-common/train-common-web/src/main/java/top/haijunit/web/config/ResourcesConfig.java

@@ -5,7 +5,9 @@ import org.springframework.context.annotation.Bean;
 import org.springframework.web.cors.CorsConfiguration;
 import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
 import org.springframework.web.filter.CorsFilter;
+import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+import top.haijunit.web.interceptor.WebInvokeTimeInterceptor;
 
 /**
  * @author zhanghaijun
@@ -15,6 +17,12 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 @AutoConfiguration
 public class ResourcesConfig implements WebMvcConfigurer {
 
+    @Override
+    public void addInterceptors(InterceptorRegistry registry) {
+        // 全局访问性能拦截
+        registry.addInterceptor(new WebInvokeTimeInterceptor());
+    }
+
     /** 跨域配置 */
     @Bean
     public CorsFilter corsFilter() {

+ 1 - 2
train-server/src/main/java/top/haijunit/train/config/filter/RepeatedlyRequestWrapper.java

@@ -1,4 +1,4 @@
-package top.haijunit.train.config.filter;
+package top.haijunit.web.filter;
 
 import cn.hutool.core.io.IoUtil;
 import jakarta.servlet.ReadListener;
@@ -14,7 +14,6 @@ import java.io.InputStreamReader;
 
 /**
  * @author zhanghaijun
- * @version 1.0
  * @date 2023/11/7 14:24
  * @description 构建可重复读取inputStream的request
  */

+ 6 - 22
train-server/src/main/java/top/haijunit/train/config/interceptor/PlusWebInvokeTimeInterceptor.java

@@ -1,40 +1,35 @@
-package top.haijunit.train.config.interceptor;
+package top.haijunit.web.interceptor;
 
 import cn.hutool.core.io.IoUtil;
 import cn.hutool.core.map.MapUtil;
 import cn.hutool.core.util.StrUtil;
-import com.alibaba.ttl.TransmittableThreadLocal;
 import jakarta.servlet.http.HttpServletRequest;
 import jakarta.servlet.http.HttpServletResponse;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.commons.lang3.time.StopWatch;
 import org.springframework.http.MediaType;
 import org.springframework.web.servlet.HandlerInterceptor;
 import org.springframework.web.servlet.ModelAndView;
 import top.haijunit.common.utils.JsonUtils;
 import top.haijunit.common.utils.SpringUtils;
-import top.haijunit.train.config.filter.RepeatedlyRequestWrapper;
+import top.haijunit.web.filter.RepeatedlyRequestWrapper;
 
 import java.io.BufferedReader;
 import java.util.Map;
 
 /**
  * @author zhanghaijun
- * @date 2023/11/7 14:02
+ * @date 2023/11/22 18:31
  * @description web的调用时间统计拦截器
  */
 @Slf4j
-public class PlusWebInvokeTimeInterceptor implements HandlerInterceptor {
+public class WebInvokeTimeInterceptor implements HandlerInterceptor {
 
-    private final String prodProfile = "prod";
-
-    private final TransmittableThreadLocal<StopWatch> invokeTimeTL = new TransmittableThreadLocal<>();
+    private final static String PRO_FILE = "prod";
 
     @Override
     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
-        if (!prodProfile.equals(SpringUtils.getActiveProfile())) {
+        if (!PRO_FILE.equals(SpringUtils.getActiveProfile())) {
             String url = request.getMethod() + " " + request.getRequestURI();
-
             // 打印请求参数
             if (isJsonRequest(request)) {
                 String jsonParam = "";
@@ -52,10 +47,6 @@ public class PlusWebInvokeTimeInterceptor implements HandlerInterceptor {
                     log.debug("[PLUS]开始请求 => URL[{}],无参数", url);
                 }
             }
-
-            StopWatch stopWatch = new StopWatch();
-            invokeTimeTL.set(stopWatch);
-            stopWatch.start();
         }
         return true;
     }
@@ -67,12 +58,6 @@ public class PlusWebInvokeTimeInterceptor implements HandlerInterceptor {
 
     @Override
     public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
-        if (!prodProfile.equals(SpringUtils.getActiveProfile())) {
-            StopWatch stopWatch = invokeTimeTL.get();
-            stopWatch.stop();
-            log.debug("[PLUS]结束请求 => URL[{}],耗时:[{}]毫秒", request.getMethod() + " " + request.getRequestURI(), stopWatch.getTime());
-            invokeTimeTL.remove();
-        }
     }
 
     /**
@@ -88,5 +73,4 @@ public class PlusWebInvokeTimeInterceptor implements HandlerInterceptor {
         }
         return false;
     }
-
 }

+ 6 - 27
train-server/pom.xml

@@ -25,30 +25,14 @@
             <groupId>top.haijunit</groupId>
             <artifactId>train-common-websocket</artifactId>
         </dependency>
-        <!-- SpringBoot Web容器 -->
         <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter-web</artifactId>
-            <exclusions>
-                <exclusion>
-                    <artifactId>spring-boot-starter-tomcat</artifactId>
-                    <groupId>org.springframework.boot</groupId>
-                </exclusion>
-            </exclusions>
-        </dependency>
-        <!-- web 容器使用 undertow 性能更强 -->
-        <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter-undertow</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter-websocket</artifactId>
+            <groupId>top.haijunit</groupId>
+            <artifactId>train-common-web</artifactId>
         </dependency>
+<!--        <dependency>-->
+<!--            <groupId>org.springframework.boot</groupId>-->
+<!--            <artifactId>spring-boot-starter</artifactId>-->
+<!--        </dependency>-->
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-thymeleaf</artifactId>
@@ -72,11 +56,6 @@
             <artifactId>junit</artifactId>
             <scope>test</scope>
         </dependency>
-        <dependency>
-            <groupId>com.alibaba</groupId>
-            <artifactId>transmittable-thread-local</artifactId>
-            <version>${alibaba-ttl.version}</version>
-        </dependency>
         <!-- https://mvnrepository.com/artifact/org.redisson/redisson-spring-boot-starter -->
         <dependency>
             <groupId>org.redisson</groupId>

+ 0 - 60
train-server/src/main/java/top/haijunit/train/config/ResourcesConfig.java

@@ -1,60 +0,0 @@
-package top.haijunit.train.config;
-
-import org.springframework.boot.autoconfigure.AutoConfiguration;
-import org.springframework.context.annotation.Bean;
-import org.springframework.web.cors.CorsConfiguration;
-import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
-import org.springframework.web.filter.CorsFilter;
-import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
-import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
-import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
-import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
-import top.haijunit.train.config.interceptor.PlusWebInvokeTimeInterceptor;
-
-/**
- * @author zhanghaijun
- * @date 2023/8/30 13:30
- * @description web相关的配置
- */
-@AutoConfiguration
-public class ResourcesConfig implements WebMvcConfigurer {
-
-    @Override
-    public void addResourceHandlers(ResourceHandlerRegistry registry) {
-        // registry.addResourceHandler("/**").addResourceLocations("classpath:static/");
-    }
-
-    @Override
-    public void addViewControllers(ViewControllerRegistry registry) {
-        // registry.addViewController("/").setViewName("/web");
-        // registry.addViewController("/index").setViewName("index.html");
-    }
-
-    @Override
-    public void addInterceptors(InterceptorRegistry registry) {
-        // 全局访问性能拦截
-        registry.addInterceptor(new PlusWebInvokeTimeInterceptor());
-    }
-
-    /**
-     * 跨域配置
-     */
-    @Bean
-    public CorsFilter corsFilter() {
-        CorsConfiguration config = new CorsConfiguration();
-        config.setAllowCredentials(true);
-        // 设置访问源地址
-        config.addAllowedOriginPattern("*");
-        // 设置访问源请求头
-        config.addAllowedHeader("*");
-        // 设置访问源请求方法
-        config.addAllowedMethod("*");
-        // 有效期 1800秒
-        config.setMaxAge(1800L);
-        // 添加映射路径,拦截一切请求
-        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
-        source.registerCorsConfiguration("/**", config);
-        // 返回新的CorsFilter
-        return new CorsFilter(source);
-    }
-}

+ 0 - 30
train-server/src/main/java/top/haijunit/train/config/UndertowConfig.java

@@ -1,30 +0,0 @@
-package top.haijunit.train.config;
-
-import io.undertow.server.DefaultByteBufferPool;
-import io.undertow.websockets.jsr.WebSocketDeploymentInfo;
-import org.springframework.boot.autoconfigure.AutoConfiguration;
-import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
-import org.springframework.boot.web.server.WebServerFactoryCustomizer;
-
-/**
- * @author zhanghaijun
- * @date 2023/11/7 15:40
- * @description Undertow 自定义配置
- */
-@AutoConfiguration
-public class UndertowConfig implements WebServerFactoryCustomizer<UndertowServletWebServerFactory> {
-
-    /**
-     * 设置 Undertow 的 websocket 缓冲池
-     */
-    @Override
-    public void customize(UndertowServletWebServerFactory factory) {
-        // 默认不直接分配内存 如果项目中使用了 websocket 建议直接分配
-        factory.addDeploymentInfoCustomizers(deploymentInfo -> {
-            WebSocketDeploymentInfo webSocketDeploymentInfo = new WebSocketDeploymentInfo();
-            webSocketDeploymentInfo.setBuffers(new DefaultByteBufferPool(false, 1024));
-            deploymentInfo.addServletContextAttribute("io.undertow.websockets.jsr.WebSocketDeploymentInfo", webSocketDeploymentInfo);
-        });
-    }
-}
-

+ 0 - 1
train-server/src/main/java/top/haijunit/train/config/WebSocketConfig.java

@@ -15,5 +15,4 @@ public class WebSocketConfig {
     public ServerEndpointExporter serverEndpointExporter() {
         return new ServerEndpointExporter();
     }
-
 }