๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Spring Boot์—์„œ ๊ตฌ์„ฑ ์„ค์ •(Configuration) ๊ด€๋ฆฌํ•˜๊ธฐ: `application.yml`๊ณผ ํ”„๋กœํ•„ ๊ธฐ๋ฐ˜ ์„ค์ • ์™„๋ฒฝ ๊ฐ€์ด๋“œ

okrestart 2024. 10. 29.

 

Spring Boot ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฐœ๋ฐœํ•  ๋•Œ ํ™˜๊ฒฝ๋ณ„ ์„ค์ • ๊ด€๋ฆฌ ๋Š” ํ•„์ˆ˜์ ์ž…๋‹ˆ๋‹ค. ๊ฐœ๋ฐœ, ํ…Œ์ŠคํŠธ, ์šด์˜ ํ™˜๊ฒฝ๋งˆ๋‹ค ๋‹ค๋ฅธ ์„ค์ • ์ด ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ํšจ์œจ์ ์ธ ๊ตฌ์„ฑ ๊ด€๋ฆฌ ๊ฐ€ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” application.properties ์™€ application.yml ํŒŒ์ผ ํ™œ์šฉ๋ฒ• , ํ™˜๊ฒฝ๋ณ„ ์„ค์ •์„ ์œ„ํ•œ ํ”„๋กœํ•„ ๊ด€๋ฆฌ( @Profile ) , ๊ทธ๋ฆฌ๊ณ  @ConfigurationProperties ๋กœ ์„ค์ • ์ฃผ์ž… ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋‹ค๋ฃน๋‹ˆ๋‹ค.

 


 

 

๋ชฉ์ฐจ

  1. application.properties์™€ application.yml์˜ ์ฐจ์ด์ 
  2. ํ”„๋กœํ•„( @Profile )์„ ์‚ฌ์šฉํ•œ ํ™˜๊ฒฝ๋ณ„ ์„ค์ • ๋ถ„๋ฆฌ
  3. @ConfigurationProperties๋กœ ์„ค์ • ์ฃผ์ž…ํ•˜๊ธฐ
  4. ๊ตฌ์„ฑ ์„ค์ •์˜ ์ตœ์‹  ํŠธ๋ Œ๋“œ์™€ ๋ชจ๋ฒ” ์‚ฌ๋ก€
  5. ์‹ค์Šต ์ฝ”๋“œ์™€ ํ™œ์šฉ ์˜ˆ์ œ

 


 

1. application.properties์™€ application.yml์˜ ์ฐจ์ด์ 

Spring Boot์—์„œ๋Š” application.properties ์™€ application.yml ํŒŒ์ผ์„ ํ†ตํ•ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์„ค์ •์„ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋‘ ๊ฐ€์ง€ ๋ฐฉ์‹์€ ๋™์ผํ•œ ์—ญํ• ์„ ํ•˜์ง€๋งŒ, ๊ตฌ์กฐ์ ์ธ ํ‘œํ˜„ ๋ฐฉ์‹ ์— ์ฐจ์ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

๋น„๊ต ํ•ญ๋ชฉ application.properties application.yml
ํŒŒ์ผ ํ˜•์‹ ํ‚ค-๊ฐ’ ์Œ (Key-Value) ๊ณ„์ธต์  ๊ตฌ์กฐ (YAML)
๊ฐ€๋…์„ฑ ๋ณต์žกํ•œ ์„ค์ •์ผ ๊ฒฝ์šฐ ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์งˆ ์ˆ˜ ์žˆ์Œ ๊ณ„์ธต ๊ตฌ์กฐ๋กœ ์„ค์ •์ด ๊ฐ„๊ฒฐํ•˜๊ณ  ๋ช…ํ™•ํ•จ
์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๊ฒฝ์šฐ ๊ฐ„๋‹จํ•œ ์„ค์ • ๋ณต์žกํ•œ ์„ค์ • ๊ด€๋ฆฌ



application.properties ์‚ฌ์šฉ ์˜ˆ์ œ

server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=1234

application.yml ์‚ฌ์šฉ ์˜ˆ์ œ

server:
  port: 8080

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mydb
    username: root
    password: 1234
  • application.yml ์€ ๊ณ„์ธต์  ํ‘œํ˜„ ์ด ๊ฐ€๋Šฅํ•ด ๋ณต์žกํ•œ ์„ค์ •์„ ๋” ๊ฐ„๊ฒฐํ•˜๊ฒŒ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 


 

2. ํ”„๋กœํ•„( @Profile )์„ ์‚ฌ์šฉํ•œ ํ™˜๊ฒฝ๋ณ„ ์„ค์ • ๋ถ„๋ฆฌ

Spring Boot๋Š” ํ™˜๊ฒฝ๋ณ„๋กœ ๋‹ค๋ฅธ ์„ค์ •์„ ๊ด€๋ฆฌ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ”„๋กœํ•„(Profile) ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๊ฐœ๋ฐœ ํ™˜๊ฒฝ๊ณผ ์šด์˜ ํ™˜๊ฒฝ์—์„œ ์„œ๋ฒ„ ํฌํŠธ๋‚˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค URL ์ด ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด @Profile ์• ๋„ˆํ…Œ์ด์…˜ ๊ณผ ํ”„๋กœํ•„ ๊ธฐ๋ฐ˜ ์„ค์ • ํŒŒ์ผ ์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

1. ํ”„๋กœํ•„๋ณ„ ์„ค์ • ํŒŒ์ผ ์ž‘์„ฑ

  • application-dev.yml (๊ฐœ๋ฐœ ํ™˜๊ฒฝ)
server:
  port: 8081

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/devdb
    username: devuser
    password: devpass
  • application-prod.yml (์šด์˜ ํ™˜๊ฒฝ)
server:
  port: 8080

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/proddb
    username: produser
    password: prodpass

2. ํ”„๋กœํ•„ ํ™œ์„ฑํ™” (application.yml)

spring:
  profiles:
    active: dev
  • spring.profiles.active : ํ˜„์žฌ ์‚ฌ์šฉ ์ค‘์ธ ํ”„๋กœํ•„์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์—์„œ๋Š” dev , ์šด์˜ ํ™˜๊ฒฝ์—์„œ๋Š” prod ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

3. @Profile ์• ๋„ˆํ…Œ์ด์…˜ ์‚ฌ์šฉ ์˜ˆ์ œ

package com.example.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
public class DataSourceConfig {

    @Bean
    @Profile("dev")
    public String devDataSource() {
        System.out.println("๊ฐœ๋ฐœ ํ™˜๊ฒฝ ๋ฐ์ดํ„ฐ์†Œ์Šค ์„ค์ •");
        return "Development DataSource";
    }

    @Bean
    @Profile("prod")
    public String prodDataSource() {
        System.out.println("์šด์˜ ํ™˜๊ฒฝ ๋ฐ์ดํ„ฐ์†Œ์Šค ์„ค์ •");
        return "Production DataSource";
    }
}
  • @Profile("dev") : ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์—์„œ๋งŒ ํ•ด๋‹น ๋นˆ์ด ํ™œ์„ฑํ™”๋ฉ๋‹ˆ๋‹ค.
  • @Profile("prod") : ์šด์˜ ํ™˜๊ฒฝ์—์„œ๋งŒ ํ•ด๋‹น ๋นˆ์ด ํ™œ์„ฑํ™”๋ฉ๋‹ˆ๋‹ค.

 


 

3. @ConfigurationProperties๋กœ ์„ค์ • ์ฃผ์ž…ํ•˜๊ธฐ

