WEB(BE)/Spring & Spring Boot

[Spring Boot] profile 개념과 profile 분리

에버듀 2025. 1. 4. 12:13
반응형

프로젝트를 개발하다보면 다양한 개발 환경이 필요하다.

내 컴퓨터에서 개발할 때는 local 개발 환경을 두고,

개발을 완료하고 프론트와 연동하면서 테스트를 하기 위해 dev 개발 환경을 두고,

dev 에서 문제가 없을 때, 실 사용자들이 접근하는 prod 개발 환경을 두는 것이 제일 대표적이다.

(여기에 더해 dev 와 prod 사이에 스테이징 서버를 두기도 한다.)

 

이렇게 환경이 나뉠 때 제일 대표적으로 달라지는 환경요소는 데이터베이스이다.

로컬에서 개발할 때 사용하는 DB와, 개발 서버에서 사용하는 DB, 운영 서버에서 사용하는 DB는 모두 다르다.

DB가 다르면 접속정보도 모두 달라진다.

이 외에도 다양한 설정값들이 로컬, 개발, 운영 서버마다 다르게 적용될 수 있다.

 

스프링 부트에서는 이렇게 개발 환경마다 서로 다른 설정 값을 적용할 수 있도록 profile 이라는 설정을 제공한다.

 

Profile

스프링 profile을 사용하면 어플리케이션 설정을 분리하고, 분리된 설정을 특정 환경에서만 적용할 수 있다.

@Configuration, @Component, @ConfigurationProperties 어노테이션 위에 @Profile 어노테이션을 통해 어떤 프로파일에서 이 빈을 등록할 것인지 명시할 수 있다.

만약 @ConfigurationProperties 어노테이션이 @EnableConfigurationProperties 어노테이션에 의해 등록되는 경우에는, 그 어노테이션(@Enable..)이 붙은 @Configuration 클래스에 @Profile 을 명시해야 한다.
만약 컴포넌트 스캔으로 @ConfigurationProperties 를 등록하는 경우에는, 해당 클래스에 직접 @Profile을 명시한다.
(@ConfigurationProperties에 대한 내용은 따로 정리할 예정)

 

 

어플리케이션에서 사용할 profile은 어플리케이션 설정파일 (application.yml) 에서 다음과 같이 명시한다.

spring:
  profiles:
    active: "dev,hsqldb"

 

spring.profiles.active 필드에 내가 활성화할 profile 이름을 , 로 나열하면 된다.

 

또는 스프링 어플리케이션을 실행할 때 명령줄에 옵션으로 활성화할 profile 을 명시할 수도 있다.

java --jar <jar file> --spring.profiles.active=dev,hsqldb

 

 

만약 어떤 profile 을 활성화할 지 명시하지 않으면, default profile 이 활성화된다.

default profile 의 이름은 default 이고, 이 값은 spring.profiles.default 설정값을 통해 바꿀 수 있다.

