From 9734ee1826fb986005ee06bcf38b2310a4256a5d Mon Sep 17 00:00:00 2001 From: huangsimin Date: Mon, 12 Aug 2019 18:49:05 +0800 Subject: [PATCH] =?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 + +