@ConfigurationProperties ๋Š” ์„ค์ • ํŒŒ์ผ์— ์ •์˜๋œ ์„ค์ • ๊ฐ’์„ ๊ฐ์ฒด์— ๋ฐ”์ธ๋”ฉ ํ•  ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์—ฌ๋Ÿฌ ์„ค์ • ๊ฐ’์„ ํ•˜๋‚˜์˜ ํด๋ž˜์Šค์— ๋ฌถ์–ด ๊ด€๋ฆฌ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

1. ์˜์กด์„ฑ ์ถ”๊ฐ€ (pom.xml)

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

2. ์„ค์ • ํด๋ž˜์Šค ์ƒ์„ฑ (AppConfig.java)

package com.example.demo.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "app")
public class AppConfig {

    private String name;
    private String version;

    // Getter, Setter ์ƒ๋žต

    public String getInfo() {
        return "์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ด๋ฆ„: " + name + ", ๋ฒ„์ „: " + version;
    }
}
  • @ConfigurationProperties : ์„ค์ • ํŒŒ์ผ์˜ ๊ฐ’์„ ํด๋ž˜์Šค ํ•„๋“œ์— ๋ฐ”์ธ๋”ฉํ•ฉ๋‹ˆ๋‹ค.
  • prefix : ํ•ด๋‹น ์ ‘๋‘์‚ฌ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ชจ๋“  ์„ค์ •์ด ๋ฐ”์ธ๋”ฉ๋ฉ๋‹ˆ๋‹ค.

3. ์„ค์ • ํŒŒ์ผ (application.yml)

app:
  name: MySpringApp
  version: 1.0.0

4. ์„ค์ • ํด๋ž˜์Šค ์‚ฌ์šฉ ์˜ˆ์ œ

package com.example.demo.controller;

import com.example.demo.config.AppConfig;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AppController {

    private final AppConfig appConfig;

    public AppController(AppConfig appConfig) {
        this.appConfig = appConfig;
    }

    @GetMapping("/info")
    public String getAppInfo() {
        return appConfig.getInfo();
    }
}

 


 

4. ๊ตฌ์„ฑ ์„ค์ •์˜ ์ตœ์‹  ํŠธ๋ Œ๋“œ์™€ ๋ชจ๋ฒ” ์‚ฌ๋ก€

  • Spring Cloud Config : ์„ค์ • ํŒŒ์ผ์„ ์ค‘์•™ ์ง‘์ค‘์‹์œผ๋กœ ๊ด€๋ฆฌ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ํ™˜๊ฒฝ ๋ณ€์ˆ˜์™€ ์™ธ๋ถ€ ์„ค์ • ํ™œ์šฉ : ๋„์ปค๋‚˜ ํด๋ผ์šฐ๋“œ ํ™˜๊ฒฝ์—์„œ๋Š” ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ๋ฅผ ํ†ตํ•ด ์„ค์ •์„ ์ฃผ์ž…ํ•ฉ๋‹ˆ๋‹ค.
  • ์•”ํ˜ธํ™”๋œ ์„ค์ • ๊ด€๋ฆฌ : ๋ฏผ๊ฐํ•œ ์ •๋ณด๋ฅผ ์•”ํ˜ธํ™” ํ•˜๊ณ , Vault ์™€ ๊ฐ™์€ ๋„๊ตฌ๋ฅผ ํ™œ์šฉํ•ด ๋ณด์•ˆ์„ ๊ฐ•ํ™”ํ•ฉ๋‹ˆ๋‹ค.
  • ํ”„๋กœํ•„ ์ž๋™ ์ „ํ™˜ : CI/CD ํŒŒ์ดํ”„๋ผ์ธ์—์„œ ์ž๋™์œผ๋กœ ํ”„๋กœํ•„์„ ์ „ํ™˜ ํ•ด ์šด์˜ ํ™˜๊ฒฝ์— ๋งž๋Š” ์„ค์ •์„ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค.

 


 

๊ด€๋ จ ๋งํฌ

 

Spring Boot ๊ณต์‹ ๋ฌธ์„œ๐Ÿ‘†

