Spring Cloud Config는 분산 시스템 환경에서 애플리케이션의 설정을 중앙 집중식으로 관리하고 제공하는 데 사용되는 도구다.
예를 들어, Spring Boot에서 자주 사용하는 application.properties나 application.yml 파일을 외부에서 관리할 수 있게 해주며, 애플리케이션의 설정을 한 곳에서 쉽게 수정하고 공유할 수 있게 해준다.
그리고 각 서비스를 다시 빌드하지 않고, 바로 적용할 수 있는 기능을 지원한다.
또한 각 환경(개발, 테스트, 운영 등) 별로 다른 설정을 관리할 수 있다.
Spring Cloud Config를 다양한 방법으로 사용하는 것을 알아보자.
구현
Spring Cloud Config를 Git을 통해 설정 파일을 관리할 수 있다.
Git에 설정 정보 파일을 올려두고 이 정보를 가져오는 방식으로 사용한다.
Git을 사용해 진행하기 위해 먼저 Local에 Git Repository를 생성하도록 하자.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
그리고 yml에서 설정 정보를 가져올 Spring Cloud Config의 주소를 입력해주자.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
그리고 이 서버의 주소(http://localhost:8080/test)로 요청을 보내보자.
아래와 같이 나온다면 성공이다!
이렇게 Git을 이용한 방법을 로컬에서뿐만 아니라 원격 저장소(Github)와 같은 곳에서도 가져올 수 있다.
config server의 yml을 아래와 같이 바꿔주고 해당 파일을 Github Repository에 업로드 하자.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
이 말은 취소 가능한 코루틴이 스스로 취소가 요청됐는지 검사해서 적절히 반응해줘야 한다는 말이다.
코루틴 Job의 cancel() 메서드를 호출하면 Job을 취소할 수 있다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
이 코드를 실행해보면 코루틴이 취소에 협력하지 않기 때문에 계속 실행되는 모습을 볼 수 있다.
이런 문제를 해결하는 방법으로 다음 작업을 시작하기 전에 코루틴이 취소됐는지 확인하는 방법이 있다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
부모 코루틴이 cancel() 메서드를 호출하면 squarePrinter의 상태가 취소 중으로 바뀌고 그다음 isActive 검사를 통해 루프를 종료시킬 수 있다.
다른 방법으로는 delay(), join(), yield() 등의 일시 중단함수를 이용해 CancellationException을 발생시키는 방법이 있다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
코루틴 취소에 대한 다른 사항으로, 부모 코루틴이 취소되면 자동으로 모든 자식의 실행을 취소한다.
이 과정은 부모에게 속한 모든 Job 계층이 취소될 때까지 계속된다.
아래 코드는 세 가지 자식 Job 중 어느 하나도 완료 상태가 되지 못한다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Ubuntu, Jenkins, Docker, Github 를 이용하여 로컬에서 Github로 push하면 Jenkins가 해당 commit을 감지한다.
Jenkins에선 해당 내용을 빌드하고 DockerHub로 push 한다. 또한 Ubuntu 서버에서 해당 도커 이미지를 pull & run 하여 서버를 실행한다.
코드 작성
단순하게 SpringBoot의 RestController에 루트 url로 요청을 하면 “Hello Jenkins”라는 글자가 나오는 API를 구현한다.
포트 번호는 80 이다.
이 API로 빌드 & 배포가 된 결과를 확인해보자
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
도커로 젠킨스 컨테이너를 실행하고 http://localhost:8080 주소로 들어가면 Jenkins 설치 창이 나온다.
여기서 초기 패스워드는 젠킨스 서버를 띄우는 로그에서 확인할 수 있다.
아니면 /var/lib/jenkins/secrets/initialAdminPassword 경로로 가서 확인할 수도 있다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
내용을 입력하고 Test Configuration 을 클릭했을 때 Success 가 나오면 성공이다.
젠킨스 파이프라인 생성
젠킨스 파이프라인을 작성해보자.
프리스타일 프로젝트, 파이프라인 프로젝트 등을 선택할 수 있는데, 파이프라인 프로젝트가 좀 더 세밀하게 제어할 수 있어서 선호하는 편이다.
Github project에 레파지토리의 url을 적고 Build Trigger 탭에서 GitHub hook trigger for GITScm polling을 체크한다.
Pipeline 탭에서는 아래와 같이 작성한다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
먼저 이벤트 처리에 필요한 Kafka와 Zookeeper를 Docker 컨테이너 형태로 띄워보자.
각 이벤트에 맞는 토픽을 1:1로 만들어준다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters