본문 바로가기

MSA

Spring Cloud Config - 1

목차

더보기

1. 이론

2. 설정

- Config 서버 생성

- Config server가 사용할 저장소 구현

이론

중앙화 된 설정 서버

  • Spring Cloud Config는 분산 시스템에서 환경설정을 외부로 분리하여 관리할 수 있는 기능을 제공한다.
  • Config Server를 사용하여 모든 환경(개발, 테스트, 프로덕션 등)에 대한 어플리케이션들의 속성을 한 곳에서 관리할 수 있다.

출처:https://coe.gitbook.io/guide/config/springcloudconfig

장점

  • 설정 관리의 용이성
  • 운영중에 서버 빌드 및 배포 없이 환경설정 변경 가능

설정

1. Config 서버 생성

1) Config Server Dependency 추가하여 새로운 스트링부트 프로젝트 생성

actuator dependency 추가 (actuator는 서버 구동 테스트용으로 사용 후 삭제)

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

2) ConfigApplication.java에 @EnableConfigServer 추가

@EnableConfigServer // 추가
@SpringBootApplication
public class ConfigApplication {

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

}

3) application.yml 설정

server:
  port: 8888

spring:
  application:
    name: {configServiceId}

2. Config server가 사용할 저장소 구현 (방법1. File System, 방법2. Git Repository)

Spring Cloud Config는 Git 이용을 권장하고 있고 git 연결이 default이다.

File System을 이용하고 싶으면 profiles를 native로 바꿔주면 된다.

1) Config File 관리

각 서비스 운영용 Config file 만들기

management:
  endpoints:
    web:
      exposure:
        include: health,busrefresh

방법1. File System

server:
  port: 8888

spring:
  application:
    name: {configServiceId}
  profiles:
    active: native
  cloud:
    config:
      server:
        native:
          search-locations: file://{Config File 경로}

2) 방법2. Git Repository

git에 private repository 생성해서 사용하는 방법 (방법1. GitHub 계정 직접 입력, 방법2. GitHub 접속용 SSH 키 등록)

방법1. GitHub 계정 직접 입력

spring:
  cloud:
    config:
      server:
        git:
          uri: {git repository 주소}
          username: {username}
          password: {password}

방법2. GitHub 접속용 SSH키 등록

1) SSH 키 등록

2) 설정 정보 추가

server:
  port: 8888

spring:
  application:
    name: {configServiceId}
  cloud:
    config:
      server:
        git:
          uri: {git repository 주소}
          default-label: main
          try-master-branch: false
          ignore-local-ssh-settings: true
          host-key: {someHostKey}
          host-key-algorithm: {ssh-rsa}
          private-key: {private key}
  • SSH Configuration Properties
ignoreLocalSshSettings If true, use property-based instead of file-based SSH config. Must be set at as spring.cloud.config.server.git.ignoreLocalSshSettings, not inside a repository definition.
privateKey Valid SSH private key. Must be set if ignoreLocalSshSettings is true and Git URI is SSH format.
hostKey Valid SSH host key. Must be set if hostKeyAlgorithm is also set.
hostKeyAlgorithm One of ssh-dss, ssh-rsa, ecdsa-sha2-nistp256, ecdsa-sha2-nistp384, or ecdsa-sha2-nistp521. Must be set if hostKey is also set.
defaultLabel The default label used for Git is main. If you do not set spring.cloud.config.server.git.defaultLabel and a branch named main does not exist, the config server will by default also try to checkout a branch named master. If you would like to disable to the fallback branch behavior you can set spring.cloud.config.server.git.tryMasterBranch to false.

references

https://coe.gitbook.io/guide/config/springcloudconfig

https://cloud.spring.io/spring-cloud-config/multi/multi__spring_cloud_config_server.html

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

https://wonit.tistory.com/502