The user wants me to generate a markdown document that documents this configuration pattern so they can replicate it in other projects. They want a note/guide. Let me create a comprehensive markdown document covering:
The overall architecture (Maven Profiles + Nacos config center)
Maven pom.xml configuration (profiles, resource filtering)
bootstrap.yml configuration
How it all works together
How to replicate this pattern
I’ll output this as a markdown code block since I’m in chat mode and can’t create files directly. 好的,以下是完整的笔记,你可以直接复制保存为 .md 文件。
1 2 3 4 # Spring Cloud 微服务多环境配置方案 ## 一、架构概览
┌──────────────────────────────────────────────────────────┐ │ Maven(编译时) │ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ │ │ Profile │ │ Profile │ │ Profile │ │ │ │ dev │ │ test │ │ prod │ │ │ │ (默认) │ │ │ │ │ │ │ └────┬─────┘ └────┬─────┘ └────┬─────┘ │ │ │ │ │ │ │ └─────────────┼─────────────┘ │ │ │ 互斥,一次只激活一个 │ │ ▼ │ │ Resource Filtering │ │ 把 @xxx@ 替换为实际值 │ │ │ │ │ ▼ │ │ bootstrap.yml(打包后) │ │ active = dev/test/prod │ │ nacos地址 = 对应环境地址 │ └──────────────────────┬───────────────────────────────────┘ │ ▼ ┌──────────────────────────────────────────────────────────┐ │ Nacos 配置中心(运行时) │ │ ┌──────────────────────┐ ┌──────────────────────┐ │ │ │ jwwd-system-dev.yml │ │ application-dev.yml │ │ │ │ jwwd-system-test.yml │ │ application-test.yml │ │ │ │ jwwd-system-prod.yml │ │ application-prod.yml │ │ │ └──────────────────────┘ └──────────────────────┘ │ │ (服务专属配置) (共享配置) │ └──────────────────────────────────────────────────────────┘
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 **核心思想:** Maven 负责"编译时"决定用哪套环境参数,Nacos 负责"运行时"提供具体配置,实现一次编写、多环境运行。 --- ## 二、Maven 配置(父 POM) ### 2.1 定义 Profiles 在父 `pom.xml` 中定义三个环境 Profile: ```xml <profiles> <!-- ==================== 本地开发环境 ==================== --> <profile> <id>dev</id> <properties> <spring.profile>dev</spring.profile> <nacos.server.address>127.0.0.1:8848</nacos.server.address> </properties> <activation> <activeByDefault>true</activeByDefault> <!-- 默认激活 --> </activation> </profile> <!-- ==================== 测试环境 ==================== --> <profile> <id>test</id> <properties> <spring.profile>test</spring.profile> <nacos.server.address>192.168.0.192:8848</nacos.server.address> </properties> <activation> <activeByDefault>false</activeByDefault> </activation> </profile> <!-- ==================== 生产环境 ==================== --> <profile> <id>prod</id> <properties> <spring.profile>prod</spring.profile> <nacos.server.address>127.0.0.1:14102</nacos.server.address> </properties> <activation> <activeByDefault>false</activeByDefault> </activation> </profile> </profiles>
2.2 开启资源过滤 1 2 3 4 5 6 7 8 <build > <resources > <resource > <directory > src/main/resources</directory > <filtering > true</filtering > </resource > </resources > </build >
2.3 关键规则
规则
说明
互斥性
一次只能激活一个 Profile
默认值
activeByDefault=true 只在未手动指定时生效
手动覆盖
一旦手动选了 test/prod,dev 自动失效
IDEA 操作
Maven 面板勾选 Profile 即可
命令行
mvn clean package -P test
三、bootstrap.yml 配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 server: port: 9201 spring: application: name: jwwd-system profiles: active: @spring.profile@ cloud: nacos: discovery: server-addr: @nacos.server.address@ config: server-addr: @nacos.server.address@ file-extension: yml shared-configs: - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
占位符替换对照
源码中的占位符
dev 打包后
test 打包后
prod 打包后
@spring.profile@
dev
test
prod
@nacos.server.address@
127.0.0.1:8848
192.168.0.192:8848
127.0.0.1:14102
四、Nacos 配置中心的数据结构 4.1 命名规则 1 2 服务专属配置: {spring.application.name}-{spring.profiles.active}.{file-extension} 共享配置: application-{spring.profiles.active}.{file-extension}
4.2 实际示例 1 2 3 4 5 6 7 Nacos 配置列表: ├── jwwd-system-dev.yml ← dev 环境的系统服务配置(数据库、Redis等) ├── jwwd-system-test.yml ← test 环境的系统服务配置 ├── jwwd-system-prod.yml ← prod 环境的系统服务配置 ├── application-dev.yml ← dev 环境的共享配置(所有服务共用) ├── application-test.yml ← test 环境的共享配置 └── application-prod.yml ← prod 环境的共享配置
4.3 配置加载顺序 1 2 3 4 ① 本地 bootstrap.yml → 获取应用名、环境、Nacos地址 ② Nacos: {服务名}-{环境}.yml → 服务专属配置 ③ Nacos: application-{环境}.yml → 共享配置 ④ 合并所有配置 → 应用启动完成
五、复刻步骤 第一步:父 POM 添加 Profiles 1 2 3 4 5 6 7 8 9 10 11 12 13 <profiles > <profile > <id > dev</id > <properties > <spring.profile > dev</spring.profile > <nacos.server.address > 127.0.0.1:8848</nacos.server.address > </properties > <activation > <activeByDefault > true</activeByDefault > </activation > </profile > </profiles >
第二步:开启资源过滤 1 2 3 4 5 6 7 8 <build > <resources > <resource > <directory > src/main/resources</directory > <filtering > true</filtering > </resource > </resources > </build >
第三步:子模块创建 bootstrap.yml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 spring: application: name: 你的服务名 profiles: active: @spring.profile@ cloud: nacos: discovery: server-addr: @nacos.server.address@ config: server-addr: @nacos.server.address@ file-extension: yml shared-configs: - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
第四步:Nacos 中创建对应配置文件
打开 Nacos 管理界面
新建配置,Data ID 格式为 {服务名}-{环境}.yml
填入对应环境的数据库连接、Redis 地址等配置
第五步:验证 1 2 3 4 5 6 7 8 9 mvn clean package mvn clean package -P test jar xf target/xxx.jar BOOT-INF/classes/bootstrap.yml cat BOOT-INF/classes/bootstrap.yml
六、常见问题 Q1: 为什么本地没有 -dev.yml 文件?
配置全部存在 Nacos 配置中心,本地只保留”引导”配置(bootstrap.yml),不需要本地环境文件。
Q2: 多个 Profile 同时勾选会怎样?
Maven 会合并所有激活的 Profile 属性。如果多个 Profile 定义了同名属性,最后一个生效 。建议只勾选一个。
Q3: 如何确认当前使用的是哪个环境?
启动日志中查看:The following profiles are active: dev 或访问 Nacos 管理界面查看服务注册的详情。
Q4: 不需要 Nacos 配置中心,只用本地文件怎么做?
创建 application-dev.yml、application-test.yml、application-prod.yml 本地文件,去掉 Nacos 相关配置即可。spring.profiles.active 仍会激活对应的本地文件。
---
这篇笔记涵盖了从原理到实践的完整内容,你可以直接保存。如果需要我补充其他内容(比如 Nacos 配置中心的具体搭建步骤,或者不用 Nacos 纯本地文件的方案),随时告诉我!