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

[Spring Cloud Config] Application์˜ ์„ค์ • ์ •๋ณด (application.yml) ๋ฅผ ์ค‘์•™์—์„œ ๊ด€๋ฆฌํ•˜๊ธฐ (by native repository)

by Wonit 2021. 4. 27.

ํ•ด๋‹น ๊ธ€์€ Spring Cloud Netflix Eureka ์™€ Spring Cloud Gateway ์˜ Built-in Route๋กœ Predicates์™€ Filter ์กฐ์ž‘ํ•˜๊ธฐ)์— ์˜์กดํ•˜๋Š” ๊ธ€์ž…๋‹ˆ๋‹ค. ์‹ค์Šต ํ™˜๊ฒฝ์„ ๋”ฐ๋ผํ•˜์‹œ๋ ค๋ฉด Eureka์™€ Gateway ๊ธ€์— ๋‚˜์˜จ ์‹ค์Šต์„ ๋”ฐ๋ผํ•˜์‹œ๊ธธ ๊ถŒ๊ณ ๋“œ๋ฆฝ๋‹ˆ๋‹ค.

๋ชฉ์ฐจ

  • Gartner ์—์„œ ์ •์˜๋œ MSA ํ‘œ์ค€ ๊ตฌ์„ฑ ์š”์†Œ์ธ Config Store Server
    • ์ž์ฃผ ๋ณ€๊ฒฝ๋˜๋Š” ์„ค์ • ์ •๋ณด๋Š” ์„ค์ • ์ •๋ณด๋งŒ ๋‹ด๊ณ  ์žˆ๋Š” ์„œ๋ฒ„์—์„œ ๊ด€๋ฆฌํ•˜๊ณ  ์ด๋ฅผ ํ•„์š”๋กœ ํ•˜๋Š” ์„œ๋ฒ„์—๊ฒŒ ๊ฐ๊ฐ ๋ฟŒ๋ ค์ฃผ๋Š” ๋ฐฉ์‹
  • ์šฐ๋ฆฌ๊ฐ€ ๊ตฌ์„ฑํ•œ ์„œ๋น„์Šค ๊ตฌ์กฐ
    • ๊ฐ๊ฐ์˜ application.yml
    • ์„œ๋น„์Šค ๊ตฌ์กฐ์˜ ๋ฌธ์ œ์ 
  • Spring Cloud Config ๋ž€?
  • ์‹ค์Šต
    • Spring Cloud Config ํ”„๋กœ์ ํŠธ ์ƒ์„ฑํ•˜๊ธฐ
      • @EnableConfigServer
      • profiles.active.native
    • User-Service ์—์„œ Config ์„œ๋ฒ„๋กœ ๋ฐ›์€ ๋ฐ์ดํ„ฐ ์‚ฌ์šฉํ•˜๊ธฐ
      • bootstrap.yml
    • ํ…Œ์ŠคํŠธํ•˜๊ธฐ

Microservice Architecture ์—์„œ Config Store Server๊ฐ€ ๊ฐ–๋Š” ์˜๋ฏธ

Config Store Server ๋Š” ๋ง ๊ทธ๋Œ€๋กœ ์„ค์ • ์ •๋ณด ์ €์žฅ ์„œ๋ฒ„์ด๋‹ค.

 

Gartner ๊ทธ๋ฃน์—์„œ ์ •์˜ํ•œ MSA ์ธํ”„๋ผ ํ•„์ˆ˜ ๊ตฌ์„ฑ ์š”์†Œ์— ์˜ํ•˜๋ฉด Config Store Server ๋Š” Service Mesh ์˜์—ญ์— ์œ„์น˜ํ•˜๋Š” ํ•„์ˆ˜ ์š”์†Œ๋ผ๊ณ  ์ •์˜๋˜์–ด์ง„๋‹ค.

 

 

