본문 바로가기
SpringBoot

Spring Boot Config Server

by ByteBridge 2023. 9. 29.
반응형
1. Spring Boot Config Server 설정
build.gradle.kts
plugins {
    kotlin("jvm") version "1.6.0"
    id("org.springframework.boot") version "2.6.2"
    id("io.spring.dependency-management") version "1.0.11.RELEASE"
}

group = "com.example"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("org.springframework.boot:spring-boot-starter-configserver")
    implementation("org.springframework.cloud:spring-cloud-config-server")
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
}

tasks.test {
    useJUnitPlatform()
}
application.yml
server:
  port: 8888

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/your-username/config-repo.git

Main.kt

package com.example.configserver

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.cloud.config.server.EnableConfigServer

@SpringBootApplication
@EnableConfigServer
class ConfigServerApplication

fun main(args: Array<String>) {
    runApplication<ConfigServerApplication>(*args)
}
이 서버를 실행하면, http://localhost:8888/{application}/{profile} 으로 설정을 가져올 수 있습니다.
 
2. 클라이언트 애플리케이션 설정
build.gradle.kts
plugins {
    kotlin("jvm") version "1.6.0"
    id("org.springframework.boot") version "2.6.2"
    id("io.spring.dependency-management") version "1.0.11.RELEASE"
}

group = "com.example"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("org.springframework.cloud:spring-cloud-starter-config")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
}

tasks.test {
    useJUnitPlatform()
}

bootstrap.yml

spring:
  application:
    name: client-app
  cloud:
    config:
      uri: http://localhost:8888
      profile: dev

Main.kt

package com.example.client

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.beans.factory.annotation.Value
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController

@SpringBootApplication
class ClientApplication

fun main(args: Array<String>) {
    runApplication<ClientApplication>(*args)
}

@RestController
class ConfigController(@Value("\${custom.message}") val message: String) {

    @GetMapping("/message")
    fun message(): String {
        return message
    }
}
이제 config-repo.git에서 client-app-dev.yml이라는 파일을 생성하고, 그 안에 custom.message: Hello from Config Server!라고 작성한 후 커밋하면, 클라이언트 애플리케이션은 Config Server를 통해 이 메시지를 가져옵니다.

클라이언트 애플리케이션을 실행하고 http://localhost:8080/message에 액세스하면 "Hello from Config Server!" 메시지를 볼 수 있습니다.
반응형