Spring Cloud Config

광부·2024년 5월 28일
0

Spring

목록 보기
8/8

왜 사용하는 거지?

클라우드 네이티브에서는 환경마다 다른 개발환경을 셋팅해주어야한다.

ex) 로컬에서는 컴퓨터의 메모리 저장소(h2, redis)나 mysql 과 같은 것을 사용하고, 운영이나 테스트 환경에서는 서버로 배포하여 각각의 환경이 분리되도록 설정해 주어야 한다.

변경사항을 클라이언트들이 어떻게 반영하지?

  1. 서버 재기동 : 서버를 내렸다가 다시 시작해야하기에 오버헤드가 크다
  2. Actuator Refresh : 수동으로 서버에게 리프레시를 지시해야하기에 번거롭다.
  3. Spring Cloud Bus : 클라이언트들이 직접 변경사항을 인식하고 갱신한다.

Spring Cloud Config 란?

  • MSA와 같은 분산 시스템에서 서버, 클라이언트 구성에 필요한 설정 정보(yaml 파일)를 외부 시스템에서 관리하기 위한 기술
    • 기존에는 Github의 Actions secrets and variables 을 통해 환경변수를 관리해 주었는데, 이 방법은 설정하고 나면 암호화되어 값의 확인이 불가능하다는 단점이 있다.
    • yml 파일로 설정을 관리하면 생산성이 향상되는 장점이 있음.
  • 하나의 중앙화 된 저장소에서 구성요소 관리 가능
    • Git Repository
    • AWS - secretmanager, paramstore, s3
    • gcp
    • svn
    • credhub
    • Local File System
    • Secure Vault storage 등
  • 각 서비스를 다시 빌드하지 않고, 바로 변경 사항을 적용할 수 있음.(중요)
  • 단점
    • Config Server에서 Config 값을 관리하기 때문에 장애가 전파될 수 있다.

네이밍 방법

  • {application-name}.yml
  • {application-name}-{profile}.yml

Config Server 설정

Dependencies

dependencies {
    implementation 'org.springframework.cloud:spring-cloud-config-server'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
  • Config Server 의존성을 추가해준다.

ecommerce.yml

spring:
  application:
    name: config-service
  cloud:
    config:
      server:
        git:
          uri: https://github.com/ajou20658/spring-cloud-config
#          username: [my username] # private 을 사용할 경우 명시 필요
#          passphrase: [my password] # private 을 사용할 경우 명시 필요
#          uri: file://C:\Users\kwy13\Desktop\git-local-repo
server:
  port: 8888

로컬의 깃에 설정 파일을 커밋해둔다면 로컬 환경에서도 사용할 수 있다.

원격의 깃의 설정 파일 또한 사용 가능하다.

application.java

@SpringBootApplication
@EnableConfigServer
public class ConfigServiceApplication {

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

}

기존의 코드에 @EnableConfigServer 어노테이션을 추가해줘야 ConfigServer가 활성화된다.

Other-Service 에서 Config 사용

의존성 추가

의존성에서 Cloud BootstrapConfig Client를 추가한다.

profile
백엔드 주니어 개발자

0개의 댓글

관련 채용 정보