环境差异化配置
Profile 实现多环境配置分离,环境间配置差异化管理。
环境类型
| 环境 | 说明 |
|---|---|
| dev | 开发环境 |
| test | 测试环境 |
| prod | 生产环境 |
| local | 本地环境 |
配置内容差异化
可差异化配置
| 配置 | 示例 |
|---|---|
| 数据库连接 | URL、用户名、密码 |
| 服务地址 | API URL、服务端点 |
| 日志级别 | DEBUG/INFO/WARN |
| 资源路径 | 配置文件路径 |
| 依赖 | 测试库、监控库 |
完整环境配置示例
XML
<profiles>
<!-- 开发环境 -->
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<env>dev</env>
<db.url>jdbc:mysql://localhost:3306/dev</db.url>
<db.user>dev</db.user>
<db.password>dev123</db.password>
<log.level>DEBUG</log.level>
<api.url>http://localhost:8080/api</api.url>
</properties>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</profile>
<!-- 测试环境 -->
<profile>
<id>test</id>
<properties>
<env>test</env>
<db.url>jdbc:mysql://test-server:3306/test</db.url>
<db.user>test</db.user>
<db.password>test123</db.password>
<log.level>INFO</log.level>
<api.url>http://test-server:8080/api</api.url>
</properties>
</profile>
<!-- 生产环境 -->
<profile>
<id>prod</id>
<properties>
<env>prod</env>
<db.url>jdbc:mysql://prod-server:3306/prod</db.url>
<db.user>${env.DB_USER}</db.user>
<db.password>${env.DB_PASSWORD}</db.password>
<log.level>WARN</log.level>
<api.url>https://api.company.com</api.url>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Environment>production</Environment>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
资源过滤配合
application.properties
properties
environment=${env}
database.url=${db.url}
database.user=${db.user}
database.password=${db.password}
log.level=${log.level}
api.url=${api.url}
启用资源过滤
XML
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
不同环境构建
Bash
# 开发环境
mvn package -Pdev
# 测试环境
mvn package -Ptest
# 生产环境
mvn package -Pprod
Spring Boot 多环境
application-{profile}.properties
XML
src/main/resources/
├── application.properties # 公共配置
├── application-dev.properties # 开发配置
├── application-test.properties # 测试配置
└──── application-prod.properties # 生产配置
Maven Profile 激活
properties
<profile>
<id>dev</id>
<properties>
<spring.profiles.active>dev</spring.profiles.active>
</properties>
</profile>
application.properties
XML
spring.profiles.active=@spring.profiles.active@
环境特定依赖
XML
<profile>
<id>dev</id>
<dependencies>
<!-- 开发环境依赖 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</profile>
<profile>
<id>prod</id>
<dependencies>
<!-- 生产环境依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
</profile>
环境特定插件
XML
<profile>
<id>prod</id>
<build>
<plugins>
<!-- 生产环境打包优化 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
</plugin>
<!-- 生产环境代码检查 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
环境特定仓库
groovy
<profile>
<id>prod</id>
<repositories>
<repository>
<id>prod-repo</id>
<url>https://nexus.company.com/releases</url>
</repository>
</repositories>
</profile>
CI/CD 环境切换
Jenkins Pipeline
XML
stage('Build Dev') {
steps {
sh 'mvn package -Pdev'
}
}
stage('Build Prod') {
steps {
sh 'mvn package -Pprod'
}
}
环境变量注入
生产环境敏感配置
Bash
<profile>
<id>prod</id>
<properties>
<db.password>${env.DB_PASSWORD}</db.password>
<api.key>${env.API_KEY}</api.key>
</properties>
</profile>
CI/CD 注入环境变量
text
export DB_PASSWORD=prod-password
mvn package -Pprod
要点总结
- Profile 区分 dev/test/prod 环境
- properties 定义环境属性
- 资源过滤将属性替换到配置文件
- 环境特定依赖、插件、仓库配置
- Spring Boot 使用 application-{profile}.properties
- 生产敏感配置使用环境变量注入
- -P 切换环境构建
📝 发现内容有误?点击此处直接编辑