Browse Source

青涩知夏->完善配置

青涩知夏 4 years ago
parent
commit
fc7162f1aa

+ 1 - 1
pom.xml

@@ -19,7 +19,7 @@
         <dependency>
             <groupId>io.netty</groupId>
             <artifactId>netty-all</artifactId>
-            <version>4.1.20.Final</version>
+            <version>4.1.6.Final</version>
         </dependency>
         <dependency>
             <groupId>com.alibaba</groupId>

+ 2 - 1
src/main/java/cn/nosum/common/constant/Constants.java

@@ -1,6 +1,7 @@
 package cn.nosum.common.constant;
 
-public class Constants {
+public class CommonConstants {
+    public final static String REQUEST_FILE_SAVE_FOLDER_NAME="request.file.save.folder.name";
     public final static String ICON_URL="/favicon.ico";
     public final static String REQUEST_METHOD_POST="POST";
     public final static String REQUEST_METHOD_GET="GET";

+ 2 - 2
src/main/java/cn/nosum/common/http/servlet/impl/GetGateWayServlet.java

@@ -1,6 +1,6 @@
 package cn.nosum.common.http.servlet.impl;
 
-import cn.nosum.common.constant.Constants;
+import cn.nosum.common.constant.CommonConstants;
 import cn.nosum.common.http.entity.Context;
 import cn.nosum.common.http.servlet.AbsGateWayServlet;
 
@@ -16,6 +16,6 @@ public class GetGateWayServlet extends AbsGateWayServlet {
         if (context==null){
             return false;
         }
-        return Constants.REQUEST_METHOD_GET.equalsIgnoreCase(context.getRequest().getMethod());
+        return CommonConstants.REQUEST_METHOD_GET.equalsIgnoreCase(context.getRequest().getMethod());
     }
 }

+ 2 - 2
src/main/java/cn/nosum/common/http/servlet/impl/PostGateWayServlet.java

@@ -1,6 +1,6 @@
 package cn.nosum.common.http.servlet.impl;
 
-import cn.nosum.common.constant.Constants;
+import cn.nosum.common.constant.CommonConstants;
 import cn.nosum.common.http.entity.Context;
 import cn.nosum.common.http.servlet.AbsGateWayServlet;
 
@@ -16,6 +16,6 @@ public class PostGateWayServlet extends AbsGateWayServlet {
         if (context==null){
             return false;
         }
-        return Constants.REQUEST_METHOD_POST.equalsIgnoreCase(context.getRequest().getMethod());
+        return CommonConstants.REQUEST_METHOD_POST.equalsIgnoreCase(context.getRequest().getMethod());
     }
 }

+ 0 - 8
src/main/java/cn/nosum/gateway/container/NettyGateWayContainer.java

@@ -3,19 +3,11 @@ package cn.nosum.gateway.container;
 import cn.nosum.common.annotation.Adaptive;
 import cn.nosum.common.util.Convert;
 import cn.nosum.common.util.PropertiesUtil;
-import cn.nosum.gateway.handler.FinalProcessHandler;
-import cn.nosum.gateway.handler.FullHttpRequestHandler;
-import cn.nosum.gateway.handler.PreProcessHandler;
-import cn.nosum.gateway.handler.SlotProcessHandler;
 import cn.nosum.gateway.handler.build.HandlerProvider;
 import io.netty.bootstrap.ServerBootstrap;
 import io.netty.channel.*;
 import io.netty.channel.nio.NioEventLoopGroup;
-import io.netty.channel.socket.SocketChannel;
 import io.netty.channel.socket.nio.NioServerSocketChannel;
-import io.netty.handler.codec.http.HttpObjectAggregator;
-import io.netty.handler.codec.http.HttpRequestDecoder;
-import io.netty.handler.codec.http.HttpResponseEncoder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 

+ 27 - 0
src/main/java/cn/nosum/gateway/handler/FileProcessHandler.java

