mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
增加 Zuul 示例
This commit is contained in:
@@ -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
|
||||
|
||||
83
labx-21/labx-21-sc-zuul-demo03-config-apollo/pom.xml
Normal file
83
labx-21/labx-21-sc-zuul-demo03-config-apollo/pom.xml
Normal file
@@ -0,0 +1,83 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>labx-21</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-21-sc-zuul-demo03-config-apollo</artifactId>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>6</source>
|
||||
<target>6</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
|
||||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
|
||||
<spring.cloud.alibaba.version>2.2.0.RELEASE</spring.cloud.alibaba.version>
|
||||
</properties>
|
||||
|
||||
<!--
|
||||
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
|
||||
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
|
||||
-->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring.cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
||||
<version>${spring.cloud.alibaba.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入 Spring Cloud Zuul 相关依赖,使用它作为网关,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 Spring Cloud Alibaba Nacos Discovery 相关依赖,将 Nacos 作为注册中心,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 Apollo 客户端,内置对 Apollo 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>com.ctrip.framework.apollo</groupId>
|
||||
<artifactId>apollo-client</artifactId>
|
||||
<version>1.5.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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!");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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。
|
||||
@@ -14,6 +14,8 @@
|
||||
<modules>
|
||||
<module>labx-21-sc-zuul-demo01</module>
|
||||
<module>labx-21-sc-zuul-demo02-registry</module>
|
||||
<module>labx-21-sc-zuul-demo03-config-apollo</module>
|
||||
|
||||
<module>labx-21-sc-user-service</module>
|
||||
</modules>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user