马府强
2 years ago
29 changed files with 454 additions and 0 deletions
@ -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/ |
@ -0,0 +1,14 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<project version="4"> |
|||
<component name="CompilerConfiguration"> |
|||
<annotationProcessing> |
|||
<profile default="true" name="Default" enabled="true" /> |
|||
<profile name="Maven default annotation processors profile" enabled="true"> |
|||
<sourceOutputDir name="target/generated-sources/annotations" /> |
|||
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" /> |
|||
<outputRelativeToContentRoot value="true" /> |
|||
<module name="mergedemo" /> |
|||
</profile> |
|||
</annotationProcessing> |
|||
</component> |
|||
</project> |
@ -0,0 +1,20 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<project version="4"> |
|||
<component name="RemoteRepositoriesConfiguration"> |
|||
<remote-repository> |
|||
<option name="id" value="central" /> |
|||
<option name="name" value="Central Repository" /> |
|||
<option name="url" value="https://repo.maven.apache.org/maven2" /> |
|||
</remote-repository> |
|||
<remote-repository> |
|||
<option name="id" value="central" /> |
|||
<option name="name" value="Maven Central repository" /> |
|||
<option name="url" value="https://repo1.maven.org/maven2" /> |
|||
</remote-repository> |
|||
<remote-repository> |
|||
<option name="id" value="jboss.community" /> |
|||
<option name="name" value="JBoss Community repository" /> |
|||
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" /> |
|||
</remote-repository> |
|||
</component> |
|||
</project> |
@ -0,0 +1,14 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<project version="4"> |
|||
<component name="ExternalStorageConfigurationManager" enabled="true" /> |
|||
<component name="MavenProjectsManager"> |
|||
<option name="originalFiles"> |
|||
<list> |
|||
<option value="$PROJECT_DIR$/pom.xml" /> |
|||
</list> |
|||
</option> |
|||
</component> |
|||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK"> |
|||
<output url="file://$PROJECT_DIR$/out" /> |
|||
</component> |
|||
</project> |
@ -0,0 +1,124 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<project version="4"> |
|||
<component name="Palette2"> |
|||
<group name="Swing"> |
|||
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false"> |
|||
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" /> |
|||
</item> |
|||
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false"> |
|||
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" /> |
|||
</item> |
|||
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false"> |
|||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" /> |
|||
</item> |
|||
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true"> |
|||
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" /> |
|||
</item> |
|||
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false"> |
|||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" /> |
|||
<initial-values> |
|||
<property name="text" value="Button" /> |
|||
</initial-values> |
|||
</item> |
|||
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false"> |
|||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" /> |
|||
<initial-values> |
|||
<property name="text" value="RadioButton" /> |
|||
</initial-values> |
|||
</item> |
|||
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false"> |
|||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" /> |
|||
<initial-values> |
|||
<property name="text" value="CheckBox" /> |
|||
</initial-values> |
|||
</item> |
|||
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false"> |
|||
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" /> |
|||
<initial-values> |
|||
<property name="text" value="Label" /> |
|||
</initial-values> |
|||
</item> |
|||
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true"> |
|||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> |
|||
<preferred-size width="150" height="-1" /> |
|||
</default-constraints> |
|||
</item> |
|||
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true"> |
|||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> |
|||
<preferred-size width="150" height="-1" /> |
|||
</default-constraints> |
|||
</item> |
|||
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true"> |
|||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> |
|||
<preferred-size width="150" height="-1" /> |
|||
</default-constraints> |
|||
</item> |
|||
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true"> |
|||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> |
|||
<preferred-size width="150" height="50" /> |
|||
</default-constraints> |
|||
</item> |
|||
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true"> |
|||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> |
|||
<preferred-size width="150" height="50" /> |
|||
</default-constraints> |
|||
</item> |
|||
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true"> |
|||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> |
|||
<preferred-size width="150" height="50" /> |
|||
</default-constraints> |
|||
</item> |
|||
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true"> |
|||
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" /> |
|||
</item> |
|||
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false"> |
|||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> |
|||
<preferred-size width="150" height="50" /> |
|||
</default-constraints> |
|||
</item> |
|||
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false"> |
|||
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3"> |
|||
<preferred-size width="150" height="50" /> |
|||
</default-constraints> |
|||
</item> |
|||
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false"> |
|||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> |
|||
<preferred-size width="150" height="50" /> |
|||
</default-constraints> |
|||
</item> |
|||
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false"> |
|||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3"> |
|||
<preferred-size width="200" height="200" /> |
|||
</default-constraints> |
|||
</item> |
|||
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false"> |
|||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3"> |
|||
<preferred-size width="200" height="200" /> |
|||
</default-constraints> |
|||
</item> |
|||
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true"> |
|||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" /> |
|||
</item> |
|||
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false"> |
|||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" /> |
|||
</item> |
|||
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false"> |
|||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" /> |
|||
</item> |
|||
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false"> |
|||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" /> |
|||
</item> |
|||
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false"> |
|||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1"> |
|||
<preferred-size width="-1" height="20" /> |
|||
</default-constraints> |
|||
</item> |
|||
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false"> |
|||
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" /> |
|||
</item> |
|||
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false"> |
|||
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" /> |
|||
</item> |
|||
</group> |
|||
</component> |
|||
</project> |
@ -0,0 +1,2 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<module type="JAVA_MODULE" version="4" /> |
@ -0,0 +1,20 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" |
|||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
|||
<modelVersion>4.0.0</modelVersion> |
|||
|
|||
<groupId>com.woniu</groupId> |
|||
<artifactId>mergedemo</artifactId> |
|||
<version>1.0-SNAPSHOT</version> |
|||
|
|||
<dependencies> |
|||
<dependency> |
|||
<groupId>org.projectlombok</groupId> |
|||
<artifactId>lombok</artifactId> |
|||
<version>1.18.24</version> |
|||
</dependency> |
|||
</dependencies> |
|||
|
|||
|
|||
</project> |
@ -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(); |
|||
} |
|||
} |
|||
} |
@ -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(); |
|||
} |
|||
} |
|||
} |
@ -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(); |
|||
} |
@ -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; |
|||
} |
@ -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; |
|||
} |
|||
} |
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
@ -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(); |
|||
} |
|||
|
@ -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; |
|||
} |
|||
|
|||
} |
@ -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<Integer, GatewayEntity> 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<Integer, GatewayEntity> entry : gatewayEntityMap.entrySet()) { |
|||
GatewayEntity value = entry.getValue(); |
|||
// 没有上一个handler的就是第一个
|
|||
if (value.getPreHandlerId() == null) { |
|||
return value; |
|||
} |
|||
} |
|||
return null; |
|||
} |
|||
} |
@ -0,0 +1,11 @@ |
|||
package com.woniu.zerenlianfactorydemo.afteryouhua; |
|||
|
|||
/** |
|||
* 用责任链模式 + 工厂模式 + 枚举,动态配置请求链并调用。 |
|||
*/ |
|||
public class GetewayClient { |
|||
public static void main(String[] args) { |
|||
GatewayHandler firstGatewayHandler = GatewayHandlerEnumFactory.getFirstGatewayHandler(); |
|||
firstGatewayHandler.service(); |
|||
} |
|||
} |
@ -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(); |
|||
} |
|||
} |
|||
} |
@ -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(); |
|||
|
|||
} |
|||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue