๐ŸŒฑ → ๐ŸŒณ

[Spring boot] properties ์‚ฌ์šฉ์‹œ "Could not resolve placeholder" ์˜ค๋ฅ˜ ๋ฐœ์ƒ ๋ณธ๋ฌธ

Server/Java & Spring

[Spring boot] properties ์‚ฌ์šฉ์‹œ "Could not resolve placeholder" ์˜ค๋ฅ˜ ๋ฐœ์ƒ

BAY 2023. 5. 28. 10:46
728x90

ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด์„œ ์‚ฌ์ง„, ๋™์˜์ƒ๊ณผ ๊ฐ™์€ ๋ฏธ๋””์–ด ํŒŒ์ผ๋“ค์„ AWS์˜ S3์— ์ €์žฅํ•˜๊ธฐ ์œ„ํ•˜์—ฌ application.yml ํŒŒ์ผ์— S3์— ๊ด€๋ จ๋œ ์ธ์ฆ ์ •๋ณด๋“ค์„ ์ถ”๊ฐ€ํ–ˆ๋‹ค.

 

๋กœ์ปฌ์—์„œ๋Š” ์ž˜ ๋Œ์•„๊ฐ€๋Š”๋ฐ github actions๋ฅผ ํ†ตํ•ด ci/cd ๊ตฌ์ถ•ํ•˜์ž ๋ฌธ์ œ ๋ฐœ์ƒ

๊ทธ๋Ÿฌ๋‹ˆ๊นŒ ๋ฐฐํฌํ•˜๋Š” ๊ณผ์ •์—์„œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒƒ ๊ฐ™๋‹ค 

 

๊ทผ๋ฐ ์ผ๋‹จ ๋ฐฐํฌ๋Š” ์ž˜ ๋จ 

 

codedeploy์—์„œ๋„ ์„ฑ๊ณต

ps -ef | grep jar ๋ช…๋ น์–ด๋กœ ์‹คํ–‰์ค‘์ธ jar ํŒŒ์ผ ํ™•์ธ 

์ฒ˜์Œ์—๋Š” ์ œ๋Œ€๋กœ ๋Œ์•„๊ฐ€๋Š” ๋“ฏ ํ–ˆ์œผ๋‚˜ 10์ดˆ ์ •๋„ ๋’ค์— ๋‹ค์‹œ ํ™•์ธํ•ด๋ณด๋ฉด ๊บผ์ง€๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค 

์•„ .. ์ด๊ฑฐ ์ง€๊ธˆ application์ด ๋Œ์•„๊ฐ€๋Š”๋ฐ ์‹คํŒจํ•œ๊ฑฐ๊ตฌ๋‚˜ 

 

๊ฐ•์ œ๋กœ ๋‹ค์‹œ java ์‹คํ–‰์‹œ์ผœ๋ณด์ž 

์—ญ์‹œ๋‚˜ .. application run failed ์˜€๊ตฌ๋‚˜ ^^ . . .

 

๋ชจ๊ฐ€ ๋ฌธ์ œ์ธ์ง€ ์ž์„ธํžˆ ๋ณด๋‹ˆ๊นŒ .. 

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 's3Config': Injection of autowired dependencies failed; 
nested exception is java.lang.IllegalArgumentException: 
Could not resolve placeholder 'cloud.aws.credentials.access-key' in value "${cloud.aws.credentials.access-key}"

 

์œ„ ์ฝ”๋“œ์—์„œ "cloud.aws.credentials.access-key"๋ฅผ ๋ชป ์ฐพ๋Š” ๋“ฏ ๋ณด์˜€์Œ 

 

