μŠ€ν”„λ§ μ™ΈλΆ€ μ„€μ • 방법 (μ„€μ • 파일 μš°μ„ μˆœμœ„)

2023. 5. 10. 16:12γ†μΈν”„λŸ°/μŠ€ν”„λ§ λΆ€νŠΈ - 핡심 원리와 ν™œμš©

728x90

 

https://hyejin.tistory.com/1166

 

μŠ€ν”„λ§ μ™ΈλΆ€ μ„€μ • 방법 (μ™ΈλΆ€ 데이터)

μŠ€ν”„λ§ μ™ΈλΆ€ μ„€μ • 방법 (OS ν™˜κ²½ λ³€μˆ˜, μžλ°” μ‹œμŠ€ν…œ 속성) (tistory.com) μŠ€ν”„λ§ μ™ΈλΆ€ μ„€μ • 방법 (OS ν™˜κ²½ λ³€μˆ˜, μžλ°” μ‹œμŠ€ν…œ 속성) 0️⃣ μ™ΈλΆ€ μ„€μ •μ΄λž€ ? 보톡 μ‹€λ¬΄μ—μ„œ κ°œλ°œμ„ ν•˜λ‹€λ³΄λ©΄ λŒ€λΆ€λΆ„ νšŒμ‚¬μ—

hyejin.tistory.com

μ €λ²ˆ κΈ€μ—μ„œ μŠ€ν”„λ§ μ™ΈλΆ€ μ„€μ • 방법 쀑 μ™ΈλΆ€ 파일, λ‚΄λΆ€ 파일둜 μ„€μ •ν•˜λŠ” 방법을 μž‘μ„±ν–ˆλ‹€. 

그리고 이 λ‚΄λΆ€ νŒŒμΌμ„ ν†΅ν•©ν•΄μ„œ #--- λ˜λŠ” !---둜 κ΅¬λΆ„ν•΄μ„œ ν•˜λ‚˜μ˜ 파일둜 μ‚¬μš©ν•  수 μžˆλŠ” 것 κΉŒμ§€ μ•Œμ•„λ΄€λŠ”λ° 

μ΄λ•Œ profile을 μ§€μ •ν•˜μ§€ μ•ŠλŠ”λ‹€λ©΄ μŠ€ν”„λ§μ€ "default" ν”„λ‘œν•„λ‘œ μ§€μ •ν•˜κ³ , ν”„λ‘œν•„μ— ν•΄λ‹Ήν•˜λŠ” 값을 μ‘°νšŒν•œλ‹€. 

 

url=local.db.com
username=local_user
password=local_pw
#---
spring.config.activate.on-profile=dev
url=dev.db.com
username=dev_user
password=dev_pw
#---
spring.config.activate.on-profile=prod
url=prod.db.com
username=prod_user
password=prod_pw

λ‹€μŒκ³Ό 같이 맨 μœ„μ— ν”„λ‘œν•„μ„ μ§€μ •ν•˜μ§€ μ•Šκ³  url, username, password 값을 지정해 기본값을 섀정해쀬닀. 

그러면 ν”„λ‘œν•„μ— 상관없이 이 값은 항상 μ‚¬μš©ν•  수 있게 λœλ‹€. 

 

μ΄λ ‡κ²Œ application.properties에 μž‘μ„±ν•΄μ£Όκ³ , ν”„λ‘œν•„ 없이 μ‹€ν–‰ν•˜λ©΄ 

μ΄λ ‡κ²Œ 기본값이 μ„€μ •λœ 것을 확인할 수 μžˆλ‹€. 

 

그리고 λ§Œμ•½ ν”„λ‘œν•„μ„ dev둜 μ„€μ •ν•˜λ©΄ 기본값보닀 ν”„λ‘œν•„ μ„€μ •ν•΄μ€€ devκ°€ μš°μ„ μˆœμœ„λ₯Ό κ°€μ Έ 

devμ—μ„œ μ§€μ •ν•œ 킀값을 μ‘°νšŒν•  수 μžˆλ‹€. 

 

✏️ μ„€μ • 데이터 μš°μ„ μˆœμœ„ 

url=local.db.com
username=local_user
password=local_pw
#---
spring.config.activate.on-profile=dev
url=dev.db.com
username=dev_user
password=dev_pw
#---
spring.config.activate.on-profile=prod
url=prod.db.com
username=prod_user
password=prod_pw

