▼ Backend/└ MSA, Spring Cloud

Spring Cloud MSA - 1 | Config-Server 구축하기

Valar 2021. 11. 12. 14:21
반응형

Spring Cloud Config Server란

  • 중앙 집중식 서비스로 애플리케이션 구성 데이터 관리를 담당하고 애플리케이션 데이터를 마이크로 서비스와 완전히 분리하는 역할을 담당한다.
  • 각각의 마이크로 서비스 배포 대상 환경에 맞게 구분하여 적용이 가능하다.
  • 설정이 변경되었을 때 서버의 재시작 없이 동적으로 적용된다.

설정 파일 GitHub

GitHub에 올라가 있는 파일 중 venh(application:name)-dev(profiles:active).yml를 예를 들어 설명하면 이 파일명은 마이크로 서비스 Properties Source에서 아래처럼 설정되어 사용된다.  지금 우리가 만드는 것은 마이크로 서비스의 config:import에 설정되는 Config Server를 만드는 것이다. 이러한 설정을 통해 마이크로서비스는 Config Server가 제공하는 설정들을 제공받는다.

 

spring: 
  application:
    name: venh
  profiles:
    active: dev
  config: 
    import: optional:configserver:http://localhost:8888

 

 

📌 프로젝트 설정 시작하기

build.gradle

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

group = 'com.venh.msa.config'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

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

dependencies {
    implementation 'org.springframework.cloud:spring-cloud-config-server'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

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

test {
    useJUnitPlatform()
}

 

application.yml

server: 
  port: 8888 
spring: 
  cloud: 
    config: 
      server: 
        git: 
          uri: https://github.com/conf312/venh-msa-config.git
          #username: 유저명
          #password: token

 

Config Server 설정

@EnableConfigServer를 Application 클래스에 추가한다.

 

@EnableConfigServer
@SpringBootApplication
public class VenhMsaConfigServerApplication

 

테스트

GitHub Repository에 올려놓은 설정 파일에 맞게 각각 호출해본다.

 

http://localhost:8888/venh/dev를 호출하면 GitHub에 venh-dev.yml이 호출된다.

{"name":"venh","profiles":["dev"],"label":null,"version":"cd49b9e89fdc5cfc582c890dbf259c8eb4ef73c7","state":null,"propertySources":[{"name":"https://github.com/conf312/venh-msa-config.git/file:C:\\Users\\conf3\\AppData\\Local\\Temp\\config-repo-2743901684204068862\\venh-dev.yml","source":{"venh.test":"venh-dev config test"}}]}

 

http://localhost:8888/venh/prod를 호출하면 GitHub에 venh-prod.yml이 호출된다.

{"name":"venh","profiles":["prod"],"label":null,"version":"cd49b9e89fdc5cfc582c890dbf259c8eb4ef73c7","state":null,"propertySources":[{"name":"https://github.com/conf312/venh-msa-config.git/file:C:\\Users\\conf3\\AppData\\Local\\Temp\\config-repo-2743901684204068862\\venh-prod.yml","source":{"venh.test":"venh-prod config test"}}]}

 

GitHub

 

Spring Cloud MSA - 1 | Config-Server 구축하기

Spring Cloud MSA - 2 | Config-Client 구축하기

Spring Cloud MSA - 3 | Eureka를 이용한 서비스 관리

 

Reference

반응형