Browse Source

青涩知夏->完善配置

青涩知夏 4 years ago
parent
commit
299afe9bd6

+ 0 - 8
.gitignore

@@ -19,13 +19,5 @@ target/
 *.iml
 *.ipr
 
-### NetBeans ###
-/nbproject/private/
-/nbbuild/
-/dist/
-/nbdist/
-/.nb-gradle/
-build/
-
 ### VS Code ###
 .vscode/

+ 1 - 1
src/main/java/cn/nosum/common/extension/ExtensionFactory.java

@@ -2,7 +2,7 @@ package cn.nosum.common.extension;
 
 import cn.nosum.common.annotation.SPI;
 
-@SPI
+@SPI("adaptive")
 public interface ExtensionFactory {
 
     /**

+ 4 - 3
src/main/java/cn/nosum/common/extension/ExtensionLoader.java

@@ -337,14 +337,15 @@ public class ExtensionLoader<T> {
                     + clazz.getName() + " is not subtype of interface.");
         }
         if (clazz.isAnnotationPresent(Adaptive.class)) {
-            cacheAdaptiveClass(clazz);
+            cacheAdaptiveClass(clazz,name);
         } else if (isWrapperClass(clazz)) {
             cacheWrapperClass(clazz);
         }
     }
 
-    private void cacheAdaptiveClass(Class<?> clazz) {
-        if (cachedAdaptiveClass == null) {
+    private void cacheAdaptiveClass(Class<?> clazz,String name) {
+        // 只有名称匹配时才会进行保存
+        if (cachedAdaptiveClass == null && name.equals(cachedDefaultName)) {
             cachedAdaptiveClass = clazz;
         } else if (!cachedAdaptiveClass.equals(clazz)) {
             throw new IllegalStateException("More than 1 adaptive class found: "

+ 1 - 0
src/main/java/cn/nosum/common/http/servlet/factory/GateWayServletFactory.java

@@ -14,6 +14,7 @@ public class GateWayServletFactory {
     public static IGateWayServlet getGateWayServlet(String method){
         return gateWayServletMap.get(method);
     }
+
     public static void register(String method,IGateWayServlet gateWayServlet){
         gateWayServletMap.put(method,gateWayServlet);
     }

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

@@ -13,9 +13,9 @@ public class GetGateWayServlet extends AbsGateWayServlet {
 
     @Override
     protected boolean doCheck(Context context) {
-        if (context!=null){
-            return Constants.REQUEST_METHOD_GET.equalsIgnoreCase(context.getRequest().getMethod());
+        if (context==null){
+            return false;
         }
-        return false;
+        return Constants.REQUEST_METHOD_GET.equalsIgnoreCase(context.getRequest().getMethod());
     }
 }

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

@@ -13,9 +13,9 @@ public class PostGateWayServlet extends AbsGateWayServlet {
 
     @Override
     protected boolean doCheck(Context context) {
-        if (context!=null){
-            return Constants.REQUEST_METHOD_POST.equalsIgnoreCase(context.getRequest().getMethod());
+        if (context==null){
+            return false;
         }
-        return false;
+        return Constants.REQUEST_METHOD_POST.equalsIgnoreCase(context.getRequest().getMethod());
     }
 }

+ 1 - 1
src/main/java/cn/nosum/gateway/container/GateWayContainer.java

@@ -5,5 +5,5 @@ import cn.nosum.common.annotation.SPI;
 
 @SPI("netty")
 public interface GateWayContainer {
-    public void start();
+    void start();
 }

+ 38 - 0
src/main/java/cn/nosum/gateway/handler/build/ContextHandlerBuilder.java

@@ -0,0 +1,38 @@
+package cn.nosum.gateway.handler.build;
+
+import cn.nosum.common.annotation.Adaptive;
+import cn.nosum.gateway.handler.*;
+import io.netty.channel.ChannelInitializer;
+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() {
+        if (channelInitializer==null){
+            channelInitializer = new ChannelInitializer<SocketChannel>() {
+                // 客户端初始化处理
+                protected void initChannel(SocketChannel client) throws Exception {
+                    // Netty对HTTP协议的封装,顺序有要求
+                    client.pipeline().addLast(new HttpRequestDecoder());
+                    client.pipeline().addLast(new HttpObjectAggregator(65535));//将多个消息转化成一个
+                    client.pipeline().addLast(new HttpResponseEncoder());
+                    client.pipeline().addLast(new FullHttpRequestHandler());
+                    client.pipeline().addLast(new SlotProcessHandler());
+                    // TODO 根据激活扩展点优化
+                    client.pipeline().addLast(new PreProcessHandler());
+                    client.pipeline().addLast(new FinalProcessHandler());
+                }
+            };
+        }
+        return channelInitializer;
+    }
+}

+ 10 - 0
src/main/java/cn/nosum/gateway/handler/build/HandlerBuilder.java

@@ -0,0 +1,10 @@
+package cn.nosum.gateway.handler.build;
+
+import cn.nosum.common.annotation.SPI;
+import io.netty.channel.ChannelInitializer;
+import io.netty.channel.socket.SocketChannel;
+
+@SPI("context")
+public interface HandlerBuilder {
+    ChannelInitializer<SocketChannel> build();
+}

+ 20 - 0
src/main/java/cn/nosum/gateway/handler/build/HandlerProvider.java

@@ -0,0 +1,20 @@
+package cn.nosum.gateway.handler.build;
+
+
+import cn.nosum.common.extension.ExtensionLoader;
+import io.netty.channel.ChannelInitializer;
+import io.netty.channel.socket.SocketChannel;
+
+public final class HandlerProvider {
+    private HandlerProvider(){}
+    private static volatile HandlerBuilder builder = null;
+    public static ChannelInitializer<SocketChannel> newHandlerChannel(){
+        if (builder != null) {
+            return builder.build();
+        }
+        if (builder == null) {
+            builder = ExtensionLoader.getExtensionLoader(HandlerBuilder.class).getAdaptiveExtension();
+        }
+        return builder.build();
+    }
+}

+ 8 - 5
src/main/java/cn/nosum/gateway/slot/build/DefaultSlotChainBuilder.java

@@ -8,13 +8,16 @@ import cn.nosum.gateway.slot.chain.LogProcessorSlotChain;
 import cn.nosum.gateway.slot.chain.UrlProcessorSlotChain;
 
 @Adaptive
-public class DefaultSlotChainBuilder implements SlotChainBuilder {
+public class LinkSlotChainBuilder implements SlotChainBuilder {
+    private ProcessorSlotChain chain=ExtensionLoader.getExtensionLoader(ProcessorSlotChain.class).getAdaptiveExtension();
+    {
+         chain.addLast(new UrlProcessorSlotChain());
+         chain.addLast(new LogProcessorSlotChain());
+         chain.addLast(new FileProcessorSlotChain());
+    }
+
     @Override
     public ProcessorSlotChain build() {
-        ProcessorSlotChain chain = ExtensionLoader.getExtensionLoader(ProcessorSlotChain.class).getAdaptiveExtension();
-        chain.addLast(new UrlProcessorSlotChain());
-        chain.addLast(new LogProcessorSlotChain());
-        chain.addLast(new FileProcessorSlotChain());
         return chain;
     }
 }

+ 20 - 0
src/main/java/cn/nosum/gateway/slot/build/SlotChainProvider.java

@@ -0,0 +1,20 @@
+package cn.nosum.gateway.slot.build;
+
+
+import cn.nosum.common.extension.ExtensionLoader;
+import cn.nosum.gateway.slot.ProcessorSlotChain;
+
+public final class SlotChainProvider {
+    private SlotChainProvider() {}
+    private static volatile SlotChainBuilder builder = null;
+
+    public static ProcessorSlotChain newSlotChain() {
+        if (builder != null) {
+            return builder.build();
+        }
+        if (builder == null) {
+            builder = ExtensionLoader.getExtensionLoader(SlotChainBuilder.class).getAdaptiveExtension();
+        }
+        return builder.build();
+    }
+}

+ 1 - 1
src/main/resources/META-INF/gateway/cn.nosum.gateway.slot.build.SlotChainBuilder

@@ -1 +1 @@
-default=cn.nosum.gateway.slot.build.DefaultSlotChainBuilder
+context=cn.nosum.gateway.slot.build.LinkSlotChainBuilder