application.properties νŒŒμΌμ—μ„œ μ‹€ν–‰μˆœμ„œλŠ” λ‹¨μˆœν•˜κ²Œ λ¬Έμ„œλ₯Ό μœ„μ—μ„œ μ•„λž˜λ‘œ μ½μœΌλ©΄μ„œ μ‚¬μš©ν•  값을 μ„€μ •ν•œλ‹€κ³  ν•  수 μžˆλ‹€. 

 

1️⃣ μš°μ„  기본값인 데이터듀을 μ½μ–΄μ„œ μ €μž₯ν•œλ‹€. 

(이 기본값듀은 ν”„λ‘œν•„κ³Ό λ¬΄κ΄€ν•˜κ²Œ 항상 값을 μ‚¬μš©ν•  수 μžˆλ‹€.) 

 

2️⃣ λ‹€μŒ μˆœμ„œμΈ dev κ΄€λ ¨ 데이터듀을 μ½λŠ”λ° μ΄λ•Œ dev ν”„λ‘œν•„μ΄ μ„€μ •λ˜μ–΄ μžˆλ‹€λ©΄ κΈ°μ‘΄ κ°’μ—μ„œ dev κ°’μœΌλ‘œ λŒ€μ²΄ν•˜κ³ , ν”„λ‘œν•„ 섀정이 λ˜μ–΄μžˆμ§€ μ•ŠλŠ”λ‹€λ©΄ dev κ΄€λ ¨ λ¬Έμ„œλŠ” λ¬΄μ‹œλ˜κ³  κ·Έ 값은 μ‚¬μš©ν•˜μ§€ μ•ŠλŠ”λ‹€. 

 

3️⃣ κ·Έ λ‹€μŒ μˆœμ„œμΈ prod κ΄€λ ¨ 데이터듀을 μ½λŠ”λ° μ΄λ•Œλ„ prod ν”„λ‘œν•„μ΄ μ„€μ •λ˜μ–΄ μžˆλ‹€λ©΄ κΈ°μ‘΄ κ°’μ—μ„œ prod κ°’μœΌλ‘œ λŒ€μ²΄ν•˜κ³ , ν”„λ‘œν•„ 섀정이 λ˜μ–΄μžˆμ§€ μ•ŠλŠ”λ‹€λ©΄ prod κ΄€λ ¨ λ°μ΄ν„°λŠ” λ¬΄μ‹œν•˜κ³  μ‚¬μš©ν•˜μ§€ μ•ŠλŠ”λ‹€. 

 

* 그리고 ν”„λ‘œν•„μ€ dev, prod λ“± 2개 이상 μ„€μ •ν•˜λŠ” 것도 κ°€λŠ₯ν•˜λ‹€. 

--spring.profiles.active = dev, prod 

 

 

url=local.db.com
username=local_user
password=local_pw
#---
spring.config.activate.on-profile=dev
url=dev.db.com
username=dev_user
password=dev_pw
#---
spring.config.activate.on-profile=prod
url=prod.db.com
username=prod_user
password=prod_pw
#---
url=test.db.com 

λ§Œμ•½ 맨 밑에 ꡬ뢄선을 μΆ”κ°€ν•˜κ³  url=test.db.com 을 μΆ”κ°€ν•˜λ©΄.. 

μŠ€ν”„λ§μ€ μœ„μ—μ„œ μ•„λž˜λ‘œ 데이터λ₯Ό μ½λŠ”λ‹€κ³  ν–ˆμœΌλ‹ˆκΉŒ url 만 test.db.com으둜 λ³€κ²½λ˜λŠ” 것을 확인할 수 μžˆλ‹€. 

* 근데 λ¬Όλ‘  μ΄λ ‡κ²ŒλŠ” 거의 μ‚¬μš©ν•˜μ§€ μ•ŠλŠ”λ‹€. 그리고 ν”„λ‘œν•„λ„ λŒ€μ²΄λ‘œ ν•œκ°œλ§Œ μ‚¬μš©ν•œλ‹€. 

 

 

πŸ’Œ 정리 

- μŠ€ν”„λ§μ€ applicatioin.properties λ¬Έμ„œλ₯Ό μœ„μ—μ„œ μ•„λž˜λ‘œ μˆœμ„œλŒ€λ‘œ μ½μœΌλ©΄μ„œ 값을 μ„€μ •ν•œλ‹€. (κΈ°μ‘΄ 값이 μžˆλ‹€λ©΄ λŒ€μ²΄ν•œλ‹€.)

