BackEnd๐Ÿงต

application.yml ์ž‘์—… ํ™˜๊ฒฝ ๋ถ„๋ฆฌ ๋ฐฉ๋ฒ•

hae02y 2023. 11. 7. 19:11
๋ฐ˜์‘ํ˜•

ํ™˜๊ฒฝ๋ถ„๋ฆฌ์˜ ํ•„์š”์„ฑ

์‹ค์ œ ๋ฐฐํฌํ•  ์„œ๋น„์Šค๋ผ๋ฉด,
๊ฐœ๋ฐœ ์„œ๋ฒ„์™€ ์šด์˜ ์„œ๋ฒ„๋ฅผ ๋‚˜๋ˆ ์„œ ๊ด€๋ฆฌํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค.
์ถ”๊ฐ€์ ์œผ๋กœ ํ…Œ์ŠคํŠธ ํ™˜๊ฒฝ๋„ ๋ถ„๋ฆฌํ•ด์ฃผ๋ฉด ์ข‹๊ณ ,
JPA๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ddl-auto: create ๋ฅผ ์‚ฌ์šฉํ•˜๋Š”
์ดˆ๊ธฐ ํ™˜๊ฒฝ์„ธํŒ…๋„ ๋ถ„๋ฆฌํ•ด๋‘๋ฉด ์šด์˜ ์„œ๋ฒ„์—์„œ create๊ฐ€ ์‹คํ–‰๋˜๋Š”
๋Œ€์ฐธ์‚ฌ๋ฅผ ์˜ˆ๋ฐฉ๊ฐ€๋Šฅํ•˜๋‹ค.

ํ™˜๊ฒฝ๋ถ„๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•

ํ™˜๊ฒฝ๋ถ„๋ฆฌ ๋ฐฉ๋ฒ•์„ ์ฐพ์•„๋ณด๋‹ˆ ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€์ด๋‹ค.

1. yml ํŒŒ์ผ ๋‚˜๋ˆ„๊ธฐ
2. ํ•˜๋‚˜์˜ yml ํŒŒ์ผ ์•ˆ์—์„œ ๋‚˜๋ˆ„๊ธฐ

๊ธฐ๋ณธ ํ™˜๊ฒฝ ์„ธํŒ…

JPA์™€ ์Šคํ”„๋ง๋ถ€ํŠธ2.7.1๋กœ ๊ธฐ๋ณธ ํ™˜๊ฒฝ์„ ๊ตฌ์„ฑ

Entity๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ User ๋ฅผ ๋งŒ๋“ค์–ด๋‘๊ณ ,

์‹คํ–‰ํ•ด๋ณด๋‹ˆ ํฌํŠธ 8080์œผ๋กœ ์ž˜ ์‹คํ–‰๋˜๊ณ ,
ddl-auto: create ์„ค์ •์— ๋งž๊ฒŒ ํ…Œ์ด๋ธ”์ด ์ƒ์„ฑ๋œ๋‹ค.

db์—๋„ ์ž˜ ์ƒ์„ฑ์ด ๋œ๋‹ค.

์ด์ œ ํ™˜๊ฒฝ์„ ๋ถ„๋ฆฌํ•ด๋ณด์ž.

yml ํŒŒ์ผ ๋‚˜๋ˆ„๊ธฐ

application.yml

server:
  port: 8080

spring:
  profiles:
    default: dev # ๊ธฐ๋ณธ ํ™˜๊ฒฝ์„ dev๋กœ

  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/profiledemo?serverTimezone=Asia/Seoul
    username: root
    password: abcd

  jpa:
    hibernate:
      ddl-auto: update
    properties:
      hibernate:
        format_sql: true
    show-sql: true

application-create.yml

server:
  port: 8081

spring:
  config:
    activate:
      on-profile: create # ํ™˜๊ฒฝ์ด๋ฆ„์„ค์ •

  jpa:
    hibernate:
      ddl-auto: create

application-dev.yml

server:
  port: 8082

spring:
  config:
    activate:
      on-profile: dev

  jpa:
    hibernate:
      ddl-auto: update

application-prod.yml

server:
  port: 8083

spring:
  config:
    activate:
      on-profile: prod

  jpa:
    hibernate:
      ddl-auto: none

์„ธ๊ฐ€์ง€ ํŒŒ์ผ์„ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.
๊ธฐ๋ณธ yml ์„ค์ •์— ์ž‘์„ฑํ•œ ๋‚ด์šฉ๋“ค์—๊ฒŒ
์ถ”๊ฐ€๋กœ profile์— ์ž‘์„ฑํ•œ ๋‚ด์šฉ๋“ค์ด ๋ฎ์–ด์“ฐ๊ธฐ ๋˜๋Š” ๋ฐฉ์‹์ด๋‹ค.