`


 

위에서 작성한 active, default 설정은 non-profile-specific document 에서만 사용할 수 있다.

이 말은 다음 2가지 케이스에 대해서 위 두가지 옵션을 사용할 수 없다는 의미이다.

 

1. profile-specific files 

스프링 부트는 application 설정 파일 외에도, application-{profile} 형식의 이름을 가진 profile-specific file 을 추가로 불러온다.

이 파일은 말 그대로 특정 profile 에서만 적용되는 파일로, 만약 prod 라는 profile을 사용하고 있다면 application.yml은 기본적으로 항상 적용되고, 추가적으로 application-prod.yml 파일이 있다면 추가로 불러온다.

 

non-profile-specific file 의 내용은 profile-specific file 에 의해 덮어지고, 만약 여러 profile-specific file 을 가져온 경우에는 제일 마지막 profile-specific file 의 내용으로 덮어쓴다.

 

예를 들어 active 옵션에 준 profile 이 'dev, db' 라고 해보자.

application.yml 내용은 application-dev.yml 내용에 의해 덮어지고 (덮어진다는 것은 설정이 겹치는 경우를 말한다.)

application-dev.yml 내용은 나중에 가져온 application-db.yml 에 의해 덮어진다.

 

(설정 파일 경로를 spring.config.location 옵션으로 지정한 경우에는 이 옵션 내용에 따라 적용 순서가 달라질 수 있다.

이 부분은 공식문서를 참고해보자. 지금은 location 옵션으로 설정파일의 디렉토리를 나눠서 관리하지 않을 것 같기에..)

 

2. document activated by spring.config.activate.on-profile

activate 설정은 이 설정파일이 어떤 profile 에서 활성화되도록 할 지 조건을 명시하는 설정이다.

만약 이 설정이 들어있는 경우엔 active, default 설정을 작성할 수 없다.

 

spring:
  config:
    activate:
      on-profile: "prod"
  profiles:
    active: "metrics"

 

즉, 이렇게 쓰는 건 유효하지 않다.

 

 

활성화할 profile 추가하기

spring.profiles.active 에 작성한 profile 들은 명령줄에 작성한 active profile 에 의해 대체된다.

따라서 local 프로파일에서 application.yml 을 적용하면, db 프로파일도 '추가적으로' 활성화하고 싶다면 active 옵션만 사용하는 경우, 명령줄에서도 local, db 를 모두 명시해야 한다.

하지만 프로파일이 많아지면 모든 프로파일을 매번 명시하는 것이 불편하다.

 

따라서 활성화할 프로파일을 '대체'하지 않고, '추가' 할 수 있는 옵션이 존재한다.

spring.profiles.include 옵션은 이 프로파일이 활성화되었을 때 추가적으로 활성화할 profile 을 지정한다.

 

spring:
  profiles:
    include:
      - "common"
      - "local"

 

include 설정 역시, active, default 와 마찬가지로 non-profile-specific 파일에서만 사용할 수 있다.

 

profile group

여러 프로파일을 모아서 한번에 활성화하는 방법으로는 profile group 도 있다.

spring:
  profiles:
    group:
      production:
      - "proddb"
      - "prodmq"

 

이렇게 작성하면 production 이라는 이름의 그룹 안에 proddb, prodmq 프로파일을 포함할 수 있고,

명령줄로 프로파일을 지정할 때, 프로파일 그룹 이름을 지정해서 그룹 안에 포함된 모든 프로파일을 한번에 활성화할 수 있다.

이 프로파일 그룹은 위에서 언급한 include 옵션에도 작성할 수 있다.

 

 

실전 적용 예시

spring:
  profiles:
    group:
      test: "test"
      local: "local, datasource"
      dev: "dev, datasource"
      prod: "prod, datasource"
    include:
      - redis
      - security

 

application.yml 파일에 위와 같이 작성했다고 해보자.

만약 local profile 로 지정해서 실행하는 경우, 다음과 같은 profile 이 활성화 된다.

 

1. 'local' group 안에 들어있는 local, datasource

2. application.yml 에서 inlcude 한 redis, security

3. application-local.yml

 

 

위 예시에서는 local group 에 local을 넣었지만, 이렇게 작성해도 local profile 로 실행하면 local, test profile 이 모두 활성화된다.

즉, profile 그룹 이름과 profile 이름이 겹치더라도 둘 다 활성화한다.

 

 

참고

https://docs.spring.io/spring-boot/reference/features/profiles.html

 

Profiles :: Spring Boot

Occasionally the profiles that you define and use in your application are too fine-grained and become cumbersome to use. For example, you might have proddb and prodmq profiles that you use to enable database and messaging features independently. To help wi

docs.spring.io

 

https://docs.spring.io/spring-boot/reference/features/external-config.html#features.external-config.files.profile-specific

 

Externalized Configuration :: Spring Boot

The RandomValuePropertySource is useful for injecting random values (for example, into secrets or test cases). It can produce integers, longs, uuids, or strings, as shown in the following example: my.secret=${random.value} my.number=${random.int} my.bignum

docs.spring.io

 

 

반응형