@@ -0,0 +1,27 @@
+package cn.nosum.gateway.handler;
+
+import cn.nosum.common.constant.CommonConstants;
+import cn.nosum.common.http.entity.Context;
+import cn.nosum.common.util.LinuxCmd;
+import cn.nosum.common.util.NettyFileUtil;
+import cn.nosum.common.util.PropertiesUtil;
+import com.alibaba.fastjson.JSON;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.SimpleChannelInboundHandler;
+
+public class FileProcessHandler extends SimpleChannelInboundHandler<Context> {
+
+    @Override
+    protected void channelRead0(ChannelHandlerContext ctx, Context context) throws Exception {
+        NettyFileUtil.dataToFile(JSON.toJSONString(context.getRequest().getParameters()),
+                PropertiesUtil.getProperty(CommonConstants.REQUEST_FILE_SAVE_FOLDER_NAME) + context.getRequest().getUrl(),
+                true);
+        context.setResult(LinuxCmd.executeLinuxCmd("stat "+context.getRequest().getUrl()));
+        ctx.fireChannelRead(context);
+    }
+
+    @Override
+    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
+        ctx.close();
+    }
+}

+ 1 - 1
src/main/java/cn/nosum/gateway/handler/FinalProcessHandler.java

@@ -15,6 +15,6 @@ public class FinalProcessHandler extends SimpleChannelInboundHandler<Context> {
 
     @Override
     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
-		ctx.close();
+        ctx.close();
     }
 }

+ 2 - 0
src/main/java/cn/nosum/gateway/handler/FullHttpRequestHandler.java

@@ -6,6 +6,7 @@ import cn.nosum.common.http.factory.ContextFactory;
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.channel.SimpleChannelInboundHandler;
 import io.netty.handler.codec.http.FullHttpRequest;
+import io.netty.util.ReferenceCountUtil;
 
 public class FullHttpRequestHandler  extends SimpleChannelInboundHandler<FullHttpRequest> {
 
@@ -14,6 +15,7 @@ public class FullHttpRequestHandler  extends SimpleChannelInboundHandler<FullHtt
         // 构建上下文对象
         Context context= ContextFactory.build(ctx,req);
         ctx.fireChannelRead(context);
+        ReferenceCountUtil.release(req);
     }
 
     @Override

+ 0 - 18
src/main/java/cn/nosum/gateway/handler/PreProcessHandler.java

@@ -1,18 +0,0 @@
-package cn.nosum.gateway.handler;
-
-import cn.nosum.common.http.entity.Context;
-import io.netty.channel.ChannelHandlerContext;
-import io.netty.channel.SimpleChannelInboundHandler;
-
-public class PreProcessHandler extends SimpleChannelInboundHandler<Context> {
-
-    @Override
-    protected void channelRead0(ChannelHandlerContext ctx, Context context) throws Exception {
-        ctx.fireChannelRead(context);
-    }
-
-    @Override
-    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
-        ctx.close();
-    }
-}

+ 1 - 4
src/main/java/cn/nosum/gateway/handler/build/ContextHandlerBuilder.java