๊ทธ๋Ÿฌ๋‹ˆ๊นŒ S3 ์ €์žฅ์†Œ์— ํŒŒ์ผ์„ ์ €์žฅํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ ๋’ค ํ…Œ์ŠคํŠธ๋ฅผ ์‹œ๋„ํ–ˆ๋Š”๋ฐ, ๋นŒ๋“œ๋Š” ์„ฑ๊ณต์ ์œผ๋กœ ์™„๋ฃŒ๋˜์—ˆ์ง€๋งŒ ์Šคํ”„๋ง์ด ๊ฐ€๋™๋  ๋•Œ Could not resolve placeholder 'cloud.aws.credentials.access-key' in value "${cloud.aws.credentials.access-key}" ๋ผ๋Š” ์—๋Ÿฌ์™€ ํ•จ๊ป˜ ๊ฐ•์ œ ์ข…๋ฃŒ๋จ

 

์ฆ‰, application.yml ํŒŒ์ผ์— ์ถ”๊ฐ€ํ•ด๋†“์€ S3์˜ ์ •๋ณด๋Š” ๋ฌผ๋ก ์ด๊ณ  IAM์˜ ํ‚ค ๊ฐ’์„ ์ฝ์–ด๋“ค์ด์ง€ ๋ชปํ•˜๋Š” ๊ฒƒ

 

ํ•˜์ง€๋งŒ ํ•ด๋‹น ํŒŒ์ผ์—๋Š” RDS์— ๊ด€ํ•œ ์ •๋ณด๋„ ์žˆ์—ˆ๋Š”๋ฐ DB์— ๊ด€ํ•œ ์ด์Šˆ๋Š” ๋ฐœ๊ฒฌ๋˜์ง€ ์•Š์•˜๋‹ค. !!!! ์•… 

๋˜ํ•œ, application.yml์ด ์œ„์น˜ํ•œ ๊ฐ™์€ ๋””๋ ‰ํ† ๋ฆฌ ๋‚ด์— ๋ณ„๋„์˜ yml ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ ๋’ค S3 ๊ด€๋ จ ์ธ์ฆ ์ •๋ณด๋“ค์„ ์˜ฎ๊ฒจ๋ณด์•˜์ง€๋งŒ ๋ฌธ์ œ๋Š” ๊ฐ™์•˜๋‹ค.

 

๊ตฌ๊ธ€๋ง์„ ํ†ตํ•ด ๋‚ด๊ฐ€ ์‹œ๋„ํ•ด ๋ณธ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์•˜๋Š”๋ฐ, ๋ฌธ์ œ ํ•ด๊ฒฐ์— ๋„์›€์ด ๋˜์ง€๋Š” ๋ชปํ•˜์˜€๋‹ค.

  1. Run > Edit Configurations > VM Option์—์„œ Dspring.profiles.active=local ์ถ”๊ฐ€ (yml์ด๋‚˜ properties ํŒŒ์ผ์— ์ถ”๊ฐ€ํ•ด๋„ ๋จ)
  2. ‘resources/META-INF/additional-spring-configuration-metadata.json’ ํŒŒ์ผ ์ƒ์„ฑํ•˜์—ฌ s3 ์ธ์ฆ value Metadata ์ง์ ‘ ์ถ”๊ฐ€
  3. S3Config ํŒŒ์ผ์— @PropertySource("classpath:์„ค์ •ํŒŒ์ผ")์„ ์ถ”๊ฐ€ํ•จ์œผ๋กœ์จ ์™ธ๋ถ€ ํŒŒ์ผ ์ธ์‹ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•จ
    (์„ค์ •ํŒŒ์ผ์„ ์ž…๋ ฅํ•  ๋•Œ ์ ˆ๋Œ€๊ฒฝ๋กœ๋กœ ์ž…๋ ฅํ•ด์•ผํ•˜๋Š”์ง€ ์ƒ๋Œ€๊ฒฝ๋กœ๋กœ ์ž…๋ ฅํ•ด์•ผํ•˜๋Š”์ง€ ๊ณ ๋ฏผํ–ˆ์—ˆ๋Š”๋ฐ, ๊ทธ๋ƒฅ ํŒŒ์ผ ์ด๋ฆ„๋งŒ ์ž…๋ ฅํ•˜๋ฉด ์•Œ์•„์„œ ์ธ์‹ํ•ด์ค€๋‹ค.)
  4. aws iam ์‚ฌ์šฉ์ž ๋‹ค์‹œ ์„ค์ •ํ•˜๊ธฐ 
  5. yml ํŒŒ์ผ์—์„œ properties ํŒŒ์ผ ์‚ฌ์šฉํ•ด๋ณด๊ธฐ 

 

