Spring Boot에서 로컬, 개발, 실서비스 같은 여러 환경을 하나의 application.yml 파일에 설정하고 사용하는 방법에 대해서 알아보자.
YAML 설정
하나의 application.yml 파일에 여러 환경의 설정 정보를 저장하려면 spring.profiles 를 통해 설정하면 된다.
프로파일 구분자(---)로 구분 한다.
# local, dev, prod 공통 설정
server:
port: 8080
tomcat:
uri-encoding: UTF-8
---
spring:
profiles: local
datasource:
url: "jdbc:mysql://test-server/test"
username: "dbuser"
password: "dbpass"
---
spring:
profiles: dev
datasource:
url: "jdbc:mysql://test-server/test"
username: "dbuser"
password: "dbpass"
---
spring:
profiles: prod
datasource:
url: "jdbc:mysql://service-server/web"
username: "proddbuser"
password: "proddbpass"
만약 Spring Boot 2.4 를 이용한다면 아래같은 방법도 고려해보자.
Spring Boot 2.4 부터는 spring.profiles 가 deprecated 되었다.
spring.profiles.group.<source> 를 이용해서 여러 프로파일들을 한꺼번에 그룹지어 하나의 프로파일로 만들수 있다.
spring:
profiles:
group:
"local": "testdb,common"
"dev": "testdb,common"
"prod": "proddb,common"
---
spring:
config:
activate:
on-profile: "proddb"
datasource:
url: "jdbc:mysql://service-server/web"
username: "proddbuser"
password: "proddbpass"
---
spring:
config:
activate:
on-profile: "testdb"
datasource:
url: "jdbc:mysql://test-server/test"
username: "dbuser"
password: "dbpass"
---
spring:
config:
activate:
on-profile: "common"
server:
port: 8080
tomcat:
uri-encoding: UTF-8
위에서도 설명했듯이 spring.profiles.group.<source>을 사용하면 관련 프로파일 그룹에 대한 논리적 이름을 정의할 수 있습니다.
위 예제는 proddb 과 common 프로파일로 구성된 prod 프로파일 그룹 생성하였다.
spring.profiles.active=prod 로 실행하게 되면 proddb 와 common 두개의 프로파일들을 한번에 실행할 수 있다.
사용
사용하는 방법은 profile 를 선택해서 실행하면 된다.
Jar
java -jar myapp.jar --spring.profiles.active=prod
인텔리J
1. Run > Edit Configurations... 선택
2. Spring Boot Application 선택
3. Active profiles 에서 원하는 profile 명을 입력 후 실행
참고
GitHub - spring-projects/spring-boot: Spring Boot
Spring Boot. Contribute to spring-projects/spring-boot development by creating an account on GitHub.
github.com
[출처] https://1minute-before6pm.tistory.com/12
Spring Boot profile 환경별 설정하기(YAML)
Spring Boot에서 로컬, 개발, 실서비스 같은 여러 환경을 하나의 application.yml 파일에 설정하고 사용하는 방법에 대해서 알아보자. YAML 설정 하나의 application.yml 파일에 여러 환경의 설정 정보를 저장
1minute-before6pm.tistory.com
'Language > Java, Android' 카테고리의 다른 글
Spring Boot - static 변수에서 @Value Annotation 사용 (0) | 2021.10.12 |
---|---|
[JPA] 다양한 연관관계 매핑 - @OneToMany @ManyToOne @OneToOne @ManyToOne (0) | 2021.10.12 |
[flutter] Vertical viewport was given unbounded height (0) | 2021.09.13 |
[Flutter] 스플래시 스크린 (1) | 2021.08.15 |
Flutter: Java uses or overrides a deprecated API (0) | 2021.08.01 |