Service Mesh ์—์„œ Config Store ์„œ๋ฒ„๋Š” ์ž์ฃผ ๋ณ€๊ฒฝ๋˜๋Š” ์„ค์ • ์ •๋ณด๋Š” ์„ค์ • ์ •๋ณด๋งŒ ๋‹ด๊ณ  ์žˆ๋Š” ์„œ๋ฒ„์—์„œ ๊ด€๋ฆฌํ•˜๊ณ  ์ด๋ฅผ ํ•„์š”๋กœ ํ•˜๋Š” ์„œ๋ฒ„์—๊ฒŒ ๊ฐ๊ฐ ๋ฟŒ๋ ค์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ์ด์šฉ๋œ๋‹ค.

 

์ด๋Ÿฌํ•œ Config Store ์„œ๋ฒ„๊ฐ€ ์žˆ์Œ์œผ๋กœ Application์˜ ์™ธ๋ถ€ ์†์„ฑ์„ ์ค‘์•™ ์ง‘์ค‘์‹ ๊ด€๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•ด์ง„๋‹ค.

 

์ค‘์•™ ์ง‘์ค‘์‹ ๊ด€๋ฆฌ๊ฐ€ ์—†์œผ๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ?

 

์ค‘์•™ ์ง‘์ค‘์‹ ๊ด€๋ฆฌ๊ฐ€ ์—†๋‹ค๋Š” ๊ฒƒ์€ ์•„์ฃผ ์ผ๋ฐ˜์ ์ธ ์šฐ๋ฆฌ์˜ application ์˜ ๊ตฌ์กฐ์ผ ๊ฒƒ์ด๋‹ค.

 

์šฐ๋ฆฌ๊ฐ€ ๊ตฌ์„ฑํ•œ ์„œ๋น„์Šค์˜ ๊ตฌ์กฐ

์ง€๋‚œ ์‹œ๊ฐ„ ๋™์•ˆ ์šฐ๋ฆฌ๋Š” ์„œ๋น„์Šค๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌ์„ฑํ–ˆ์—ˆ๋‹ค.

  • Eureka Server
  • Gateway Service
  • User Service
  • Order Service

Some Server๋Š” ๊ตฌ์„ฑํ•˜์ง€ ์•Š์•˜์ง€๋งŒ ๋‹ค์–‘ํ•œ ์„œ๋น„์Šค๊ฐ€ ์žˆ๋‹ค๋Š” ์ „์ œ๋ฅผ ์œ„ํ•˜์—ฌ ์ถ”๊ฐ€ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

๊ฐ๊ฐ์˜ ์„œ๋ฒ„๋Š” ์™ธ๋ถ€ ์„ค์ • ์ •๋ณด๋ฅผ ์ž์‹ ์˜ ์„œ๋ฒ„์—์„œ ๊ด€๋ฆฌํ•˜๊ณ  ์žˆ๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ ๋ง ํ•˜๋Š” ์™ธ๋ถ€ ์„ค์ • ์ •๋ณด๋Š” application.yml์— ๋“ค์–ด๊ฐ€๋Š” ์ •๋ณด๋“ค์„ ์ด์•ผ๊ธฐํ•œ๋‹ค.

 

์˜ˆ๋ฅผ ๋“ค์–ด์„œ Gateway ์„œ๋ฒ„์™€ User, Order ์„œ๋ฒ„์— ๊ฐ๊ฐ ํ† ํฐ ๋ณตํ˜ธ๋ฅผ ์œ„ํ•œ Secret Key์™€ DB ์„ค์ • ์ •๋ณด๋“ค์ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์กด์žฌํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž.

 

User & Order-Service Application.yml

token:
  jwt:
    secret: my_secret_key

spring:
  datasource:
    driver-class-name: org.h2.Driver
    url: jdbc:h2:mem:users
    username: sa
    password: my_password

 

User ์™€ Order ์—๋Š” JWT ํ† ํฐ์„ encoding, decoding ์„ ์œ„ํ•œ ๋น„๋ฐ€ ํ‚ค ์ •๋ณด์™€ Database ์„ค์ • ์ •๋ณด๋“ค์ด ๋“ค์–ด์žˆ๋‹ค.

 

Gateway Services Application.yml

token:
  jwt:
    secret: my_secret_key

 

Gateway ์—๋Š” JWT ํ† ํฐ์„ encoding, decoding ์„ ์œ„ํ•œ ๋น„๋ฐ€ ํ‚ค ์ •๋ณด๋งŒ ์กด์žฌํ•œ๋‹ค.

 

