diff --git a/zerenliandongtai/.idea/.gitignore b/zerenliandongtai/.idea/.gitignore new file mode 100644 index 0000000..c68e6c2 --- /dev/null +++ b/zerenliandongtai/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/../../../../:\java project\mergedemo\.idea/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/zerenliandongtai/.idea/compiler.xml b/zerenliandongtai/.idea/compiler.xml new file mode 100644 index 0000000..b15642a --- /dev/null +++ b/zerenliandongtai/.idea/compiler.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/zerenliandongtai/.idea/jarRepositories.xml b/zerenliandongtai/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/zerenliandongtai/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/zerenliandongtai/.idea/misc.xml b/zerenliandongtai/.idea/misc.xml new file mode 100644 index 0000000..4b661a5 --- /dev/null +++ b/zerenliandongtai/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/zerenliandongtai/.idea/uiDesigner.xml b/zerenliandongtai/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/zerenliandongtai/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/zerenliandongtai/mergedemo.iml b/zerenliandongtai/mergedemo.iml new file mode 100644 index 0000000..78b2cc5 --- /dev/null +++ b/zerenliandongtai/mergedemo.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/zerenliandongtai/pom.xml b/zerenliandongtai/pom.xml new file mode 100644 index 0000000..93accab --- /dev/null +++ b/zerenliandongtai/pom.xml @@ -0,0 +1,20 @@ + + + 4.0.0 + + com.woniu + mergedemo + 1.0-SNAPSHOT + + + + org.projectlombok + lombok + 1.18.24 + + + + + \ No newline at end of file diff --git a/zerenliandongtai/src/main/java/com/woniu/zerenlianfactorydemo/afteryouhua/ApiLimitGatewayHandler.java b/zerenliandongtai/src/main/java/com/woniu/zerenlianfactorydemo/afteryouhua/ApiLimitGatewayHandler.java new file mode 100644 index 0000000..2949b34 --- /dev/null +++ b/zerenliandongtai/src/main/java/com/woniu/zerenlianfactorydemo/afteryouhua/ApiLimitGatewayHandler.java @@ -0,0 +1,12 @@ +package com.woniu.zerenlianfactorydemo.afteryouhua; + +public class ApiLimitGatewayHandler extends GatewayHandler { + + @Override + public void service() { + System.out.println("api接口限流"); + if (this.next != null) { + this.next.service(); + } + } +} diff --git a/zerenliandongtai/src/main/java/com/woniu/zerenlianfactorydemo/afteryouhua/BlacklistGatewayHandler.java b/zerenliandongtai/src/main/java/com/woniu/zerenlianfactorydemo/afteryouhua/BlacklistGatewayHandler.java new file mode 100644 index 0000000..2647582 --- /dev/null +++ b/zerenliandongtai/src/main/java/com/woniu/zerenlianfactorydemo/afteryouhua/BlacklistGatewayHandler.java @@ -0,0 +1,12 @@ +package com.woniu.zerenlianfactorydemo.afteryouhua; + +public class BlacklistGatewayHandler extends GatewayHandler { + + @Override + public void service() { + System.out.println("黑名单拦截"); + if (this.next != null) { + this.next.service(); + } + } +} diff --git a/zerenliandongtai/src/main/java/com/woniu/zerenlianfactorydemo/afteryouhua/GatewayDao.java b/zerenliandongtai/src/main/java/com/woniu/zerenlianfactorydemo/afteryouhua/GatewayDao.java new file mode 100644 index 0000000..366250f --- /dev/null +++ b/zerenliandongtai/src/main/java/com/woniu/zerenlianfactorydemo/afteryouhua/GatewayDao.java @@ -0,0 +1,24 @@ +package com.woniu.zerenlianfactorydemo.afteryouhua; + +/** + * @className: GatewayDao + * @author: woniuge + * @date: 2023/2/2 + **/ +public interface GatewayDao { + + /** + * 根据 handlerId 获取配置项 + * + * @param handlerId + * @return + */ + GatewayEntity getGatewayEntity(Integer handlerId); + + /** + * 获取第一个处理者 + * + * @return + */ + GatewayEntity getFirstGatewayEntity(); +} \ No newline at end of file diff --git a/zerenliandongtai/src/main/java/com/woniu/zerenlianfactorydemo/afteryouhua/GatewayEntity.java b/zerenliandongtai/src/main/java/com/woniu/zerenlianfactorydemo/afteryouhua/GatewayEntity.java new file mode 100644 index 0000000..a4a2ecf --- /dev/null +++ b/zerenliandongtai/src/main/java/com/woniu/zerenlianfactorydemo/afteryouhua/GatewayEntity.java @@ -0,0 +1,21 @@ +package com.woniu.zerenlianfactorydemo.afteryouhua; + + +import lombok.AllArgsConstructor; +import lombok.Data; + +@Data +@AllArgsConstructor +public class GatewayEntity { + + private Integer handlerId; + + private String name; + + private String conference; + + + private Integer preHandlerId; + + private Integer nextHandlerId; +} \ No newline at end of file diff --git a/zerenliandongtai/src/main/java/com/woniu/zerenlianfactorydemo/afteryouhua/GatewayEnum.java b/zerenliandongtai/src/main/java/com/woniu/zerenlianfactorydemo/afteryouhua/GatewayEnum.java new file mode 100644 index 0000000..60178dc --- /dev/null +++ b/zerenliandongtai/src/main/java/com/woniu/zerenlianfactorydemo/afteryouhua/GatewayEnum.java @@ -0,0 +1,29 @@ +package com.woniu.zerenlianfactorydemo.afteryouhua; + +/** + * 我们也可以把这个关系维护到配置文件中或者一个枚举中。 + */ +public enum GatewayEnum { + // handlerId, 拦截者名称,全限定类名,preHandlerId,nextHandlerId + API_HANDLER(new GatewayEntity(1, "api接口限流", "com.woniu.zerenlianfactorydemo.afteryouhua.ApiLimitGatewayHandler", null, 2)), + BLACKLIST_HANDLER(new GatewayEntity(2, "黑名单拦截", "com.woniu.zerenlianfactorydemo.afteryouhua.BlacklistGatewayHandler", 1, 3)), + SESSION_HANDLER(new GatewayEntity(3, "用户会话拦截", "com.woniu.zerenlianfactorydemo.afteryouhua.SessionGatewayHandler", 2, null)), + ; + + GatewayEntity gatewayEntity; + + public GatewayEntity getGatewayEntity() { + return gatewayEntity; + } + + GatewayEnum(GatewayEntity gatewayEntity) { + this.gatewayEntity = gatewayEntity; + } +} + + + + + + + diff --git a/zerenliandongtai/src/main/java/com/woniu/zerenlianfactorydemo/afteryouhua/GatewayHandler.java b/zerenliandongtai/src/main/java/com/woniu/zerenlianfactorydemo/afteryouhua/GatewayHandler.java new file mode 100644 index 0000000..92fc4f3 --- /dev/null +++ b/zerenliandongtai/src/main/java/com/woniu/zerenlianfactorydemo/afteryouhua/GatewayHandler.java @@ -0,0 +1,17 @@ +package com.woniu.zerenlianfactorydemo.afteryouhua; + + +public abstract class GatewayHandler { + + /** + * 下一关用当前抽象类来接收 + */ + protected GatewayHandler next; + + public void setNext(GatewayHandler next) { + this.next = next; + } + + public abstract void service(); +} + diff --git a/zerenliandongtai/src/main/java/com/woniu/zerenlianfactorydemo/afteryouhua/GatewayHandlerEnumFactory.java b/zerenliandongtai/src/main/java/com/woniu/zerenlianfactorydemo/afteryouhua/GatewayHandlerEnumFactory.java new file mode 100644 index 0000000..cdbae17 --- /dev/null +++ b/zerenliandongtai/src/main/java/com/woniu/zerenlianfactorydemo/afteryouhua/GatewayHandlerEnumFactory.java @@ -0,0 +1,50 @@ +package com.woniu.zerenlianfactorydemo.afteryouhua; + +public class GatewayHandlerEnumFactory { + + private static GatewayDao gatewayDao = new GatewayImpl(); + + // 提供静态方法,获取第一个handler + public static GatewayHandler getFirstGatewayHandler() { + + GatewayEntity firstGatewayEntity = gatewayDao.getFirstGatewayEntity(); + GatewayHandler firstGatewayHandler = newGatewayHandler(firstGatewayEntity); + if (firstGatewayHandler == null) { + return null; + } + + GatewayEntity tempGatewayEntity = firstGatewayEntity; + Integer nextHandlerId = null; + GatewayHandler tempGatewayHandler = firstGatewayHandler; + // 迭代遍历所有handler,以及将它们链接起来 + while ((nextHandlerId = tempGatewayEntity.getNextHandlerId()) != null) { + GatewayEntity gatewayEntity = gatewayDao.getGatewayEntity(nextHandlerId); + GatewayHandler gatewayHandler = newGatewayHandler(gatewayEntity); + tempGatewayHandler.setNext(gatewayHandler); + tempGatewayHandler = gatewayHandler; + tempGatewayEntity = gatewayEntity; + } + // 返回第一个handler + return firstGatewayHandler; + } + + /** + * 反射实体化具体的处理者 + * + * @param firstGatewayEntity + * @return + */ + private static GatewayHandler newGatewayHandler(GatewayEntity firstGatewayEntity) { + // 获取全限定类名 + String className = firstGatewayEntity.getConference(); + try { + // 根据全限定类名,加载并初始化该类,即会初始化该类的静态段 + Class clazz = Class.forName(className); + return (GatewayHandler) clazz.newInstance(); + } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) { + e.printStackTrace(); + } + return null; + } + +} \ No newline at end of file diff --git a/zerenliandongtai/src/main/java/com/woniu/zerenlianfactorydemo/afteryouhua/GatewayImpl.java b/zerenliandongtai/src/main/java/com/woniu/zerenlianfactorydemo/afteryouhua/GatewayImpl.java new file mode 100644 index 0000000..c67a80d --- /dev/null +++ b/zerenliandongtai/src/main/java/com/woniu/zerenlianfactorydemo/afteryouhua/GatewayImpl.java @@ -0,0 +1,38 @@ +package com.woniu.zerenlianfactorydemo.afteryouhua; + + +import java.util.HashMap; +import java.util.Map; + +public class GatewayImpl implements GatewayDao { + + /** + * 初始化,将枚举中配置的handler初始化到map中,方便获取 + */ + private static Map gatewayEntityMap = new HashMap<>(); + + static { + GatewayEnum[] values = GatewayEnum.values(); + for (GatewayEnum value : values) { + GatewayEntity gatewayEntity = value.getGatewayEntity(); + gatewayEntityMap.put(gatewayEntity.getHandlerId(), gatewayEntity); + } + } + + @Override + public GatewayEntity getGatewayEntity(Integer handlerId) { + return gatewayEntityMap.get(handlerId); + } + + @Override + public GatewayEntity getFirstGatewayEntity() { + for (Map.Entry entry : gatewayEntityMap.entrySet()) { + GatewayEntity value = entry.getValue(); + // 没有上一个handler的就是第一个 + if (value.getPreHandlerId() == null) { + return value; + } + } + return null; + } +} \ No newline at end of file diff --git a/zerenliandongtai/src/main/java/com/woniu/zerenlianfactorydemo/afteryouhua/GetewayClient.java b/zerenliandongtai/src/main/java/com/woniu/zerenlianfactorydemo/afteryouhua/GetewayClient.java new file mode 100644 index 0000000..47ba7d5 --- /dev/null +++ b/zerenliandongtai/src/main/java/com/woniu/zerenlianfactorydemo/afteryouhua/GetewayClient.java @@ -0,0 +1,11 @@ +package com.woniu.zerenlianfactorydemo.afteryouhua; + +/** + * 用责任链模式 + 工厂模式 + 枚举,动态配置请求链并调用。 + */ +public class GetewayClient { + public static void main(String[] args) { + GatewayHandler firstGatewayHandler = GatewayHandlerEnumFactory.getFirstGatewayHandler(); + firstGatewayHandler.service(); + } +} \ No newline at end of file diff --git a/zerenliandongtai/src/main/java/com/woniu/zerenlianfactorydemo/afteryouhua/SessionGatewayHandler.java b/zerenliandongtai/src/main/java/com/woniu/zerenlianfactorydemo/afteryouhua/SessionGatewayHandler.java new file mode 100644 index 0000000..1b02845 --- /dev/null +++ b/zerenliandongtai/src/main/java/com/woniu/zerenlianfactorydemo/afteryouhua/SessionGatewayHandler.java @@ -0,0 +1,12 @@ +package com.woniu.zerenlianfactorydemo.afteryouhua; + +public class SessionGatewayHandler extends GatewayHandler { + + @Override + public void service() { + System.out.println("用户会话拦截"); + if (this.next != null) { + this.next.service(); + } + } +} diff --git a/zerenliandongtai/src/main/java/com/woniu/zerenlianfactorydemo/before/GetewayClientBefore.java b/zerenliandongtai/src/main/java/com/woniu/zerenlianfactorydemo/before/GetewayClientBefore.java new file mode 100644 index 0000000..a1610fc --- /dev/null +++ b/zerenliandongtai/src/main/java/com/woniu/zerenlianfactorydemo/before/GetewayClientBefore.java @@ -0,0 +1,26 @@ +package com.woniu.zerenlianfactorydemo.before; + +import com.woniu.zerenlianfactorydemo.afteryouhua.ApiLimitGatewayHandler; +import com.woniu.zerenlianfactorydemo.afteryouhua.BlacklistGatewayHandler; +import com.woniu.zerenlianfactorydemo.afteryouhua.GatewayHandler; +import com.woniu.zerenlianfactorydemo.afteryouhua.SessionGatewayHandler; + +/** + * 我们可以通过链表将每一关连接起来, + * 形成责任链的方式 + */ +public class GetewayClientBefore { + public static void main(String[] args) { + //api接口限流 黑名单拦截 用户会话拦截 + + GatewayHandler firstPassHandler = new ApiLimitGatewayHandler(); + GatewayHandler secondPassHandler = new BlacklistGatewayHandler(); + GatewayHandler thirdPassHandler = new SessionGatewayHandler(); + + firstPassHandler.setNext(secondPassHandler); + secondPassHandler.setNext(thirdPassHandler); + + firstPassHandler.service(); + + } +} diff --git a/zerenliandongtai/target/classes/com/woniu/zerenlianfactorydemo/afteryouhua/ApiLimitGatewayHandler.class b/zerenliandongtai/target/classes/com/woniu/zerenlianfactorydemo/afteryouhua/ApiLimitGatewayHandler.class new file mode 100644 index 0000000..9d761a8 Binary files /dev/null and b/zerenliandongtai/target/classes/com/woniu/zerenlianfactorydemo/afteryouhua/ApiLimitGatewayHandler.class differ diff --git a/zerenliandongtai/target/classes/com/woniu/zerenlianfactorydemo/afteryouhua/BlacklistGatewayHandler.class b/zerenliandongtai/target/classes/com/woniu/zerenlianfactorydemo/afteryouhua/BlacklistGatewayHandler.class new file mode 100644 index 0000000..8cfc356 Binary files /dev/null and b/zerenliandongtai/target/classes/com/woniu/zerenlianfactorydemo/afteryouhua/BlacklistGatewayHandler.class differ diff --git a/zerenliandongtai/target/classes/com/woniu/zerenlianfactorydemo/afteryouhua/GatewayDao.class b/zerenliandongtai/target/classes/com/woniu/zerenlianfactorydemo/afteryouhua/GatewayDao.class new file mode 100644 index 0000000..ef8a6c5 Binary files /dev/null and b/zerenliandongtai/target/classes/com/woniu/zerenlianfactorydemo/afteryouhua/GatewayDao.class differ diff --git a/zerenliandongtai/target/classes/com/woniu/zerenlianfactorydemo/afteryouhua/GatewayEntity.class b/zerenliandongtai/target/classes/com/woniu/zerenlianfactorydemo/afteryouhua/GatewayEntity.class new file mode 100644 index 0000000..f66b691 Binary files /dev/null and b/zerenliandongtai/target/classes/com/woniu/zerenlianfactorydemo/afteryouhua/GatewayEntity.class differ diff --git a/zerenliandongtai/target/classes/com/woniu/zerenlianfactorydemo/afteryouhua/GatewayEnum.class b/zerenliandongtai/target/classes/com/woniu/zerenlianfactorydemo/afteryouhua/GatewayEnum.class new file mode 100644 index 0000000..d1de4f2 Binary files /dev/null and b/zerenliandongtai/target/classes/com/woniu/zerenlianfactorydemo/afteryouhua/GatewayEnum.class differ diff --git a/zerenliandongtai/target/classes/com/woniu/zerenlianfactorydemo/afteryouhua/GatewayHandler.class b/zerenliandongtai/target/classes/com/woniu/zerenlianfactorydemo/afteryouhua/GatewayHandler.class new file mode 100644 index 0000000..41989d9 Binary files /dev/null and b/zerenliandongtai/target/classes/com/woniu/zerenlianfactorydemo/afteryouhua/GatewayHandler.class differ diff --git a/zerenliandongtai/target/classes/com/woniu/zerenlianfactorydemo/afteryouhua/GatewayHandlerEnumFactory.class b/zerenliandongtai/target/classes/com/woniu/zerenlianfactorydemo/afteryouhua/GatewayHandlerEnumFactory.class new file mode 100644 index 0000000..6fd84fe Binary files /dev/null and b/zerenliandongtai/target/classes/com/woniu/zerenlianfactorydemo/afteryouhua/GatewayHandlerEnumFactory.class differ diff --git a/zerenliandongtai/target/classes/com/woniu/zerenlianfactorydemo/afteryouhua/GatewayImpl.class b/zerenliandongtai/target/classes/com/woniu/zerenlianfactorydemo/afteryouhua/GatewayImpl.class new file mode 100644 index 0000000..421eb45 Binary files /dev/null and b/zerenliandongtai/target/classes/com/woniu/zerenlianfactorydemo/afteryouhua/GatewayImpl.class differ diff --git a/zerenliandongtai/target/classes/com/woniu/zerenlianfactorydemo/afteryouhua/GetewayClient.class b/zerenliandongtai/target/classes/com/woniu/zerenlianfactorydemo/afteryouhua/GetewayClient.class new file mode 100644 index 0000000..224d6d4 Binary files /dev/null and b/zerenliandongtai/target/classes/com/woniu/zerenlianfactorydemo/afteryouhua/GetewayClient.class differ diff --git a/zerenliandongtai/target/classes/com/woniu/zerenlianfactorydemo/afteryouhua/SessionGatewayHandler.class b/zerenliandongtai/target/classes/com/woniu/zerenlianfactorydemo/afteryouhua/SessionGatewayHandler.class new file mode 100644 index 0000000..495eb65 Binary files /dev/null and b/zerenliandongtai/target/classes/com/woniu/zerenlianfactorydemo/afteryouhua/SessionGatewayHandler.class differ diff --git a/zerenliandongtai/target/classes/com/woniu/zerenlianfactorydemo/before/GetewayClientBefore.class b/zerenliandongtai/target/classes/com/woniu/zerenlianfactorydemo/before/GetewayClientBefore.class new file mode 100644 index 0000000..57e3222 Binary files /dev/null and b/zerenliandongtai/target/classes/com/woniu/zerenlianfactorydemo/before/GetewayClientBefore.class differ