Spring Cloud Config 의미

 

Spring Cloud Config는 분산 시스템에서 외부화된 구성에 대한 서버 측 미 클라이언트 측 지원을 제공합니다.

Config 서버를 사용하면 모든 환경에서 애플리케이션의 외부 속성을 중앙에서 관리할 수 있습니다.

클라이언트와 서버의 개념은 모두 Spring Environment 및 PropertySource 추상화와 동일하게 매핑되므로 Spring 응용 프로그램과 매우 잘 맞지만 모든 언어로 실행되는 응용 프로그램에서 사용할 수 있습니다.

애플리케이션이 개발에서 테스트로, 운영으로 배포 파이프라인을 통해 이동할 때 이러한 환경 간의 구성을 관리하고 마이그레이션 시 애플리케이션이 실행하는 데 필요한 모든 사항을 갖추도록 할 수 있습니다.

서버 스토리지 백엔드의 기본 구현은 git을 사용하므로 레이블링된 버전의 구성 환경을 쉽게 지원할 뿐만 아니라 컨텐츠 관리를 위한 다양한 툴링에 액세스할 수 있습니다.

대체 구현을 추가하고 Spring 구성을 통해 간편하게 연결할 수 있습니다.

 

https://cloud.spring.io/spring-cloud-config/reference/html/

 

Spring Cloud Config

Many source code repository providers (such as Github, Gitlab, Gitea, Gitee, Gogs, or Bitbucket) notify you of changes in a repository through a webhook. You can configure the webhook through the provider’s user interface as a URL and a set of events in

cloud.spring.io

 


 

이슈

 

서비스를 개발하고 운영을 하다보면 설정 값을 변경해야 하는 경우가 있다.

이 경우에 설정 값을 변경 후 빌드 후 배포하는 과정이 필요하다.

빌드 후 배포하는 과정에서 서버를 재기동해야한다.

Spring Cloud Config는 서버 재기동 없이 설정 값 변경이 가능하다.

 

인프라

 

 

Admin : 관리자 클라이언트

Main Server : Spring Cloud Config Client가 될 곳이다. 서비스 서버이다.

SCC Server : Spring Cloud Config Server이다. Repository 정보를 받아서 Client에 전달해준다.

Repository : 설정 값을 관리할 저장소이다. Git이 될 수도 있고, 로컬에서 관리할 수도 있다.

 

현재는 Admin에서 요청하면 Main Server를 거쳐서 SCC Server로 요청되고 Repository를 조회한 다음 반환된다.

 

구현 Spring Cloud Config Server

 

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cg.park</groupId>
    <artifactId>config-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config-server</name>
    <description>config-server</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>2020.0.4</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

 

application.yml

server:
  port: 9100 #SCC Server의 포트설정

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/qkrcksrbs8/project-config #Repository 주소.

위의 Repository 주소는 본인 로컬경로나 본인 깃주소로 사용하길 바랍니다.

 

ConfigServerApplication

package cg.park.configserver;

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

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

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

}

 

 

구현 Spring Cloud Config Client

 

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cg.park</groupId>
    <artifactId>config-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config-client</name>
    <description>config-client</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>2021.0.0</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

 

application.yml

server:
  port: 9000
spring:
  profiles:
    active: local
  config:
    import: "configserver:http://localhost:9100" # config server url
  cloud:
    config:
      name: config # config name
      profile: local # config sub name
      label: main # branch name
management:
  endpoints:
    web:
      exposure:
        include: refresh # refresh

 

ConfigClientApplication

package cg.park.configclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ConfigClientApplication {

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

}

 

ApiController

package cg.park.configclient.controllers;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ApiController {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @RequestMapping("/test")
    public String apiTest() {

        logger.info("### info !!!");
        logger.debug("### debug !!!");
        return "test입니다.";
    }

}

 

설명

 

서비스를 운영하다 보면 INFO 레벨로 자세한 로그 확인이 불가능하여

DEBUG로 로그레벨 변경 후 로그를 확인하는 경우가 있다.

하지만 로그레벨 변경 후 빌드를 하고 배포를 해야하기 때문에 서비스 운영에 문제가 있다.

 

Spring Cloud Config를 사용하면 빌드 후 재배포 과정없이 로그레벨 변경 가능하다.

 

Admin(관리자) 측이 설정 파일이 있는 Repository의 설정 값을 수정한다. (본인은 Git으로 했음. 로컬도 가능)

 

info 설정으로 되어있는 로그레벨을 debug로 수정.

 

Spring Cloud Config의 Refresh 기능으로 설정 값을 갱신한다.

 

PostMan을 이용하여 Refresh 사용

http://localhost:9000/actuator/refresh

 

로그레벨이 info일 때는 debug는 나오지 않음.

 

로그레벨을 debug로 바꾼 후에는 debug까지 나옴.

 

위의 과정은 서버 재기동없이 진행되었습니다.

 

깃 주소

 

Client : https://github.com/qkrcksrbs8/config-client2

Server : https://github.com/qkrcksrbs8/config-server


 

후기

 

Spring Cloud Config를 보고 처음에는 설정파일을 프로젝트로부터 분리시켜 관리를 용이하게 만든다. 라는 정보만 알고있었다.

위의 내용만으로는 굳이 지금 서비스에 적용할 이유가 없다.

하지만 현재 Spring Cloud Config 기술이 필요한 부분이 발생하여 적용하게 되었다.

위의 내용처럼 Log 레벨 변경 시 서버를 재기동하는 과정이 매우 불편했기 때문이다.

지금 예시로 보여주는 기능은 Log 레벨 변경이지만, 응용을 한다면 properties에 들어가는 모든 설정 값들을 외부파일로 분리시켜서 관리할 수 있다.

 

 

 

 

+ Recent posts