์œ„์˜ ์„œ๋น„์Šค ๊ตฌ์กฐ์˜ ๋ฌธ์ œ์ 

์œ„์˜ ์„œ๋น„์Šค์—์„œ๋Š” ์–ด๋–ค ๋ฌธ์ œ์ ๋“ค์ด ์กด์žฌํ• ๊นŒ?

 

๊ฐ๊ฐ์˜ ์„œ๋ฒ„๋Š” ์™ธ๋ถ€ ์„ค์ • ์ •๋ณด๋ฅผ ์ž์‹ ์˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋‚ด์—์„œ ๊ด€๋ฆฌํ•˜๊ฒŒ ๋œ๋‹ค.

 

๋งŒ์•ฝ ์ด๋Ÿฐ ์ƒํ™ฉ์ด๋ผ๋ฉด ์–ด๋–จ๊นŒ?

 

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ŠคํŽ™์ด ๋ณ€๊ฒฝ๋˜์–ด JWT ๊ด€๋ จ๋œ ๊ณตํ†ต ์„ค์ • ์ •๋ณด๋ฅผ ๋ณ€๊ฒฝํ•ด์•ผ ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์„œ๋ฒ„๊ฐ€ 100๋Œ€๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด?

 

๊ทธ๋Ÿผ ๋‹ค๋ฅธ ์ƒ๊ฐ์„ ํ•  ์—ฌ์ง€ ์—†์ด 100๋Œ€์˜ ์„œ๋ฒ„์— ๊ณตํ†ต ์„ค์ • ์ •๋ณด์ธ JWT ์ •๋ณด๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ์ผ์„ ์ˆ˜๋™์œผ๋กœ 100๋ฒˆ ์ž‘์—…ํ•ด์•ผ ํ•œ๋‹ค.

 

๋‹จ์ˆœํ•œ ์ •๋ณด ํ•˜๋‚˜๊ฐ€ ๋ณ€๊ฒฝ๋œ ์ด์œ  ๋•Œ๋ฌธ์— ์ž˜ ๋Œ์•„๊ฐ€๋Š” 100๊ฐœ์˜ ์„œ๋ฒ„๋ฅผ ๋„๊ณ  ๋‹ค์‹œ ํ…Œ์ŠคํŠธ -> ๋นŒ๋“œ -> ๋ฐฐํฌ ๊ณผ์ •์„ ๊ฑฐ์ณ์•ผ ํ•œ๋‹ค.

 

CI CD ํŒŒ์ดํ”„๋ผ์ธ์ด ์ž˜ ๊ตฌ์ถ•๋˜์—ˆ๋‹ค๊ณ  ํ•˜๋”๋ผ๋„ ์ด๋Š” ํฐ ๋ถ€๋‹ด์ผ ์ˆ˜ ๋ฐ–์— ์—†๋‹ค.

 

๊ทธ๋Ÿผ ์ด๋Ÿฐ ์ƒ๊ฐ์„ ํ•  ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ?

 

๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค๋“ค์˜ ์™ธ๋ถ€ ์„ค์ • ์ •๋ณด๋“ค์„ ํ•˜๋‚˜์˜ ์„œ๋ฒ„์—์„œ ๊ด€๋ฆฌํ•˜๊ณ  ๊ฐ๊ฐ์˜ ์„œ๋ฒ„๊ฐ€ ์ด๋ฅผ ๊ฐ€์ ธ๊ฐ€๋Š” ๊ตฌ์กฐ๋Š” ์–ด๋–จ๊นŒ?

 

์ด๋Ÿฐ ์ด์œ  ๋•์— Config Store Server ๊ฐ€ ํƒ„์ƒํ•˜๊ฒŒ ๋˜์—ˆ๊ณ , Spring Cloud ์—์„œ๋Š” Spring Cloud Config ๋ฅผ ์ œ๊ณตํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.

 

Spring Cloud Config

Spring Cloud Config ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ๋ด๋ณด์ž.

Spring Cloud Config provides server-side and client-side support for externalized configuration in a distributed system. With the Config Server, you have a central place to manage external properties for applications across all environments.

