This is an automated email from the ASF dual-hosted git repository.
zhangyonglun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 214bd5a [type: refactor] refactor shenyu config (#2002)
214bd5a is described below
commit 214bd5a20e8142d23221f06fe00b3352ac25e07d
Author: xiaoyu <[email protected]>
AuthorDate: Wed Sep 1 18:00:20 2021 +0800
[type: refactor] refactor shenyu config (#2002)
* [type: refactor] refactor shenyu config
* [type: refactor] refactor shenyu config
---
.../src/main/resources/application-local.yml | 11 +-
.../apache/shenyu/common/config/ShenyuConfig.java | 117 ++++++++++++---------
.../src/main/resources/application-local.yml | 3 +-
.../shenyu-integrated-test-spring-cloud/pom.xml | 7 ++
.../src/main/resources/application-local.yml | 3 +-
.../starter/gateway/ShenyuConfiguration.java | 4 +-
.../org/apache/shenyu/web/filter/CrossFilter.java | 6 +-
.../shenyu/web/loader/ShenyuLoaderService.java | 2 +-
.../shenyu/web/loader/ShenyuPluginLoader.java | 5 +-
.../shenyu/web/loader/ShenyuPluginPathBuilder.java | 28 ++---
.../apache/shenyu/web/filter/CrossFilterTest.java | 8 +-
11 files changed, 113 insertions(+), 81 deletions(-)
diff --git a/shenyu-bootstrap/src/main/resources/application-local.yml
b/shenyu-bootstrap/src/main/resources/application-local.yml
index 452f7f5..6151846 100644
--- a/shenyu-bootstrap/src/main/resources/application-local.yml
+++ b/shenyu-bootstrap/src/main/resources/application-local.yml
@@ -46,9 +46,16 @@ management:
enabled: false
shenyu:
+ cross:
+ enabled: true
+ allowedHeaders:
+ allowedMethods: "*"
+ allowedOrigin: "*"
+ allowedExpose: "*"
+ maxAge: "18000"
+ allowCredentials: true
switchConfig:
local: true
- cross: true
file:
enabled: true
maxSize : 10
@@ -83,6 +90,7 @@ shenyu:
paths:
- /favicon.ico
extPlugin:
+ path:
enabled: true
threads: 1
scheduleTime: 300
@@ -100,7 +108,6 @@ shenyu:
printEnabled: true
printInterval: 60000
-
#eureka:
# client:
# serviceUrl:
diff --git
a/shenyu-common/src/main/java/org/apache/shenyu/common/config/ShenyuConfig.java
b/shenyu-common/src/main/java/org/apache/shenyu/common/config/ShenyuConfig.java
index 02d9cb5..ddb56ee 100644
---
a/shenyu-common/src/main/java/org/apache/shenyu/common/config/ShenyuConfig.java
+++
b/shenyu-common/src/main/java/org/apache/shenyu/common/config/ShenyuConfig.java
@@ -43,8 +43,8 @@ public class ShenyuConfig {
private UpstreamCheck upstreamCheck = new UpstreamCheck();
- private CrosFilterConfig crosFilterConfig = new CrosFilterConfig();
-
+ private CrossFilterConfig cross = new CrossFilterConfig();
+
/**
* Gets switch config.
*
@@ -152,25 +152,25 @@ public class ShenyuConfig {
public void setUpstreamCheck(final UpstreamCheck upstreamCheck) {
this.upstreamCheck = upstreamCheck;
}
-
+
/**
- * Gets crosFilterConfig.
+ * Gets cross.
*
- * @return the crosFilterConfig
+ * @return the cross
*/
- public CrosFilterConfig getCrosFilterConfig() {
- return crosFilterConfig;
+ public CrossFilterConfig getCross() {
+ return cross;
}
-
+
/**
- * Sets crosFilterConfig.
+ * Sets cross.
*
- * @param crosFilterConfig the crosFilterConfig
+ * @param cross the cross
*/
- public void setCrosFilterConfig(final CrosFilterConfig crosFilterConfig) {
- this.crosFilterConfig = crosFilterConfig;
+ public void setCross(final CrossFilterConfig cross) {
+ this.cross = cross;
}
-
+
/**
* The type Scheduler.
*/
@@ -243,6 +243,8 @@ public class ShenyuConfig {
*/
public static class ExtPlugin {
+ private String path;
+
private Boolean enabled = true;
private Integer threads = 1;
@@ -252,6 +254,24 @@ public class ShenyuConfig {
private Integer scheduleDelay = 30;
/**
+ * Gets path.
+ *
+ * @return the path
+ */
+ public String getPath() {
+ return path;
+ }
+
+ /**
+ * Sets path.
+ *
+ * @param path the path
+ */
+ public void setPath(final String path) {
+ this.path = path;
+ }
+
+ /**
* Gets enabled.
*
* @return the enabled
@@ -424,8 +444,6 @@ public class ShenyuConfig {
private Boolean local = true;
- private Boolean cross = true;
-
/**
* Gets local.
*
@@ -443,24 +461,7 @@ public class ShenyuConfig {
public void setLocal(final Boolean local) {
this.local = local;
}
-
- /**
- * Gets cross.
- *
- * @return the cross
- */
- public Boolean getCross() {
- return cross;
- }
-
- /**
- * Sets cross.
- *
- * @param cross the cross
- */
- public void setCross(final Boolean cross) {
- this.cross = cross;
- }
+
}
/**
@@ -608,11 +609,11 @@ public class ShenyuConfig {
this.printInterval = printInterval;
}
}
-
+
/**
- * The Cros Filter Config.
+ * The Cross Filter Config.
*/
- public static class CrosFilterConfig {
+ public static class CrossFilterConfig {
private static final Set<String> DEFAULT_ALLOWED_HEADERS;
@@ -631,6 +632,8 @@ public class ShenyuConfig {
}
};
}
+
+ private Boolean enabled = true;
/**
* Comma-separated of “header”.
@@ -663,7 +666,25 @@ public class ShenyuConfig {
}
return String.join(",", headerSet);
}
-
+
+ /**
+ * Gets enabled.
+ *
+ * @return the enabled
+ */
+ public Boolean getEnabled() {
+ return enabled;
+ }
+
+ /**
+ * Sets enabled.
+ *
+ * @param enabled the enabled
+ */
+ public void setEnabled(final Boolean enabled) {
+ this.enabled = enabled;
+ }
+
/**
* Gets the value of allowedHeaders.
*
@@ -672,7 +693,7 @@ public class ShenyuConfig {
public String getAllowedHeaders() {
return allowedHeaders = wrapperHeaders(allowedHeaders);
}
-
+
/**
* Sets the allowedHeaders.
*
@@ -681,7 +702,7 @@ public class ShenyuConfig {
public void setAllowedHeaders(final String allowedHeaders) {
this.allowedHeaders = wrapperHeaders(allowedHeaders);
}
-
+
/**
* Gets the value of allowedMethods.
*
@@ -690,7 +711,7 @@ public class ShenyuConfig {
public String getAllowedMethods() {
return allowedMethods;
}
-
+
/**
* Sets the allowedMethods.
*
@@ -699,7 +720,7 @@ public class ShenyuConfig {
public void setAllowedMethods(final String allowedMethods) {
this.allowedMethods = allowedMethods;
}
-
+
/**
* Gets the value of allowedOrigin.
*
@@ -708,7 +729,7 @@ public class ShenyuConfig {
public String getAllowedOrigin() {
return allowedOrigin;
}
-
+
/**
* Sets the allowedOrigin.
*
@@ -717,7 +738,7 @@ public class ShenyuConfig {
public void setAllowedOrigin(final String allowedOrigin) {
this.allowedOrigin = allowedOrigin;
}
-
+
/**
* Gets the value of allowedExpose.
*
@@ -726,7 +747,7 @@ public class ShenyuConfig {
public String getAllowedExpose() {
return allowedExpose;
}
-
+
/**
* Sets the allowedExpose.
*
@@ -735,7 +756,7 @@ public class ShenyuConfig {
public void setAllowedExpose(final String allowedExpose) {
this.allowedExpose = allowedExpose;
}
-
+
/**
* Gets the value of maxAge.
*
@@ -744,7 +765,7 @@ public class ShenyuConfig {
public String getMaxAge() {
return maxAge;
}
-
+
/**
* Sets the maxAge.
*
@@ -753,7 +774,7 @@ public class ShenyuConfig {
public void setMaxAge(final String maxAge) {
this.maxAge = maxAge;
}
-
+
/**
* Gets the value of allowCredentials.
*
@@ -762,7 +783,7 @@ public class ShenyuConfig {
public boolean isAllowCredentials() {
return allowCredentials;
}
-
+
/**
* Sets the allowCredentials.
*
diff --git
a/shenyu-integrated-test/shenyu-integrated-test-http/src/main/resources/application-local.yml
b/shenyu-integrated-test/shenyu-integrated-test-http/src/main/resources/application-local.yml
index ff92dbb..f68ee6c 100644
---
a/shenyu-integrated-test/shenyu-integrated-test-http/src/main/resources/application-local.yml
+++
b/shenyu-integrated-test/shenyu-integrated-test-http/src/main/resources/application-local.yml
@@ -35,7 +35,8 @@ management:
shenyu:
switchConfig:
local: true
- cross: true
+ cross:
+ enabled: true
sync:
websocket:
urls: ws://shenyu-admin:9095/websocket
diff --git a/shenyu-integrated-test/shenyu-integrated-test-spring-cloud/pom.xml
b/shenyu-integrated-test/shenyu-integrated-test-spring-cloud/pom.xml
index 38a371f..55919fd 100644
--- a/shenyu-integrated-test/shenyu-integrated-test-spring-cloud/pom.xml
+++ b/shenyu-integrated-test/shenyu-integrated-test-spring-cloud/pom.xml
@@ -102,6 +102,13 @@
</exclusion>
</exclusions>
</dependency>
+
+ <dependency>
+ <groupId>org.springframework.cloud</groupId>
+ <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
+ <version>${spring-cloud.version}</version>
+ </dependency>
+
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
diff --git
a/shenyu-integrated-test/shenyu-integrated-test-spring-cloud/src/main/resources/application-local.yml
b/shenyu-integrated-test/shenyu-integrated-test-spring-cloud/src/main/resources/application-local.yml
index d3dd6be..f8d844b 100644
---
a/shenyu-integrated-test/shenyu-integrated-test-spring-cloud/src/main/resources/application-local.yml
+++
b/shenyu-integrated-test/shenyu-integrated-test-spring-cloud/src/main/resources/application-local.yml
@@ -35,7 +35,8 @@ management:
shenyu:
switchConfig:
local: true
- cross: true
+ cross:
+ enabled: true
sync:
websocket:
urls: ws://localhost:9095/websocket
diff --git
a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-gateway/src/main/java/org/apache/shenyu/springboot/starter/gateway/ShenyuConfiguration.java
b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-gateway/src/main/java/org/apache/shenyu/springboot/starter/gateway/ShenyuConfiguration.java
index 3535f1c..d909d99 100644
---
a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-gateway/src/main/java/org/apache/shenyu/springboot/starter/gateway/ShenyuConfiguration.java
+++
b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-gateway/src/main/java/org/apache/shenyu/springboot/starter/gateway/ShenyuConfiguration.java
@@ -169,9 +169,9 @@ public class ShenyuConfiguration {
*/
@Bean
@Order(-100)
- @ConditionalOnProperty(name = "shenyu.switchConfig.cross", havingValue =
"true")
+ @ConditionalOnProperty(name = "shenyu.cross.enabled", havingValue = "true")
public WebFilter crossFilter(final ShenyuConfig shenyuConfig) {
- return new CrossFilter(shenyuConfig.getCrosFilterConfig());
+ return new CrossFilter(shenyuConfig.getCross());
}
/**
diff --git
a/shenyu-web/src/main/java/org/apache/shenyu/web/filter/CrossFilter.java
b/shenyu-web/src/main/java/org/apache/shenyu/web/filter/CrossFilter.java
index 159a0a7..4afd457 100644
--- a/shenyu-web/src/main/java/org/apache/shenyu/web/filter/CrossFilter.java
+++ b/shenyu-web/src/main/java/org/apache/shenyu/web/filter/CrossFilter.java
@@ -17,7 +17,7 @@
package org.apache.shenyu.web.filter;
-import org.apache.shenyu.common.config.ShenyuConfig;
+import org.apache.shenyu.common.config.ShenyuConfig.CrossFilterConfig;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
@@ -34,9 +34,9 @@ import reactor.core.publisher.Mono;
*/
public class CrossFilter implements WebFilter {
- private final ShenyuConfig.CrosFilterConfig filterConfig;
+ private final CrossFilterConfig filterConfig;
- public CrossFilter(final ShenyuConfig.CrosFilterConfig filterConfig) {
+ public CrossFilter(final CrossFilterConfig filterConfig) {
this.filterConfig = filterConfig;
}
diff --git
a/shenyu-web/src/main/java/org/apache/shenyu/web/loader/ShenyuLoaderService.java
b/shenyu-web/src/main/java/org/apache/shenyu/web/loader/ShenyuLoaderService.java
index f499c24..5953524 100644
---
a/shenyu-web/src/main/java/org/apache/shenyu/web/loader/ShenyuLoaderService.java
+++
b/shenyu-web/src/main/java/org/apache/shenyu/web/loader/ShenyuLoaderService.java
@@ -53,7 +53,7 @@ public class ShenyuLoaderService {
ScheduledThreadPoolExecutor executor = new
ScheduledThreadPoolExecutor(config.getThreads(),
ShenyuThreadFactory.create("plugin-ext-loader", true));
executor.scheduleAtFixedRate(() -> {
try {
- List<ShenyuLoaderResult> results =
ShenyuPluginLoader.getInstance().loadExtendPlugins();
+ List<ShenyuLoaderResult> results =
ShenyuPluginLoader.getInstance().loadExtendPlugins(config.getPath());
List<ShenyuPlugin> shenyuExtendPlugins =
results.stream().map(ShenyuLoaderResult::getShenyuPlugin).filter(Objects::nonNull).collect(Collectors.toList());
webHandler.putExtPlugins(shenyuExtendPlugins);
List<PluginDataHandler> handlers =
results.stream().map(ShenyuLoaderResult::getPluginDataHandler).filter(Objects::nonNull).collect(Collectors.toList());
diff --git
a/shenyu-web/src/main/java/org/apache/shenyu/web/loader/ShenyuPluginLoader.java
b/shenyu-web/src/main/java/org/apache/shenyu/web/loader/ShenyuPluginLoader.java
index 12064ae..580ba96 100644
---
a/shenyu-web/src/main/java/org/apache/shenyu/web/loader/ShenyuPluginLoader.java
+++
b/shenyu-web/src/main/java/org/apache/shenyu/web/loader/ShenyuPluginLoader.java
@@ -85,14 +85,15 @@ public final class ShenyuPluginLoader extends ClassLoader
implements Closeable {
/**
* Load extend plugins list.
*
+ * @param path the path
* @return the list
* @throws IOException the io exception
* @throws ClassNotFoundException the class not found exception
* @throws InstantiationException the instantiation exception
* @throws IllegalAccessException the illegal access exception
*/
- public List<ShenyuLoaderResult> loadExtendPlugins() throws IOException,
ClassNotFoundException, InstantiationException, IllegalAccessException {
- File[] jarFiles =
ShenyuPluginPathBuilder.getPluginPath().listFiles(file ->
file.getName().endsWith(".jar"));
+ public List<ShenyuLoaderResult> loadExtendPlugins(final String path)
throws IOException, ClassNotFoundException, InstantiationException,
IllegalAccessException {
+ File[] jarFiles =
ShenyuPluginPathBuilder.getPluginPath(path).listFiles(file ->
file.getName().endsWith(".jar"));
if (null == jarFiles) {
return Collections.emptyList();
}
diff --git
a/shenyu-web/src/main/java/org/apache/shenyu/web/loader/ShenyuPluginPathBuilder.java
b/shenyu-web/src/main/java/org/apache/shenyu/web/loader/ShenyuPluginPathBuilder.java
index 801ed4e..c4cc3c6 100644
---
a/shenyu-web/src/main/java/org/apache/shenyu/web/loader/ShenyuPluginPathBuilder.java
+++
b/shenyu-web/src/main/java/org/apache/shenyu/web/loader/ShenyuPluginPathBuilder.java
@@ -17,6 +17,7 @@
package org.apache.shenyu.web.loader;
+import org.apache.commons.lang3.StringUtils;
import org.apache.shenyu.common.exception.ShenyuException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -25,7 +26,6 @@ import java.io.File;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
-import java.util.Objects;
/**
* The type Shenyu plugin path builder.
@@ -36,19 +36,21 @@ public final class ShenyuPluginPathBuilder {
private static final String PLUGIN_PATH = "plugin-ext";
- private static File pluginPath;
-
- static {
- pluginPath = buildPluginPath();
- }
-
/**
* Gets plugin path.
*
+ * @param path the path
* @return the plugin path
*/
- public static File getPluginPath() {
- return pluginPath;
+ public static File getPluginPath(final String path) {
+ if (StringUtils.isNotEmpty(path)) {
+ return new File(path);
+ }
+ String pluginPath = System.getProperty(PLUGIN_PATH);
+ if (StringUtils.isNotEmpty(pluginPath)) {
+ return new File(pluginPath);
+ }
+ return new File(String.join("", buildPluginJarPath().getPath(),
"/ext-lib"));
}
private static File buildPluginJarPath() {
@@ -80,12 +82,4 @@ public final class ShenyuPluginPathBuilder {
return null;
}
}
-
- private static File buildPluginPath() {
- String pluginPath = System.getProperty(PLUGIN_PATH);
- if (Objects.nonNull(pluginPath)) {
- return new File(pluginPath);
- }
- return new File(String.join("", buildPluginJarPath().getPath(),
"/ext-lib"));
- }
}
diff --git
a/shenyu-web/src/test/java/org/apache/shenyu/web/filter/CrossFilterTest.java
b/shenyu-web/src/test/java/org/apache/shenyu/web/filter/CrossFilterTest.java
index 60d4036..215d8ab 100644
--- a/shenyu-web/src/test/java/org/apache/shenyu/web/filter/CrossFilterTest.java
+++ b/shenyu-web/src/test/java/org/apache/shenyu/web/filter/CrossFilterTest.java
@@ -17,7 +17,7 @@
package org.apache.shenyu.web.filter;
-import org.apache.shenyu.common.config.ShenyuConfig;
+import org.apache.shenyu.common.config.ShenyuConfig.CrossFilterConfig;
import org.junit.Test;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
import org.springframework.mock.web.server.MockServerWebExchange;
@@ -44,7 +44,7 @@ public final class CrossFilterTest {
.build());
WebFilterChain chainNoHeader = mock(WebFilterChain.class);
when(chainNoHeader.filter(exchangeNoHeader)).thenReturn(Mono.empty());
- CrossFilter filterNoHeader = new CrossFilter(new
ShenyuConfig.CrosFilterConfig());
+ CrossFilter filterNoHeader = new CrossFilter(new CrossFilterConfig());
StepVerifier.create(filterNoHeader.filter(exchangeNoHeader,
chainNoHeader))
.expectSubscription()
.verifyComplete();
@@ -55,7 +55,7 @@ public final class CrossFilterTest {
.build());
WebFilterChain chainNormal = mock(WebFilterChain.class);
when(chainNormal.filter(exchangeNormal)).thenReturn(Mono.empty());
- CrossFilter filterNormal = new CrossFilter(new
ShenyuConfig.CrosFilterConfig());
+ CrossFilter filterNormal = new CrossFilter(new CrossFilterConfig());
StepVerifier.create(filterNormal.filter(exchangeNormal, chainNormal))
.expectSubscription()
.verifyComplete();
@@ -66,7 +66,7 @@ public final class CrossFilterTest {
.build());
WebFilterChain chainOption = mock(WebFilterChain.class);
when(chainOption.filter(exchangeOption)).thenReturn(Mono.empty());
- CrossFilter filterOption = new CrossFilter(new
ShenyuConfig.CrosFilterConfig());
+ CrossFilter filterOption = new CrossFilter(new CrossFilterConfig());
StepVerifier.create(filterOption.filter(exchangeOption, chainOption))
.expectSubscription()
.verifyComplete();