Spring Config Server

duckbill413·2024년 8월 5일
0

Spring Cloud

목록 보기
7/10
post-thumbnail

Spring Config Server은 중앙 집중식 구성 서버로, 분산 애프리케이션에서 구성을 외부화하고 중앙에서 쉽게 관리할 수 있도록 합니다.

이를 통해 애플리케이션 인스턴스가 증가하더라도 일관된 구성을 유지하고 쉽게 변경할 수 있습니다.

또한, application.yml등의 환경 정보가 변경될때 즉각적으로 서버에 반영될 수 있도록 하는 등의 기능을 별도로 제공합니다.


Spring Config Server 의 생성

  • 실습 환경 : spring boot 3.3.1
  • 자바 버전 : jdk 17
  1. Project 생성 (git 활용)

    1. spring config server 를 생성하기 앞서 실습을 위한 폴더를 생성

    2. 해당하는 폴더에서 powershell → git init 명령을 이용하여 git 으로 관리

      Web Git 환경에서 repository 를 생성하고 clone 한 뒤 작업하는 경우 로컬과 원격 사이에 working tree 가 일치하지 않아 문제가 생길 수 있으므로 local 에서 작업한 뒤 remote 와 연결 할 것

    3. 폴더에 환경 변수를 관리 하기 위한 yml 파일 생성 (ex: ecommerce.yml)

      token:
        expiration_time: 86500000
        secret: token-secret
      
      gateway:
        ip: 192.168.0.15
    4. 커밋하기 (푸쉬는 X) → 커밋하지 않을 경우 Config Server 가 브랜치를 찾지 못함

    5. Spring Project 생성

  2. 의존성 주입

    implementation 'org.springframework.cloud:spring-cloud-config-server'
  3. @EnableConfigServer

    @EnableConfigServer
    @SpringBootApplication
    public class ConfigServerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ConfigServerApplication.class, args);
        }
    
    }
    • @EnableConfigServer 어노테이션을 통해 Config Server 로 사용될 것임을 알려준다.
  4. application.yml 설정

    server:
      port: 8888
      
    spring:
      application:
        name: config-server
      cloud:
        config:
          server:
            git:
              uri: file:///D:/git/spring-cloud-microservice
    • Config Server 은 주로 8888 포트에서 실행
    • uri 에는 git 폴더의 위치를 지정
      • macOS: file://D:/git/spring-cloud-microservice
      • windowOS: file:///D:/git/spring-cloud-microservice
  5. Config Server 로 부터 환경 변수를 불러올 서버들의 설정

    1. dependency 추가

      // https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-config
      implementation 'org.springframework.cloud:spring-cloud-starter-config'
      
      // https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-bootstrap
      implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap'
      • bootstrap 라이브러리
        • 주로 Config Server과 함꼐 사용되어 애플리케이션이 시작되기 전에 외부 설정을 가져올 수 있도록 함
        • bootstrap.ymlapplication.yml 보다 먼저 로드 되며 외부 설정 서버와의 연결등을 포함
    2. bootstrap.ymlapplication.yml과 동일한 위치에 생성

      spring:
        cloud:
          config:
            name: ecommerce
            uri: http://127.0.0.1:8888
      #  profiles:
      #    active: dev # Configuration 정보의 Active profiles 에서 설정 가능
      • name: 사용할 yml 파일의 이름을 지정
      • uri: Config Server 의 주소를 입력
      • profiles: 실행할 yml의 Profile을 지정 (ex: dev 설정시 ecommerce-dev.yml)

Spring Config Server with Git

Spring Config Server 를 보다 원활하게 관리하고 다른 사람들과 동일한 정보를 유지하기 위해서 Git 에 연동할 수 있습니다.

  1. local git 폴더를 git repository와 연동하기

    1. git 에서 현재 local git 폴더를 push 하기 위한 repository 를 생성

    2. local git 폴더에서 console 창 열기

    3. remote 와 local git 연동

      git remote add origin {{git-repository-url}}
      git push --set-upstream origin master # 초기 설정시 필요
  2. Config Server application.yml 설정

    server:
      port: 8888
    
    spring:
      application:
        name: config-server
      cloud:
        config:
          server:
    	      git:
              default-label: master
              uri: https://github.com/duckbill413/spring-cloud-microservice.git
              #          username: [username] # private repository 인 경우
              #          password: [password] # private repository 인 경우
    • uri: git repository 주소
    • username, password: git repository 를 private 로 관리할 경우 username, password 를 입력

local folder 의 config 파일 활용

Git 과 연동을 완료하였으나 로컬 환경에서 개발 및 테스트를 위해 로컬 환경의 환경 변수를 활용하고 싶을 수 있다. 이때는 native 옵션을 활용하면 된다.

server:
  port: 8888

spring:
  application:
    name: config-server
  profiles:
    active: native # 로컬 폴더의 환경 파일을 읽기 위한 profile 설정 (default: )
  cloud:
    config:
      server:
        native:
          search-locations: file:///${user.home}/native-file-repo # 로컬 폴더의 환경 파일 위치
        git:
          default-label: master
          uri: https://github.com/duckbill413/spring-cloud-microservice.git
          #          uri: file:///D:/git/spring-cloud-microservice
          #          username: [username] # private repository 인 경우
          #          password: [password] # private repository 인 경우
  • profiles.active: native 로컬 파일을 활용할 것임을 알려줌
  • search-location: 로컬 환경 변수 파일들이 위치하는 폴더 경로 (${user.home} : window 는 C:\{username}
profile
같이 공부합시다~

0개의 댓글