Spring Cloud Config ๋Š” ๋ถ„์‚ฐ ์‹œ์Šคํ…œ (๋งˆ์ดํฌ๋กœ ์„œ๋น„์Šค)์—์„œ ์™ธ๋ถ€ ์„ค์ • ์ •๋ณด (application.yml ๋“ฑ๋“ฑ)์„ ์œ„ํ•œ ์„œ๋ฒ„์™€ ํด๋ผ์ด์–ธํŠธ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

 

Spring Cloud Config ์—์„œ๋Š” ์„œ๋ฒ„์™€ ํด๋ผ์ด์–ธํŠธ๋ฅผ ์ œ๊ณตํ•œ๋‹ค๊ณ  ํ•˜๋Š”๋ฐ, ์„œ๋ฒ„๊ฐ€ ๋ฐ”๋กœ ์„ค์ • ์ •๋ณด๋ฅผ ๋ชจ์•„๋†“๊ณ  ์ €์žฅํ•˜์—ฌ ๋ฟŒ๋ ค์ฃผ๋Š” ์ฃผ์ฒด๊ฐ€ ๋˜๊ณ  ํด๋ผ์ด์–ธํŠธ๋Š” ์„œ๋ฒ„์—์„œ ๋ฟŒ๋ ค์ฃผ๋Š” ์„ค์ • ์ •๋ณด๋ฅผ ๋ฐ›์•„ ์ด์šฉํ•˜๋Š” ์ธก์œผ๋กœ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๋˜ํ•œ Spring Cloud Config ๋Š” ๋‹จ์ง€ Spring Application ์—์„œ๋งŒ ๋Œ์•„๊ฐ€๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ ์–ธ์–ด๋กœ ์ž‘์„ฑ๋œ Applicaiton ์—์„œ๋„ ๋Œ์•„๊ฐ„๋‹ค๊ณ  ํ•œ๋‹ค.

 

Config File ๊ด€๋ฆฌ

Spring Cloud Config ์—์„œ๋Š” ์„ค์ • ์ •๋ณด๋“ค์„ ๋‹ค์–‘ํ•œ ๋ฐฉ์‹์œผ๋กœ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

  • FTP
  • native file system
  • Local Git Stage
  • Git Hosting Service (Github, Gitlab)

 

ํ•˜์ง€๋งŒ ๊ธฐ๋ณธ์ ์œผ๋กœ Spring Cloud Config๋Š” Git์„ ์ด์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•˜๊ณ  ์žˆ๊ณ  default ๊ฐ€ git ์—ฐ๊ฒฐ์œผ๋กœ ํ•œ๋‹ค.

 

๊ทธ๋ž˜์„œ ๋งŒ์•ฝ native file ์˜ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ค๊ณ ์‹ถ๋‹ค๋ฉด spring boot profile ์ •๋ณด๋ฅผ native๋กœ ๋ฐ”๊ฟ”์ค˜์•ผ ํ•œ๋‹ค.

 

์•„๋ž˜์—์„œ ์ž์„ธํžˆ ์‹ค์Šตํ•ด๋ณด์ž.

 

Architecture with Config server

Config ์„œ๋ฒ„๊ฐ€ ์šฐ๋ฆฌ์˜ ์•„ํ‚คํ…์ฒ˜๋กœ ๋“ค์–ด์˜จ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์„ ๊ฒƒ์ด๋‹ค.

 

  1. ๊ฐ๊ฐ์˜ ์„ค์ • ์ •๋ณด๋“ค์„ ๋นผ์„œ ํ•˜๋‚˜์˜ github repo ๋กœ ์—…๋กœ๋“œํ•œ๋‹ค.
  2. Config ์„œ๋ฒ„์™€ remote ์„œ๋ฒ„(github repository) ์„ ์—ฐ๊ฒฐํ•œ๋‹ค.
  3. Remote ์„œ๋ฒ„์™€ local git stage๋ฅผ ์„œ๋กœ ์—ฐ๊ฒฐํ•˜๊ณ  push ํ•œ๋‹ค.
  4. ๊ฐ๊ฐ์˜ Application ์„œ๋ฒ„๋Š” ์„ค์ • ์ •๋ณด๋ฅผ Config ์„œ๋ฒ„์—๊ฒŒ ์š”์ฒญํ•œ๋‹ค.

