-
Spring Cloud ConfigSpring Cloud/Spring Cloud Config 2022. 7. 10. 22:30728x90
❓Spring Cloud Config 란 ?
MSA 구조로 분산 시스템에서 설정 정보를 외부에 보관할 수 있도록 지원해주는 서비스
💪Spring Cloud Config 의 장점.
- 여러 서버의 설정 파일을 중앙 서버에서 관리 할 수 있다.
- 서버를 재배포 하지 않고 설정 파일의 변경 사항을 반영할 수 있다. (Actuator, Spring Cloud Bus)
⚙Spring Cloud Config 방식
- 구조 : 설정 파일이 저장되는 Config 저장소 (Git, Config Server 내부 파일 등) 가 있고, 이러한 저장소와 연결되어 설정 파일을 관리해주는 Spring Cloud Config Server, Spring Clout Config 를 이용하는 여러 Client 가 있다.
- Client 가 Spring Cloud Config Server 에게 설정 값을 요청.
- Server 는 설정 파일이 저장된 Config 저장소에 접근해 조건에 맞는 영역을 탐색한다. 탐색한 결과로 가져온 최신 설정 값을 가지고 Client 에게 전달하게 된다.
- 운영 중인 상태에 바뀐 설정 값을 갱신하고 싶다면 /actuator/refresh API 를 호출하여 갱신한다.
- Client 가 Server 에게 설정 값을 요청하고 이렇게 가져온 최신의 설정 값을 Client 에게 전달하고 이를 통해 변경된 설정 정보를 갱신하게 된다.
- Spring Cloud Config 를 이용하는 Client 수가 엄청나게 많다고 했을 때 /actuator/refresh API 를 일일이 호출하는 것도 일이 될 수 있다. Spring Cloud Bus 를 사용해 이러한 문제를 개선 할 수 있다.
📋구조
Repository (저장소)
- 실제 설정 정보들을 담고 있는 문서. Git에 YAML, application.properties 파일, Config Server의 문서 일 수도 있다. (현재 Git YAML 사용 - ironforge/cloude_config_repository )
- Repository가 public 이면 spring.cloud.config.server.git.uri 값만 필요하고, private 면 id, password 또는 SSH key인증 방식으로 사용 할 수 있습니다. (현재 id, password 사용)
- yaml 파일은 {application-name}-{env}.yaml 형식으로 입력한다.
(ex: finance 의 개발계 yaml을 가져온다 => finance-develop.yaml) - 접속 주소 방식은 다음과 같다. (label은 git의 branch 명이라 생각)
http://10.0.13.14:8889/finance/develop http://10.0.13.14:8889/finance-develop.yml http://10.0.13.14:8889/finance-develop.properties … Spring Cloude Config Server (응답)
- Client들의 요청을 받아 Repository에 접속하여 값들을 가져와 주는 역할을 한다. (http://10.0.13.14:8889)
- run Application.class에 @EnableConfigServer를 입력하면 Config Server가 활성화 된다.
- YAML 설정
server: prot: 8889 spring: cloud: config: server: git: uri: {Git Config Repository URI} username: {Git Config Repository Username} password: {Git Config Repository Password} basedir: repository ignoreLocalSshSettings: false strictHostKeyChecking: false default-label: develop force-pull: true #--------- # Git repository 가 아닌 server의 로컬 디렉토리의 설정파일을 가져오는 방법 spring: profiles: active: native cloud: config: server: native: search-location: classpath:/config
- 설정값.
- basedir: git을 사용하면 파일들을 로컬 시스템의 임시파일에 복제 되는데, 일부 운영 체제에서는 정기적으로 임시 디렉토리를 정리하여 그로 인해 프로퍼티가 유실되는 등의 예기지 못한 상황이 발생할 수 있는데, 이것을 방지하기 위해 저장 장소를 시스템 임시 구조에 속하지 않는 곳으로 변경할 때 사용
- git.uri , username, password: git repository의 연동 값.
- ignoreLocalSshSettings: username, password 대신 SSH 값으로 연동할 때 사용한다. (true or false)
- strictHostKeyChecking: host key 관련 에러에 대해 무시 여부를 정한다 (true or false)
- default-labal : Repository에 적은 yaml 형식에서 [{label}] 의 기본 값을 설정한다.
- force-pull : Spring cloud config server는 remote git repository 를 clone 하는데, pull하는 과정에 있어 local에 있는 값과 달라 에러가 났을 때 강제로 pull 을 하는 명령어
Spring Cloud Config Client (요청)
- 실제 서비스하는 곳으로 Spring Cloud Server로 설정 값을 요청하여 데이터를 받는 곳
- YAML 설정
spring: cloud: config: uri: http://localhost:8080 profile: ${spring.profiles.active} // local, develop, product
설정값
- uri : Spring Cloud Config Server 의 URL
- name : 가져올 YAML 파일이름
- profile: ${spring.profiles.active} : local, develop, product 등 해당 서비스의 profile을 지정.
build.gradle - def profiles = ‘local’ bootRun { args = [“--spring.profiles.active=” + profiles] }
- application.yml
- user-service.yml
- user-service-test.yml
- user-service-prod.yml
- order-service-prod.yml
그럼 각각의 name과 profiles 정보로 나눌 수 있다.
- application.yml
- name: none
- profiles: none
- user-service.yml
- name: user-service
- profiles: default
- user-service-test.yml
- name: user-service
- profiles: test
- user-service-prod.yml
- name: user-service
- profiles: profiles
- order-service-prod.yml
- name: order-service
- profiles: prod
yaml, properties 파일의 이름을 application-{profile name keyword}.yaml 규칙에 맞춰 만들면 Spring boot가 읽을 수 있게 된다.
ex) application-test.yaml, application-develop.yaml
(https://wonit.tistory.com/501)
Actuator (갱신)
- Spring Boot Actuator 는 애플리케이션을 Production 으로 빌드할 때 이를 모니터링 하고 관리하는 데 도움이 되는 기능을 가진 라이브러리이고, HTTP Endpoint를 통해서 어플리케이션의 상태 관리 및 모니터링을 할 수 있도록 한다.
- 위와 같이 Repository, Server, Client를 설정했고, 설정 정보 갱신은 Actuator의 Endpoint중에 refresh 를 사용한다.
더보기Endpoints? (https://toneyparky.tistory.com/6)
서비스 간의 요청과 응답을 위한 명세로 정리할 수 있다. Endpoints는 서비스를 사용 가능하도록 하는 서비스에서 제공하는 커뮤니케이션 채널의 한쪽 끝. 즉 요청을 받아 응답을 제공하는 서비스를 사용할 수 있는 지점을 의미한다
사용방법
- Git repository의 yaml 파일을 수정.
- Config client - application.yaml 설정에 actuator 명령어 추가.
management: endpoints: web: exposure: include: refresh
- refresh 할 controller에 @RefreshScope annotation 추가.
- http://10.0.13.14:8889/actuator/refresh 를 POST 방식으로 통신하면 변경된 값을 다시 가져온다.
단점 : MSA 구조에서 Git Repository의 변경에 따라 해당되는 Client를 수동으로 컨트롤 하기엔 무리가 있다.
728x90반응형LIST