From 9734ee1826fb986005ee06bcf38b2310a4256a5d Mon Sep 17 00:00:00 2001 From: huangsimin Date: Mon, 12 Aug 2019 18:49:05 +0800 Subject: [PATCH 1/9] =?UTF-8?q?=E6=88=90=E5=8A=9FApolloListen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- usergw-service/pom.xml | 8 +++- .../java/cn/ecpark/service/usergw/App.java | 7 ++++ .../service/usergw/config/ConfigApollo.java | 40 +++++++++++++++++++ .../service/usergw/config/ConfigGateway.java | 21 +++++++--- .../src/main/resources/application.properties | 6 +++ 6 files changed, 76 insertions(+), 8 deletions(-) create mode 100644 usergw-service/src/main/java/cn/ecpark/service/usergw/config/ConfigApollo.java diff --git a/pom.xml b/pom.xml index 9f89c20..2c69996 100644 --- a/pom.xml +++ b/pom.xml @@ -18,7 +18,7 @@ Demo project for Yame User Gateway - 1.8 + 1.11 UTF-8 UTF-8 Greenwich.SR2 diff --git a/usergw-service/pom.xml b/usergw-service/pom.xml index 34909de..71110c8 100644 --- a/usergw-service/pom.xml +++ b/usergw-service/pom.xml @@ -16,7 +16,7 @@ Demo project for Gateway Service - 1.8 + 1.11 @@ -70,6 +70,12 @@ usergw-api ${project.parent.version} + + + com.ctrip.framework.apollo + apollo-client + 1.4.0 + org.springframework.boot 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..b22f7b2 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,19 +1,26 @@ package cn.ecpark.service.usergw; +import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig; + 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 @EnableDubboConfig @ComponentScan("cn.ecpark.service.usergw") +@EnableApolloConfig public class App { public static void main(String[] args) { 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..e24642f --- /dev/null +++ b/usergw-service/src/main/java/cn/ecpark/service/usergw/config/ConfigApollo.java @@ -0,0 +1,40 @@ +package cn.ecpark.service.usergw.config; + +import javax.annotation.Resource; + +import org.springframework.beans.factory.annotation.Configurable; +import org.springframework.cloud.gateway.event.RefreshRoutesEvent; +import org.springframework.cloud.gateway.route.RouteDefinitionWriter; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.context.ApplicationEventPublisherAware; +import org.springframework.stereotype.Service; + +import lombok.extern.slf4j.Slf4j; + +import com.ctrip.framework.apollo.model.ConfigChangeEvent; +import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener; +import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig; + +@Slf4j +@Configurable +@Service +@EnableApolloConfig +public class ConfigApollo implements ApplicationEventPublisherAware { + + private ApplicationEventPublisher publisher; + + public void notifyChanged() { + this.publisher.publishEvent(new RefreshRoutesEvent(this)); + } + + @ApolloConfigChangeListener("gateway.yml") + private void listenApolloChange(ConfigChangeEvent changeEvent) { + log.warn(changeEvent.getNamespace()); + } + + @Override + public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { + this.publisher = applicationEventPublisher; + } + +} \ 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..e072cf0 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 @@ -64,11 +64,7 @@ public class ConfigGateway implements RouteDefinitionLocator { ignoreKey.add("filters"); } - @Override - @SuppressWarnings("unchecked") - public Flux getRouteDefinitions() { - // WebFluxConfigurationSupport a; - defaultFilters.clear(); + private InputStream loadLocalFileConfig() { Object inputStream = null; String[] gatewayConfigPathList; if (yameConfigPath == null) { @@ -83,9 +79,22 @@ public class ConfigGateway implements RouteDefinitionLocator { break; } } + return (InputStream)inputStream; + } + + private InputStream loadApolloConfige() { + return null; + } + + @Override + @SuppressWarnings("unchecked") + public Flux getRouteDefinitions() { + // WebFluxConfigurationSupport a; + defaultFilters.clear(); + InputStream inputStream = loadLocalFileConfig(); if (inputStream != null) { - Map configYaml = new Yaml().load((InputStream) inputStream); + Map configYaml = new Yaml().load(inputStream); if (configYaml != null) { List defaultFiltersYaml = null; diff --git a/usergw-service/src/main/resources/application.properties b/usergw-service/src/main/resources/application.properties index 4afec56..adfa478 100644 --- a/usergw-service/src/main/resources/application.properties +++ b/usergw-service/src/main/resources/application.properties @@ -1,5 +1,9 @@ spring.application.name=gateway + +app.id=gateway +apollo.meta=http://127.0.0.1:8180 + dubbo.scan.base-packages=cn.ecpark.service.usergw.impl dubbo.protocol.name=dubbo dubbo.protocol.port=20999 @@ -13,3 +17,5 @@ logging.file=logs/log yame.config=gateway.yaml + + From ce404ecf6bbba54e37513bf44ffc7d706cee2f72 Mon Sep 17 00:00:00 2001 From: huangsimin Date: Tue, 13 Aug 2019 18:28:05 +0800 Subject: [PATCH 2/9] TODO: ConfigApollo Created In Bean. --- pom.xml | 2 +- usergw-service/pom.xml | 2 +- .../java/cn/ecpark/service/usergw/App.java | 3 - .../service/usergw/biz/events/Change.java | 23 ++++ .../service/usergw/config/ConfigApollo.java | 50 +++++--- .../service/usergw/config/ConfigGateway.java | 109 ++++++++++++------ .../usergw/config/bean/ConfigBean.java | 8 ++ .../src/main/resources/application.properties | 13 ++- 8 files changed, 151 insertions(+), 59 deletions(-) create mode 100644 usergw-service/src/main/java/cn/ecpark/service/usergw/biz/events/Change.java diff --git a/pom.xml b/pom.xml index 2c69996..9f89c20 100644 --- a/pom.xml +++ b/pom.xml @@ -18,7 +18,7 @@ Demo project for Yame User Gateway - 1.11 + 1.8 UTF-8 UTF-8 Greenwich.SR2 diff --git a/usergw-service/pom.xml b/usergw-service/pom.xml index 71110c8..44ae320 100644 --- a/usergw-service/pom.xml +++ b/usergw-service/pom.xml @@ -16,7 +16,7 @@ Demo project for Gateway Service - 1.11 + 1.8 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 b22f7b2..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,7 +1,5 @@ package cn.ecpark.service.usergw; -import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig; - import org.apache.dubbo.config.spring.context.annotation.EnableDubboConfig; import org.springframework.beans.factory.annotation.Configurable; import org.springframework.boot.SpringApplication; @@ -20,7 +18,6 @@ import org.springframework.context.annotation.Configuration; @EnableAutoConfiguration @EnableDubboConfig @ComponentScan("cn.ecpark.service.usergw") -@EnableApolloConfig public class App { public static void main(String[] args) { diff --git a/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/events/Change.java b/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/events/Change.java new file mode 100644 index 0000000..9271c54 --- /dev/null +++ b/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/events/Change.java @@ -0,0 +1,23 @@ +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 Change 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/config/ConfigApollo.java b/usergw-service/src/main/java/cn/ecpark/service/usergw/config/ConfigApollo.java index e24642f..0bd1f1d 100644 --- 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 @@ -3,6 +3,7 @@ package cn.ecpark.service.usergw.config; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Configurable; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.cloud.gateway.event.RefreshRoutesEvent; import org.springframework.cloud.gateway.route.RouteDefinitionWriter; import org.springframework.context.ApplicationEventPublisher; @@ -11,30 +12,47 @@ import org.springframework.stereotype.Service; import lombok.extern.slf4j.Slf4j; +import com.ctrip.framework.apollo.Config; +import com.ctrip.framework.apollo.ConfigService; +import com.ctrip.framework.apollo.model.ConfigChange; import com.ctrip.framework.apollo.model.ConfigChangeEvent; +import com.ctrip.framework.apollo.spring.annotation.ApolloConfig; import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener; import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig; -@Slf4j -@Configurable -@Service -@EnableApolloConfig -public class ConfigApollo implements ApplicationEventPublisherAware { - private ApplicationEventPublisher publisher; - public void notifyChanged() { - this.publisher.publishEvent(new RefreshRoutesEvent(this)); +// @EnableApolloConfig("gateway.yml") +// @ConditionalOnProperty(prefix="app", value="id", matchIfMissing=true) +// @Configurable + +public class ConfigApollo { + + // private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ConfigApollo.class); + + // @ApolloConfig("gateway.yml") + private Config config; + + public ConfigApollo() { + } - - @ApolloConfigChangeListener("gateway.yml") + // @ApolloConfigChangeListener("gateway.yml") private void listenApolloChange(ConfigChangeEvent changeEvent) { - log.warn(changeEvent.getNamespace()); + + } + + /** + * @return Config return the config + */ + public Config getConfig() { + return config; + } + + /** + * @param config the config to set + */ + public void setConfig(Config config) { + this.config = config; } - @Override - public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { - this.publisher = applicationEventPublisher; - } - } \ 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 e072cf0..58ce415 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,6 +1,8 @@ package cn.ecpark.service.usergw.config; +import java.io.ByteArrayInputStream; import java.io.InputStream; +import java.lang.reflect.Field; import java.net.URI; import java.util.ArrayList; import java.util.HashMap; @@ -12,6 +14,11 @@ 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.YmlConfigFile; + import org.apache.dubbo.config.ReferenceConfig; import org.apache.dubbo.rpc.service.GenericService; import org.springframework.beans.factory.annotation.Autowired; @@ -27,21 +34,26 @@ 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.config}") + @Value("${yame.config:}") private String yameConfigPath; + @Value("${apollo.meta:}") + private String apolloMeta; + + @Value("${app.id:}") + private String appID; + private List defaultFilters = new ArrayList<>(); private HashMap, Object>> specialField = new HashMap, Object>>(); private Set ignoreKey = new HashSet<>(); @@ -67,7 +79,7 @@ public class ConfigGateway implements RouteDefinitionLocator { private InputStream loadLocalFileConfig() { Object inputStream = null; String[] gatewayConfigPathList; - if (yameConfigPath == null) { + if (yameConfigPath.equals("")) { gatewayConfigPathList = new String[] { "gateway.yml", "gateway.yaml", "Gateway.yml", "Gateway.yaml" }; } else { gatewayConfigPathList = new String[] { yameConfigPath }; @@ -79,20 +91,47 @@ public class ConfigGateway implements RouteDefinitionLocator { break; } } - return (InputStream)inputStream; + return (InputStream) inputStream; } - - private InputStream loadApolloConfige() { + + + private InputStream loadApolloConfig() { + if (true) return null; + ConfigApollo configApollo = appContext.getBean(ConfigApollo.class); + DefaultConfig config = (DefaultConfig) configApollo.getConfig(); + try { + 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); + YmlConfigFile ymlConfigFile = (YmlConfigFile) configFile.get(cr); + String content = ymlConfigFile.getContent(); + 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; } @Override @SuppressWarnings("unchecked") public Flux getRouteDefinitions() { - // WebFluxConfigurationSupport a; defaultFilters.clear(); - InputStream inputStream = loadLocalFileConfig(); - + InputStream inputStream; + if(!this.apolloMeta.equals("") && !this.appID.equals("")) { + inputStream = loadApolloConfig(); + } else { + inputStream = loadLocalFileConfig(); + } + if (inputStream != null) { Map configYaml = new Yaml().load(inputStream); if (configYaml != null) { @@ -100,7 +139,7 @@ public class ConfigGateway implements RouteDefinitionLocator { List defaultFiltersYaml = null; Map restfulYaml = null; Map dubboYaml = null; - + Object unknown; unknown = configYaml.get("default-filters"); if (unknown != null) { @@ -264,7 +303,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 */ @@ -294,12 +333,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); @@ -358,9 +396,9 @@ public class ConfigGateway implements RouteDefinitionLocator { /** * - * @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) { @@ -374,9 +412,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) { @@ -391,43 +429,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..bb3774e 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,6 +6,7 @@ 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 @@ -24,4 +25,11 @@ public class ConfigBean { KeyResolver ipResolver() { return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostString()); } + + @Bean + ConfigApollo configApollo() { + ConfigApollo ca = new ConfigApollo(); + + return ca; + } } \ 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 adfa478..bb2bb48 100644 --- a/usergw-service/src/main/resources/application.properties +++ b/usergw-service/src/main/resources/application.properties @@ -1,8 +1,16 @@ spring.application.name=gateway -app.id=gateway -apollo.meta=http://127.0.0.1:8180 +# 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 @@ -14,7 +22,6 @@ server.port=8888 # logging.level.org.springframework.cloud.gateway=debug logging.file=logs/log - yame.config=gateway.yaml From 430d22b680eb8d0afd5a908389745309ee27afd5 Mon Sep 17 00:00:00 2001 From: huangsimin Date: Wed, 14 Aug 2019 14:17:45 +0800 Subject: [PATCH 3/9] =?UTF-8?q?=E5=85=BC=E5=AE=B9=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E5=92=8CApollo=E6=A8=A1=E5=BC=8F.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{Change.java => ConfigRefresh.java} | 2 +- .../service/usergw/config/ConfigApollo.java | 97 +++++++++++++---- .../service/usergw/config/ConfigGateway.java | 100 +++++++++--------- .../usergw/config/bean/ConfigBean.java | 12 +-- .../src/main/resources/application.properties | 4 +- 5 files changed, 131 insertions(+), 84 deletions(-) rename usergw-service/src/main/java/cn/ecpark/service/usergw/biz/events/{Change.java => ConfigRefresh.java} (90%) diff --git a/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/events/Change.java b/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/events/ConfigRefresh.java similarity index 90% rename from usergw-service/src/main/java/cn/ecpark/service/usergw/biz/events/Change.java rename to usergw-service/src/main/java/cn/ecpark/service/usergw/biz/events/ConfigRefresh.java index 9271c54..f164f28 100644 --- a/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/events/Change.java +++ b/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/events/ConfigRefresh.java @@ -6,7 +6,7 @@ import org.springframework.context.ApplicationEventPublisherAware; import org.springframework.stereotype.Service; @Service -public class Change implements ApplicationEventPublisherAware { +public class ConfigRefresh implements ApplicationEventPublisherAware { private ApplicationEventPublisher publisher; 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 index 0bd1f1d..112600a 100644 --- 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 @@ -1,24 +1,15 @@ package cn.ecpark.service.usergw.config; -import javax.annotation.Resource; - -import org.springframework.beans.factory.annotation.Configurable; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.cloud.gateway.event.RefreshRoutesEvent; -import org.springframework.cloud.gateway.route.RouteDefinitionWriter; -import org.springframework.context.ApplicationEventPublisher; -import org.springframework.context.ApplicationEventPublisherAware; -import org.springframework.stereotype.Service; - -import lombok.extern.slf4j.Slf4j; - import com.ctrip.framework.apollo.Config; +import com.ctrip.framework.apollo.ConfigChangeListener; import com.ctrip.framework.apollo.ConfigService; -import com.ctrip.framework.apollo.model.ConfigChange; import com.ctrip.framework.apollo.model.ConfigChangeEvent; -import com.ctrip.framework.apollo.spring.annotation.ApolloConfig; -import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener; -import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.ApplicationContext; + +import cn.ecpark.service.usergw.biz.events.ConfigRefresh; @@ -26,22 +17,54 @@ import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig; // @ConditionalOnProperty(prefix="app", value="id", matchIfMissing=true) // @Configurable -public class ConfigApollo { +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; + + private int tryConnect; public ConfigApollo() { - - } - // @ApolloConfigChangeListener("gateway.yml") - private void listenApolloChange(ConfigChangeEvent changeEvent) { - + this.tryConnect = 1; } - /** + public void Connect() { + if(this.isConnected() && this.tryConnect > 0) { + this.tryConnect --; + config = ConfigService.getConfig("gateway.yml"); + 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() { @@ -55,4 +78,32 @@ public class ConfigApollo { 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 58ce415..4b92ea8 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 @@ -48,11 +48,7 @@ public class ConfigGateway implements RouteDefinitionLocator { @Value("${yame.config:}") private String yameConfigPath; - @Value("${apollo.meta:}") - private String apolloMeta; - @Value("${app.id:}") - private String appID; private List defaultFilters = new ArrayList<>(); private HashMap, Object>> specialField = new HashMap, Object>>(); @@ -76,58 +72,16 @@ public class ConfigGateway implements RouteDefinitionLocator { ignoreKey.add("filters"); } - private InputStream loadLocalFileConfig() { - Object inputStream = null; - String[] gatewayConfigPathList; - if (yameConfigPath.equals("")) { - 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; - } - } - return (InputStream) inputStream; - } - - - private InputStream loadApolloConfig() { - if (true) return null; - ConfigApollo configApollo = appContext.getBean(ConfigApollo.class); - DefaultConfig config = (DefaultConfig) configApollo.getConfig(); - try { - 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); - YmlConfigFile ymlConfigFile = (YmlConfigFile) configFile.get(cr); - String content = ymlConfigFile.getContent(); - 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; - } - @Override @SuppressWarnings("unchecked") public Flux getRouteDefinitions() { defaultFilters.clear(); InputStream inputStream; - if(!this.apolloMeta.equals("") && !this.appID.equals("")) { - inputStream = loadApolloConfig(); + + ConfigApollo configApollo = appContext.getBean(ConfigApollo.class); + configApollo.Connect(); + if(configApollo.isConnected()) { + inputStream = loadApolloConfig(configApollo); } else { inputStream = loadLocalFileConfig(); } @@ -169,13 +123,55 @@ public class ConfigGateway implements RouteDefinitionLocator { if (!routeList.isEmpty()) { return Flux.fromIterable(routeList); } - } } return Flux.empty(); } + private InputStream loadLocalFileConfig() { + Object inputStream = null; + String[] gatewayConfigPathList; + if (yameConfigPath.equals("")) { + 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; + } + } + return (InputStream) inputStream; + } + + + private InputStream loadApolloConfig(ConfigApollo configApollo) { + DefaultConfig config = (DefaultConfig) configApollo.getConfig(); + try { + 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); + YmlConfigFile ymlConfigFile = (YmlConfigFile) configFile.get(cr); + String content = ymlConfigFile.getContent(); + 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 路由列表 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 bb3774e..d2a7462 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 @@ -11,6 +11,11 @@ import reactor.core.publisher.Mono; @Component public class ConfigBean { + @Bean + ConfigApollo configApollo() { + return new ConfigApollo(); + } + @Bean public GenericServicePool genericServicePool() { return new GenericServicePool(); @@ -26,10 +31,5 @@ public class ConfigBean { return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostString()); } - @Bean - ConfigApollo configApollo() { - ConfigApollo ca = new ConfigApollo(); - - return ca; - } + } \ 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 bb2bb48..aa0ee59 100644 --- a/usergw-service/src/main/resources/application.properties +++ b/usergw-service/src/main/resources/application.properties @@ -1,8 +1,8 @@ spring.application.name=gateway -# app.id=gateway -# apollo.meta=http://localhost:8180 +app.id=gateway +apollo.meta=http://localhost:8180 # local.meta=http://localhost:8180 # dev.meta=http://localhost:8180 # fat.meta=http://localhost:8180 From bd1f4b577bf44bb22af21db576108c842946205a Mon Sep 17 00:00:00 2001 From: huangsimin Date: Wed, 14 Aug 2019 18:27:06 +0800 Subject: [PATCH 4/9] =?UTF-8?q?TODO:=20=E5=8D=95=E5=85=83=E6=B5=8B?= =?UTF-8?q?=E8=AF=95,=20test.properties,=20test-gateway,=20yameConfigPath?= =?UTF-8?q?=20=E8=B7=AF=E5=BE=84=E8=83=BD=E5=9C=A8=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=8E=AF=E5=A2=83=E6=AD=A3=E7=A1=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/ecpark/service/usergw/TestHttp2DubboConfig.java | 2 +- .../java/cn/ecpark/service/usergw}/test-gateway.yaml | 0 .../service/usergw/{application.properties => test.properties} | 0 3 files changed, 1 insertion(+), 1 deletion(-) rename usergw-service/src/{main/resources => test/java/cn/ecpark/service/usergw}/test-gateway.yaml (100%) rename usergw-service/src/test/java/cn/ecpark/service/usergw/{application.properties => test.properties} (100%) 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..609f416 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 @@ -18,7 +18,7 @@ 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 = "test.properties", properties = {"apollo.meta=", "app.id="}) public class TestHttp2DubboConfig { @LocalServerPort diff --git a/usergw-service/src/main/resources/test-gateway.yaml b/usergw-service/src/test/java/cn/ecpark/service/usergw/test-gateway.yaml similarity index 100% rename from usergw-service/src/main/resources/test-gateway.yaml rename to usergw-service/src/test/java/cn/ecpark/service/usergw/test-gateway.yaml diff --git a/usergw-service/src/test/java/cn/ecpark/service/usergw/application.properties b/usergw-service/src/test/java/cn/ecpark/service/usergw/test.properties similarity index 100% rename from usergw-service/src/test/java/cn/ecpark/service/usergw/application.properties rename to usergw-service/src/test/java/cn/ecpark/service/usergw/test.properties From e7dacaa09bd8bdffe6ba0d546acb6e0d2d962803 Mon Sep 17 00:00:00 2001 From: eson <474420502@qq.com> Date: Thu, 15 Aug 2019 04:07:32 +0800 Subject: [PATCH 5/9] =?UTF-8?q?=E9=9C=80=E8=A6=81=E7=89=B9=E6=AE=8A?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=E8=A7=A3=E5=86=B3,=20Test=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...itional-spring-configuration-metadata.json | 12 ++-- .../usergw/biz/events/ConfigRefresh.java | 3 +- .../service/usergw/config/ConfigGateway.java | 25 ++++++--- .../src/main/resources/application.properties | 6 +- .../service/usergw/TestHttp2DubboConfig.java | 4 +- .../ecpark/service/usergw/test-gateway.yaml | 51 ----------------- .../java/resources/application.properties | 13 ----- .../src/test/java/resources/gateway.yaml | 51 ----------------- .../src/test/java/resources/test-gateway.yaml | 55 ++++++++++--------- .../usergw => resources}/test.properties | 3 +- 10 files changed, 59 insertions(+), 164 deletions(-) delete mode 100644 usergw-service/src/test/java/cn/ecpark/service/usergw/test-gateway.yaml delete mode 100644 usergw-service/src/test/java/resources/application.properties delete mode 100644 usergw-service/src/test/java/resources/gateway.yaml rename usergw-service/src/test/java/{cn/ecpark/service/usergw => resources}/test.properties (91%) 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..99e2dc0 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,7 @@ -{"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" + } +]} \ No newline at end of file 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 index f164f28..e860b4c 100644 --- 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 @@ -14,10 +14,9 @@ public class ConfigRefresh implements ApplicationEventPublisherAware { 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/config/ConfigGateway.java b/usergw-service/src/main/java/cn/ecpark/service/usergw/config/ConfigGateway.java index 4b92ea8..16935b1 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,9 +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; @@ -30,6 +35,7 @@ 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; @@ -45,8 +51,8 @@ public class ConfigGateway implements RouteDefinitionLocator { // List mediaTypes = new ArrayList<>(); private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ConfigGateway.class); - @Value("${yame.config:}") - private String yameConfigPath; + @Value("${yame.gateway.config:}") + private String gatewayConfigPath; @@ -130,21 +136,24 @@ public class ConfigGateway implements RouteDefinitionLocator { } private InputStream loadLocalFileConfig() { - Object inputStream = null; + InputStream inputStream = null; String[] gatewayConfigPathList; - if (yameConfigPath.equals("")) { + if (gatewayConfigPath.equals("")) { gatewayConfigPathList = new String[] { "gateway.yml", "gateway.yaml", "Gateway.yml", "Gateway.yaml" }; } else { - gatewayConfigPathList = new String[] { yameConfigPath }; + gatewayConfigPathList = new String[] { gatewayConfigPath }; } + for (String gatewayConfigPath : gatewayConfigPathList) { gatewayConfigPath = gatewayConfigPath.trim(); inputStream = this.getClass().getClassLoader().getResourceAsStream(gatewayConfigPath); - if (inputStream != null) { - break; + if(inputStream != null) { + return inputStream; } } - return (InputStream) inputStream; + + log.error("can't find the config of gateway, like gateway.yaml"); + return inputStream; } diff --git a/usergw-service/src/main/resources/application.properties b/usergw-service/src/main/resources/application.properties index aa0ee59..b37bb8e 100644 --- a/usergw-service/src/main/resources/application.properties +++ b/usergw-service/src/main/resources/application.properties @@ -1,8 +1,8 @@ spring.application.name=gateway -app.id=gateway -apollo.meta=http://localhost:8180 +# app.id=gateway +# apollo.meta=http://localhost:8180 # local.meta=http://localhost:8180 # dev.meta=http://localhost:8180 # fat.meta=http://localhost:8180 @@ -22,7 +22,7 @@ server.port=8888 # logging.level.org.springframework.cloud.gateway=debug logging.file=logs/log -yame.config=gateway.yaml +yame.gateway.config=gateway.yaml 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 609f416..ba0b099 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,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.context.annotation.PropertySource; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringRunner; @@ -15,10 +16,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 = "test.properties", properties = {"apollo.meta=", "app.id="}) +@TestPropertySource(locations = "classpath:/resources/test.properties", properties = {"apollo.meta=", "app.id="}) public class TestHttp2DubboConfig { @LocalServerPort diff --git a/usergw-service/src/test/java/cn/ecpark/service/usergw/test-gateway.yaml b/usergw-service/src/test/java/cn/ecpark/service/usergw/test-gateway.yaml deleted file mode 100644 index 6ee7e1b..0000000 --- a/usergw-service/src/test/java/cn/ecpark/service/usergw/test-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+ - -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 - - - id: test2 - 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/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 index 39bd62a..6ee7e1b 100644 --- a/usergw-service/src/test/java/resources/test-gateway.yaml +++ b/usergw-service/src/test/java/resources/test-gateway.yaml @@ -1,44 +1,26 @@ 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 + - Path=/get + - Header=XX, \d+ dubbo: routes: - id: test order: 0 application: dubbo-exchange - methods: # 如果没填就从 request拿 意味着所有接口都可以使用 - - name: Say - param-types: - - java.lang.String - - name: Hello + # methods: # 如果没填就从 request拿 意味着所有接口都可以使用 + + # - name: Say + # param-types: + # - java.lang.String + # - name: Hello connections: 4 group: test @@ -47,4 +29,23 @@ dubbo: version: 1.0.0 predicates: - Path=/dubbo/hello + + - id: test2 + 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/test.properties b/usergw-service/src/test/java/resources/test.properties similarity index 91% rename from usergw-service/src/test/java/cn/ecpark/service/usergw/test.properties rename to usergw-service/src/test/java/resources/test.properties index 31e134b..905c6b3 100644 --- a/usergw-service/src/test/java/cn/ecpark/service/usergw/test.properties +++ b/usergw-service/src/test/java/resources/test.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 From b5a19c78cf57c153b18ee8a254ac4b0a646bdb26 Mon Sep 17 00:00:00 2001 From: huangsimin Date: Thu, 15 Aug 2019 17:42:04 +0800 Subject: [PATCH 6/9] =?UTF-8?q?add:=20=E4=BF=AE=E6=94=B9Pom=20Assembly?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE,=20=20=E5=AE=8C=E7=BE=8E=E6=89=93=E5=8C=85sc?= =?UTF-8?q?ripts.=20fix:=20test=E7=9A=84=E8=AE=BE=E7=BD=AE,=20=20=E4=BD=BF?= =?UTF-8?q?unittest=E5=8F=AF=E4=BB=A5=E6=A0=B9=E6=8D=AE=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E5=8F=98=E9=87=8F=E5=90=AF=E5=8A=A8=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 10 ++-- usergw-service/pom.xml | 57 +++++++++++++------ usergw-service/src/main/assembly/assembly.xml | 45 +++++++++++++++ .../src/main/assembly/scripts/run.bat | 10 ++++ .../src/main/assembly/scripts/run.sh | 8 +++ .../java => main}/resources/test-gateway.yaml | 0 .../service/usergw/TestHttp2DubboConfig.java | 2 +- .../service/usergw/TestHttp2DubboConfig2.java | 4 +- .../service/usergw/TestHttp2DubboConfig3.java | 4 +- .../service/usergw/TestHttp2DubboConfig4.java | 2 +- .../{test.properties => test1.properties} | 0 .../test2.properties} | 2 +- .../test3.properties} | 2 +- .../test4.properties} | 2 +- 14 files changed, 116 insertions(+), 32 deletions(-) create mode 100644 usergw-service/src/main/assembly/assembly.xml create mode 100644 usergw-service/src/main/assembly/scripts/run.bat create mode 100644 usergw-service/src/main/assembly/scripts/run.sh rename usergw-service/src/{test/java => main}/resources/test-gateway.yaml (100%) rename usergw-service/src/test/java/resources/{test.properties => test1.properties} (100%) rename usergw-service/src/test/java/{cn/ecpark/service/usergw/application2.properties => resources/test2.properties} (91%) rename usergw-service/src/test/java/{cn/ecpark/service/usergw/application3.properties => resources/test3.properties} (91%) rename usergw-service/src/test/java/{cn/ecpark/service/usergw/application4.properties => resources/test4.properties} (92%) 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 44ae320..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,6 @@ fastjson 1.2.58 - - - cn.ecpark.service - usergw-api - ${project.parent.version} - com.ctrip.framework.apollo @@ -91,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 @@ -129,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/test/java/resources/test-gateway.yaml b/usergw-service/src/main/resources/test-gateway.yaml similarity index 100% rename from usergw-service/src/test/java/resources/test-gateway.yaml rename to usergw-service/src/main/resources/test-gateway.yaml 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 ba0b099..1877ac6 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 @@ -18,7 +18,7 @@ import reactor.netty.http.client.HttpClientResponse; @SpringBootTest(classes= App.class, webEnvironment = WebEnvironment.RANDOM_PORT) @RunWith(SpringRunner.class) -@TestPropertySource(locations = "classpath:/resources/test.properties", properties = {"apollo.meta=", "app.id="}) +@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..967eaa0 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 @@ -13,10 +13,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..787d1a1 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 @@ -17,10 +17,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..9d2786e 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 @@ -16,7 +16,7 @@ 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 diff --git a/usergw-service/src/test/java/resources/test.properties b/usergw-service/src/test/java/resources/test1.properties similarity index 100% rename from usergw-service/src/test/java/resources/test.properties rename to usergw-service/src/test/java/resources/test1.properties 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 From 866c7b4850ee1b024b458e2543c89fda0bff8e9c Mon Sep 17 00:00:00 2001 From: huangsimin Date: Thu, 15 Aug 2019 18:28:50 +0800 Subject: [PATCH 7/9] =?UTF-8?q?TODO:=20=E6=B7=BB=E5=8A=A0Apollo=E7=9A=84?= =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/usergw/TestHttp2DubboConfig5.java | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig5.java 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..c69bf3b --- /dev/null +++ b/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig5.java @@ -0,0 +1,59 @@ +package cn.ecpark.service.usergw; + +import org.junit.Assert; +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.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/test4.properties", properties = {"apollo.meta=", "app.id="}) +public class TestHttp2DubboConfig5 { + + @LocalServerPort + private int serverPort; + + @Test + public void Test1RequestHttp2DubboSetHeader() { + // Test Base Url + for(int i = 0; i < 50;i++) { + HttpClient client = HttpClient.create(); + ResponseReceiver receiver; + String content; + + receiver = client.baseUrl("http://localhost:" + serverPort + "/dubbo/hello") + .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(content, "Hello Dubbo"); + } else { + Assert.assertEquals(response.status().code(), 429); + } + } + + } + + + // @Test + // @SuppressWarnings("unchecked") + // public void Test3RestfulFilters() { + + + // } + + +} \ No newline at end of file From d97b088117761bbe04ed9a166925cb40ebc0edea Mon Sep 17 00:00:00 2001 From: huangsimin Date: Fri, 16 Aug 2019 18:46:34 +0800 Subject: [PATCH 8/9] =?UTF-8?q?=E6=94=B9=E4=BA=86=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95.=20=E5=8F=AF=E4=BB=A5=E6=AD=A3=E5=BC=8F?= =?UTF-8?q?=E4=B8=8A=E7=BA=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...itional-spring-configuration-metadata.json | 5 ++ .../biz/filters/bean/GenericServicePool.java | 43 +++++++++++-- .../service/usergw/config/ConfigApollo.java | 51 +++++++++++---- .../service/usergw/config/ConfigGateway.java | 28 ++++++--- .../usergw/config/bean/ConfigBean.java | 15 +++-- .../src/main/resources/application.properties | 6 +- .../src/main/resources/test-gateway4.yaml | 4 +- .../src/main/resources/test-gateway5.yaml | 19 ++++++ .../service/usergw/TestHttp2DubboConfig.java | 1 + .../service/usergw/TestHttp2DubboConfig2.java | 1 + .../service/usergw/TestHttp2DubboConfig3.java | 1 + .../service/usergw/TestHttp2DubboConfig4.java | 17 +++-- .../service/usergw/TestHttp2DubboConfig5.java | 63 +++++++++---------- .../service/usergw/TestHttp2DubboConfig6.java | 59 +++++++++++++++++ .../src/test/java/resources/test5.properties | 33 ++++++++++ 15 files changed, 264 insertions(+), 82 deletions(-) create mode 100644 usergw-service/src/main/resources/test-gateway5.yaml create mode 100644 usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig6.java create mode 100644 usergw-service/src/test/java/resources/test5.properties 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 99e2dc0..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 @@ -3,5 +3,10 @@ "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/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 index 112600a..ee7eb53 100644 --- 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 @@ -3,23 +3,25 @@ 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); + + private static final org.slf4j.Logger log = + org.slf4j.LoggerFactory.getLogger(ConfigApollo.class); // @ApolloConfig("gateway.yml") private Config config; @@ -28,10 +30,13 @@ public class ConfigApollo implements ConfigChangeListener { @Value("${apollo.meta:}") private String apolloMeta; - + @Value("${app.id:}") private String appID; + @Value("${apollo.namespace:}") + private String apolloNamespace; + private int tryConnect; public ConfigApollo() { @@ -39,15 +44,37 @@ public class ConfigApollo implements ConfigChangeListener { } public void Connect() { - if(this.isConnected() && this.tryConnect > 0) { - this.tryConnect --; - config = ConfigService.getConfig("gateway.yml"); - config.addChangeListener(this); + 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("") ) { + if (!this.appID.equals("") && !this.apolloMeta.equals("")) { return true; } return false; @@ -55,16 +82,16 @@ public class ConfigApollo implements ConfigChangeListener { // @ApolloConfigChangeListener("gateway.yml") // public void listenApolloChange(ConfigChangeEvent changeEvent) { - + // } @Override public void onChange(ConfigChangeEvent changeEvent) { - ConfigRefresh change = (ConfigRefresh)appContext.getBean(ConfigRefresh.class); + ConfigRefresh change = (ConfigRefresh) appContext.getBean(ConfigRefresh.class); change.notifyChanged(); } - /** + /** * @return Config return the config */ public Config getConfig() { 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 16935b1..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 @@ -22,6 +22,7 @@ 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; @@ -160,15 +161,22 @@ public class ConfigGateway implements RouteDefinitionLocator { private InputStream loadApolloConfig(ConfigApollo configApollo) { DefaultConfig config = (DefaultConfig) configApollo.getConfig(); try { - 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); - YmlConfigFile ymlConfigFile = (YmlConfigFile) configFile.get(cr); - String content = ymlConfigFile.getContent(); - log.info("Load Apollo YAML Config: \n{}", content); - return new ByteArrayInputStream(content.getBytes()); + 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) { @@ -394,7 +402,7 @@ 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; } 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 d2a7462..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 @@ -11,9 +11,10 @@ import reactor.core.publisher.Mono; @Component public class ConfigBean { + @Bean - ConfigApollo configApollo() { - return new ConfigApollo(); + KeyResolver ipResolver() { + return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostString()); } @Bean @@ -22,14 +23,12 @@ public class ConfigBean { } @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 b37bb8e..683b2c2 100644 --- a/usergw-service/src/main/resources/application.properties +++ b/usergw-service/src/main/resources/application.properties @@ -1,8 +1,8 @@ spring.application.name=gateway - -# app.id=gateway -# apollo.meta=http://localhost:8180 +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 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..9a3633d --- /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/hello + \ 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 1877ac6..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 @@ -7,6 +7,7 @@ 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; 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 967eaa0..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; 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 787d1a1..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; 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 9d2786e..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; @@ -23,7 +29,7 @@ public class TestHttp2DubboConfig4 { 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 index c69bf3b..22376ff 100644 --- a/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig5.java +++ b/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig5.java @@ -1,11 +1,13 @@ 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; @@ -16,44 +18,41 @@ 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/test4.properties", properties = {"apollo.meta=", "app.id="}) +@TestPropertySource(locations = "classpath:/resources/test5.properties") +@Ignore public class TestHttp2DubboConfig5 { @LocalServerPort private int serverPort; @Test - public void Test1RequestHttp2DubboSetHeader() { + public void Test1LoadApolloConfig() { // Test Base Url - for(int i = 0; i < 50;i++) { - HttpClient client = HttpClient.create(); - ResponseReceiver receiver; - String content; - - receiver = client.baseUrl("http://localhost:" + serverPort + "/dubbo/hello") - .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(content, "Hello Dubbo"); - } else { - Assert.assertEquals(response.status().code(), 429); - } + + 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()); } - - } - - - // @Test - // @SuppressWarnings("unchecked") - // public void Test3RestfulFilters() { - + - // } - - -} \ No newline at end of file + 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/test5.properties b/usergw-service/src/test/java/resources/test5.properties new file mode 100644 index 0000000..c513743 --- /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=1000 + +server.port=8888 + +logging.file=logs/log +yame.gateway.config=test-gateway5.yaml + + + + + From f98dafd921e3889d4c55e4175263b94853b3f495 Mon Sep 17 00:00:00 2001 From: eson <474420502@qq.com> Date: Sat, 17 Aug 2019 02:46:15 +0800 Subject: [PATCH 9/9] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=9A=84test-gateway5.yaml=E5=B1=9E=E6=80=A7,=20=E6=96=B9?= =?UTF-8?q?=E4=BE=BF=E6=B7=BB=E5=8A=A0=E5=88=B0apollo=E4=B8=8A.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- usergw-service/src/main/resources/test-gateway5.yaml | 2 +- usergw-service/src/test/java/resources/test5.properties | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/usergw-service/src/main/resources/test-gateway5.yaml b/usergw-service/src/main/resources/test-gateway5.yaml index 9a3633d..54320bd 100644 --- a/usergw-service/src/main/resources/test-gateway5.yaml +++ b/usergw-service/src/main/resources/test-gateway5.yaml @@ -15,5 +15,5 @@ dubbo: interface: ocean.demo.api.IExchange version: 1.0.0 predicates: - - Path=/dubbo/hello + - Path=/dubbo/gateway/test5 \ No newline at end of file diff --git a/usergw-service/src/test/java/resources/test5.properties b/usergw-service/src/test/java/resources/test5.properties index c513743..b70354d 100644 --- a/usergw-service/src/test/java/resources/test5.properties +++ b/usergw-service/src/test/java/resources/test5.properties @@ -20,7 +20,7 @@ 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=1000 +dubbo.consumer.timeout=5000 server.port=8888