์‹ค์Šต

์šฐ๋ฆฌ์˜ ์‹ค์Šต์€ Github ์—์„œ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ Native File System ์—์„œ yml ํŒŒ์ผ๋“ค์„ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ฒƒ์ด๋‹ค.

 

Github ์—์„œ ์„ค์ • ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์€ ๋‹ค์Œ ์‹œ๊ฐ„์— ํ•ด๋ณผ ๊ฒƒ์ด๋‹ค.

 

์šฐ์„  ๊ฐ๊ฐ์˜ ์„œ๋น„์Šค๋“ค์„ ๊ธฐ๋™์‹œ์ผœ์ฃผ์ž.

 

  1. Eureka Server ๊ฐ€๋™
  2. Gateway Service ๊ฐ€๋™
  3. User Service ๊ฐ€๋™
  4. Order Service ๊ฐ€๋™

Config-Service

์ด์ œ ์ง‘์ค‘ํ˜• ์„ค์ • ๊ด€๋ฆฌ ์„œ๋ฒ„์ธ Config Service ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•ด๋ณด์ž.

 

ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ๊ณผ ์˜์กด์„ฑ ์ถ”๊ฐ€

 

ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ ์ดˆ๊ธฐ์— IDE ์—์„œ ์„ ํƒํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ๋งŒ์•ฝ ๊ทธ๋ ‡์ง€ ๋ชปํ•œ ๊ฒฝ์šฐ๋ผ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด gradle ์— ์ง์ ‘ ๋ช…์‹œํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

 

build.gradle

ext {
    set('springCloudVersion', "2020.0.2")
}