๊ฒฐ๋ก ์ ์œผ๋กœ ํ•ด๊ฒฐํ–ˆ๋˜ ๋ฐฉ๋ฒ• 

์ผ๋‹จ ์ˆœ์„œ์˜ ๋ฌธ์ œ๋„ ์žˆ์—ˆ๋˜ ๊ฒƒ ๊ฐ™์€๋ฐ 

spring.profiles.active=db-dev ์ด๊ฒƒ๋ณด๋‹ค cloud.aws.credentials.access-key๊ฐ€ ์œ„์— ์˜ฌ๋ผ์™€์žˆ์–ด์„œ ์•ˆ๋˜์—ˆ๋˜ ๊ฒƒ ๊ฐ™๊ณ 

์–ด ์ž ๋งŒ ์ง„์งœ ์ด๊ฒŒ ๋‹ค ์ธ ๊ฒƒ ๊ฐ™๋‹ค ... ์ˆœ์„œ ....๊ฐ€ .. ๋ฌธ์ œ ....

์ˆœ์„œ.. ๋ฅผ ์ž˜ ์ง€ํ‚ค์ž ...... 

 

์—‡ ์•„๋‹ˆ๋‹ค ๋ฐฉ๊ธˆ ์‹คํ—˜์‚ผ์•„ ๋ชจ๋“  ๊ฒƒ์„ ๋˜‘๊ฐ™์ด ํ•˜๊ณ  

spring.profiles.active=db-dev๋งŒ 

spring.config.activate.on-profile=db-dev ๋กœ ๋ฐ”๊ฟจ๋Š”๋ฐ fail์ด ๋–ด๋‹ค 

 

์–ด๋ผ ๋‚ด๊ฐ€ ์•Œ๊ธฐ๋กœ๋Š” spring boot 2.4 ๋ฒ„์ „ ์ดํ›„ ์„ค์ • ํŒŒ์ผ์—๋Š” spring.profiles ๊ฐ€ deprecated ๋˜๊ณ  spring.config.activate.on-profile ๋กœ ๋ณ€๊ฒฝ๋œ ๊ฑธ๋กœ ์•„๋Š”๋ฐ 

 

๋ถ„๋ช… ๋‚œ ์ด๋ฒˆ ํ”„๋กœ์ ํŠธ์—์„œ 2.4 ์ดํ›„ ๋ฒ„์ „์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Œ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  github secrets๋กœ application.properties๋ฅผ ๋„ฃ์„๋•Œ๋Š” spring.config.activate.on-profile=db-dev๋ฅผ ์ธ์‹ํ•˜์ง€ ๋ชปํ•˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค .. !?

 

์ด๊ฑด ์ข€ ๋” ์•Œ์•„๋ด์•ผ ํ•  ๊ฒƒ ๊ฐ™๋‹ค 

 

๊ทธ๋ฆฌ๊ณ  ์ด๊ฑด ๋˜ ์•Œ๊ฒŒ๋œ ๊ฒƒ 

์Šคํ”„๋ง 2.4๋ถ€ํ„ฐ spring.profiles.active์™€ spring.profiles.include๋ฅผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋„๋ก ๋ฐ”๊ผˆ์Šด

https://spring.io/blog/2020/08/14/config-file-processing-in-spring-boot-2-4

 

Config file processing in Spring Boot 2.4

Spring Boot 2.4.0.M2 has just been released, and it brings with it some interesting changes to the way that application.properties and application.yml files are loaded. If your applications use the fairly typical setup of only using a single application.pr

spring.io

728x90