From d5a24753557250481d12a73e74d11baa02c67d12 Mon Sep 17 00:00:00 2001 From: linwaiwai Date: Tue, 29 Jun 2021 17:50:09 +0800 Subject: [PATCH 1/3] add dynamic input support --- .gitignore | 1 + fizz-bootstrap/pom.xml | 8 +++- fizz-core/pom.xml | 14 +++++++ .../src/main/java/we/fizz/ConfigLoader.java | 19 +++------ fizz-core/src/main/java/we/fizz/Step.java | 1 + .../src/main/java/we/fizz/input/IInput.java | 1 + .../src/main/java/we/fizz/input/Input.java | 13 +++++++ .../main/java/we/fizz/input/InputFactory.java | 16 +++++++- .../input/extension/mysql/MySQLInput.java | 39 ------------------- .../extension/mysql/MySQLInputConfig.java | 35 ----------------- pom.xml | 3 ++ 11 files changed, 59 insertions(+), 91 deletions(-) delete mode 100644 fizz-core/src/main/java/we/fizz/input/extension/mysql/MySQLInput.java delete mode 100644 fizz-core/src/main/java/we/fizz/input/extension/mysql/MySQLInputConfig.java diff --git a/.gitignore b/.gitignore index 44e637c..17427be 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ .factorypath .idea /fizz-gateway-community.iml +*.iml diff --git a/fizz-bootstrap/pom.xml b/fizz-bootstrap/pom.xml index 8db531c..a01fa2a 100644 --- a/fizz-bootstrap/pom.xml +++ b/fizz-bootstrap/pom.xml @@ -46,7 +46,13 @@ fizz-spring-boot-starter ${project.version} - + + + + + org.reflections + reflections + ${reflections.version} + + org.apache.curator curator-client @@ -279,5 +285,13 @@ repo file://${project.basedir}/../repo + + sonatype-snapshots + SonaType Snapshots + https://oss.sonatype.org/content/repositories/snapshots + + true + + \ No newline at end of file diff --git a/fizz-core/src/main/java/we/fizz/ConfigLoader.java b/fizz-core/src/main/java/we/fizz/ConfigLoader.java index 632d6bd..86598ea 100644 --- a/fizz-core/src/main/java/we/fizz/ConfigLoader.java +++ b/fizz-core/src/main/java/we/fizz/ConfigLoader.java @@ -31,15 +31,10 @@ import org.apache.commons.io.FileUtils; import org.noear.snack.ONode; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Value; import org.springframework.data.redis.core.ReactiveStringRedisTemplate; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; -import we.fizz.input.extension.grpc.GrpcInput; -import we.fizz.input.extension.dubbo.DubboInput; -import we.fizz.input.extension.mysql.MySQLInput; -import we.fizz.input.extension.request.RequestInput; import we.flume.clients.log4j2appender.LogService; import we.util.Constants; import we.util.ReactorUtils; @@ -55,11 +50,7 @@ import java.io.IOException; import java.io.Serializable; import java.lang.ref.SoftReference; import java.nio.charset.StandardCharsets; -import java.util.AbstractMap; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Objects; +import java.util.*; import java.util.concurrent.ConcurrentHashMap; /** @@ -147,10 +138,6 @@ public class ConfigLoader { public Pipeline createPipeline(String configStr) throws IOException { ONode cfgNode = ONode.loadStr(configStr); - InputFactory.registerInput(RequestInput.TYPE, RequestInput.class); - InputFactory.registerInput(MySQLInput.TYPE, MySQLInput.class); - InputFactory.registerInput(GrpcInput.TYPE, GrpcInput.class); - InputFactory.registerInput(DubboInput.TYPE, DubboInput.class); Pipeline pipeline = new Pipeline(); pipeline.setApplicationContext(appContext); @@ -213,8 +200,10 @@ public class ConfigLoader { @PostConstruct public synchronized void init() throws Exception { this.refreshLocalCache(); + InputFactory.loadInputClasses(); } + public synchronized void refreshLocalCache() throws Exception { if (formalPathPrefix == null) { formalPathPrefix = appContext.getEnvironment().getProperty("gateway.prefix", "/proxy"); @@ -372,6 +361,8 @@ public class ConfigLoader { } } String key = method.toUpperCase() + ":" + path; + // config file entry ,if you want modify the aggregate config json but not use the interface of fizz, + // you can just read the config ,transform to json format and modify it if (aggregateResources.containsKey(key) && aggregateResources.get(key) != null) { String configStr = aggregateResources.get(key); Input input = null; diff --git a/fizz-core/src/main/java/we/fizz/Step.java b/fizz-core/src/main/java/we/fizz/Step.java index 2a7e9e0..bb85c26 100644 --- a/fizz-core/src/main/java/we/fizz/Step.java +++ b/fizz-core/src/main/java/we/fizz/Step.java @@ -44,6 +44,7 @@ import we.fizz.component.IComponent; import we.fizz.component.StepContextPosition; import we.fizz.component.circle.Circle; import we.fizz.component.condition.Condition; +import we.fizz.exception.FizzRuntimeException; import we.fizz.input.Input; import we.fizz.input.InputConfig; import we.fizz.input.InputContext; diff --git a/fizz-core/src/main/java/we/fizz/input/IInput.java b/fizz-core/src/main/java/we/fizz/input/IInput.java index 5709c0f..5b02c74 100644 --- a/fizz-core/src/main/java/we/fizz/input/IInput.java +++ b/fizz-core/src/main/java/we/fizz/input/IInput.java @@ -7,6 +7,7 @@ import we.fizz.StepContext; import we.fizz.StepResponse; import java.lang.ref.SoftReference; +import java.lang.reflect.Field; import java.util.Map; public interface IInput { diff --git a/fizz-core/src/main/java/we/fizz/input/Input.java b/fizz-core/src/main/java/we/fizz/input/Input.java index a5c8ec1..f8e3952 100644 --- a/fizz-core/src/main/java/we/fizz/input/Input.java +++ b/fizz-core/src/main/java/we/fizz/input/Input.java @@ -17,8 +17,11 @@ package we.fizz.input; import java.lang.ref.SoftReference; +import java.lang.reflect.Field; import java.util.Map; + +import org.reflections.Reflections; import org.springframework.context.ConfigurableApplicationContext; import reactor.core.publisher.Mono; import we.fizz.Step; @@ -95,4 +98,14 @@ public class Input { return InputConfig.class; } + public static void initialize(Classclazz) throws IllegalAccessException { + Field field = null; + try { + field = clazz.getDeclaredField("TYPE"); + InputFactory.registerInput((InputType) field.get(null), clazz); + } catch (NoSuchFieldException e) { + // doing nothing is right + } + } + } diff --git a/fizz-core/src/main/java/we/fizz/input/InputFactory.java b/fizz-core/src/main/java/we/fizz/input/InputFactory.java index c0b611b..4a269b1 100644 --- a/fizz-core/src/main/java/we/fizz/input/InputFactory.java +++ b/fizz-core/src/main/java/we/fizz/input/InputFactory.java @@ -18,6 +18,7 @@ package we.fizz.input; import we.fizz.component.ComponentHelper; +import org.reflections.Reflections; import we.fizz.exception.FizzRuntimeException; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; @@ -25,6 +26,7 @@ import java.lang.reflect.Method; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; /** * @@ -61,8 +63,9 @@ public class InputFactory { inputConfig.setComponents(ComponentHelper.buildComponents((List>) config.get("components"))); inputConfig.parse(); return inputConfig; + } else { + throw new FizzRuntimeException("can't find input config type:" + type); } - return null; } public static Input createInput(String type) { @@ -79,8 +82,17 @@ public class InputFactory { } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) { throw new FizzRuntimeException(e.getMessage()); } + } else { + throw new FizzRuntimeException("can't find input type:" + type); } - return null; } + public static void loadInputClasses() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + Reflections reflections = new Reflections("we.fizz.input"); + Set> subTypes = reflections.getSubTypesOf(Input.class); + for (ClassinputType : subTypes){ + Method initializeMethod = inputType.getMethod("initialize", Class.class); + initializeMethod.invoke(null, inputType); + } + } } diff --git a/fizz-core/src/main/java/we/fizz/input/extension/mysql/MySQLInput.java b/fizz-core/src/main/java/we/fizz/input/extension/mysql/MySQLInput.java deleted file mode 100644 index bb64ff8..0000000 --- a/fizz-core/src/main/java/we/fizz/input/extension/mysql/MySQLInput.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (C) 2020 the original author or authors. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package we.fizz.input.extension.mysql; - -import we.fizz.input.IInput; -import we.fizz.input.Input; -import we.fizz.input.InputType; - - - - - -/** - * - * @author linwaiwai - * - */ - -public class MySQLInput extends Input implements IInput { - static public InputType TYPE = new InputType("MYSQL"); - public static Class inputConfigClass (){ - return MySQLInputConfig.class; - } - -} diff --git a/fizz-core/src/main/java/we/fizz/input/extension/mysql/MySQLInputConfig.java b/fizz-core/src/main/java/we/fizz/input/extension/mysql/MySQLInputConfig.java deleted file mode 100644 index 129a925..0000000 --- a/fizz-core/src/main/java/we/fizz/input/extension/mysql/MySQLInputConfig.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (C) 2020 the original author or authors. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package we.fizz.input.extension.mysql; - -import we.fizz.input.InputConfig; - -import java.util.Map; - -/** - * - * @author linwaiwai - * - */ -public class MySQLInputConfig extends InputConfig { - - public MySQLInputConfig(Map configBody) { - super(configBody); - } - -} diff --git a/pom.xml b/pom.xml index be1fb34..01afc1c 100644 --- a/pom.xml +++ b/pom.xml @@ -19,6 +19,8 @@ 3.4.6 4.0.1 3.5.9 + 0.8.2 + 0.9.12 @@ -39,6 +41,7 @@ fizz-plugin fizz-spring-boot-starter + From 6483ff54c2f6db7681771d6c030ede4d4f83e4f1 Mon Sep 17 00:00:00 2001 From: linwaiwai Date: Fri, 9 Jul 2021 17:10:20 +0800 Subject: [PATCH 2/3] avoid compile error --- fizz-core/src/main/java/we/fizz/component/circle/Circle.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/fizz-core/src/main/java/we/fizz/component/circle/Circle.java b/fizz-core/src/main/java/we/fizz/component/circle/Circle.java index 85e3e3b..3eec6db 100644 --- a/fizz-core/src/main/java/we/fizz/component/circle/Circle.java +++ b/fizz-core/src/main/java/we/fizz/component/circle/Circle.java @@ -225,7 +225,7 @@ public class Circle implements IComponent { ONode ctxNode = ComponentHelper.toONode(stepContext); CircleItem nextItem = this.next(ctxNode); if (nextItem != null) { - return Mono.just(new CircleItemResult(nextItem, null)).expand(circleItemResult -> { + Mono> colloctList = Mono.just(new CircleItemResult(nextItem, null)).expand(circleItemResult -> { // put nextItem to step context and ctxNode for further JSON path mapping CircleItem cItem = circleItemResult.nextItem; if (stepCtxPos.getRequestName() != null) { @@ -250,7 +250,8 @@ public class Circle implements IComponent { } return Mono.just(new CircleItemResult(nextItem2, r)); }); - }).flatMap(circleItemResult -> Flux.just(circleItemResult)).collectList().flatMap(list -> { + }).flatMap(circleItemResult -> Flux.just(circleItemResult)).collectList(); + return colloctList.flatMap(list -> { if (list != null && list.size() > 0) { Collections.reverse(list); for (int i = 0; i < list.size(); i++) { From 82cf2fbb740eb9f95e949d044daca2b8c5b3badd Mon Sep 17 00:00:00 2001 From: linwaiwai Date: Wed, 21 Jul 2021 09:53:26 +0800 Subject: [PATCH 3/3] avoid compile issue --- .../main/java/we/fizz/component/circle/Circle.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/fizz-core/src/main/java/we/fizz/component/circle/Circle.java b/fizz-core/src/main/java/we/fizz/component/circle/Circle.java index 988b800..7094439 100644 --- a/fizz-core/src/main/java/we/fizz/component/circle/Circle.java +++ b/fizz-core/src/main/java/we/fizz/component/circle/Circle.java @@ -266,8 +266,8 @@ public class Circle implements IComponent { ONode ctxNode1 = ComponentHelper.toONode(stepContext); CircleItem nextItem = this.next(ctxNode1); if (nextItem != null) { - Mono> colloctList = Mono.just(new CircleItemResult(nextItem, null)).expand(circleItemResult -> { - // put nextItem to step context and ctxNode for further JSON path mapping + return Mono.just(new CircleItemResult(ctxNode1, nextItem, null)).expand(circleItemResult -> { + // put nextItem to step context CircleItem cItem = circleItemResult.nextItem; if (stepCtxPos.getRequestName() != null) { stepContext.setRequestCircleItem(stepCtxPos.getStepName(), stepCtxPos.getRequestName(), @@ -278,13 +278,13 @@ public class Circle implements IComponent { ONode ctxNode = circleItemResult.ctxNode; PathMapping.setByPath(ctxNode, stepCtxPos.getPath() + ".item", cItem.getItem(), true); PathMapping.setByPath(ctxNode, stepCtxPos.getPath() + ".index", cItem.getIndex(), true); - + if (!this.canExec(cItem.getIndex(), ctxNode, stepContext, stepCtxPos)) { return Mono.just(new CircleItemResult(ctxNode, this.next(ctxNode), null)); } return f.apply(stepContext, stepCtxPos).flatMap(r -> { ONode ctxNode2 = ComponentHelper.toONode(stepContext); - if (this.breakCircle(cItem.getIndex(), ctxNode, stepContext, stepCtxPos)) { + if (this.breakCircle(cItem.getIndex(), ctxNode2, stepContext, stepCtxPos)) { return Mono.empty(); } CircleItem nextItem2 = this.next(ctxNode2); @@ -293,8 +293,8 @@ public class Circle implements IComponent { } return Mono.just(new CircleItemResult(ctxNode2, nextItem2, r)); }); - }).flatMap(circleItemResult -> Flux.just(circleItemResult)).collectList(); - return colloctList.flatMap(list -> { + }).flatMap(circleItemResult -> Flux.just(circleItemResult)).collectList().flatMap(r -> { + List list = (List) r; if (list != null && list.size() > 0) { Collections.reverse(list); for (int i = 0; i < list.size(); i++) {