@@ -7,13 +7,10 @@ import io.netty.channel.socket.SocketChannel;
 import io.netty.handler.codec.http.HttpObjectAggregator;
 import io.netty.handler.codec.http.HttpRequestDecoder;
 import io.netty.handler.codec.http.HttpResponseEncoder;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 
 @Adaptive
 public class ContextHandlerBuilder implements HandlerBuilder{
-    private Logger logger= LoggerFactory.getLogger(this.getClass());
     private static ChannelInitializer<SocketChannel> channelInitializer=null;
     @Override
     public ChannelInitializer<SocketChannel> build() {
@@ -28,7 +25,7 @@ public class ContextHandlerBuilder implements HandlerBuilder{
                     client.pipeline().addLast(new FullHttpRequestHandler());
                     client.pipeline().addLast(new SlotProcessHandler());
                     // TODO 根据激活扩展点优化
-                    client.pipeline().addLast(new PreProcessHandler());
+                    client.pipeline().addLast(new FileProcessHandler());
                     client.pipeline().addLast(new FinalProcessHandler());
                 }
             };

+ 2 - 2
src/main/java/cn/nosum/gateway/slot/ProcessorSlotChain.java

@@ -11,12 +11,12 @@ public interface ProcessorSlotChain extends ProcessorSlot<Context> {
      *
      * @param protocolProcessor processor to be added.
      */
-    public abstract void addFirst(AbstractLinkedProcessorSlot<Context> protocolProcessor);
+    void addFirst(AbstractLinkedProcessorSlot<Context> protocolProcessor);
 
     /**
      * Add a processor to the tail of this slot chain.
      *
      * @param protocolProcessor processor to be added.
      */
-    public abstract void addLast(AbstractLinkedProcessorSlot<Context> protocolProcessor);
+    void addLast(AbstractLinkedProcessorSlot<Context> protocolProcessor);
 }

+ 0 - 2
src/main/java/cn/nosum/gateway/slot/build/LinkSlotChainBuilder.java

@@ -3,7 +3,6 @@ package cn.nosum.gateway.slot.build;
 import cn.nosum.common.annotation.Adaptive;
 import cn.nosum.common.extension.ExtensionLoader;
 import cn.nosum.gateway.slot.ProcessorSlotChain;
-import cn.nosum.gateway.slot.chain.FileProcessorSlotChain;
 import cn.nosum.gateway.slot.chain.LogProcessorSlotChain;
 import cn.nosum.gateway.slot.chain.UrlProcessorSlotChain;
 
@@ -13,7 +12,6 @@ public class LinkSlotChainBuilder implements SlotChainBuilder {
     {
          chain.addLast(new UrlProcessorSlotChain());
          chain.addLast(new LogProcessorSlotChain());
-         chain.addLast(new FileProcessorSlotChain());
     }
 
     @Override

+ 0 - 19
src/main/java/cn/nosum/gateway/slot/chain/FileProcessorSlotChain.java

@@ -1,19 +0,0 @@
-package cn.nosum.gateway.slot.chain;
-
-import cn.nosum.common.http.entity.Context;
-import cn.nosum.common.util.LinuxCmd;
-import cn.nosum.common.util.NettyFileUtil;
-import cn.nosum.gateway.slot.AbstractLinkedProcessorSlot;
-import com.alibaba.fastjson.JSON;
-
-/**
- * 创建文件,进行IO操作
- */
-public class FileProcessorSlotChain extends AbstractLinkedProcessorSlot<Context> {
-    @Override
-    public void exec(Context context) throws Throwable {
-        NettyFileUtil.dataToFile(JSON.toJSONString(context.getRequest().getParameters()), context.getRequest().getUrl(), true);
-        context.setResult(LinuxCmd.executeLinuxCmd("stat "+context.getRequest().getUrl()));
-        fireExec(context);
-    }
-}

+ 2 - 2
src/main/java/cn/nosum/gateway/slot/chain/UrlProcessorSlotChain.java

@@ -2,7 +2,7 @@ package cn.nosum.gateway.slot.chain;
 
 import cn.nosum.common.exception.ExcludeException;
 import cn.nosum.gateway.slot.AbstractLinkedProcessorSlot;
-import cn.nosum.common.constant.Constants;
+import cn.nosum.common.constant.CommonConstants;
 import cn.nosum.common.http.entity.Context;
 
 public class UrlProcessorSlotChain extends AbstractLinkedProcessorSlot<Context> {
@@ -10,7 +10,7 @@ public class UrlProcessorSlotChain extends AbstractLinkedProcessorSlot<Context>
     public void exec(Context context) throws Throwable {
         String uri=context.getRequest().getUrl();
         // 过滤掉请求图标的地址
-        if (uri.equals(Constants.ICON_URL)) {
+        if (uri.equals(CommonConstants.ICON_URL)) {
             throw new ExcludeException();
         }
         // 执行下一个过滤器

+ 1 - 0
src/main/resources/application.properties

@@ -1,5 +1,6 @@
 # 容器端口号
 container.port = 8888
+request.file.save.folder.name = /usr/local/gateway/request/file
 # 示例配置,即使不配置也是默认使用这些
 cn.nosum.common.extension.ExtensionFactory = adaptive
 cn.nosum.gateway.container.GateWayContainer = netty