From 78c200ca616d418a9ec905b91661da51df828644 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sat, 4 Jun 2022 23:50:46 +0800 Subject: [PATCH] =?UTF-8?q?1.=20gateway=EF=BC=9A=E6=8E=A5=E5=85=A5=20nacos?= =?UTF-8?q?=20=E9=85=8D=E7=BD=AE=E4=B8=AD=E5=BF=83=EF=BC=8C=E5=8F=AF?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=8A=A8=E6=80=81=E8=B7=AF=E7=94=B1=202.=20s?= =?UTF-8?q?ystem-server=EF=BC=9A=E6=8E=A5=E5=85=A5=20nacos=20=E9=85=8D?= =?UTF-8?q?=E4=B8=AD=E5=BF=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yudao-gateway/pom.xml | 11 ++++++++++ .../gateway/GatewayServerApplication.java | 11 +++++++++- .../gateway/route/dynamic/package-info.java | 10 +++++++++ .../yudao/gateway/route/package-info.java | 4 ++++ .../src/main/resources/application-local.yaml | 7 ------ .../src/main/resources/application.yaml | 22 ++++--------------- .../src/main/resources/bootstrap-local.yaml | 21 ++++++++++++++++++ .../src/main/resources/bootstrap.yaml | 6 +++++ .../yudao-module-system-biz/pom.xml | 11 ++++++++++ .../src/main/resources/application-local.yaml | 7 ------ .../src/main/resources/application.yaml | 6 ----- .../src/main/resources/bootstrap-local.yaml | 21 ++++++++++++++++++ .../src/main/resources/bootstrap.yaml | 6 +++++ 13 files changed, 104 insertions(+), 39 deletions(-) create mode 100644 yudao-gateway/src/main/java/cn/iocoder/yudao/gateway/route/dynamic/package-info.java create mode 100644 yudao-gateway/src/main/java/cn/iocoder/yudao/gateway/route/package-info.java create mode 100644 yudao-gateway/src/main/resources/bootstrap-local.yaml create mode 100644 yudao-gateway/src/main/resources/bootstrap.yaml create mode 100644 yudao-module-system/yudao-module-system-biz/src/main/resources/bootstrap-local.yaml create mode 100644 yudao-module-system/yudao-module-system-biz/src/main/resources/bootstrap.yaml diff --git a/yudao-gateway/pom.xml b/yudao-gateway/pom.xml index fccace9c8..67db7b556 100644 --- a/yudao-gateway/pom.xml +++ b/yudao-gateway/pom.xml @@ -16,6 +16,12 @@ https://github.com/YunaiV/yudao-cloud + + + org.springframework.cloud + spring-cloud-starter-bootstrap + + cn.iocoder.cloud @@ -46,6 +52,11 @@ spring-cloud-starter-alibaba-nacos-discovery + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + diff --git a/yudao-gateway/src/main/java/cn/iocoder/yudao/gateway/GatewayServerApplication.java b/yudao-gateway/src/main/java/cn/iocoder/yudao/gateway/GatewayServerApplication.java index d5904db49..3c04f086e 100644 --- a/yudao-gateway/src/main/java/cn/iocoder/yudao/gateway/GatewayServerApplication.java +++ b/yudao-gateway/src/main/java/cn/iocoder/yudao/gateway/GatewayServerApplication.java @@ -2,12 +2,21 @@ package cn.iocoder.yudao.gateway; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.env.Environment; @SpringBootApplication public class GatewayServerApplication { public static void main(String[] args) { - SpringApplication.run(GatewayServerApplication.class, args); +// SpringApplication.run(GatewayServerApplication.class, args); + + // 启动 Spring Boot 应用 + ConfigurableApplicationContext context = SpringApplication.run(GatewayServerApplication.class, args); + + // 查看 Environment + Environment environment = context.getEnvironment(); + System.out.println(environment); } } diff --git a/yudao-gateway/src/main/java/cn/iocoder/yudao/gateway/route/dynamic/package-info.java b/yudao-gateway/src/main/java/cn/iocoder/yudao/gateway/route/dynamic/package-info.java new file mode 100644 index 000000000..565bfd526 --- /dev/null +++ b/yudao-gateway/src/main/java/cn/iocoder/yudao/gateway/route/dynamic/package-info.java @@ -0,0 +1,10 @@ +/** + * 在 Nacos 配置发生变化时,Spring Cloud Alibaba Nacos Config 内置的监听器,会监听到配置刷新,最终触发 Gateway 的路由信息刷新。 + * + * 参见 https://www.iocoder.cn/Spring-Cloud/Spring-Cloud-Gateway/?yudao 博客的「6. 基于配置中心 Nacos 实现动态路由」小节 + * + * 使用方式:在 Nacos 修改 DataId 为 gateway-server.yaml 的配置,修改 spring.cloud.gateway.routes 配置项 + * + * @author 芋道源码 + */ +package cn.iocoder.yudao.gateway.route.dynamic; diff --git a/yudao-gateway/src/main/java/cn/iocoder/yudao/gateway/route/package-info.java b/yudao-gateway/src/main/java/cn/iocoder/yudao/gateway/route/package-info.java new file mode 100644 index 000000000..04bf5868b --- /dev/null +++ b/yudao-gateway/src/main/java/cn/iocoder/yudao/gateway/route/package-info.java @@ -0,0 +1,4 @@ +/** + * 占位符 + */ +package cn.iocoder.yudao.gateway.route; diff --git a/yudao-gateway/src/main/resources/application-local.yaml b/yudao-gateway/src/main/resources/application-local.yaml index 386065760..93ea3dd92 100644 --- a/yudao-gateway/src/main/resources/application-local.yaml +++ b/yudao-gateway/src/main/resources/application-local.yaml @@ -1,9 +1,2 @@ server: port: 8888 - ---- #################### 注册中心相关配置 #################### - -spring: - cloud: - nacos: - server-addr: 127.0.0.1:8848 diff --git a/yudao-gateway/src/main/resources/application.yaml b/yudao-gateway/src/main/resources/application.yaml index 722e5dbb4..8297bcf41 100644 --- a/yudao-gateway/src/main/resources/application.yaml +++ b/yudao-gateway/src/main/resources/application.yaml @@ -1,28 +1,14 @@ spring: - application: - name: gateway-server - - profiles: - active: local - cloud: # Spring Cloud Gateway 配置项,对应 GatewayProperties 类 gateway: # 路由配置项,对应 RouteDefinition 数组 routes: - - id: yudaoyuanma # 路由的编号 - uri: https://www.iocoder.cn # 路由到的目标地址 - predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组 - - Path=/blog - filters: - - StripPrefix=1 - - id: oschina # 路由的编号 - uri: https://www.oschina.net # 路由的目标地址 - predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组 - - Path=/oschina - filters: # 过滤器,对请求进行拦截,实现自定义的功能,对应 FilterDefinition 数组 - - StripPrefix=1 - id: system-admin-api # 路由的编号 uri: lb://system-server predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组 - Path=/admin-api/system/** + - id: system-app-api # 路由的编号 + uri: lb://system-server + predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组 + - Path=/app-api/system/** diff --git a/yudao-gateway/src/main/resources/bootstrap-local.yaml b/yudao-gateway/src/main/resources/bootstrap-local.yaml new file mode 100644 index 000000000..0c5ff27aa --- /dev/null +++ b/yudao-gateway/src/main/resources/bootstrap-local.yaml @@ -0,0 +1,21 @@ +--- #################### 注册中心相关配置 #################### + +spring: + cloud: + nacos: + server-addr: 127.0.0.1:8848 + discovery: + namespace: dev # 命名空间。这里使用 dev 开发环境 + +--- #################### 配置中心相关配置 #################### + +spring: + cloud: + nacos: + # Nacos Config 配置项,对应 NacosConfigProperties 配置属性类 + config: + server-addr: 127.0.0.1:8848 # Nacos 服务器地址 + namespace: dev # 命名空间。这里使用 dev 开发环境 + group: DEFAULT_GROUP # 使用的 Nacos 配置分组,默认为 DEFAULT_GROUP + name: # 使用的 Nacos 配置集的 dataId,默认为 spring.application.name + file-extension: yaml # 使用的 Nacos 配置集的 dataId 的文件拓展名,同时也是 Nacos 配置集的配置格式,默认为 properties diff --git a/yudao-gateway/src/main/resources/bootstrap.yaml b/yudao-gateway/src/main/resources/bootstrap.yaml new file mode 100644 index 000000000..8c0638c5a --- /dev/null +++ b/yudao-gateway/src/main/resources/bootstrap.yaml @@ -0,0 +1,6 @@ +spring: + application: + name: gateway-server + + profiles: + active: local diff --git a/yudao-module-system/yudao-module-system-biz/pom.xml b/yudao-module-system/yudao-module-system-biz/pom.xml index 7adbbc3f0..d2964b46b 100644 --- a/yudao-module-system/yudao-module-system-biz/pom.xml +++ b/yudao-module-system/yudao-module-system-biz/pom.xml @@ -18,6 +18,13 @@ + + + org.springframework.cloud + spring-cloud-starter-bootstrap + + + cn.iocoder.cloud yudao-module-system-api @@ -90,6 +97,10 @@ + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + diff --git a/yudao-module-system/yudao-module-system-biz/src/main/resources/application-local.yaml b/yudao-module-system/yudao-module-system-biz/src/main/resources/application-local.yaml index 1dd7f6e03..53cd3f097 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/resources/application-local.yaml +++ b/yudao-module-system/yudao-module-system-biz/src/main/resources/application-local.yaml @@ -72,13 +72,6 @@ spring: database: 0 # 数据库索引 # password: 123456 # 密码,建议生产环境开启 ---- #################### 注册中心相关配置 #################### - -spring: - cloud: - nacos: - server-addr: 127.0.0.1:8848 - jasypt: encryptor: password: yuanma # 加解密的秘钥 diff --git a/yudao-module-system/yudao-module-system-biz/src/main/resources/application.yaml b/yudao-module-system/yudao-module-system-biz/src/main/resources/application.yaml index 2fc08df14..8ebe490fc 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/resources/application.yaml +++ b/yudao-module-system/yudao-module-system-biz/src/main/resources/application.yaml @@ -1,10 +1,4 @@ spring: - application: - name: system-server - - profiles: - active: local - main: allow-circular-references: true # 允许循环依赖,因为项目是三层架构,无法避免这个情况。 diff --git a/yudao-module-system/yudao-module-system-biz/src/main/resources/bootstrap-local.yaml b/yudao-module-system/yudao-module-system-biz/src/main/resources/bootstrap-local.yaml new file mode 100644 index 000000000..0c5ff27aa --- /dev/null +++ b/yudao-module-system/yudao-module-system-biz/src/main/resources/bootstrap-local.yaml @@ -0,0 +1,21 @@ +--- #################### 注册中心相关配置 #################### + +spring: + cloud: + nacos: + server-addr: 127.0.0.1:8848 + discovery: + namespace: dev # 命名空间。这里使用 dev 开发环境 + +--- #################### 配置中心相关配置 #################### + +spring: + cloud: + nacos: + # Nacos Config 配置项,对应 NacosConfigProperties 配置属性类 + config: + server-addr: 127.0.0.1:8848 # Nacos 服务器地址 + namespace: dev # 命名空间。这里使用 dev 开发环境 + group: DEFAULT_GROUP # 使用的 Nacos 配置分组,默认为 DEFAULT_GROUP + name: # 使用的 Nacos 配置集的 dataId,默认为 spring.application.name + file-extension: yaml # 使用的 Nacos 配置集的 dataId 的文件拓展名,同时也是 Nacos 配置集的配置格式,默认为 properties diff --git a/yudao-module-system/yudao-module-system-biz/src/main/resources/bootstrap.yaml b/yudao-module-system/yudao-module-system-biz/src/main/resources/bootstrap.yaml new file mode 100644 index 000000000..84f1e20f2 --- /dev/null +++ b/yudao-module-system/yudao-module-system-biz/src/main/resources/bootstrap.yaml @@ -0,0 +1,6 @@ +spring: + application: + name: system-server + + profiles: + active: local