dependencies {
    implementation 'org.springframework.cloud:spring-cloud-config-server'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

ํ•ด๋‹น ์‹ค์Šต์€ IDE ์—์„œ ์˜์กด์„ฑ์„ ์„ ํƒํ•˜์ง€ ์•Š๊ณ  ์ง์ ‘ ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

Spring Cloud์˜ 2020 ๋ฒ„์ „๊ณผ cloud config ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.

 

ConfigServiceApplication.java

@SpringBootApplication
@EnableConfigServer
public class ConfigServiceApplication {

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

}

 

๊ทธ๋ฆฌ๊ณ  Application ์—์„œ @EnableConfigServer ๋ฅผ ์ด์šฉํ•ด์„œ Config ์„œ๋ฒ„๋กœ ๋™์ž‘ํ•˜๊ฒŒ ์ง€์ •ํ•ด์ค€๋‹ค.

 

Config Server ์—์„œ ์‚ฌ์šฉํ•  yml ํŒŒ์ผ ์ €์žฅ์†Œ

 

๋ณธ์ธ์˜ PC ์•„๋ฌด ๊ณณ์— configuration ํŒŒ์ผ๋“ค์„ ์ €์žฅํ•  ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค๊ณ  message.yml ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜์ž.

 

๊ทธ๋ฆฌ๊ณ  ํ•ด๋‹น ๋””๋ ‰ํ† ๋ฆฌ์— ์กด์žฌํ•˜๋Š” ํŒŒ์ผ์˜ ์ด๋ฆ„์— ๋”ฐ๋ผ์„œ Config ์„œ๋ฒ„์˜ url์ด ๊ฒฐ์ •๋˜๋Š”๋ฐ, ์ด๋Š” ๋‹ค๋ฅธ ์„œ๋น„์Šค๊ฐ€ ๊ฐ€์ ธ์™€์•ผํ•  name์ด ๋˜๊ณ  ์ด๋ฅผ ํ†ตํ•ด์„œ ์‹๋ณ„ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

 

message.yml ์•ˆ์— ๋“ค์–ด๊ฐˆ ๋‚ด์šฉ์€ ์•„๋ž˜์—์„œ ๋” ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ณ  ์ผ๋‹จ ํ•ด๋‹น ํŒŒ์ผ์˜ ๋””๋ ‰ํ† ๋ฆฌ์˜ ์œ„์น˜๋งŒ ํŒŒ์•…ํ•œ๋‹ค.

 

์ด์ œ Config Server๋Š” ํ•ด๋‹น yml ํŒŒ์ผ์„ ๋‹ค๋ฅธ ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค๋“ค์—๊ฒŒ ๋‚˜๋ˆ ์ค„ ๊ฒƒ์ด๋‹ค.

 

Config Server์˜ Application.yml ์ˆ˜์ •

server:
  port: 8888

spring:
  application:
    name: config-service

  profiles:
    active: native

  cloud:
    config:
      server:
        native:
          search-locations: file://${user.home}/์™ธ๋ถ€-์„ค์ •-ํŒŒ์ผ-๊ฒฝ๋กœ

๊ธฐ๋ณธ์ ์œผ๋กœ Config ์„œ๋ฒ„๋Š” 8888 ํฌํŠธ์—์„œ ๋™์ž‘ํ•˜๊ฒŒ ํ•˜๊ณ  config ํŒŒ์ผ๋“ค์„ ์–ด๋””์„œ ๋ถˆ๋Ÿฌ์˜ฌ์ง€ ์ง€์ •ํ•ด์ค€๋‹ค.

 

์šฐ๋ฆฌ๋Š” native file system ์—์„œ ๋ถˆ๋Ÿฌ์˜ฌ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— active๋ฅผ native ๋กœ ์ง€์ •ํ•ด์ฃผ์ž.

 

๊ทธ๋ฆฌ๊ณ 

 

spring.cloud.config.server.native.search-locations: file://${user.home}/ํŒŒ์ผ ๊ฒฝ๋กœ

๋ฅผ ์ด์šฉํ•ด์„œ ๋ถˆ๋Ÿฌ์˜ค๋„๋ก ํ•  ๊ฒƒ์ธ๋ฐ, ๊ฐ์ž ์ƒ์„ฑํ•œ ํด๋”์˜ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•ด์ฃผ์ž.


${user.home} ๋Š” ๋‹จ์ถ•์–ด๋กœ ํ™ˆ ๋””๋ ‰ํ† ๋ฆฌ์˜ ์œ„์น˜๋ฅผ ๋œปํ•œ๋‹ค.

 

๋งŒ์•ฝ native ํŒŒ์ผ์ด ์•„๋‹ˆ๋ผ git ํŒŒ์ผ์•„๋ผ๋ฉด ๋‹ค์Œ ์‹œ๊ฐ„์— ๋” ์ž์„ธํžˆ ์ด์•ผ๊ธฐํ•˜๊ฒ ์ง€๋งŒ git.uri ๋กœ ์ง€์ •ํ•ด์•ผ ํ•œ๋‹ค.

 

์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•ด์•ผํ•  ์ ์ด Spring Boot ์˜ default profiles๋ฅผ native๋กœ ๋ณ€๊ฒฝํ•ด์ค˜์•ผ ํ•œ๋‹ค. ๋งŒ์•ฝ profile์™€ active์— ๋Œ€ํ•ด์„œ ๋ชจ๋ฅธ๋‹ค๋ฉด Spring Boot ์˜ Profile๊ณผ ์„ค์ • ์ •๋ณด ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

native file directory ์—์„œ message.yml ์ž‘์„ฑํ•˜๊ธฐ

 

์•„๊นŒ ์ƒ์„ฑํ•œ ๋กœ์ปฌ ํŒŒ์ผ ๋””๋ ‰ํ† ๋ฆฌ์˜ message.yml ์„ ์ฑ„์›Œ๋ณด์ž.

 

message:
  owner: config-service's native folder
  content: :) ์•ˆ๋…•ํ•˜์„ธ์š” ๊ฐ๊ฐ์˜ ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค์—์„œ ์‚ฌ์šฉ๋  ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค. :)

spring:
  datasource:
    url: dbUrl
    driver: com.h2.Driver
    username: admin
    password: password1234

jwt:
  token:
    key: my_secret_key

 

๊ทธ๋ฆฌ๊ณ  api tester๋‚˜ ์›น ๋ธŒ๋ผ์šฐ์ €๋กœ localhost:8888/message/native ๋กœ ์ ‘์†ํ•ด๋ณด์ž.


