Profile 外部化配置管理
外部配置文件分离环境配置,Profile 动态加载。
外部配置方式
方式对比
| 方式 | 说明 |
|---|---|
| Profile 属性 | pom.xml 内定义 |
| 外部文件 | 单独配置文件 |
| 环境变量 | 系统环境注入 |
外部配置文件
目录结构
properties
project/
├── pom.xml
├── config/
│ ├── dev.properties
│ ├── test.properties
│ └── prod.properties
配置文件内容
XML
# dev.properties
db.url=jdbc:mysql://localhost:3306/dev
db.user=dev
db.password=dev123
api.url=http://localhost:8080/api
Profile 加载外部文件
使用 properties-maven-plugin
Bash
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>${project.basedir}/config/${env}.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
<profiles>
<profile>
<id>dev</id>
<properties>
<env>dev</env>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<env>prod</env>
</properties>
</profile>
</profiles>
执行
XML
mvn package -Pdev # 加载 config/dev.properties
mvn package -Pprod # 加载 config/prod.properties
Spring Boot 外部配置
application-{profile}.properties
properties
src/main/resources/
├── application.properties
├── application-dev.properties
├── application-prod.properties
Maven Profile 控制
XML
<profile>
<id>dev</id>
<properties>
<spring.profiles.active>dev</spring.profiles.active>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<spring.profiles.active>prod</spring.profiles.active>
</properties>
</profile>
application.properties
Bash
spring.profiles.active=@spring.profiles.active@
环境变量注入
Profile 配置
XML
<profile>
<id>prod</id>
<properties>
<db.url>${env.DB_URL}</db.url>
<db.user>${env.DB_USER}</db.user>
<db.password>${env.DB_PASSWORD}</db.password>
</properties>
</profile>
CI/CD 注入
Bash
export DB_URL=jdbc:mysql://prod-server:3306/prod
export DB_USER=prod_user
export DB_PASSWORD=prod_password
mvn package -Pprod
settings.xml 外部配置
用户级配置
XML
<settings>
<profiles>
<profile>
<id>local-config</id>
<properties>
<db.url>jdbc:mysql://localhost:3306/local</db.url>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>local-config</activeProfile>
</activeProfiles>
</settings>
配置文件排除版本库
.gitignore
Bash
config/
*.properties
credentials.properties
提供模板
Bash
config/
├── dev.properties.template
├── prod.properties.template
开发者复制模板创建实际配置。
加密敏感配置
Maven Settings 加密
YAML
mvn --encrypt-password prod_password
使用加密密码
JSON
<server>
<id>prod-db</id>
<password>{encrypted_password}</password>
</server>
配置验证
检查配置加载
text
mvn help:effective-pom -Pdev
mvn help:effective-settings
验证属性值
text
mvn properties:read-project-properties -Pdev
多环境配置管理最佳实践
推荐做法
| 做法 | 说明 |
|---|---|
| 配置分离 | 外部文件存放配置 |
| 模板提供 | 提供配置模板 |
| 敏感加密 | 加密密码等敏感信息 |
| 文档说明 | 配置项说明文档 |
不推荐做法
| 做法 | 风险 |
|---|---|
| pom.xml 存密码 | 密码暴露版本库 |
| 硬编码配置 | 无法环境切换 |
| 明文传输 | 安全风险 |
配置文件格式
YAML 格式
text
# config-dev.yml
database:
url: jdbc:mysql://localhost:3306/dev
user: dev
password: dev123
api:
url: http://localhost:8080
JSON 格式
text
{
"database": {
"url": "jdbc:mysql://localhost:3306/dev",
"user": "dev"
}
}
要点总结
- 外部配置文件分离环境配置
- properties-maven-plugin 加载外部文件
- ${env}.properties 动态选择配置文件
- Spring Boot 使用 application-{profile}.properties
- 环境变量注入敏感配置
- settings.xml 用户级配置
- 配置文件排除版本库,提供模板
- 加密敏感配置信息
📝 发现内容有误?点击此处直接编辑