Programming/Spring

Springcloud Config Server 설정 방법(git 활용)

로빈씨 2021. 9. 17. 16:18

$ ssh-keygen -m PEM -t rsa -b 4096 -C "your email address" -f config-server.id_rsa

일반적으로 Spring 어플리케이션을 개발하게되면 설정파일(properties, yml)을 로컬에서 관리하도록 합니다. 그러나 config server를 활용하면 원격으로 부터 설정을 불러와서 애플리케이션을 구동할 수 있기 때문에 운영 및 관리가 간단해 집니다. 특히 구동 중 설정을 변경해야할 경우가 있는데 config server를 이용하면 간단하게 원격 설정을 변경하는 것으로 반영이 될 수 있도록 합니다. 

config server는 다양한 설정 리포지토리를 지원합니다. 전통적인 file방식부터, git, db 등의 연동이 가능합니다.

본 글에서는 git을 이용한 방법을 설명합니다.

 

1. private repository

먼저 git repository를 생성합니다.

그리고 설정파일들을 미리 올려놓습니다.

설정파일의 파일 구조는 {application-name}-{profile}.yml 또는 {application-name}-{profile}.properties 입니다.

 

2. config-server에서 repository에 접근할 수 있도록 권한을 주기 위해 ssh key를 생성하여 git repository에 등록합니다.

ssh key가 없다면 아래 명령어를 통해 생성합니다.

$ ssh-keygen -m PEM -t rsa -b 4096 -C "your email address" -f config-server.id_rsa

 

gitlab 기준 해당 리포지토리에서 Settings-Repository-Deploy Keys에 공개키(pub)를 등록합니다.

 

3. spring cloud config 프로젝트 생성

build.gradle의 dependencies 참고.

dependencies {
    implementation 'org.springframework.cloud:spring-cloud-config-server'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    implementation group: 'org.springframework.boot', name: 'spring-boot-configuration-processor'
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-actuator'
}

ConfigServiceApplication

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class ConfigServiceApplication {

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

}

application.yml

privateKey에는 앞서 생성한 ssh key의 private key파일 text전문을 복사하여 붙여넣습니다.

server:
  port: 8888
spring:
  application:
    name: config-service
  cloud:
    config:
      server:
        git:
          uri: enter your git ssh url
          ignore-local-ssh-settings: true
          privateKey: |
            -----BEGIN RSA PRIVATE KEY-----
            Proc-Type: 4,ENCRYPTED
            DEK-Info: AES-128-CBC,1241234123123123124124

            edc3p7L/o3HRNlBEvyixViGRz+Cohh4wJa78EJV2Rp7c7paH8NQOxzhlAN2ijZ/i
            .............
            -----END RSA PRIVATE KEY-----
          strict-host-key-checking: false
          passphrase: enter your ssh password

 

4. config-server를 실행하면 정상적으로 동작합니다.

테스트는 postman이나 브라우저에서 http://localhost:8888/{application-nam}/{profile} 으로 호출해 봅니다.

만일 특정 설정 파일을 불러오고 싶다면 다음과 같이 콜을 해봅니다.

http://localhost:8888/{application-nam}/{profile}/{label}/log4j2.xml

ex) http://localhost:8888/clientService/dev/master/log4j2.xml

 

5.spring cloud config client 설정

build.gradle

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

configurations {
    all {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }
}

group = 'com.test.test'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    implementation('org.springframework.boot:spring-boot-starter-web')
    implementation('org.springframework.boot:spring-boot-starter-log4j2')
    implementation('org.springframework.cloud:spring-cloud-starter-config')
    implementation group: 'org.springframework.cloud', name: 'spring-cloud-starter-config', version: '2.2.8.RELEASE'
    implementation('org.springframework.boot:spring-boot-starter-actuator')
}

test {
    useJUnitPlatform()
}

log4j2 적용 및 테스트를 위해 configurations 부분 및 spring-boot-starter-log4j2 추가

server:
  port: 8889
spring:
  profiles: dev
  application:
    name: clientService
  cloud:
    config:
      uri: http://localhost:8888
      name: ${spring.application.name}, ${spring.application.name}-${spring.profiles}-db

logging:
  config: http://localhost:8888/workerService/dev/master/log4j2.xml

management:
  endpoints:
    web:
      exposure:
        include: refresh

cloud.config.name 부분에 ${spring.application.name}-${spring.profiles}-db 처럼 추가를 하면 다중 설정 파일을 로드 할 수 있음.

ex) clientService-dev-db.yml 파일 추가 로드 가능

logging.config 설정시 log4j2 설정파일 로드 가능.

 

반응형