Merge pull request #244 from wehotel/feature/core

add dynamice input support
This commit is contained in:
linwaiwai
2021-07-22 10:44:00 +08:00
committed by GitHub
12 changed files with 60 additions and 92 deletions

1
.gitignore vendored
View File

@@ -6,3 +6,4 @@
.factorypath .factorypath
.idea .idea
/fizz-gateway-community.iml /fizz-gateway-community.iml
*.iml

View File

@@ -46,7 +46,13 @@
<artifactId>fizz-spring-boot-starter</artifactId> <artifactId>fizz-spring-boot-starter</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
</dependencies> <!-- import fizz-input-mysql -->
<!-- <dependency>
<groupId>com.fizzgate</groupId>
<artifactId>fizz-input-mysql</artifactId>
<version>${project.version}</version>
</dependency> -->
</dependencies>
<profiles> <profiles>
<!--<profile> <!--<profile>

View File

@@ -225,6 +225,12 @@
</dependency> </dependency>
<!-- grpc --> <!-- grpc -->
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>${reflections.version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.apache.curator</groupId> <groupId>org.apache.curator</groupId>
<artifactId>curator-client</artifactId> <artifactId>curator-client</artifactId>
@@ -279,5 +285,13 @@
<id>repo</id> <id>repo</id>
<url>file://${project.basedir}/../repo</url> <url>file://${project.basedir}/../repo</url>
</repository> </repository>
<repository>
<id>sonatype-snapshots</id>
<name>SonaType Snapshots</name>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories> </repositories>
</project> </project>

View File

@@ -31,15 +31,10 @@ import org.apache.commons.io.FileUtils;
import org.noear.snack.ONode; import org.noear.snack.ONode;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.ReactiveStringRedisTemplate; import org.springframework.data.redis.core.ReactiveStringRedisTemplate;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils; 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.flume.clients.log4j2appender.LogService;
import we.util.Constants; import we.util.Constants;
import we.util.ReactorUtils; import we.util.ReactorUtils;
@@ -55,11 +50,7 @@ import java.io.IOException;
import java.io.Serializable; import java.io.Serializable;
import java.lang.ref.SoftReference; import java.lang.ref.SoftReference;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.AbstractMap; import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
/** /**
@@ -147,10 +138,6 @@ public class ConfigLoader {
public Pipeline createPipeline(String configStr) throws IOException { public Pipeline createPipeline(String configStr) throws IOException {
ONode cfgNode = ONode.loadStr(configStr); 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 pipeline = new Pipeline();
pipeline.setApplicationContext(appContext); pipeline.setApplicationContext(appContext);
@@ -213,8 +200,10 @@ public class ConfigLoader {
@PostConstruct @PostConstruct
public synchronized void init() throws Exception { public synchronized void init() throws Exception {
this.refreshLocalCache(); this.refreshLocalCache();
InputFactory.loadInputClasses();
} }
public synchronized void refreshLocalCache() throws Exception { public synchronized void refreshLocalCache() throws Exception {
if (formalPathPrefix == null) { if (formalPathPrefix == null) {
formalPathPrefix = appContext.getEnvironment().getProperty("gateway.prefix", "/proxy"); formalPathPrefix = appContext.getEnvironment().getProperty("gateway.prefix", "/proxy");
@@ -372,6 +361,8 @@ public class ConfigLoader {
} }
} }
String key = method.toUpperCase() + ":" + path; 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) { if (aggregateResources.containsKey(key) && aggregateResources.get(key) != null) {
String configStr = aggregateResources.get(key); String configStr = aggregateResources.get(key);
Input input = null; Input input = null;

View File

@@ -44,6 +44,7 @@ import we.fizz.component.IComponent;
import we.fizz.component.StepContextPosition; import we.fizz.component.StepContextPosition;
import we.fizz.component.circle.Circle; import we.fizz.component.circle.Circle;
import we.fizz.component.condition.Condition; import we.fizz.component.condition.Condition;
import we.fizz.exception.FizzRuntimeException;
import we.fizz.input.Input; import we.fizz.input.Input;
import we.fizz.input.InputConfig; import we.fizz.input.InputConfig;
import we.fizz.input.InputContext; import we.fizz.input.InputContext;

View File

@@ -278,7 +278,7 @@ public class Circle implements IComponent {
ONode ctxNode = circleItemResult.ctxNode; ONode ctxNode = circleItemResult.ctxNode;
PathMapping.setByPath(ctxNode, stepCtxPos.getPath() + ".item", cItem.getItem(), true); PathMapping.setByPath(ctxNode, stepCtxPos.getPath() + ".item", cItem.getItem(), true);
PathMapping.setByPath(ctxNode, stepCtxPos.getPath() + ".index", cItem.getIndex(), true); PathMapping.setByPath(ctxNode, stepCtxPos.getPath() + ".index", cItem.getIndex(), true);
if (!this.canExec(cItem.getIndex(), ctxNode, stepContext, stepCtxPos)) { if (!this.canExec(cItem.getIndex(), ctxNode, stepContext, stepCtxPos)) {
return Mono.just(new CircleItemResult(ctxNode, this.next(ctxNode), null)); return Mono.just(new CircleItemResult(ctxNode, this.next(ctxNode), null));
} }

View File

@@ -7,6 +7,7 @@ import we.fizz.StepContext;
import we.fizz.StepResponse; import we.fizz.StepResponse;
import java.lang.ref.SoftReference; import java.lang.ref.SoftReference;
import java.lang.reflect.Field;
import java.util.Map; import java.util.Map;
public interface IInput { public interface IInput {

View File

@@ -17,8 +17,11 @@
package we.fizz.input; package we.fizz.input;
import java.lang.ref.SoftReference; import java.lang.ref.SoftReference;
import java.lang.reflect.Field;
import java.util.Map; import java.util.Map;
import org.reflections.Reflections;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import we.fizz.Step; import we.fizz.Step;
@@ -95,4 +98,14 @@ public class Input {
return InputConfig.class; return InputConfig.class;
} }
public static void initialize(Class<?>clazz) throws IllegalAccessException {
Field field = null;
try {
field = clazz.getDeclaredField("TYPE");
InputFactory.registerInput((InputType) field.get(null), clazz);
} catch (NoSuchFieldException e) {
// doing nothing is right
}
}
} }

View File

@@ -18,6 +18,7 @@
package we.fizz.input; package we.fizz.input;
import we.fizz.component.ComponentHelper; import we.fizz.component.ComponentHelper;
import org.reflections.Reflections;
import we.fizz.exception.FizzRuntimeException; import we.fizz.exception.FizzRuntimeException;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
@@ -25,6 +26,7 @@ import java.lang.reflect.Method;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
/** /**
* *
@@ -61,8 +63,9 @@ public class InputFactory {
inputConfig.setComponents(ComponentHelper.buildComponents((List<Map<String, Object>>) config.get("components"))); inputConfig.setComponents(ComponentHelper.buildComponents((List<Map<String, Object>>) config.get("components")));
inputConfig.parse(); inputConfig.parse();
return inputConfig; return inputConfig;
} else {
throw new FizzRuntimeException("can't find input config type:" + type);
} }
return null;
} }
public static Input createInput(String type) { public static Input createInput(String type) {
@@ -79,8 +82,17 @@ public class InputFactory {
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) { } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new FizzRuntimeException(e.getMessage()); 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<Class<? extends Input>> subTypes = reflections.getSubTypesOf(Input.class);
for (Class<?>inputType : subTypes){
Method initializeMethod = inputType.getMethod("initialize", Class.class);
initializeMethod.invoke(null, inputType);
}
}
} }

View File

@@ -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 <https://www.gnu.org/licenses/>.
*/
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;
}
}

View File

@@ -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 <https://www.gnu.org/licenses/>.
*/
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);
}
}

View File

@@ -19,6 +19,8 @@
<mockito.version>3.4.6</mockito.version> <mockito.version>3.4.6</mockito.version>
<curator.version>4.0.1</curator.version> <curator.version>4.0.1</curator.version>
<zookeeper.version>3.5.9</zookeeper.version> <zookeeper.version>3.5.9</zookeeper.version>
<r2dbc-mysql.version>0.8.2</r2dbc-mysql.version>
<reflections.version>0.9.12</reflections.version>
</properties> </properties>
<parent> <parent>
@@ -39,6 +41,7 @@
<module>fizz-plugin</module> <module>fizz-plugin</module>
<!--<module>fizz-bootstrap</module>--> <!--<module>fizz-bootstrap</module>-->
<module>fizz-spring-boot-starter</module> <module>fizz-spring-boot-starter</module>
<!--<module>fizz-input-mysql</module>-->
</modules> </modules>
<repositories> <repositories>