- application.properties λ¬Έμ„œμ— spring-config.activate.on-profiles μ˜΅μ…˜μ΄ 있으면 ν•΄λ‹Ή ν”„λ‘œν•„μ„ μ‚¬μš©ν•  λ•Œλ§Œ 논리 λ¬Έμ„œλ₯Ό μ μš©ν•œλ‹€. 

 

 

 

⭐️ μŠ€ν”„λ§ μ™ΈλΆ€ μ„€μ • κ΄€λ ¨ μš°μ„ μˆœμœ„ 

μš°μ„ μˆœμœ„λŠ” μœ μ—°ν•˜λ©΄μ„œ λ²”μœ„κ°€ 쒁은 것이 μš°μ„ κΆŒμ„ κ°–κ²Œλœλ‹€. (λ”± μ§€μ •ν•΄μ„œ λ³€κ²½ν•˜κΈ° μ–΄λ €μš΄ νŒŒμΌλ³΄λ‹€λŠ” μ‹€ν–‰ μ‹œ μ›ν•˜λŠ” 값을 쀄 수 μžˆλŠ” 것이 더 μš°μ„ κΆŒμ„ κ°–κ²Œλ˜λ©°, 적용 λ²”μœ„κ°€ 더 쒁은 것이 μš°μ„ κΆŒμ„ κ°–κ²Œ λœλ‹€.) 

 

- μ™ΈλΆ€ μ„€μ • κ΄€λ ¨ μš°μ„ μˆœμœ„ 

1. @TestPropertySource (ν…ŒμŠ€νŠΈμ—μ„œ μ‚¬μš©ν•˜λŠ” κ²ƒμœΌλ‘œ ν…ŒμŠ€νŠΈ μ„€μ • 값을 λ”°λ‘œ 쀄 수 μžˆλ„λ‘ ν•˜λŠ” μ–΄λ…Έν…Œμ΄μ…˜) 

2. μ»€λ§¨λ“œ 라인 μ˜΅μ…˜ 인수 

3. μžλ°” μ‹œμŠ€ν…œ 속성 

4. OS ν™˜κ²½ λ³€μˆ˜ 

5. μ„€μ • 데이터 (application.properties)

 

-> λ”°λΌμ„œ Environmnet κ°€ μ‘°νšŒν•  λ•Œ  μš°μ„ μˆœμœ„μ— 따라 μš°μ„ μˆœμœ„κ°€ 더 높은 것을 λ¨Όμ € μ‘°νšŒν•΄μ„œ 값을 μ„€μ •ν•œλ‹€. 

 

- μ„€μ • 데이터 κ΄€λ ¨ μš°μ„ μˆœμœ„ 

1. jar μ™ΈλΆ€ ν”„λ‘œν•„ 적용 파일 application-{profile}.properties 

2. jar μ™ΈλΆ€ application.properties 

3. jar λ‚΄λΆ€ application-{profile}.properties 

4. jar λ‚΄λΆ€ application.properties 

 

 

μ‹€λ¬΄μ—μ„œλŠ” 보톡 application.properties에 μ„€μ • 데이터λ₯Ό λ³΄κ΄€ν•˜κ³  μ‚¬μš©ν•˜λ‹€κ°€ κΈ‰ 속성을 λ³€κ²½ν•΄μ•Ό ν•œλ‹€κ³  ν•œλ‹€λ©΄ μš°μ„ μˆœμœ„λ₯Ό μƒκ°ν•΄μ„œ 

μžλ°” μ‹œμŠ€ν…œ μ†μ„±μ΄λ‚˜ μ»€λ§¨λ“œ 라인 μ˜΅μ…˜ 인수λ₯Ό μ‚¬μš©ν•΄μ„œ 속성 값을 λŒ€μ²΄ν•΄μ£Όλ©΄ λœλ‹€. 

그리고 κΈ°λ³ΈμœΌλ‘œλŠ” jar λ‚΄λΆ€μ˜ application.properties λ¬Έμ„œλ₯Ό μ‚¬μš©ν•˜λ‹€κ°€ νŠΉλ³„ν•œ ν™˜κ²½μ—μ„œλŠ” μžλ°” μ‹€ν–‰μœ„μΉ˜μΈ μ™ΈλΆ€ application.properties λ₯Ό μƒˆλ‘œ λ§Œλ“€κ³  λ³€κ²½ν•΄μ•Όν•˜λŠ” 값을 λ°˜μ˜ν•΄μ„œ μ‚¬μš©ν•˜λŠ” 방법도 μžˆλ‹€. 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

728x90