๋‹ค์Œ ์‹œ๊ฐ„์— ๋” ์ž์„ธํžˆ ์ด์•ผ๊ธฐํ•˜๊ฒ ์ง€๋งŒ config server ์˜ url ์ •๋ณด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌ์„ฑ๋œ๋‹ค.

  • https://config-server.com/${name ์ •๋ณด}/${profile ์ •๋ณด}
    • application.yml : https://config-server.com/message/native

๊ทธ๋ฆฌ๊ณ  ์š”์ฒญ์„ ๋ณด๋‚ธ ๊ฒฐ๊ณผ๋ฅผ ๋‹ค์‹œ ๋ณด๋ฉด

 

 

ํ•ด๋‹น url๋กœ ์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด ์œ„์™€ ๊ฐ™์€ yml ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

 

์ด์ œ ๊ฐ๊ฐ์˜ ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค์—์„œ ์ด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์—ฐ๊ฒฐ์‹œ์ผœ๋ณด์ž.

 

User & Order Service

์ด์ œ ๊ฐ๊ฐ์˜ ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค์—์„œ ๋…๋ฆฝ์ ์ธ ์„ค์ • ํŒŒ์ผ์— ๋”๋ถˆ์–ด Config ์„œ๋ฒ„๊ฐ€ ๋‚ด๋ ค์ฃผ๋Š” ์„ค์ • ํŒŒ์ผ๋“ค์„ ์ด์šฉํ•ด์•ผ ํ•œ๋‹ค.

 

์„ค์ • ์ •๋ณด๋ฅผ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” bootstrap ์„ ์ด์šฉํ•˜๋Š”๋ฐ, 2021๋…„๋„ ๊ธฐ์ค€์œผ๋กœ spring cloud ์—์„œ๋Š” bootstrap ์„ ์ž๋™์œผ๋กœ ์ฝ์–ด์˜ค๋Š” ๊ธฐ๋Šฅ์ด ๋น ์ ธ์žˆ๋‹ค.

 

๊ทธ๋Ÿฌ๋ฏ€๋กœ spring-cloud-starter-bootstrap ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

 

์ฐธ๊ณ ๋กœ bootstrap.yml ์€ application.yml ๋ณด๋‹ค ๋จผ์ € ๋กœ๋“œํ•˜๊ธฐ ๋•Œ๋ฌธ์— config ์„œ๋ฒ„์˜ ์ •๋ณด๋ฅผ consuming ํ•˜๋Š” ์„œ๋ฒ„์—์„œ ์„ค์ • ์ •๋ณด๊ฐ€ ๋ˆ„๋ฝ๋˜์–ด application ์ด ์‹คํ–‰๋˜์ง€ ์•Š๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•œ๋‹ค.

 

๋งŒ์•ฝ ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด spring.cloud.bootstrap.enbled=true ๋กœ ์„ค์ •ํ•ด์•ผํ•˜๋Š”๋ฐ, ์šฐ๋ฆฌ๋Š” ์™ธ๋ถ€ ์„ค์ • ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ณณ์„ ๋ถ„๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด์„œ bootstrap ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•˜๊ณ  bootstrap.yml ํŒŒ์ผ์—์„œ config ๊ด€๋ จ ์ •๋ณด๋“ค์„ ๋‹ค๋ฃจ๋„๋กํ•˜์ž.

 

Config ์˜์กด์„ฑ ์ถ”๊ฐ€ ๋ฐ bootstrap ์˜์กด์„ฑ ์ถ”๊ฐ€

implementation 'org.springframework.cloud:spring-cloud-starter-config'
implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap'

bootstrap.yml ์ƒ์„ฑ

 

์•ž์„œ ์ด์•ผ๊ธฐํ•œ ๊ฒƒ ์ฒ˜๋Ÿผ bootstrap.yml ์—์„œ ์„ค์ •๋“ค์„ ๊ด€๋ฆฌํ•˜๊ธฐ๋กœ ํ–ˆ์—ˆ๋‹ค.


application.yml ํŒŒ์ผ๊ณผ ๋™์ผํ•œ depth ์—์„œ bootstrap.yml ํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•˜๊ณ  ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ช…์‹œํ•ด์ฃผ์ž.


