toy project-msa: Spring Cloud Config

bo-yoonยท2021๋…„ 11์›” 2์ผ
0

msa

๋ชฉ๋ก ๋ณด๊ธฐ
3/7
post-thumbnail

๐Ÿค” ์Šคํ”„๋ง ํด๋ผ์šฐ๋“œ ์‹œ๋ฆฌ์ฆˆ!

์Šคํ”„๋ง ํด๋ผ์šฐ๋“œ ์ปจํ”ผ๊ทธ ์„œ๋ฒ„๋ฅผ ์„ธํŒ…ํ•˜๊ณ  ํ™•์ธํ•ด๋ณด์ž

์Šคํ”„๋ง ํด๋ผ์šฐ๋“œ ์ปจํ”ผ๊ทธ ์„œ๋ฒ„๋Š” ๋ฌด์—‡์ธ๊ฐ€?

ํด๋ผ์šฐ๋“œ ๋„ค์ดํ‹ฐ๋ธŒ์˜ 12 factor (https://12factor.net/ko/ ) ์š”์†Œ ์ค‘ ํ•˜๋‚˜๊ฐ€ ์„ค์ •์„ ์ฝ”๋“œ์—์„œ ์—„๊ฒฉํ•˜๊ฒŒ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ ์ด๋‹ค

์Šคํ”„๋ง ํด๋ผ์šฐ๋“œ ์ปจํ”ผ๊ทธ๋Š” ์ด ๊ธฐ๋Šฅ์„ ์Šคํ”„๋ง ๋ถ€ํŠธ ํ”„๋ ˆ์ž„์›Œํฌ ๊ธฐ๋ฐ˜์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๋”ฐ๋ผ์„œ MSA ํ™˜๊ฒฝ์—์„œ ์—ฌ๋Ÿฌ ์„œ๋ฒ„์— ๋Œ€ํ•œ ์„ค์ •์„ ๊ณตํ†ต์œผ๋กœ ๊ด€๋ฆฌํ•˜๋ฉฐ ์ฝ”๋“œ ์ค‘๋ณต์„ ์ค„์—ฌ์ค€๋‹ค.

"Config Server" ์™€ "Config Client", ์„ค์ • ํŒŒ์ผ(์ฃผ๋กœ ๊นƒ์— ๊ด€๋ฆฌํ•œ๋‹ค) ๊ตฌ์„ฑ๋˜์–ด ์žˆ์œผ๋ฉฐ, git์— ์˜ฌ๋ฆฐ ์„ค์ • ํŒŒ์ผ์„ Config Server๊ฐ€ ๋Œ์–ด์™€ ๋„์›Œ์ฃผ๊ณ , ์šฐ๋ฆฌ๊ฐ€ ์ž‘์„ฑํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์€ ( Config Client) ๋Š” ๋„์›Œ์ง„ ์„ค์ • ํŒŒ์ผ์„ ๊ฐ€์ ธ์™€ ์‚ฌ์šฉํ•œ๋‹ค.

์ด์ œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด๋ณด์ž!




Config Server

๋จผ์ €, Config Server ๋ฅผ ์„ธํŒ…ํ•ด๋ณด์ž!

Application ์ƒ์„ฑ

์Šคํ”„๋ง ๋ถ€ํŠธ ์Šคํƒ€ํ„ฐ์—์„œ https://start.spring.io/ ์—์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ๋งŒ๋“ค์–ด ์คฌ๋‹ค.

gradle
java 11
springboot version : 2.5.6
packaging : jar
dependency : web



Gradle

gradle ์˜์กด์„ฑ์„ ์„ธํŒ…ํ•ด์ฃผ์ž


plugins {
	id 'org.springframework.boot' version '2.5.6'
	id 'io.spring.dependency-management' version '1.0.11.RELEASE'
	id 'java'
}

group = 'com.springcloud'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'


apply plugin: 'io.spring.dependency-management'

repositories {
	mavenCentral()
}


ext {
	set('springCloudVersion', "2020.0.4")
}
apply plugin: "io.spring.dependency-management"
dependencyManagement {
	imports {
		mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
	}
}


dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-web'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'

	//actuator
	implementation 'org.springframework.boot:spring-boot-starter-actuator:2.1.8.RELEASE'

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

test {
	useJUnitPlatform()
}


์–ด๋–ค ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ–ˆ๋Š”์ง€ ํ™•์ธํ•ด๋ณด๋ฉด, "Config Server" ๋กœ์„œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•  ์˜์กด์„ฑ spring-cloud-config-server ๋ฅผ ์ถ”๊ฐ€ํ•˜์˜€๊ณ 

implementation 'org.springframework.cloud:spring-cloud-config-server'

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋ชจ๋‹ˆํ„ฐ๋งํ•˜๊ณ  ์ƒํƒœ๋ฅผ ์ œ๊ณตํ•ด์ฃผ๋Š” spring-boot-starter-actuator ๋ฅผ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค. (์„ ํƒ์‚ฌํ•ญ)

implementation 'org.springframework.boot:spring-boot-starter-actuator:2.1.8.RELEASE'


git์— ์„ค์ • ์˜ฌ๋ฆฌ๊ธฐ

github ์— ๋ ˆํฌ์ง€ํ† ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์•„๋ž˜์™€ ๊ฐ™์€ ๊ตฌ์กฐ๋กœ

โ”œโ”€โ”€ dev
โ”‚   โ””โ”€โ”€ profile-dev.yml
โ”œโ”€โ”€ prd
โ”‚   โ””โ”€โ”€ profile-prd.yml
โ””โ”€โ”€ stg
    โ””โ”€โ”€ profile-stg.yml

๋‹ค์Œ ๋‚ด์šฉ์˜ ํŒŒ์ผ์„ ์˜ฌ๋ ค์ฃผ์—ˆ๋‹ค.

config:
  name: dev
info:
  description: Spring Cloud Dev



Config ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— application.yml ์„ค์ •

resources ๋ฐ‘์— application.yml ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ 

์•„๋ž˜์™€ ๊ฐ™์ด ๊ฐ’์„ ์ง‘์–ด ๋„ฃ์—ˆ๋‹ค
(Tmi : ํ˜„์žฌ ์„ธํŒ…ํ•œ ์Šคํ”„๋ง๋ถ€ํŠธ ๋ฒ„์ „์—์„œ๋Š” bootstrap.yml์ด ์ง€์›์•ˆ๋œ๋‹ค)



Application.yml


server:
  port: 8888


spring:
  application:
    name: configserver
  cloud:
    config:
      server:
        git:
          uri: https://github.com/bo-yoon/springcloud-config-profiles.git
          basedir: target/config
          searchPaths: dev, stg, prd

management:
  endpoints:
    enabled-by-default: false
  endpoint:
    info:
      enabled: true

์„ค์ •์„ ํ•˜๋‚˜ํ•˜๋‚˜ ์‚ดํŽด๋ณด์ž

  • ์„œ๋ฒ„ ํฌํŠธ๋Š” ํฌํŠธ๋ฒˆํ˜ธ๋ฅผ 8888 ๋กœ ์ง€์ •ํ•˜๋Š” ์„ค์ •์ด๋‹ค
  • spring.application.name์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ด๋ฆ„์„ ์ง€์ •ํ•œ๋‹ค.
  • cloud.config.server.git.uri์€ ์•„๊นŒ ์Šคํ”„๋ง ๋ถ€ํŠธ ์„ค์ •ํŒŒ์ผ์„ ์˜ฌ๋ฆฐ ๋ ˆํฌ์ง€ํ† ๋ฆฌ url์„ ์„ธํŒ…ํ•ด์ฃผ์—ˆ๋‹ค.
  • basedir์€ config ์ •๋ณด๋ฅผ ์ €์žฅํ•  ์œ„์น˜์ด๋‹ค. target/config ๋ผ๊ณ  ์„ธํŒ…ํ•˜๋ฉด

    ์œ„์™€ ๊ฐ™์ด ์„ธํŒ…๋œ๋‹ค.
  • searchPaths๋Š” ๋””๋ ‰ํ† ๋ฆฌ ์•ˆ์— yml ํŒŒ์ผ ๋ณด๊ด€์‹œ ์ฐพ์•„๊ฐ€๊ธฐ ์œ„ํ•œ ์„ค์ •์ด๋‹ค
  • management.endpoint.enabled-by-default ๋Š” ๊ธฐ๋ณธ์ ์ธ actuator์˜ ๊ธฐ๋ณธ์—”๋“œํฌ์ธํŠธ๊ฐ€ false๋กœ ์„ค์ •๋˜๊ณ , ์•„๋ž˜ endpoint ์ •๋ณด์—์˜ํ•ด์„œ ํŠน์ • ์ •๋ณด๋งŒ true๋กœ ์„ธํŒ…ํ•œ๋‹ค.


@EnableConfigServer ์„ค์ •

main์— @EnableConfigServer ์„ ๋ถ™์—ฌ ์ค€๋‹ค.



@EnableConfigServer
@SpringBootApplication
public class ConfigApplication {

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

}

์ด๋ ‡๊ฒŒ ์„ธํŒ…ํ•˜๊ณ  ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹คํ–‰ํ•˜๋ฉด ๋œ๋‹ค!



ConfigServer ์‹คํ–‰

localhost:8888/application/dev ๋กœ ์ ‘๊ทผํ•ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ธํŒ… ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.




Config Client

์ด๋ฒˆ์—๋Š” "Config Client" ๋ฅผ ์„ธํŒ…ํ•ด๋ณด์ž!

Application ์ƒ์„ฑ

Config Server ๋•Œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์Šคํ”„๋ง ๋ถ€ํŠธ ์Šคํƒ€ํ„ฐ์—์„œ https://start.spring.io/ ์—์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ๋งŒ๋“ค์–ด ์คฌ๋‹ค.

gradle
java 11
springboot version : 2.5.6
packaging : jar
dependency : web



Config Client Gradle ์„ธํŒ…

๊ทธ๋ฆฌ๊ณ  gradle ์˜์กด์„ฑ์„ ์„ธํŒ…ํ•ด์ค€๋‹ค.
์—ฌ๊ธฐ์„œ ์ฐธ๊ณ ํ•  ์ ์€ mvn repository ์—์„œ ์˜์กด์„ฑ์„ ์ฐพ์„ ๋•Œ "Spring Cloud Starter config"์™€ "Spring Cloud Config Client" ๊ฐ€ ๋ณด์ด๋Š”๋ฐ ์‚ฌ์‹ค ์ด ๋‘˜๋‹ค Config Client ๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๋‘˜ ์ค‘ ํ•˜๋‚˜๋งŒ ์„ธํŒ…ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. ๋‘˜์˜ ์ฐจ์ด๋Š” Spring Cloud Starter Config๊ฐ€ Spring Cloud Config client ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋‹ค.

๋˜ํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‹คํ–‰๋˜๊ธฐ ์ „์— config ์ •๋ณด๋ฅผ localhost:8888์— ๋– ์žˆ๋Š” config server์—์„œ ๊ฐ€์ ธ์™€์•ผ ํ•ด์„œ application.yml ๋ณด๋‹ค ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’๊ณ , ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹œ์ž‘์ „ ์‹คํ–‰๋˜๋Š” bootstrap starter๋„ ์ง‘์–ด ๋„ฃ์–ด์ค€๋‹ค.

์ฐธ๊ณ ๋กœ bootstrap๊ฐ™์€ ๊ฒฝ์šฐ ํ˜„์žฌ ์„ธํŒ…ํ•œ ์Šคํ”„๋ง 2.5.6๋ฒ„์ „์—์„œ๋Š” ๋”ฐ๋กœ ์˜์กด์„ฑ์„ ์ง€์ •ํ•ด์•ผํ•˜์ง€๋งŒ 2.3.x ๋Œ€ ๋ฒ„์ „ ์ด์ „๊ฐ™์€ ๊ฒฝ์šฐ ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

๋”ฐ๋ผ์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด Gradle์˜์กด์„ฑ์„ ์„ธํŒ…ํ•ด์ค€๋‹ค.

plugins {
	id 'org.springframework.boot' version '2.5.6'
	id 'io.spring.dependency-management' version '1.0.11.RELEASE'
	id 'java'
}

group = 'com.springcloud'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

apply plugin: 'io.spring.dependency-management'

repositories {
	mavenCentral()
}


ext {
	set('springCloudVersion', "2020.0.4")
}
apply plugin: "io.spring.dependency-management"
dependencyManagement {
	imports {
		mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
	}
}


dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-web'
	developmentOnly 'org.springframework.boot:spring-boot-devtools'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'


	//actuator
	implementation 'org.springframework.boot:spring-boot-starter-actuator:2.1.8.RELEASE'

	// config
	implementation group: 'org.springframework.cloud', name: 'spring-cloud-starter-config', version: '3.0.5'
    implementation group: 'org.springframework.cloud', name: 'spring-cloud-starter-bootstrap', version: '3.0.4'

}

