Notice
Recent Posts
Recent Comments
Link
«   2025/03   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
Archives
Today
Total
관리 메뉴

csct3434

[Spring Cloud Config Server] Github Private Repository 연동하기 본문

개발 일지

[Spring Cloud Config Server] Github Private Repository 연동하기

csct3434 2025. 2. 8. 02:08

1. ECDSA 키 생성

ssh-keygen -t ecdsa -b 256 -f config-repo-key

 

생성 파일

  • 'config-repo-key' : 개인키 -> Config 서버에 등록
  • 'config-repo-key.pub' : 공개키 -> GitHub Repository에 등록

2. Github Repository에 공개키 등록

  • [Repository Settings] > [Deploy keys] -> [Add deploy key] 선택
  • 공개키 파일 (config-server-key.pub) 내용 붙여넣기
  • "Allow write access" 체크 해제 (읽기 전용)

3. Spring Cloud Config Server 설정

[build.gradle]

tasks.named("bootJar") {
    enabled = true
}

ext {
    set('springCloudVersion', '2024.0.0')
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

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

 

[application.yml]

server:
  port: 8888
spring:
  cloud:
    config:
      server:
        git:
          uri: git@github.com:example/config.git
          default-label: main
          ignore-local-ssh-settings: true
          private-key: |
            -----BEGIN OPENSSH PRIVATE KEY-----
            AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
            AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
            AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
            AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
            AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
            AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
            AAAAAAAAAAAAAAAA
            -----END OPENSSH PRIVATE KEY-----
          strict-host-key-checking: true
          hostKeyAlgorithm: ecdsa-sha2-nistp256
          host-key: AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBEmKSENjQEezOmxkZMy7opKgwFB9nkt5YRrYMjNuG5N87uRgg6CLrbo5wAdT/y6v0mKV0U2w0WZ2YB/++Tpockg=

 

  • uri : Github Repository > Code > SSH 주소
  • default-label : Repository 기본 브랜치명
  • private-key : 개인키 파일 (config-server-key) 내용 붙여넣기
  • strict-host-key-checking: true : GitHub 호스트 키 검증 활성화
  • host-key : GitHub의 호스트 키 / hostKeyAlgorithm : Github의 호스트 키 알고리즘
    • ssh-keyscan -t ecdsa github.com 명령어로 확인

 

[SpringApplication]

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

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}
  • @EnableConfigServer 어노테이션 추가

4. 테스트

요청

# 예시 : gateway.yml
curl http://localhost:8888/gateway/default

# 예시 : gateway-stage.yml
curl http://localhost:8888/gateway/stage

# 예시 : gateway-local.yml
curl http://localhost:8888/gateway/local

 

결과 : gateway.yml 

{
  "name": "gateway",
  "profiles": [
    "default"
  ],
  "label": null,
  "version": "585372dd4775ade63ff15580e3b78a1cf187cc8d",
  "state": "",
  "propertySources": [
    {
      "name": "git@github.com:example/config.git/gateway.yml",
      "source": {
        "GATEWAY.USER_SERVICE_URL": "http://${USER_SERVICE_URL:localhost}:8090",
        "GATEWAY.STORE_SERVICE_URL": "http://${STORE_SERVICE_URL:localhost}:8095",
      }
    }
  ]
}

5. 클라이언트측 application.yml 예시 (gateway 서비스)

  • spring.config.import : Config 서버 접속 주소
  • 각 서비스는 Config 서버에게 /${spring.application.name}/${spring.profiles.active} 경로로 요청을 보내 yml을 주입받는다