
오늘은 Spring 기반 멀티모듈 프로젝트에서 효율적인 환경변수 관리 방법을 소개합니다!
멀티모듈 프로젝트는 독립적인 모듈을 조합하여 확장성과 유지보수성을 높입니다.
하지만 각 모듈이 다른 환경에서 실행되므로, 체계적인 환경변수 관리가 필수적입니다.
Spring에서는 @Profile과 @PropertySource를 활용하여 프로필별 환경변수를 로드할 수 있습니다.
이를 통해 개발, 테스트, 운영 환경에 맞는 설정을 유연하게 적용할 수 있습니다.
@Profile과 @PropertySource 조합을 활용하여 프로필별 환경변수를 효과적으로 관리했습니다.
이 방식은 유지보수성을 높이면서도 명확한 환경 구분을 가능하게 한다 생각합니다.
대규모 Spring 기반 서비스를 개발하다 보면 코드의 재사용성과 유지보수성을 위해 멀티모듈 구조를 채택하게 됩니다.
하지만 모듈이 늘어날수록 환경변수 관리의 복잡도도 함께 증가하게 됩니다.
이 포스팅에서는 멀티모듈 프로젝트에서 환경변수를 효율적으로 관리하는 방법들을 비교 분석하고, 실제 프로젝트에 적용할 수 있는 최적의 전략을 제시하고자 합니다.
멀티모듈 프로젝트에서 환경변수 관리시 다음과 같은 문제들을 마주치게 됩니다.
이러한 문제들을 해결하기 위해 Spring에서 제공하는 4가지 주요 환경변수 설정 방법을 살펴보겠습니다.
spring:
config:
import:
- 'classpath:module-config.yml'
장점
단점
spring:
profiles:
include: module-profile
장점
단점
@Configuration
@PropertySource("classpath:/config/module-config.properties")
class ModuleConfig
장점
단점
@Configuration
class ProfileConfig {
@Configuration
@Profile("dev")
@PropertySource("classpath:/config/dev.properties")
class DevConfig
@Configuration
@Profile("prod")
@PropertySource("classpath:/config/prod.properties")
class ProdConfig
}
장점
단점
실무 경험을 바탕으로, @Profile과 @PropertySource를 조합한 방식을 추천합니다.
그 이유는 다음과 같습니다.
1. 명확한 책임 분리
2. 유지보수 용이성
3. 확장성
4. 설정 오류 조기 발견
// 모듈별 설정 기본 구조
src/main/resources/
├── config/
│ ├── database/
│ │ ├── dev.properties
│ │ └── prod.properties
│ └── redis/
│ ├── dev.properties
│ └── prod.properties
// 데이터베이스 설정
@Configuration
class DatabaseConfig {
@Configuration
@Profile("dev")
@PropertySource("classpath:/config/database/dev.properties")
class DevConfig
@Configuration
@Profile("prod")
@PropertySource("classpath:/config/database/prod.properties")
class ProdConfig
}
// Redis 설정
@Configuration
class RedisConfig {
@Configuration
@Profile("dev")
@PropertySource("classpath:/config/redis/dev.properties")
class DevConfig
@Configuration
@Profile("prod")
@PropertySource("classpath:/config/redis/prod.properties")
class ProdConfig
}
1. 설정 파일 구조화
2. 중복 설정 방지
3. 보안 고려사항
4. 테스트 용이성
멀티모듈 프로젝트의 환경변수 관리는 서비스의 안정성과 유지보수성에 직접적인 영향을 미치는 중요한 요소입니다.
@Profile과 @PropertySource를 조합한 방식은 초기 설정 비용이 다소 높을 수 있으나, 장기적인 관점에서 프로젝트의 확장성과 유지보수성을 크게 향상시킬 수 있습니다.
이러한 설정 전략은 단순히 기술적인 선택이 아닌, 프로젝트의 아키텍처와 운영 효율성에 직접적인 영향을 미치는 중요한 의사결정입니다.
각 프로젝트의 특성과 팀의 상황을 고려하여 최적의 전략을 선택하시기 바랍니다.