From b89422dff4249e0ce7b50a687a18fe55c392d00f Mon Sep 17 00:00:00 2001 From: YunaiV <> Date: Sun, 3 May 2020 21:17:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20Zuul=20=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/application.yaml | 6 ++ .../pom.xml | 83 +++++++++++++++++++ .../labx21/zuuldemo/ZuulApplication.java | 15 ++++ .../zuuldemo/ZuulPropertiesRefresher.java | 51 ++++++++++++ .../src/main/resources/application.yaml | 23 +++++ labx-21/pom.xml | 2 + 6 files changed, 180 insertions(+) create mode 100644 labx-21/labx-21-sc-zuul-demo03-config-apollo/pom.xml create mode 100644 labx-21/labx-21-sc-zuul-demo03-config-apollo/src/main/java/cn/iocoder/springcloud/labx21/zuuldemo/ZuulApplication.java create mode 100644 labx-21/labx-21-sc-zuul-demo03-config-apollo/src/main/java/cn/iocoder/springcloud/labx21/zuuldemo/ZuulPropertiesRefresher.java create mode 100644 labx-21/labx-21-sc-zuul-demo03-config-apollo/src/main/resources/application.yaml diff --git a/labx-21/labx-21-sc-zuul-demo02-registry/src/main/resources/application.yaml b/labx-21/labx-21-sc-zuul-demo02-registry/src/main/resources/application.yaml index 91d2b8db..0f459cf7 100644 --- a/labx-21/labx-21-sc-zuul-demo02-registry/src/main/resources/application.yaml +++ b/labx-21/labx-21-sc-zuul-demo02-registry/src/main/resources/application.yaml @@ -5,6 +5,12 @@ spring: application: name: zuul-application + cloud: + nacos: + # Nacos 作为注册中心的配置项 + discovery: + server-addr: 127.0.0.1:8848 # Nacos 服务器地址 + # Zuul 配置项,对应 ZuulProperties 配置类 zuul: servlet-path: / # ZuulServlet 匹配的路径,默认为 /zuul diff --git a/labx-21/labx-21-sc-zuul-demo03-config-apollo/pom.xml b/labx-21/labx-21-sc-zuul-demo03-config-apollo/pom.xml new file mode 100644 index 00000000..6bd53c0c --- /dev/null +++ b/labx-21/labx-21-sc-zuul-demo03-config-apollo/pom.xml @@ -0,0 +1,83 @@ + + + + labx-21 + cn.iocoder.springboot.labs + 1.0-SNAPSHOT + + 4.0.0 + + labx-21-sc-zuul-demo03-config-apollo + + + + org.apache.maven.plugins + maven-compiler-plugin + + 6 + 6 + + + + + + + 2.2.4.RELEASE + Hoxton.SR1 + 2.2.0.RELEASE + + + + + + + org.springframework.boot + spring-boot-starter-parent + ${spring.boot.version} + pom + import + + + org.springframework.cloud + spring-cloud-dependencies + ${spring.cloud.version} + pom + import + + + com.alibaba.cloud + spring-cloud-alibaba-dependencies + ${spring.cloud.alibaba.version} + pom + import + + + + + + + + org.springframework.cloud + spring-cloud-starter-netflix-zuul + + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + + + + + com.ctrip.framework.apollo + apollo-client + 1.5.1 + + + + diff --git a/labx-21/labx-21-sc-zuul-demo03-config-apollo/src/main/java/cn/iocoder/springcloud/labx21/zuuldemo/ZuulApplication.java b/labx-21/labx-21-sc-zuul-demo03-config-apollo/src/main/java/cn/iocoder/springcloud/labx21/zuuldemo/ZuulApplication.java new file mode 100644 index 00000000..e9f0fce2 --- /dev/null +++ b/labx-21/labx-21-sc-zuul-demo03-config-apollo/src/main/java/cn/iocoder/springcloud/labx21/zuuldemo/ZuulApplication.java @@ -0,0 +1,15 @@ +package cn.iocoder.springcloud.labx21.zuuldemo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.zuul.EnableZuulProxy; + +@SpringBootApplication +@EnableZuulProxy // 开启 Zuul 网关 +public class ZuulApplication { + + public static void main(String[] args) { + SpringApplication.run(ZuulApplication.class, args); + } + +} diff --git a/labx-21/labx-21-sc-zuul-demo03-config-apollo/src/main/java/cn/iocoder/springcloud/labx21/zuuldemo/ZuulPropertiesRefresher.java b/labx-21/labx-21-sc-zuul-demo03-config-apollo/src/main/java/cn/iocoder/springcloud/labx21/zuuldemo/ZuulPropertiesRefresher.java new file mode 100644 index 00000000..558ebe84 --- /dev/null +++ b/labx-21/labx-21-sc-zuul-demo03-config-apollo/src/main/java/cn/iocoder/springcloud/labx21/zuuldemo/ZuulPropertiesRefresher.java @@ -0,0 +1,51 @@ +package cn.iocoder.springcloud.labx21.zuuldemo; + +import com.ctrip.framework.apollo.model.ConfigChangeEvent; +import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.context.environment.EnvironmentChangeEvent; +import org.springframework.cloud.netflix.zuul.RoutesRefreshedEvent; +import org.springframework.cloud.netflix.zuul.filters.RouteLocator; +import org.springframework.context.ApplicationContext; +import org.springframework.stereotype.Component; + +/** + * 由 https://github.com/ctripcorp/apollo-use-cases/blob/master/spring-cloud-zuul 提供代码,感谢~ + */ +@Component +public class ZuulPropertiesRefresher { + + private static final Logger logger = LoggerFactory.getLogger(ZuulPropertiesRefresher.class); + + @Autowired + private ApplicationContext applicationContext; + + @Autowired + private RouteLocator routeLocator; + + @ApolloConfigChangeListener(interestedKeyPrefixes = "zuul.") + public void onChange(ConfigChangeEvent changeEvent) { + refreshZuulProperties(changeEvent); + } + + private void refreshZuulProperties(ConfigChangeEvent changeEvent) { + logger.info("Refreshing zuul properties!"); + + /* + * rebind configuration beans, e.g. ZuulProperties + * @see org.springframework.cloud.context.properties.ConfigurationPropertiesRebinder#onApplicationEvent + */ + this.applicationContext.publishEvent(new EnvironmentChangeEvent(changeEvent.changedKeys())); + + /* + * refresh routes + * @see org.springframework.cloud.netflix.zuul.ZuulServerAutoConfiguration.ZuulRefreshListener#onApplicationEvent + */ + this.applicationContext.publishEvent(new RoutesRefreshedEvent(routeLocator)); + + logger.info("Zuul properties refreshed!"); + } + +} diff --git a/labx-21/labx-21-sc-zuul-demo03-config-apollo/src/main/resources/application.yaml b/labx-21/labx-21-sc-zuul-demo03-config-apollo/src/main/resources/application.yaml new file mode 100644 index 00000000..871cb3e5 --- /dev/null +++ b/labx-21/labx-21-sc-zuul-demo03-config-apollo/src/main/resources/application.yaml @@ -0,0 +1,23 @@ +server: + port: 8888 + +spring: + application: + name: zuul-application + + cloud: + nacos: + # Nacos 作为注册中心的配置项 + discovery: + server-addr: 127.0.0.1:8848 # Nacos 服务器地址 + +# Apollo 相关配置项 +app: + id: ${spring.application.name} # 使用的 Apollo 的项目(应用)编号 +apollo: + meta: http://127.0.0.1:8080 # Apollo Meta Server 地址 + bootstrap: + enabled: true # 是否开启 Apollo 配置预加载功能。默认为 false。 + eagerLoad: + enable: true # 是否开启 Apollo 支持日志级别的加载时机。默认为 false。 + namespaces: application # 使用的 Apollo 的命名空间,默认为 application。 diff --git a/labx-21/pom.xml b/labx-21/pom.xml index 4fc32f46..e6cd05a1 100644 --- a/labx-21/pom.xml +++ b/labx-21/pom.xml @@ -14,6 +14,8 @@ labx-21-sc-zuul-demo01 labx-21-sc-zuul-demo02-registry + labx-21-sc-zuul-demo03-config-apollo + labx-21-sc-user-service