Spring Cloud Config ์‚ฌ์šฉ๋ฒ•๐Ÿ‘†

YAML vs Properties ํŒŒ์ผ ๋น„๊ต๐Ÿ‘†

Spring @Profile ๊ฐ€์ด๋“œ๐Ÿ‘†

@ConfigurationProperties ์‚ฌ์šฉ๋ฒ•๐Ÿ‘†

Vault๋กœ ์„ค์ • ๊ด€๋ฆฌํ•˜๊ธฐ๐Ÿ‘†

 


 

FAQ

 

1. application.properties์™€ application.yml ์ค‘ ๋ฌด์—‡์„ ์„ ํƒํ•ด์•ผ ํ•˜๋‚˜์š”?

  • ๊ฐ„๋‹จํ•œ ์„ค์ •์€ properties ๋กœ ์ถฉ๋ถ„ํ•˜์ง€๋งŒ, ๋ณต์žกํ•œ ๊ณ„์ธตํ˜• ์„ค์ • ์€ yml ํŒŒ์ผ์ด ๋” ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค.

2. ์—ฌ๋Ÿฌ ํ”„๋กœํ•„์„ ๋™์‹œ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‚˜์š”?

  • ๋„ค, ์‰ผํ‘œ(,) ๋กœ ๊ตฌ๋ถ„ํ•ด ์—ฌ๋Ÿฌ ํ”„๋กœํ•„์„ ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ: spring.profiles.active=dev,prod

3. ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋กœ ์„ค์ •์„ ์˜ค๋ฒ„๋ผ์ด๋“œํ•  ์ˆ˜ ์žˆ๋‚˜์š”?

  • ๋„ค, ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ํŒŒ์ผ ์„ค์ •์„ ๋ฎ์–ด์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

4. Spring Cloud Config๋Š” ์–ธ์ œ ์‚ฌ์šฉํ•˜๋‚˜์š”?

  • ์—ฌ๋Ÿฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๋™์ผํ•œ ์„ค์ •์„ ์‚ฌ์šฉํ•  ๋•Œ, ์ค‘์•™ ์ง‘์ค‘์‹ ์„ค์ • ๊ด€๋ฆฌ ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

5. ๋ฏผ๊ฐํ•œ ์ •๋ณด๋Š” ์–ด๋–ป๊ฒŒ ๋ณดํ˜ธํ•˜๋‚˜์š”?

  • Vault ๋‚˜ AWS Parameter Store ์™€ ๊ฐ™์€ ๋„๊ตฌ๋กœ ์•”ํ˜ธํ™”๋œ ์„ค์ • ์„ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

 


 

๋งˆ๋ฌด๋ฆฌ

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” Spring Boot์—์„œ ๊ตฌ์„ฑ ์„ค์ •์„ ํšจ์œจ์ ์œผ๋กœ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ• ์„ ๋‹ค๋ค˜์Šต๋‹ˆ๋‹ค. application.yml ๊ณผ ํ”„๋กœํ•„ ๊ธฐ๋ฐ˜ ์„ค์ • ์„ ํ†ตํ•ด ํ™˜๊ฒฝ๋ณ„๋กœ ๊ตฌ๋ถ„๋œ ์„ค์ •์„ ์ ์šฉํ•˜๊ณ , @ConfigurationProperties ๋ฅผ ์‚ฌ์šฉํ•ด ์„ค์ • ๊ฐ’์„ ๊ฐ์ฒด์— ๋ฐ”์ธ๋”ฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์› ์Šต๋‹ˆ๋‹ค.

์ด์ œ ์—ฌ๋Ÿฌ๋ถ„์˜ ํ”„๋กœ์ ํŠธ์—์„œ ๊ตฌ์„ฑ ๊ด€๋ฆฌ์˜ ํšจ์œจ์„ฑ ์„ ๊ทน๋Œ€ํ™”ํ•ด๋ณด์„ธ์š”! ๐Ÿš€

๋Œ“๊ธ€