test {
	useJUnitPlatform()
}



bootstrap.yml ์„ธํŒ…

์ด์ œ config server๋ฅผ ๋ฐ”๋ผ๋ณด๋Š” bootstrap.yml์„ ๋งŒ๋“ค์–ด๋ณด์ž.

Application.yml

server:
  port: 9090

spring:
  application:
    name: api-member
  cloud:
    config:
      uri: http://localhost:8888


ํ…Œ์ŠคํŠธ ์šฉ Controller ์ž‘์„ฑ

ํ•ด๋‹น ์œ„์น˜์— ๊ฐ„๋‹จํ•œ Config ํ…Œ์ŠคํŠธ์šฉ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค.

@RestController
public class ConfigClient {

    @Value("${config.name}")
    private String profile;

    @GetMapping( value = "/config/profile", produces = "application/json; charset=UTF8")
    public String configConnection() {
         return profile;
    }
}


์‹คํ–‰

์ด์ œ ์ „๋ถ€ ์„ธํŒ…์ด ์™„๋ฃŒ๋˜์—ˆ์œผ๋‹ˆ ์‹คํ–‰ํ•ด๋ณด์ž

๋จผ์ € ๋”ฐ๋กœ profile ์„ธํŒ…์„ ํ•˜์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ ์ธํ…”๋ฆฌ์ œ์ด์—์„œ VM options์„ ์‚ฌ์šฉํ•ด์„œ profile์„ ์žก์•„๋ณด์ž

๊ทธ๋ฆฌ๊ณ  ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹คํ–‰ํ•ด์ค€๋‹ค.

๊ทธ๋Ÿฌ๋ฉด profile๊ฐ€ dev๋ผ๊ณ  ๋“ค์–ด๊ฐ„ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

๋˜ ํ•ด๋‹น url ์„ ํ˜ธ์ถœํ•ด๋ณด๋ฉด config.name์„ ์ •์ƒ์œผ๋กœ ์ฝ์–ด์˜ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.



tobe...

๋‹ค์Œ์—๋Š” ์Šคํ”„๋ง ํด๋ผ์šฐ๋“œ ๋„ทํ”Œ๋ฆญ์Šค oss์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž

profile
๊ฐœ๋ฐœ ๋กœ๊ทธ ๐ŸŽ ๐ŸŽ ๐ŸŽ

0๊ฐœ์˜ ๋Œ“๊ธ€