在 Spring Boot 里,能够通过多种途径达成多环境的部署和配置,下面为你详细介绍常见的方法。
1. 使用不同的配置文件
Spring Boot 允许依据不同的环境创建不同的配置文件,文件名格式为 application-{profile}.properties
或者 application-{profile}.yml
,其中 {profile}
代表具体的环境名称,如 dev
(开发环境)、test
(测试环境)、prod
(生产环境)。
步骤
- 创建配置文件:在
src/main/resources
目录下创建不同环境的配置文件。application-dev.properties
(开发环境):
spring.datasource.url=jdbc:mysql://localhost:3306/dev_db
spring.datasource.username=dev_user
spring.datasource.password=dev_password
- `application-test.properties`(测试环境):
spring.datasource.url=jdbc:mysql://test-server:3306/test_db
spring.datasource.username=test_user
spring.datasource.password=test_password
- `application-prod.properties`(生产环境):
spring.datasource.url=jdbc:mysql://prod-server:3306/prod_db
spring.datasource.username=prod_user
spring.datasource.password=prod_password
- 指定激活的环境:有多种方式来指定要激活的环境。
- 命令行参数:启动应用时,通过
--spring.profiles.active
参数指定。
- 命令行参数:启动应用时,通过
java -jar your-application.jar --spring.profiles.active=dev
- **`application.properties` 或 `application.yml` 中指定**:在默认的配置文件里设置要激活的环境。
spring.profiles.active=dev
- **系统环境变量**:设置 `SPRING_PROFILES_ACTIVE` 环境变量。
export SPRING_PROFILES_ACTIVE=dev
java -jar your-application.jar
2. 使用 @Profile
注解
@Profile
注解可用于根据不同的环境启用或禁用特定的 Bean。
示例代码
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
public class AppConfig {
@Bean
@Profile("dev")
public MyService devService() {
return new MyService("Development Service");
}
@Bean
@Profile("prod")
public MyService prodService() {
return new MyService("Production Service");
}
}
class MyService {
private String name;
public MyService(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
在上述代码中,devService
Bean 仅在开发环境下启用,prodService
Bean 仅在生产环境下启用。
3. 使用 Spring Cloud Config
Spring Cloud Config 能够实现集中化的配置管理,支持多环境配置。
步骤
- 搭建 Config Server:创建一个 Spring Boot 项目,添加
spring-cloud-config-server
依赖,并在主类上添加@EnableConfigServer
注解。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
- 配置 Config Server:在
application.properties
中配置 Git 仓库地址等信息。
spring.cloud.config.server.git.uri=https://github.com/your-repo/config-repo
- 客户端应用:在客户端应用中添加
spring-cloud-starter-config
依赖,并配置 Config Server 的地址。
spring.cloud.config.uri=http://config-server:8888
spring.profiles.active=dev
完整代码示例展示
以下是一个包含不同环境配置文件和 @Profile
注解使用的完整示例:
application-dev.properties
spring.datasource.url=jdbc:mysql://localhost:3306/dev_db
spring.datasource.username=dev_user
spring.datasource.password=dev_password
application-prod.properties
spring.datasource.url=jdbc:mysql://prod-server:3306/prod_db
spring.datasource.username=prod_user
spring.datasource.password=prod_password
AppConfig.java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
public class AppConfig {
@Bean
@Profile("dev")
public MyService devService() {
return new MyService("Development Service");
}
@Bean
@Profile("prod")
public MyService prodService() {
return new MyService("Production Service");
}
}
class MyService {
private String name;
public MyService(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
DemoApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
通过以上这些方法,你可以在 Spring Boot 中方便地进行多环境的部署和配置。