์—ฌ๊ธฐ์„œ๋Š” port ๋ฒˆํ˜ธ์™€ ddl-auto ์„ค์ •๋งŒ ๋ฐ”๊ฟจ๋Š”๋ฐ,
datasource ์„ธํŒ…์ด๋‚˜ ๋‹ค๋ฅธ ์„ค์ •๋“ค๋„ ์ž‘์„ฑํ•˜์—ฌ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค.

์ธํ…”๋ฆฌ์ œ์ด์—์„œ Edit Configurations๋กœ ๋“ค์–ด๊ฐ€์„œ
Active profiles ๋ถ€๋ถ„์— ์›ํ•˜๋Š” profile๋ช…์„ ์ž…๋ ฅํ•˜๋ฉด
ํ•ด๋‹น ์„ธํŒ…์œผ๋กœ ์‹คํ–‰๋œ๋‹ค.


์•„๋ฌด ์„ค์ • ์—†์ด ์‹คํ–‰ํ•˜๋ฉด default๋กœ ์„ค์ •ํ•ด๋‘” dev ์„ธํŒ…์œผ๋กœ ์‹คํ–‰๋œ๋‹ค.
ํฌํŠธ8082๋กœ ์ž˜ ์‹คํ–‰๋˜์—ˆ๋‹ค!


์ด๋ฒˆ์—” create ์„ธํŒ…์œผ๋กœ ์‹คํ–‰ํ•ด๋ณด์ž.
ํฌํŠธ 8081๋กœ ์‹คํ–‰๋˜๋ฉฐ ddl-auto: create๊ฐ€ ์ ์šฉ๋œ๋‹ค.


prod ์„ธํŒ…๋„ ์ž˜ ์‹คํ–‰๋œ๋‹ค. (ํฌํŠธ 8083)

yml ํ•˜๋‚˜๋กœ ๋‚˜๋ˆ„๊ธฐ

application.yml

server:
  port: 8080

spring:
  profiles:
    default: dev # ๊ธฐ๋ณธ ํ™˜๊ฒฝ์„ dev๋กœ

  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/profiledemo?serverTimezone=Asia/Seoul
    username: root
    password: abcd

  jpa:
    hibernate:
      ddl-auto: update
    properties:
      hibernate:
        format_sql: true
    show-sql: true

---
server:
  port: 8081

spring:
  config:
    activate:
      on-profile: create # ํ™˜๊ฒฝ์ด๋ฆ„์„ค์ •

  jpa:
    hibernate:
      ddl-auto: create

---
server:
  port: 8082

spring:
  config:
    activate:
      on-profile: dev

  jpa:
    hibernate:
      ddl-auto: update

---
server:
  port: 8083

spring:
  config:
    activate:
      on-profile: prod

  jpa:
    hibernate:
      ddl-auto: none

ํ•˜๋‚˜์˜ yml ํŒŒ์ผ๋กœ ๋‚˜๋ˆŒ๋•Œ์—๋Š”
--- ๋กœ ๋‚˜๋ˆ ์„œ ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค.

yml ํŒŒ์ผ ์ž‘์„ฑ๋ฒ•๋งŒ ๋‹ค๋ฅผ๋ฟ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‚˜ ์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” ์—ฌ๋Ÿฌ yml์ผ๋•Œ์™€ ๋˜‘๊ฐ™์ด ํ•˜๋ฉด๋œ๋‹ค.


ํ…Œ์ŠคํŠธ ํ™˜๊ฒฝ ๋‚˜๋ˆ„๊ธฐ

ํ…Œ์ŠคํŠธ ํ™˜๊ฒฝ ๋‚˜๋ˆ„๊ธฐ๋Š” test ํด๋”์— resources ํด๋”๋ฅผ ์ƒ์„ฑํ•ด ์ฃผ์‹œ๊ณ 
application.yml ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด ์ฃผ์ž.

ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด๋งŒ ๋‘๋ฉด ํ…Œ์ŠคํŠธ ์‹คํ–‰์‹œ ํ…Œ์ŠคํŠธ์šฉ ํ™˜๊ฒฝ์„ธํŒ…์œผ๋กœ ์‹คํ–‰๋œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  JPA์˜ ๊ฒฝ์šฐ ํ…Œ์ŠคํŠธ application.yml์—์„œ datasource๋ฅผ ์•„์— ์ž…๋ ฅํ•˜์ง€ ์•Š์œผ๋ฉด,
์ž๋™์œผ๋กœ H2 db๋กœ ์„ธํŒ…๋œ๋‹ค.(H2 ์˜์กด์„ฑ ์ถ”๊ฐ€๋Š” ํ•ด์ค˜์•ผํ•จ)

๋งˆ๋ฌด๋ฆฌ

์Šคํ”„๋ง ๋ถ€ํŠธ ์˜ˆ์ „๋ฒ„์ „2.4์ด์ „์—์„œ๋Š”

spring:
  profiles: dev

๋ฐฉ์‹์œผ๋กœ profile ์ด๋ฆ„์„ค์ •์„ ํ–ˆ๋‹ค๊ณ  ํ•œ๋‹ค.

๋ฐ˜์‘ํ˜•