config ์„œ๋ฒ„์—์„œ data๋ฅผ fetching ํ•˜๊ธฐ ์œ„ํ•ด์„œ config server์˜ uri ๋ฅผ ๋ช…์‹œํ•ด์ค€๋‹ค.

 

spring:
  cloud:
    config:
      uri: http://localhost:8888
      name: message

spring.cloud.config.uri ๋ฐ‘์— spring.cloud.config.name ์œผ๋กœ ์–ด๋–ค ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜ฌ์ง€ ๋ช…์‹œํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ง์ ‘ ๋ช…์‹œํ•˜์ง€ ์•Š์œผ๋ฉด application.yml ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜จ๋‹ค.

 

์œ„์™€ ๊ฐ™์ด ์ž‘์„ฑํ•˜๊ฑฐ๋‚˜ ํ˜น์€

 

spring:
  cloud:
    config:
      uri: http://localhost:8888
      name: message
      profile: native

 

๋กœ ์ง์ ‘ name๊ณผ profile ์„ ์ง€์ •ํ•ด์„œ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜๋„ ์žˆ๋‹ค.

 

ํ˜„์žฌ๋กœ์„œ๋Š” ์–ด๋– ํ•œ ๋ฐฉ๋ฒ•๋„ ์ƒ๊ด€ ์—†์œผ๋‹ˆ ํŽธํ•œ ๋ฐฉ๋ฒ•๋Œ€๋กœ ์ž‘์„ฑํ•˜์ž

 

ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•œ Request Mapping End Point ์ž‘์„ฑ

UserServiceApplication.class ์— Config ์„œ๋ฒ„์˜ configuration ์ •๋ณด๋ฅผ ์ž˜ ๋ถˆ๋Ÿฌ์˜ค๋Š”์ง€ ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•œ End Point ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

 

์—ญ์‹œ Eureka ์„œ๋ฒ„๊ฐ€ ๊ธฐ๋™๋˜์—ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ  ํ…Œ์ŠคํŠธํ•˜๊ฒ ๋‹ค.

 

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class UserServiceApplication {

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

    @GetMapping("/config")
    public String string(@Value("${message.owner}") String messageOwner,
                         @Value("${message.content}") String messageContent) {
        return "Configuration File's Message Owner: " + messageOwner + "\n"
                + "Configuration File's Message Content: " + messageContent;
    }

}

 

์œ„์˜ ์ฝ”๋“œ์— ๋Œ€ํ•ด์„œ ์ž ์‹œ ์ด์•ผ๊ธฐ๋ฅผ ํ•˜์ž๋ฉด @Value ์–ด๋…ธํ…Œ์ด์…˜์„ ํ†ตํ•ด์„œ Environment ๊ฐ์ฒด์— ์กด์žฌํ•˜๋Š” ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋Š”๋ฐ, ์šฐ๋ฆฌ๊ฐ€ bootstrap.yml ์„ ์ด์šฉํ•ด์„œ application.yml ํŒŒ์ผ๋ณด๋‹ค ๋จผ์ € ๋กœ๋“œํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์— ์„ค์ • ํŒŒ์ผ์— ์žˆ๋Š” message.owner ๊ณผ content๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

 

ํ…Œ์ŠคํŠธ

 

์šฐ๋ฆฌ์˜ ์„ค์ • ์ •๋ณด๋ฅผ config ์„œ๋ฒ„์—์„œ ์ž˜ ๋ถˆ๋Ÿฌ์™”๋Š”์ง€ gateway๋ฅผ ํ†ตํ•ด์„œ user-service ์— ์ ‘๊ทผํ•ด๋ณด์ž.

 

๋งŒ์•ฝ gateway ์„œ๋น„์Šค๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ๋ผ๋ฉด ๊ทธ๋ƒฅ user-service url์„ ํ˜ธ์ถœํ•ด๋„ ๋ฌด๋ฐฉํ•˜๋‹ค.

 

๊ทธ๋Ÿผ ์œ„์™€ ๊ฐ™์ด ์ž˜ ์ž‘๋™ํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

๋Œ“๊ธ€