๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • ์žฅ์›์ต ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ
๐Ÿ’Š Java & Kotlin & Spring/- spring framework +

[Spring Boot] Profile.active ์„ ์ด์šฉํ•˜์—ฌ ๋‹ค๋ฅธ ์„ค์ • ์ •๋ณด(application.properties) ๋ถˆ๋Ÿฌ์˜ค๊ธฐ

by Wonit 2021. 4. 26.

application.properties ํŒŒ์ผ

์šฐ๋ฆฌ๋Š” application.yml ์ด๋‚˜ application.properties ํŒŒ์ผ์„ ์ด์šฉํ•ด์„œ ์„ค์ • ์ •๋ณด๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณค ํ•œ๋‹ค.

 

server:
  port: 56237

spring:
  application:
    name: user-service

  datasource:
    url: 232.221.123.33
    driver: h2.Driver
    username: ffowidl123
    password: "{cipher}ff01fjosdifj02034j5;jasdj5rq239y48f"

 

์šฐ๋ฆฌ๊ฐ€ ๊ฐœ๋ฐœํ•˜๋Š” application์€ ๋‹ค์–‘ํ•œ ํ™˜๊ฒฝ์—์„œ ๊ฐœ๋ฐœ๋˜์–ด์ง„๋‹ค.


์ด๋ฅผํ…Œ๋ฉด ๊ธฐ๋Šฅ ๊ฐœ๋ฐœ์„ ์œ„ํ•œ dev ํ™˜๊ฒฝ, ๊ธฐ๋Šฅ ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•œ test ํ™˜๊ฒฝ, ๊ธฐ๋Šฅ ๋ฐฐํฌ๋ฅผ ์œ„ํ•œ production ํ™˜๊ฒฝ ๋“ฑ๋“ฑ

 

๊ฐ๊ฐ์— ๋งž๋Š” ํ™˜๊ฒฝ๊ณผ ๊ฐ๊ฐ์— ๋งž๋Š” ์„ค์ •์„ ์ง€์ •ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

 

์˜ˆ๋ฅผ ๋“ค์–ด ๊ฐ๊ฐ์˜ ํ™˜๊ฒฝ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž.

 

  • dev : h2
  • test : MySQL
  • prod : PostgresQL

๊ทธ๋Ÿด ๋•Œ ์šฐ๋ฆฌ๋Š” ๊ฐ๊ฐ ํ™˜๊ฒฝ ์ •๋ณด๋ฅผ ์–ด๋–ป๊ฒŒ ๋ฐ”๊ฟ”์ค˜์•ผ ํ• ๊นŒ?

 

๋ฐ”๋กœ profile ์ •๋ณด๋ฅผ ์ˆ˜์ •ํ•˜์—ฌ ๊ฐ๊ฐ์— ํ™˜๊ฒฝ์— ๋งž๋Š” ์ •๋ณด๋ฅผ ๋„ฃ์–ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

 

profile

Spring Boot ์˜ profile์ด๋ž€ ๊ฐ๊ฐ์˜ ํ™˜๊ฒฝ ์ •๋ณด๋“ค์„ ์˜๋ฏธํ•œ๋‹ค.

 

๊ธฐ๋ณธ์ ์ธ profile ์ •๋ณด๋Š”

  • application.properties
  • application.yml

์ธ๋ฐ, ์—ฌ๊ธฐ์„œ ํŠน์ • ๊ทœ์น™์— ๋งŒ์กฑํ•˜๊ฒŒ ์„ค์ • ํŒŒ์ผ์„ ๋งŒ๋“ค๋ฉด Spring boot๊ฐ€ ์ฝ์–ด์˜ฌ ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

  • application.{ํ”„๋กœํ•„ ๋„ค์ž„ ํ‚ค์›Œ๋“œ}.properties
    • ex) application-test.properties
  • application.{ํ”„๋กœํ•„ ๋„ค์ž„ ํ‚ค์›Œ๋“œ}.yml
    • ex) application-prod.properties

