본문 바로가기
IT/Spring Cloud

Spring Cloud 시리즈 2 - Spring Cloud Config

by 최고영회 2023. 5. 16.
728x90
반응형
SMALL

Spring Cloud Config

- 분산 시스템에서 외부화된 설정 정보를 서버 및 클라이언트에 제공하는 시스템

- Spring Cloud Config Server (설정 서버) 

  . Version 관리 Repository로 백업된 중앙 집중식 구성 노출 지원 

  . 특징

    1) HTTP, resource-based API for external configuration 

    2) Encrypt and decrypt 

    3) Embeddable easily in a Spring Boot application using @EnableConfigServer 

- Spring Cloud Config Client (설정 클라이언트)

  . 애플리케이션이 설정 서버에 연결하도록 지원 

  . 특징

    1) Bind to the Config Server and initialize Spring Environment with remote property sources

    2) Encrypt and decrypt 

- 단점은?

  . Git 서버 도는 설정 서버에 의해 장애가 전파 가능성

  . 우선 순위에 의해 설정 정보가 덮어씌워질 가능성 

     > 우선순위: 프로젝트의 application.yaml > 설정 저장소의 application.yaml > 프로젝트의 application-{profile}.yaml > 설정 저장소의 {application-name}/{application-name}-{profile}.yaml

     > 위 파일에 모두 같은 항목의 값이 있다면 최종적으로 {application-name}/{application-name}-{profile}.yaml 의 값

 

 

Spring Cloud Config Server

dependenciy 에 spring-cloud-config-server 추가

main class 에 @EnableConfigServer 추가

application.yaml 에 cloud 관련 설정

server:
  port: 8888
spring:
  application:
    name: config
  cloud:
    config:
      server:
        git:
          # git address
          uri: https://github.com/kimyounghoi/study-spring-cloud
          # git config files path
          search-paths: test-cloud-config-files/**
          # git branch name
          default-label: main

끝이다....

git 에 테스트용 yaml 파일들을 올려 두고

 

Spring Cloud Config Client 

dependency 에 spring-cloud-starter-config 추가 

main class 에 @EnableConfigurationProperties 추가 

Configuration 컴포넌트 추가 

@Configuration
@ConfigurationProperties(prefix = "private")
@RefreshScope
@Setter @Getter @ToString
public class PrivateProp {
    private String t1;
    private String t2;
    private String t3;
}

Test 용 Controller 추가

@RestController
@RequiredArgsConstructor @Slf4j
public class ConfigController {
    private final PrivateProp privateProp;
    
    @GetMapping("/private-config")
    public ResponseEntity<String> privateConfig() {
        log.info("private properties: {}", privateProp.toString());
        return ResponseEntity.ok(privateProp.toString());
    }
}

application.yaml > config 설정에 위에서 추가한 Spring Cloud Config Server 주소 입력

spring:
  application:
    name: yhkimsp
  profiles:
    active: prod
  config:
    import: optional:configserver:http://localhost:8888
server:
  port: 8080

테스트 해 보면

잘된다.

 

github public 을 private 으로 변경해 보자. 

ssh-keygen with `github 계정`

ssh-keygen -m PEM -t rsa -b 4096 -C kimyhcj@gmail.com

생성된 공개키 정보를 copy (cat kimyhcj.pub)

Settings > Deploy Keys > Add deploy key 로 추가 

SMALL

Settings > Danger Zone > Change repository visibility > Private (public --> private 으로 변경)

 

테스트 해 보면 > 접근 안되는 것을 확인 

Spring Cloud Config Server > application.yaml 을 수정

- git uri 변경

- private-key 입력 > 위에서 만들었던 kimyhcj.pub 는 public key 이고 함께 생성된 kimyhcj key 를 cat 으로 확인

- passphrase 입력 > key 생성 시 입력한 값 입력 

- 그 외 추가적인 setting 값 입력 

잘된다. 

 

private key 의 경우 암호화 해서 저장할 필요가 있고 암호화는 jasypt 를 이용하면 된다. 

https://kimyhcj.tistory.com/388 (암호화 방법 참고)

 

Spring Boot SSL 설정 팁 (alias 문제, jasypt를 이용한 암호화)

2020/02/20 - [IT/Spring] - Spring Boot HTTPS 설정 & WebClient HTTPS 이전 글에서 spring boot 에서 HTTPS 설정을 아주 쉽게 하는 방법을 작성했었다. 그런데 테스트 하다보니 재미있는(?) 현상이 발견 되었다. 바로 ss

kimyhcj.tistory.com

 

728x90
반응형
LIST