Spring Cloud Config

Dasole Kwon·2022년 9월 5일
0

Spring Cloud Config

  • 분산 시스템에서 서버, 클라이언트 구성에 필요한 설정 정보(application.yml)를 외부 시스템에서 관리
  • 하나의 중앙화 된 저장소에서 구성요소 관리 가능
  • 각 서비스를 다시 빌드하지않고, 바로 적응 가능
  • 애플리케이션 배포 파이프라인을 통해 DEV-UAT-PROD 환경에 맞는 구성 정보 사용

위 도식은 Spring Cloud Config 가 동작하는 과정이다. 조금 간단하게 설명하자면 우선 세 가지 요소로 생각하면 좋다.

실제로 설정파일이 저장되는 Config 저장소(Git Repository, File System 등)가 있고, 이러한 저장소와 연결되어 설정파일을 관리해주는 Spring Cloud Config Server 가 있다. 마지막으로 Spring Cloud Config 를 이용하고 싶은 다양한 Client 들이 존재한다.

Client 가 Spring Cloud Config Server 에게 설정값을 요청하면 Server 는 설정파일이 저장된 Config 저장소에 접근해 조건에 맞는 영역을 탐색한다. 탐색한 결과로 가져온 최신 설정값을 가지고 Client 에게 전달하게 된다.

위 과정은 최초 Client 구동시이고 운영중인 상태에 바뀐 설정 값을 갱신하고 싶다면 /actuator/refresh API 를 호출함으로써 가능하다. Client 가 Server 에게 설정값을 요청하고 이렇게 가져온 최신의 설정 값을 Client 에게 전달하고 이를 통해 변경된 설정정보를 갱신하게 된다.

즉, /actuator/refresh API 를 사용해 서버를 재배포하지 않고도 설정 정보를 갱신하는 것이다.

하지만 Spring Cloud Config 를 이용하는 Client 수가 엄청나게 많다고 했을 때 /actuator/refresh API 를 일일히 호출하는 것도 일이 될 수 있다.


Spring Cloud Config Server

Spring Cloud Config Server는 Config 저장소와 연결되어 중간 역할을 수행한다. 간단하게 몇가지 dependency와 설정 정보만 추가하면 된다.

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

위 Dependency를 추가하고 Spring Boot의 Auto Configuration을 이용해 @EnableConfigServer 어노테이션을 선언해주면 된다.

그리고 application.yml파일에 다음과 같은 설정 정보를 입력하면 Spring Cloud Config Server의 설정은 완료된다.

server:
    prot: 8888
spring:
    cloud:
    	config:
            server:
            	git:
                    uri: {Git Config Repository URI}
                    username: {Git Config Repository Username}
                    password: {Git Config Repository Password}
                    
                    # Spring Cloud ConfigRemote 에 있는 Git Repository 를 클론해온다.
                    # 하지만 이전에 클론해와서 로컬에 있는 정보의 폴더구조가 변경되는 등의 문제가 발생하면 제대로 업데이트할 수 없는데,
                    # 이러한 상황에서 강제로 Git Repository 를 클론하기 위한 설정이다.
                    force-pull: true
                    timeout: 30
                    
                    # Config 저장소 상에서 설정 파일을 탐색할 경로를 지정하는 설정이다.
                    # {application} 에는 Client 의 spring.application.name 값이 사용된다.
                    searchPaths:
                    	- '{application}'
                        - '{application}/*'

위 설정에서는 다양한 설정들을 해줬지만 간단하게 spring.cloud.config.server.git.uri만 있어도 동작은 한다. 조금 더 디테일한 부분을 설정해주는 코드라고 생각하면 된다.

하지만 해당 Config Server 를 사용하는 Client 가 여러개라면 searchPaths 설정은 해주는게 좋다고 생각한다. 이를 설정했을 때 Config 저장소에서 Client 별로 디렉토리를 구분지어서 설정 파일을 관리할 수 있기 때문이다.

아니면 spring.cloud.config.server.git.uri = https://github.com/{application} 와 같이 설정해 저장소 자체를 Client 별로 구분하도록 설정할 수도 있다.

searchPaths 에서 사용된 {application} 은 뭘까..?
Client 가 Spring Cloud Config Server 에 설정 값을 요청할 때 다양한 정보를 제공해주는데

{application} : Client 측의 spring.application.name 에 대한 정보
{profile} : Client 측의 spring.profiles.active 에 대한 정보
{label} : 버전을 매긴 설정파일들을 지정하는 Config Server 측의 기능
따라서 Spring Cloud Config Server 는 이러한 정보들을 활용해 설정 값에 사용할 수도 있고, 이를 통해서 적절한 설정 값을 찾아 Client 에게 보내주게 된다.

예를 들면 {application}-{profile}.yml 와 같은 설정 파일을 찾는 것이다.

그러면 Config Server 에서는 {application}.yml, {application}-{profile}.yml 와 같이 여러 설정 파일이 존재할 수 있는데 어떤 우선순위를 가지고 설정 파일을 선택할까?

1. {application}-{profile}.yml
2. {application}.yml
3. application.yml
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

Spring Cloud Config Client
Spring CLoud Config Client도 Server 못지 않게 몇가지 Dependency와 설정 정보로 충분히 구현이 가능하다.

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

spring-cloud-starter-config는 그렇다치더라도, spring-boot-starter-actuator는 어떤 역할을 수행하는가? actuator의 기본적인 역할은 해당 애플리케이션의 상태를 종합적으로 정리해서 Client에게 제공해주는 역할을 수행한다.

/actuator/health, /actuator/env, /actuator/info 등 다양한 API 로 애플리케이션의 정보를 제공한다.

# actuator 기본 설정
management:
  endpoint:
    health:
      show-details: never
  endpoints:
    web:
      base-path: /servicemanager
      exposure:
        include: "*"

그 중에서도 /actuator/refresh는 애플리케이션의 설정 파일을 다시 로딩하는 역할을 수행한다.즉, Spring Cloud Config와 결합하여 사용한다면 중앙에 있는 Config 저장소의 값을 변경하고 해당 API를 실행하게 된다면 재배포 없이도 변경한 설정 정보를 반영할 수 있다는 것이다.

이후 bootstrap.yml 파일에 Spring Cloud Config Server 에 대한 정보를 입력하면 된다.

spring:
  profiles: dev

  cloud:
    config:
      uri: "http://config.ch4njun.com:8888"
      profile: ${spring.profiles.active}, sample-${spring.profiles.active}
      label: develop

---
spring:
  profiles: qa

  cloud:
    config:
      uri: "http://config.ch4njun.com:8888"
      profile: ${spring.profiles.active}, sample-${spring.profiles.active}
      label: develop

spring.cloud.config.profile은 Spring Cloud Config Server에 전달할 Profiles 목록이다. Server는 해당 목록에 해당하는 모든 설정 파일을 Client에게 보내준다.

참조: 링크텍스트

0개의 댓글