์‹ค์Šตํ•˜๊ธฐ

๊ฐ๊ฐ์˜ ํ™˜๊ฒฝ์— ๋”ฐ๋ฅธ ์„ค์ • ํŒŒ์ผ๋“ค์„ ๋งŒ๋“ค์–ด์„œ ๋ณด์ž.

 

๊ฐ๊ฐ ํ”„๋กœํŒŒ์ผ ํŒŒ์ผ ๋งŒ๋“ค๊ธฐ

 

์œ„์™€ ๊ฐ™์ด application.yml ๊ณผ ๊ฐ™์€ depth ๋กœ ์—ฌ๋Ÿฌ ์„ค์ • ํŒŒ์ผ๋“ค์„ ๋ถˆ๋Ÿฌ์˜จ๋‹ค.

์‹คํ–‰ํ•˜๊ธฐ

์„ค์ • ์ •๋ณด๋ฅผ ๋ฐ”๊ฟ”์„œ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์ด ์กด์žฌํ•œ๋‹ค.

 

ํ•˜์ง€๋งŒ ํฌ๊ฒŒ 2๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์ด์•ผ๊ธฐํ•ด๋ณด๋ ค ํ•œ๋‹ค.

  1. application.yml ์— profile.active ์ง€์ •ํ•˜๊ธฐ
  2. java -jar ์— ์˜ต์…˜์„ ์ค˜์„œ ํŠน์ • profile ๋กœ๋“œํ•˜๊ธฐ

1. application.yml ์— profile.active ์ง€์ •ํ•˜๊ธฐ

yml์ด๋‚˜ properties ํŒŒ์ผ์—์„œ ์ง์ ‘ ์„ค์ • ์ •๋ณด๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜์žˆ๋Š”๋ฐ, profile.active ์„ ์ง€์ •ํ•˜๋ฉด ๋œ๋‹ค.

 

spring:
  profiles:
    active: test

spring:
  profiles:
    active: test

 

๋งŒ์•ฝ ์•„๋ฌด๊ฒƒ๋„ ์ง€์ •ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด application.yml ์ด๋‚˜ application.properties ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜ค๊ฒŒ ๋œ๋‹ค.

 

2. java -jar ์— ์˜ต์…˜์„ ์ค˜์„œ ํŠน์ • profile ๋กœ๋“œํ•˜๊ธฐ

 

java jar ํŒŒ์ผ๋กœ ๋นŒ๋“œ๋ฅผ ํ•˜๊ณ  jar ํŒŒ์ผ์„ ์‹คํ–‰์‹œํ‚ค๋Š” ์‹œ์ ์— ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ profile ์„ ๋ณ€๊ฒฝ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

 

java -jar -Dspring.profiles.active=dev ./my-app-0.0.1-SNAPSHOT.jar

java -jar -Dspring.profiles.active=test ./my-app-0.0.1-SNAPSHOT.jar

java -jar -Dspring.profiles.active=prod ./my-app-0.0.1-SNAPSHOT.jar

ํ…Œ์ŠคํŠธ

 

application.yml

server:
  port: 8080

spring:
  application:
    name: user-service

  profiles:
    active: test # ๋งŒ์•ฝ application-prod.yml ํŒŒ์ผ์„ ๋กœ๋”ฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, prod

greeting:
  message: this is main application.yml

 

application-test.yml

greeting:
  message: this is TEST application.yml

 

application-prod.yml

greeting:
  message: this is PRODUCTION application.yml

 

UserServiceApplication.java

@SpringBootApplication
@RestController
public class UserServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(UserServiceApplication.class, args);
    }

    @GetMapping
    public String string(@Value("${greeting.message}") String message) {
        return message;
    }
}

 

๊ทธ๋ฆฌ๊ณ  ๊ฐ๊ฐ ํ™˜๊ฒฝ์—์„œ ์ˆ˜ํ–‰ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜จ๋‹ค.

 

๋Œ“๊ธ€