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