diff --git a/pom.xml b/pom.xml
index 9f89c20..e60075d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -5,7 +5,7 @@
org.springframework.boot
spring-boot-starter-parent
- 2.1.6.RELEASE
+ 2.1.7.RELEASE
@@ -22,7 +22,7 @@
UTF-8
UTF-8
Greenwich.SR2
- 2.7.2
+ 2.7.3
@@ -47,19 +47,19 @@
org.apache.dubbo
dubbo-spring-boot-starter
- 2.7.1
+ ${dubbo.version}
org.apache.dubbo
dubbo
- 2.7.2
+ ${dubbo.version}
org.apache.dubbo
dubbo-dependencies-zookeeper
- 2.7.1
+ ${dubbo.version}
pom
diff --git a/usergw-service/pom.xml b/usergw-service/pom.xml
index 34909de..9a657ba 100644
--- a/usergw-service/pom.xml
+++ b/usergw-service/pom.xml
@@ -17,9 +17,11 @@
1.8
+ false
-
+
+
cn.ecpark.service
usergw-api
@@ -56,7 +58,7 @@
org.yaml
snakeyaml
- 1.21
+ 1.24
@@ -64,12 +66,12 @@
fastjson
1.2.58
-
-
- cn.ecpark.service
- usergw-api
- ${project.parent.version}
-
+
+
+ com.ctrip.framework.apollo
+ apollo-client
+ 1.4.0
+
org.springframework.boot
@@ -85,17 +87,42 @@
org.springframework.boot
spring-boot-maven-plugin
+
+ cn.ecpark.service.usergw.App
+
-
+
+ maven-compiler-plugin
+ maven-resources-plugin
+ maven-source-plugin
+
- maven-compiler-plugin
-
-
- maven-resources-plugin
-
-
- maven-source-plugin
-
+ maven-assembly-plugin
+ 3.1.1
+
+
+ make-assembly
+ package
+
+ single
+
+
+
+
+
+
+
+ true
+
+ src/main/assembly/assembly.xml
+
+
+
+
+
+
org.jacoco
@@ -123,4 +150,4 @@
-
\ No newline at end of file
+
diff --git a/usergw-service/src/main/assembly/assembly.xml b/usergw-service/src/main/assembly/assembly.xml
new file mode 100644
index 0000000..c913986
--- /dev/null
+++ b/usergw-service/src/main/assembly/assembly.xml
@@ -0,0 +1,45 @@
+
+ assembly
+
+ dir
+ zip
+
+ false
+
+
+ src/main/assembly/scripts
+ bin
+ 0755
+ true
+
+
+
+ src/main/resources
+ config
+
+ *.properties
+ gateway.yaml
+
+
+
+
+ ${project.build.directory}
+ /
+
+ *.jar
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/usergw-service/src/main/assembly/scripts/run.bat b/usergw-service/src/main/assembly/scripts/run.bat
new file mode 100644
index 0000000..9e69887
--- /dev/null
+++ b/usergw-service/src/main/assembly/scripts/run.bat
@@ -0,0 +1,10 @@
+@echo off & setlocal enabledelayedexpansion
+set BIN_DIR=%~dp0
+
+::run
+cd %BIN_DIR%\..
+ set APP_JAR=""
+ for %%f in (*-*-*.jar) do set APP_JAR=%%f
+ java -Dloader.path=config -Dfile.encoding=UTF-8 -Xms256m -Xmx512m -jar %APP_JAR%
+cd %BIN_DIR%
+
diff --git a/usergw-service/src/main/assembly/scripts/run.sh b/usergw-service/src/main/assembly/scripts/run.sh
new file mode 100644
index 0000000..caf978d
--- /dev/null
+++ b/usergw-service/src/main/assembly/scripts/run.sh
@@ -0,0 +1,8 @@
+#!/bin/sh
+##cnf
+APP_PMS="-Xms256m -Xmx512m"
+APP_JAR=`echo *-*-*.jar`
+##run
+cur=$(dirname $(readlink -f "$0")) && cd $cur/..
+
+exec java -Dloader.path=config -Dfile.encoding=UTF-8 ${APP_PMS} -jar ${APP_JAR}
diff --git a/usergw-service/src/main/java/META-INF/additional-spring-configuration-metadata.json b/usergw-service/src/main/java/META-INF/additional-spring-configuration-metadata.json
index 59ed6c5..79fccbc 100644
--- a/usergw-service/src/main/java/META-INF/additional-spring-configuration-metadata.json
+++ b/usergw-service/src/main/java/META-INF/additional-spring-configuration-metadata.json
@@ -1,5 +1,12 @@
-{"properties": [{
- "name": "yame.config",
- "type": "java.lang.String",
- "description": "用于配置网关的路径, 如果不填.默认: gateway.yml, gateway.yaml, Gateway.yml, Gateway.yaml"
-}]}
\ No newline at end of file
+{"properties": [
+ {
+ "name": "yame.gateway.config",
+ "type": "java.lang.String",
+ "description": "用于配置网关的路径, 如果不填.默认: gateway.yml, gateway.yaml, Gateway.yml, Gateway.yaml"
+ },
+ {
+ "name": "apollo.namespace",
+ "type": "java.lang.String",
+ "description": "默认gateway.yml和gateway.yaml"
+ }
+]}
\ No newline at end of file
diff --git a/usergw-service/src/main/java/cn/ecpark/service/usergw/App.java b/usergw-service/src/main/java/cn/ecpark/service/usergw/App.java
index faf39c3..4894246 100644
--- a/usergw-service/src/main/java/cn/ecpark/service/usergw/App.java
+++ b/usergw-service/src/main/java/cn/ecpark/service/usergw/App.java
@@ -1,14 +1,18 @@
package cn.ecpark.service.usergw;
import org.apache.dubbo.config.spring.context.annotation.EnableDubboConfig;
+import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
+@Configurable
+@ConfigurationProperties
@SpringBootApplication
@SpringBootConfiguration
@EnableAutoConfiguration
diff --git a/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/events/ConfigRefresh.java b/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/events/ConfigRefresh.java
new file mode 100644
index 0000000..e860b4c
--- /dev/null
+++ b/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/events/ConfigRefresh.java
@@ -0,0 +1,22 @@
+package cn.ecpark.service.usergw.biz.events;
+
+import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
+import org.springframework.context.ApplicationEventPublisher;
+import org.springframework.context.ApplicationEventPublisherAware;
+import org.springframework.stereotype.Service;
+
+@Service
+public class ConfigRefresh implements ApplicationEventPublisherAware {
+
+ private ApplicationEventPublisher publisher;
+
+ @Override
+ public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
+ this.publisher = applicationEventPublisher;
+ }
+
+ public void notifyChanged() {
+ this.publisher.publishEvent(new RefreshRoutesEvent(this));
+ }
+
+}
\ No newline at end of file
diff --git a/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/filters/bean/GenericServicePool.java b/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/filters/bean/GenericServicePool.java
index 99154e0..0d298e9 100644
--- a/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/filters/bean/GenericServicePool.java
+++ b/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/filters/bean/GenericServicePool.java
@@ -1,7 +1,10 @@
package cn.ecpark.service.usergw.biz.filters.bean;
import java.util.HashMap;
+import java.util.Map.Entry;
+import org.apache.dubbo.config.ReferenceConfig;
+import org.apache.dubbo.config.utils.ReferenceConfigCache;
import org.apache.dubbo.rpc.service.GenericService;
/**
@@ -9,17 +12,47 @@ import org.apache.dubbo.rpc.service.GenericService;
*/
public class GenericServicePool {
- HashMap gsDictionary;
+ private class ReferenceConfigStore {
+ private ReferenceConfig ref;
+ private GenericService gs;
+
+ public ReferenceConfigStore(ReferenceConfig refValue) {
+ this.ref = refValue;
+ this.gs = ref.get();
+ }
+
+ public GenericService get() {
+ return gs;
+ }
+
+ public void destroy() {
+ this.ref.destroy();
+ this.ref = null;
+ this.gs = null ;
+ }
+ }
+
+ HashMap gsDictionary;
public GenericServicePool() {
- gsDictionary = new HashMap();
+ gsDictionary = new HashMap();
+ }
+
+ @Override
+ protected void finalize() throws Throwable {
+
+ for(Entry entry : gsDictionary.entrySet()) {
+ entry.getValue(). destroy();
+ }
+ super.finalize();
}
public GenericService get(String key) {
- return gsDictionary.get(key);
+ return gsDictionary.get(key).get();
}
- public void put(String key, GenericService genericService) {
- gsDictionary.put(key, genericService);
+ public void put(String key, ReferenceConfig ref) {
+ ReferenceConfigStore refStore = new ReferenceConfigStore(ref);
+ gsDictionary.put(key, refStore);
}
}
\ No newline at end of file
diff --git a/usergw-service/src/main/java/cn/ecpark/service/usergw/config/ConfigApollo.java b/usergw-service/src/main/java/cn/ecpark/service/usergw/config/ConfigApollo.java
new file mode 100644
index 0000000..ee7eb53
--- /dev/null
+++ b/usergw-service/src/main/java/cn/ecpark/service/usergw/config/ConfigApollo.java
@@ -0,0 +1,136 @@
+package cn.ecpark.service.usergw.config;
+
+import com.ctrip.framework.apollo.Config;
+import com.ctrip.framework.apollo.ConfigChangeListener;
+import com.ctrip.framework.apollo.ConfigService;
+import com.ctrip.framework.apollo.enums.ConfigSourceType;
+import com.ctrip.framework.apollo.model.ConfigChangeEvent;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ConfigurableApplicationContext;
+
+import cn.ecpark.service.usergw.biz.events.ConfigRefresh;
+
+// @EnableApolloConfig("gateway.yml")
+// @ConditionalOnProperty(prefix="app", value="id", matchIfMissing=true)
+// @Configurable
+
+public class ConfigApollo implements ConfigChangeListener {
+
+
+ private static final org.slf4j.Logger log =
+ org.slf4j.LoggerFactory.getLogger(ConfigApollo.class);
+
+ // @ApolloConfig("gateway.yml")
+ private Config config;
+ @Autowired
+ private ApplicationContext appContext;
+
+ @Value("${apollo.meta:}")
+ private String apolloMeta;
+
+ @Value("${app.id:}")
+ private String appID;
+
+ @Value("${apollo.namespace:}")
+ private String apolloNamespace;
+
+ private int tryConnect;
+
+ public ConfigApollo() {
+ this.tryConnect = 1;
+ }
+
+ public void Connect() {
+ if (this.isConnected() && this.tryConnect > 0) {
+ this.tryConnect--;
+ ConfigSourceType stype = null;
+ if (apolloNamespace.equals("")) {
+ String[] tryExamples = { "gateway.yml", "gateway.yaml", "Gateway.yml", "Gateway.yaml" };
+ for (String ns : tryExamples) {
+ apolloNamespace = ns;
+ config = ConfigService.getConfig(apolloNamespace);
+ stype = config.getSourceType();
+ if (!stype.name().equals("None")) {
+ break;
+ }
+ }
+ } else {
+ config = ConfigService.getConfig(apolloNamespace);
+ stype = config.getSourceType();
+ }
+
+ if(stype.name().equals("None") || stype == null) {
+ log.error("getSourceType = {} is error, please check apollo server, or check apollo properties", stype.name());
+ ((ConfigurableApplicationContext)this.appContext).close();
+ }
+
+ if (config != null) {
+ config.addChangeListener(this);
+ }
+ }
+ }
+
+ public boolean isConnected() {
+ if (!this.appID.equals("") && !this.apolloMeta.equals("")) {
+ return true;
+ }
+ return false;
+ }
+
+ // @ApolloConfigChangeListener("gateway.yml")
+ // public void listenApolloChange(ConfigChangeEvent changeEvent) {
+
+ // }
+
+ @Override
+ public void onChange(ConfigChangeEvent changeEvent) {
+ ConfigRefresh change = (ConfigRefresh) appContext.getBean(ConfigRefresh.class);
+ change.notifyChanged();
+ }
+
+ /**
+ * @return Config return the config
+ */
+ public Config getConfig() {
+ return config;
+ }
+
+ /**
+ * @param config the config to set
+ */
+ public void setConfig(Config config) {
+ this.config = config;
+ }
+
+ /**
+ * @return String return the apolloMeta
+ */
+ public String getApolloMeta() {
+ return apolloMeta;
+ }
+
+ /**
+ * @param apolloMeta the apolloMeta to set
+ */
+ public void setApolloMeta(String apolloMeta) {
+ this.apolloMeta = apolloMeta;
+ }
+
+ /**
+ * @return String return the appID
+ */
+ public String getAppID() {
+ return appID;
+ }
+
+ /**
+ * @param appID the appID to set
+ */
+ public void setAppID(String appID) {
+ this.appID = appID;
+ }
+
+}
\ No newline at end of file
diff --git a/usergw-service/src/main/java/cn/ecpark/service/usergw/config/ConfigGateway.java b/usergw-service/src/main/java/cn/ecpark/service/usergw/config/ConfigGateway.java
index cafb945..9dfec7d 100644
--- a/usergw-service/src/main/java/cn/ecpark/service/usergw/config/ConfigGateway.java
+++ b/usergw-service/src/main/java/cn/ecpark/service/usergw/config/ConfigGateway.java
@@ -1,7 +1,14 @@
package cn.ecpark.service.usergw.config;
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
import java.io.InputStream;
+import java.lang.reflect.Field;
import java.net.URI;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
@@ -12,6 +19,12 @@ import java.util.Map.Entry;
import java.util.Set;
import java.util.function.BiConsumer;
+import com.ctrip.framework.apollo.internals.ConfigRepository;
+import com.ctrip.framework.apollo.internals.DefaultConfig;
+import com.ctrip.framework.apollo.internals.PropertiesCompatibleFileConfigRepository;
+import com.ctrip.framework.apollo.internals.YamlConfigFile;
+import com.ctrip.framework.apollo.internals.YmlConfigFile;
+
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.rpc.service.GenericService;
import org.springframework.beans.factory.annotation.Autowired;
@@ -23,24 +36,26 @@ import org.springframework.cloud.gateway.route.RouteDefinitionLocator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;
+import org.springframework.util.ResourceUtils;
import org.yaml.snakeyaml.Yaml;
import cn.ecpark.service.usergw.biz.filters.bean.GenericServicePool;
import cn.ecpark.service.usergw.utils.Extract;
-import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Flux;
/**
* ConfigGateway
*/
@Component
-@Slf4j
public class ConfigGateway implements RouteDefinitionLocator {
// List mediaTypes = new ArrayList<>();
+ private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ConfigGateway.class);
+
+ @Value("${yame.gateway.config:}")
+ private String gatewayConfigPath;
+
- @Value("${yame.config}")
- private String yameConfigPath;
private List defaultFilters = new ArrayList<>();
private HashMap, Object>> specialField = new HashMap, Object>>();
@@ -67,31 +82,25 @@ public class ConfigGateway implements RouteDefinitionLocator {
@Override
@SuppressWarnings("unchecked")
public Flux getRouteDefinitions() {
- // WebFluxConfigurationSupport a;
defaultFilters.clear();
- Object inputStream = null;
- String[] gatewayConfigPathList;
- if (yameConfigPath == null) {
- gatewayConfigPathList = new String[] { "gateway.yml", "gateway.yaml", "Gateway.yml", "Gateway.yaml" };
- } else {
- gatewayConfigPathList = new String[] { yameConfigPath };
- }
- for (String gatewayConfigPath : gatewayConfigPathList) {
- gatewayConfigPath = gatewayConfigPath.trim();
- inputStream = this.getClass().getClassLoader().getResourceAsStream(gatewayConfigPath);
- if (inputStream != null) {
- break;
- }
- }
+ InputStream inputStream;
+ ConfigApollo configApollo = appContext.getBean(ConfigApollo.class);
+ configApollo.Connect();
+ if(configApollo.isConnected()) {
+ inputStream = loadApolloConfig(configApollo);
+ } else {
+ inputStream = loadLocalFileConfig();
+ }
+
if (inputStream != null) {
- Map configYaml = new Yaml().load((InputStream) inputStream);
+ Map configYaml = new Yaml().load(inputStream);
if (configYaml != null) {
List defaultFiltersYaml = null;
Map restfulYaml = null;
Map dubboYaml = null;
-
+
Object unknown;
unknown = configYaml.get("default-filters");
if (unknown != null) {
@@ -121,13 +130,65 @@ public class ConfigGateway implements RouteDefinitionLocator {
if (!routeList.isEmpty()) {
return Flux.fromIterable(routeList);
}
-
}
}
return Flux.empty();
}
+ private InputStream loadLocalFileConfig() {
+ InputStream inputStream = null;
+ String[] gatewayConfigPathList;
+ if (gatewayConfigPath.equals("")) {
+ gatewayConfigPathList = new String[] { "gateway.yml", "gateway.yaml", "Gateway.yml", "Gateway.yaml" };
+ } else {
+ gatewayConfigPathList = new String[] { gatewayConfigPath };
+ }
+
+ for (String gatewayConfigPath : gatewayConfigPathList) {
+ gatewayConfigPath = gatewayConfigPath.trim();
+ inputStream = this.getClass().getClassLoader().getResourceAsStream(gatewayConfigPath);
+ if(inputStream != null) {
+ return inputStream;
+ }
+ }
+
+ log.error("can't find the config of gateway, like gateway.yaml");
+ return inputStream;
+ }
+
+
+ private InputStream loadApolloConfig(ConfigApollo configApollo) {
+ DefaultConfig config = (DefaultConfig) configApollo.getConfig();
+ try {
+ String content = null ;
+ for(int i = 0; i < 3; i++) {
+ Field m_configRepository = DefaultConfig.class.getDeclaredField("m_configRepository");
+ m_configRepository.setAccessible(true);
+ ConfigRepository cr = (ConfigRepository) m_configRepository.get(config);
+ Field configFile = PropertiesCompatibleFileConfigRepository.class.getDeclaredField("configFile");
+ configFile.setAccessible(true);
+ YamlConfigFile ymlConfigFile = (YamlConfigFile) configFile.get(cr);
+ content = ymlConfigFile.getContent();
+ if(content == null) {
+ log.error("eson:debug {}", config.toString());
+ continue;
+ }
+ log.info("Load Apollo YAML Config: \n{}", content);
+ return new ByteArrayInputStream(content.getBytes());
+ }
+ } catch (NoSuchFieldException e) {
+ e.printStackTrace();
+ } catch (SecurityException e) {
+ e.printStackTrace();
+ } catch (IllegalArgumentException e) {
+ e.printStackTrace();
+ } catch (IllegalAccessException e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+
/**
*
* @param routeList 路由列表
@@ -255,7 +316,7 @@ public class ConfigGateway implements RouteDefinitionLocator {
/**
*
- * @param rd {@link RouteDefinition}
+ * @param rd {@link RouteDefinition}
* @param iter Yaml.dubbo.routes
* @return dubboUri 字符串标识 eg. dubbo://application/group/com.a.b:1.0.0
*/
@@ -285,12 +346,11 @@ public class ConfigGateway implements RouteDefinitionLocator {
// 设置uri
Object order = iter.get("order");
if (order != null) {
- rd.setOrder((int)order);
+ rd.setOrder((int) order);
iter.remove("order");
} else {
rd.setOrder(0);
}
-
ReferenceConfig reference = new ReferenceConfig();
reference.setConnections(3);
@@ -342,16 +402,16 @@ public class ConfigGateway implements RouteDefinitionLocator {
UriString += Extract.getReferenceConfigKey(reference);
GenericServicePool gsPool = appContext.getBean(GenericServicePool.class);
reference.setGeneric(true);
- gsPool.put(UriString, reference.get());
+ gsPool.put(UriString, reference);
return UriString;
}
/**
*
- * @param predicates List<{@link PredicateDefinition}> 断言列表
- * @param iter restful.routes.predicates
- * @param yamlField 根据域的key 获取 predicates 字符串列
+ * @param predicates List<{@link PredicateDefinition}> 断言列表
+ * @param iter restful.routes.predicates
+ * @param yamlField 根据域的key 获取 predicates 字符串列
*/
private void parseAndAddPredicates(List predicates, LinkedHashMap> iter,
String yamlField) {
@@ -365,9 +425,9 @@ public class ConfigGateway implements RouteDefinitionLocator {
/**
*
- * @param filters List<{@link FilterDefinition}> 过滤器列表
- * @param iter restful.routes.filters
- * @param yamlField 根据域的key 获取 filters 字符串列
+ * @param filters List<{@link FilterDefinition}> 过滤器列表
+ * @param iter restful.routes.filters
+ * @param yamlField 根据域的key 获取 filters 字符串列
*/
private void parseAndAddFilters(List filters, LinkedHashMap> iter,
String yamlField) {
@@ -382,43 +442,44 @@ public class ConfigGateway implements RouteDefinitionLocator {
/**
*
- * @param dubboUri 获取返回的字符串 {@link #parseDubboUriAndSetBase(RouteDefinition, LinkedHashMap)}
- * @param filters List<{@link FilterDefinition}> 过滤器列表
- * @param iter dubbo.routes.filters
+ * @param dubboUri 获取返回的字符串
+ * {@link #parseDubboUriAndSetBase(RouteDefinition, LinkedHashMap)}
+ * @param filters List<{@link FilterDefinition}> 过滤器列表
+ * @param iter dubbo.routes.filters
* @param yamlField 根据域的key 获取 filters 字符串列
*/
@SuppressWarnings("unchecked")
private void parseAndAddDubboFilters(String dubboUri, List filters,
LinkedHashMap> iter, String yamlField) {
List filtersYaml = iter.get(yamlField);
-
+
filters.addAll(defaultFilters);
if (filtersYaml != null) {
for (Object filterObject : filtersYaml) {
// 现阶段只支持两种情况
- if(filterObject.getClass() == String.class) {
- String filterString = (String)filterObject;
+ if (filterObject.getClass() == String.class) {
+ String filterString = (String) filterObject;
FilterDefinition fd = new FilterDefinition(filterString);
log.info(fd.getName());
if (!fd.getName().equals("Dubbo")) {
filters.add(fd);
}
} else if (filterObject.getClass() == LinkedHashMap.class) {
- Map filterMap = (LinkedHashMap)filterObject;
+ Map filterMap = (LinkedHashMap) filterObject;
FilterDefinition fd = new FilterDefinition();
-
- fd.setName((String)filterMap.get("name"));
- if(filterMap.containsKey("args")) {
+
+ fd.setName((String) filterMap.get("name"));
+ if (filterMap.containsKey("args")) {
Object args = filterMap.get("args");
- for( Entry kv : ((Map)args).entrySet()) {
- if(kv.getValue().getClass() != String.class) {
- kv.setValue( String.valueOf(kv.getValue()));
+ for (Entry kv : ((Map) args).entrySet()) {
+ if (kv.getValue().getClass() != String.class) {
+ kv.setValue(String.valueOf(kv.getValue()));
}
}
- fd.setArgs((Map)args);
+ fd.setArgs((Map) args);
}
-
+
log.info(fd.getName());
if (!fd.getName().equals("Dubbo")) {
filters.add(fd);
diff --git a/usergw-service/src/main/java/cn/ecpark/service/usergw/config/bean/ConfigBean.java b/usergw-service/src/main/java/cn/ecpark/service/usergw/config/bean/ConfigBean.java
index 3fae651..0e58bfe 100644
--- a/usergw-service/src/main/java/cn/ecpark/service/usergw/config/bean/ConfigBean.java
+++ b/usergw-service/src/main/java/cn/ecpark/service/usergw/config/bean/ConfigBean.java
@@ -6,22 +6,29 @@ import org.springframework.stereotype.Component;
import cn.ecpark.service.usergw.biz.filters.bean.GenericServicePool;
import cn.ecpark.service.usergw.biz.filters.factory.DubboGatewayFilterFactory;
+import cn.ecpark.service.usergw.config.ConfigApollo;
import reactor.core.publisher.Mono;
@Component
public class ConfigBean {
+
+ @Bean
+ KeyResolver ipResolver() {
+ return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostString());
+ }
+
@Bean
public GenericServicePool genericServicePool() {
return new GenericServicePool();
}
@Bean
- public DubboGatewayFilterFactory dubboFilterFactory() {
- return new DubboGatewayFilterFactory();
+ ConfigApollo configApollo() {
+ return new ConfigApollo();
}
@Bean
- KeyResolver ipResolver() {
- return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostString());
+ public DubboGatewayFilterFactory dubboFilterFactory() {
+ return new DubboGatewayFilterFactory();
}
}
\ No newline at end of file
diff --git a/usergw-service/src/main/resources/application.properties b/usergw-service/src/main/resources/application.properties
index 4afec56..683b2c2 100644
--- a/usergw-service/src/main/resources/application.properties
+++ b/usergw-service/src/main/resources/application.properties
@@ -1,5 +1,17 @@
spring.application.name=gateway
+apollo.namespace=gateway.yml
+app.id=gateway
+apollo.meta=http://localhost:8180
+# local.meta=http://localhost:8180
+# dev.meta=http://localhost:8180
+# fat.meta=http://localhost:8180
+# uat.meta=http://localhost:8180
+# lpt.meta=http://localhost:8180
+# pro.meta=http://localhost:8180
+# eureka.instance.ip-address=http://localhost:8180
+
+
dubbo.scan.base-packages=cn.ecpark.service.usergw.impl
dubbo.protocol.name=dubbo
dubbo.protocol.port=20999
@@ -10,6 +22,7 @@ server.port=8888
# logging.level.org.springframework.cloud.gateway=debug
logging.file=logs/log
+yame.gateway.config=gateway.yaml
+
-yame.config=gateway.yaml
diff --git a/usergw-service/src/main/resources/test-gateway4.yaml b/usergw-service/src/main/resources/test-gateway4.yaml
index 01c7412..0e0fb85 100644
--- a/usergw-service/src/main/resources/test-gateway4.yaml
+++ b/usergw-service/src/main/resources/test-gateway4.yaml
@@ -46,8 +46,8 @@ dubbo:
- name: RequestRateLimiter
args:
key-resolver: '#{@ipResolver}' #SPEL表达式去的对应的bean
- redis-rate-limiter.replenishRate: 40 # 令牌桶的容积
- redis-rate-limiter.burstCapacity: 80 # 流速 每秒
+ redis-rate-limiter.replenishRate: 30 # 令牌桶的容积
+ redis-rate-limiter.burstCapacity: 60 # 流速 每秒
# - name: RequestRateLimiter1
# args:
diff --git a/usergw-service/src/main/resources/test-gateway5.yaml b/usergw-service/src/main/resources/test-gateway5.yaml
new file mode 100644
index 0000000..54320bd
--- /dev/null
+++ b/usergw-service/src/main/resources/test-gateway5.yaml
@@ -0,0 +1,19 @@
+dubbo:
+ routes:
+ - id: test
+ order: 0
+ application: dubbo-exchange
+ methods: # 如果没填就从 request拿 意味着所有接口都可以使用
+ - name: Say
+ param-types:
+ - java.lang.String
+ - name: Hello
+
+ connections: 4
+ group: test
+ # registry: zookeeper://127.0.0.1:2181
+ interface: ocean.demo.api.IExchange
+ version: 1.0.0
+ predicates:
+ - Path=/dubbo/gateway/test5
+
\ No newline at end of file
diff --git a/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig.java b/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig.java
index 75922c6..bd623d0 100644
--- a/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig.java
+++ b/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig.java
@@ -6,6 +6,8 @@ import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.web.server.LocalServerPort;
+import org.springframework.context.annotation.PropertySource;
+import org.springframework.core.annotation.Order;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
@@ -15,10 +17,9 @@ import reactor.netty.http.client.HttpClientResponse;
-// TODO: 添加附加Dubbo Service for Test启动进程
@SpringBootTest(classes= App.class, webEnvironment = WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
-@TestPropertySource(locations = {"application.properties"})
+@TestPropertySource(locations = "classpath:/resources/test1.properties", properties = {"apollo.meta=", "app.id="})
public class TestHttp2DubboConfig {
@LocalServerPort
diff --git a/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig2.java b/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig2.java
index 5c3e37c..64fffee 100644
--- a/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig2.java
+++ b/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig2.java
@@ -6,6 +6,7 @@ import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.web.server.LocalServerPort;
+import org.springframework.core.annotation.Order;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
@@ -13,10 +14,10 @@ import reactor.netty.http.client.HttpClient;
import reactor.netty.http.client.HttpClient.ResponseReceiver;
import reactor.netty.http.client.HttpClientResponse;
-// TODO: 添加附加Dubbo Service for Test启动进程
+
@SpringBootTest(classes = App.class, webEnvironment = WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
-@TestPropertySource(locations = { "application2.properties" })
+@TestPropertySource(locations = "classpath:/resources/test2.properties", properties = {"apollo.meta=", "app.id="})
public class TestHttp2DubboConfig2 {
@LocalServerPort
diff --git a/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig3.java b/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig3.java
index 9e9335d..e4379a2 100644
--- a/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig3.java
+++ b/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig3.java
@@ -10,6 +10,7 @@ import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.web.server.LocalServerPort;
+import org.springframework.core.annotation.Order;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
@@ -17,10 +18,10 @@ import reactor.netty.http.client.HttpClient;
import reactor.netty.http.client.HttpClient.ResponseReceiver;
import reactor.netty.http.client.HttpClientResponse;
-// TODO: 添加附加Dubbo Service for Test启动进程
+
@SpringBootTest(classes = App.class, webEnvironment = WebEnvironment.DEFINED_PORT)
@RunWith(SpringRunner.class)
-@TestPropertySource(locations = { "application3.properties" })
+@TestPropertySource(locations = "classpath:/resources/test3.properties", properties = {"apollo.meta=", "app.id="})
public class TestHttp2DubboConfig3 {
@LocalServerPort
diff --git a/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig4.java b/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig4.java
index 37c79ac..6d00513 100644
--- a/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig4.java
+++ b/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig4.java
@@ -1,3 +1,8 @@
+
+
+
+
+
package cn.ecpark.service.usergw;
import org.junit.Assert;
@@ -6,6 +11,7 @@ import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.web.server.LocalServerPort;
+import org.springframework.core.annotation.Order;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
@@ -16,14 +22,14 @@ import reactor.netty.http.client.HttpClientResponse;
// TODO: 添加附加Dubbo Service for Test启动进程
@SpringBootTest(classes = App.class, webEnvironment = WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
-@TestPropertySource(locations = { "application4.properties" })
+@TestPropertySource(locations = "classpath:/resources/test4.properties", properties = {"apollo.meta=", "app.id="})
public class TestHttp2DubboConfig4 {
@LocalServerPort
private int serverPort;
@Test
- public void Test1RequestHttp2DubboSetHeader() {
+ public void Test1RedisLimit() {
// Test Base Url
for(int i = 0; i < 50;i++) {
HttpClient client = HttpClient.create();
@@ -46,14 +52,5 @@ public class TestHttp2DubboConfig4 {
}
}
-
-
- // @Test
- // @SuppressWarnings("unchecked")
- // public void Test3RestfulFilters() {
-
-
- // }
-
}
\ No newline at end of file
diff --git a/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig5.java b/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig5.java
new file mode 100644
index 0000000..22376ff
--- /dev/null
+++ b/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig5.java
@@ -0,0 +1,58 @@
+package cn.ecpark.service.usergw;
+
+import org.junit.Assert;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
+import org.springframework.boot.web.server.LocalServerPort;
+import org.springframework.core.annotation.Order;
+import org.springframework.test.context.TestPropertySource;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import reactor.netty.http.client.HttpClient;
+import reactor.netty.http.client.HttpClient.ResponseReceiver;
+import reactor.netty.http.client.HttpClientResponse;
+
+// TODO: 添加附加Dubbo Service for Test启动进程
+@SpringBootTest(classes = App.class, webEnvironment = WebEnvironment.RANDOM_PORT)
+@RunWith(SpringRunner.class)
+@TestPropertySource(locations = "classpath:/resources/test5.properties")
+@Ignore
+public class TestHttp2DubboConfig5 {
+
+ @LocalServerPort
+ private int serverPort;
+
+ @Test
+ public void Test1LoadApolloConfig() {
+ // Test Base Url
+
+ HttpClient client = HttpClient.create();
+ ResponseReceiver> receiver;
+ String content;
+
+ receiver = client.baseUrl("http://localhost:" + serverPort + "/dubbo/gateway/test5")
+ .headers(h -> h.set("method", "Hello")).get();
+
+ HttpClientResponse response = receiver.response().block();
+ Assert.assertNotNull(response);
+
+ if(response.status().code() == 200) {
+ content = receiver.responseContent().asString().blockLast();
+ Assert.assertNotNull(content);
+ Assert.assertEquals("Hello Dubbo", content);
+ } else {
+ Assert.assertEquals(429, response.status().code());
+ }
+
+
+ receiver = client.baseUrl("http://localhost:" + serverPort + "/dubbo/hello")
+ .headers(h -> h.set("method", "Hello")).get();
+
+ response = receiver.response().block();
+ Assert.assertNotNull(response);
+ Assert.assertNotEquals(200, response.status().code());
+ }
+}
diff --git a/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig6.java b/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig6.java
new file mode 100644
index 0000000..93558ab
--- /dev/null
+++ b/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig6.java
@@ -0,0 +1,59 @@
+
+
+package cn.ecpark.service.usergw;
+
+import org.junit.Assert;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
+import org.springframework.boot.web.server.LocalServerPort;
+import org.springframework.core.annotation.Order;
+import org.springframework.test.context.TestPropertySource;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import reactor.netty.http.client.HttpClient;
+import reactor.netty.http.client.HttpClient.ResponseReceiver;
+import reactor.netty.http.client.HttpClientResponse;
+
+// TODO: 添加附加Dubbo Service for Test启动进程
+@SpringBootTest(classes = App.class, webEnvironment = WebEnvironment.RANDOM_PORT)
+@RunWith(SpringRunner.class)
+@TestPropertySource(locations = "classpath:/resources/test5.properties", properties = {"apollo.namespace=", "app.id=gateway"})
+@Ignore
+public class TestHttp2DubboConfig6 {
+
+ @LocalServerPort
+ private int serverPort;
+
+ @Test
+
+ public void Test1LoadDefaultApolloConfig() {
+ // Test Base Url
+
+ HttpClient client = HttpClient.create();
+ ResponseReceiver> receiver;
+ String content;
+
+ receiver = client.baseUrl("http://localhost:" + serverPort + "/dubbo/hello1")
+ .headers(h -> h.set("method", "Hello")).get();
+
+ HttpClientResponse response = receiver.response().block();
+ Assert.assertNotNull(response);
+
+ if(response.status().code() == 200) {
+ content = receiver.responseContent().asString().blockLast();
+ Assert.assertNotNull(content);
+ Assert.assertEquals("Hello Dubbo", content);
+ } else {
+ Assert.assertEquals(429, response.status().code());
+ }
+ }
+
+
+
+
+
+}
+
\ No newline at end of file
diff --git a/usergw-service/src/test/java/resources/application.properties b/usergw-service/src/test/java/resources/application.properties
deleted file mode 100644
index 71b9ee3..0000000
--- a/usergw-service/src/test/java/resources/application.properties
+++ /dev/null
@@ -1,13 +0,0 @@
-
-spring.application.name=gateway
-dubbo.scan.base-packages=cn.ecpark.service.usergw.impl
-dubbo.protocol.name=dubbo
-dubbo.protocol.port=20999
-dubbo.registry.address=zookeeper://127.0.0.1:2181
-dubbo.config-center.address=zookeeper://127.0.0.1:2181
-dubbo.metadata-report.address=zookeeper://127.0.0.1:2181
-server.port=8888
-
-# logging.level.org.springframework.cloud.gateway=debug
-logging.file=logs/log
-
diff --git a/usergw-service/src/test/java/resources/gateway.yaml b/usergw-service/src/test/java/resources/gateway.yaml
deleted file mode 100644
index 7e9c5b4..0000000
--- a/usergw-service/src/test/java/resources/gateway.yaml
+++ /dev/null
@@ -1,51 +0,0 @@
-default-filters:
- - AddResponseHeader=X-Response-Default-Foo, Test-Default-Bar
-
-restful:
- routes:
- - id: path_route
- uri: http://httpbin.org:80/*
- order: 9
- predicates:
- - Path=/get
- - Header=XX, \d+
-
- - id: path_route12;
- uri: http://httpbin.org:80/*
- order: 9
- predicates:
- - Path=/get
- - Header=XX, \d+
-
- - id: path_route13
- uri: http://httpbin.org:80/*
- order: 9
- predicates:
- - Path=/get
- - Header=XX, \d+
- # - id: redirect_to
- # uri: http://localhost/test/**
- # order: 11
- # filters:
- # - RedirectTo=302, http://httpbin.org:80/get
-
-dubbo:
- routes:
- - id: test
- order: 0
- application: dubbo-exchange
- # methods: # 如果没填就从 request拿 意味着所有接口都可以使用
-
- # - name: Say
- # param-types:
- # - java.lang.String
- # - name: Hello
-
- connections: 4
- group: test
- # registry: zookeeper://127.0.0.1:2181
- interface: ocean.demo.api.IExchange
- version: 1.0.0
- predicates:
- - Path=/dubbo/hello
-
\ No newline at end of file
diff --git a/usergw-service/src/test/java/resources/test-gateway.yaml b/usergw-service/src/test/java/resources/test-gateway.yaml
deleted file mode 100644
index 39bd62a..0000000
--- a/usergw-service/src/test/java/resources/test-gateway.yaml
+++ /dev/null
@@ -1,50 +0,0 @@
-default-filters:
- - AddResponseHeader=X-Response-Default-Foo, Test-Default-Bar
-
-restful:
- routes:
- - id: path_route
- uri: http://httpbin.org:80/*
- order: 9
- predicates:
- - Path=/get
- - Header=XX, \d+
-
- - id: path_route12;
- uri: http://httpbin.org:80/*
- order: 9
- predicates:
- - Path=/get
- - Header=XX, \d+
-
- - id: path_route13
- uri: http://httpbin.org:80/*
- order: 9
- predicates:
- - Path=/get
- - Header=XX, \d+
- # - id: redirect_to
- # uri: http://localhost/test/**
- # order: 11
- # filters:
- # - RedirectTo=302, http://httpbin.org:80/get
-
-dubbo:
- routes:
- - id: test
- order: 0
- application: dubbo-exchange
- methods: # 如果没填就从 request拿 意味着所有接口都可以使用
- - name: Say
- param-types:
- - java.lang.String
- - name: Hello
-
- connections: 4
- group: test
- # registry: zookeeper://127.0.0.1:2181
- interface: ocean.demo.api.IExchange
- version: 1.0.0
- predicates:
- - Path=/dubbo/hello
-
\ No newline at end of file
diff --git a/usergw-service/src/test/java/cn/ecpark/service/usergw/application.properties b/usergw-service/src/test/java/resources/test1.properties
similarity index 91%
rename from usergw-service/src/test/java/cn/ecpark/service/usergw/application.properties
rename to usergw-service/src/test/java/resources/test1.properties
index 31e134b..905c6b3 100644
--- a/usergw-service/src/test/java/cn/ecpark/service/usergw/application.properties
+++ b/usergw-service/src/test/java/resources/test1.properties
@@ -10,6 +10,5 @@ server.port=8888
# logging.level.org.springframework.cloud.gateway=debug
logging.file=logs/log
-
-yame.config=test-gateway.yaml
+yame.gateway.config=test-gateway.yaml
diff --git a/usergw-service/src/test/java/cn/ecpark/service/usergw/application2.properties b/usergw-service/src/test/java/resources/test2.properties
similarity index 91%
rename from usergw-service/src/test/java/cn/ecpark/service/usergw/application2.properties
rename to usergw-service/src/test/java/resources/test2.properties
index 592aa8c..7152322 100644
--- a/usergw-service/src/test/java/cn/ecpark/service/usergw/application2.properties
+++ b/usergw-service/src/test/java/resources/test2.properties
@@ -11,5 +11,5 @@ server.port=8888
# logging.level.org.springframework.cloud.gateway=debug
logging.file=logs/log
-yame.config=test-gateway2.yaml
+yame.gateway.config=test-gateway2.yaml
diff --git a/usergw-service/src/test/java/cn/ecpark/service/usergw/application3.properties b/usergw-service/src/test/java/resources/test3.properties
similarity index 91%
rename from usergw-service/src/test/java/cn/ecpark/service/usergw/application3.properties
rename to usergw-service/src/test/java/resources/test3.properties
index 1dcad2f..27ac3b7 100644
--- a/usergw-service/src/test/java/cn/ecpark/service/usergw/application3.properties
+++ b/usergw-service/src/test/java/resources/test3.properties
@@ -11,5 +11,5 @@ server.port=8888
# logging.level.org.springframework.cloud.gateway=debug
logging.file=logs/log
-yame.config=test-gateway3.yaml
+yame.gateway.config=test-gateway3.yaml
diff --git a/usergw-service/src/test/java/cn/ecpark/service/usergw/application4.properties b/usergw-service/src/test/java/resources/test4.properties
similarity index 92%
rename from usergw-service/src/test/java/cn/ecpark/service/usergw/application4.properties
rename to usergw-service/src/test/java/resources/test4.properties
index fa63143..de36ee3 100644
--- a/usergw-service/src/test/java/cn/ecpark/service/usergw/application4.properties
+++ b/usergw-service/src/test/java/resources/test4.properties
@@ -17,5 +17,5 @@ server.port=8888
# logging.level.org.springframework.cloud.gateway=debug
logging.file=logs/log
-yame.config=test-gateway4.yaml
+yame.gateway.config=test-gateway4.yaml
diff --git a/usergw-service/src/test/java/resources/test5.properties b/usergw-service/src/test/java/resources/test5.properties
new file mode 100644
index 0000000..b70354d
--- /dev/null
+++ b/usergw-service/src/test/java/resources/test5.properties
@@ -0,0 +1,33 @@
+
+spring.application.name=gateway
+# AppID 非 NameSpace
+app.id=test-gateway5
+apollo.meta=http://localhost:8180
+apollo.namespace=gateway-test5.yaml
+# local.meta=http://localhost:8180
+# dev.meta=http://localhost:8180
+# fat.meta=http://localhost:8180
+# uat.meta=http://localhost:8180
+# lpt.meta=http://localhost:8180
+# pro.meta=http://localhost:8180
+# eureka.instance.ip-address=http://localhost:8180
+
+dubbo.scan.base-packages=cn.ecpark.service.usergw.impl
+dubbo.protocol.name=dubbo
+dubbo.protocol.port=20999
+dubbo.registry.address=zookeeper://127.0.0.1:2181
+dubbo.config-center.address=zookeeper://127.0.0.1:2181
+dubbo.metadata-report.address=zookeeper://127.0.0.1:2181
+# 这个链接Apollo非常有意义.
+# 因为在一个线程内, 同时Connect Zookeeper 和 Apollo 会导致 Apollo可能超过3秒再加其他操作.Zookeeper会出现阻塞.
+dubbo.consumer.timeout=5000
+
+server.port=8888
+
+logging.file=logs/log
+yame.gateway.config=test-gateway5.yaml
+
+
+
+
+