Spring Cloud Config Monitor 와 Spring Cloud Bus, Kafka 를 이용한 설정 변경내용 적용 아키텍쳐
- Git Repository 에 별도로 구성되어있는 설정파일을 변경하면 Hook을 통해 Spring Cloud Config 의
/monitor
EndPoint 를 호출 - Spring Cloud Bus Kafka 가 Kafka 를 통해 Spring Cloud Config Client 들에게 refresh 메세지를 전송하여 변경된 설정사항이 적용되게 한다
Spring Cloud Config Architecher
1) Config Server 도메인 생성 및 ALB 호스트 헤더 기반 규칙 추가
ALB 호스트 헤더 기반 규칙 추가하는 방법은 링크 참조
https://freedeveloper.tistory.com/462
[AWS] ALB 호스트 헤더 기반 규칙 추가하는 방법
1. 도메인 생성 ALB 리스너 규칙에 등록하기 위해 Route53 도메인 생성 ALB 규칙 정의에서 호스트 헤더 규칙으로 처리할 것이기 때문에 규칙을 정의할 도메인에 다른 용도로 생성한 ALB Endpoint로 단순
freedeveloper.tistory.com
2) Spring Cloud Config Server 구성
1. Config Server Dependency 설정
Spring Cloud Config server 구성하기위해 필요한 Dependency 는
org.springframework.cloud:spring-cloud-starter-netflix-eureka-client
- Spring Cloud Bus를 사용하기 위해서 Config Server 를 Eureka 에 등록 시켜야 한다
org.springframework.cloud:spring-cloud-config-server
- Config Server Dependency
org.springframework.cloud:spring-cloud-starter-bus-kafka
- Kafka 로 bus-refresh 메세지를 전송하여 Config Client 들에게 모두 변경된 설정 정보가 적용될 수 있도록 한다
org.springframework.cloud:spring-cloud-config-monitor
- bus-refresh 가 가능한 /monitor 엔드포인트를 생성해준다
org.springframework.boot:spring-boot-starter-actuator
- health 및 refresh 처리를 위해 필요 monitor dependency 가 있다면 actuator 의 refresh 나 bus-refresh 는 사용하지 않음
2. Config Server Yaml 설정 내용
application.yml
server:
port: ${HOST_PORT:8888}
spring:
profiles:
#active: native
active: local
application:
name: config-server
cloud:
config:
server:
# native profile 시 로컬 PC에 있는 config 파일을 사용
native:
search-locations: file://${user.home}/config-local
# search-locations: classpath:config/
# 그외 profile 의 경우 git에 있는 config 파일을 사용
git:
uri: https://github.com/config/config-prod.git
username: username
password: password
search-paths: gateway
bus:
# Bus로 설정변경 새로고침 활성화
enabled: true
# bus-refresh EndPoint 활성화 호출시 전체 Config Bus Client 들에게 메세지 전송
refresh:
enabled: true
env:
enabled: true
# Spring Clud Bus Kafka 를 사용하기 위한 Kafka Broker 서버정보 입력
kafka:
bootstrap-servers: ${KAFKA_URI1:msk.kafka.ap-northeast-2.amazonaws.com:9092},${KAFKA_URI2:msk.kafka.ap-northeast-2.amazonaws.com:9092},${KAFKA_URI3:msk.kafka.ap-northeast-2.amazonaws.com:9092}
# Eureka Client 설정 등록
eureka:
instance:
# 서비스 이름 대신 IP 주소 등록
prefer-ip-address: true
# Necessary for Docker otherwise you will get 172.0.0.x IP
#ip-address: ${SERVER_IP:127.0.0.1}
hostname: ${spring.application.name}
# renewl 관련 interval은 변경하지 않는것을 권장 함(서버 내부적으로 client를 관리하는 로직 때문)
#lease-renewal-interval-in-seconds: 10 # 클라이언트가 서버로 하트 비트를 보내는 주기(default : 30초)
#lease-expiration-duration-in-seconds: 10 # 이 시간만큼 하트비트를 받지 못했을 때 해당 유레카 클라이언트를 서버에서 등록 해제(default: 90)
metadata-map:
zone: ${ZONE:zone1}
client:
# 유레카 서버에 서비스 등록
register-with-eureka: true
# 레지스트리 정보를 로컬에 캐싱
fetch-registry: true
prefer-same-zone-eureka: true
region: ap-northeast-2
service-url:
defaultZone: http://${EUREKA_SERVER1:172.31.10.114}:8761/eureka,http://${EUREKA_SERVER1:172.31.10.114}:8762/eureka,http://${EUREKA_SERVER2:172.31.11.28}:8761/eureka,http://${EUREKA_SERVER2:172.31.11.28}:8762/eureka
zone1: http://${EUREKA_SERVER1:172.31.10.114}:8761/eureka,http://${EUREKA_SERVER1:172.31.10.114}:8762/eureka
zone2: http://${EUREKA_SERVER2:172.31.11.28}:8761/eureka,http://${EUREKA_SERVER2:172.31.11.28}:8762/eureka
healthcheck:
enabled: true
availability-zones:
ap-northeast-2: zone1, zone2
# 유레카 클라이언트의 레지스트리를 받아오는 주기를 조정.
registry-fetch-interval-seconds: 10 # 서비스 목록 3초마다 캐싱(default : 30초)
disable-delta: true #캐싱할 때 변경된 부분만 업데이트(default : false)
# Actuator 허용 EndPoint 정의
management:
endpoints:
web:
exposure:
include: health,refresh,beans,httptrace,busrefresh
3. @EnableConfigServer 적용
@EnableConfigServer
@SpringBootApplication
class ConfigServerApplication
fun main(args: Array<String>) {
runApplication<ConfigServerApplication>(*args)
}
2. Spring Cloud Config Client 구성
1. Config Server Dependency 설정
Spring Cloud Config Client 구성을 위해 필요한 Dependency 는
org.springframework.cloud:spring-cloud-starter-netflix-eureka-client
- Spring Cloud Bus를 사용하기 위해서 Config Client 를 Eureka 에 등록 시켜야 한다
org.springframework.cloud:spring-cloud-starter-bus-kafka
- Kafka 로 bus-refresh 메세지를 전송하여 Config Client 들에게 모두 변경된 설정 정보가 적용될 수 있도록 한다
org.springframework.cloud:spring-cloud-starter-bootstrap
- 최신버전 Spring Cloud Config 설정에서 Deprecated 된 bootstrap 설정을 사용가능하게 해준다
org.springframework.boot:spring-boot-starter-actuator
- health 및 refresh 처리를 위해 필요 monitor dependency 가 있다면 actuator 의 refresh 나 bus-refresh 는 사용하지 않음
3. Config Client Yaml 설정 내용
아래 설정 파일에 없는 내용은 모두 Config Server에 접속해서 Git Repository 에 있는 파일에서 가져온다
bootstrap.yml
server:
port: ${HOST_PORT:5555}
# kill -15 : 정상 종료
# kill -9 : 강제 종료
shutdown: graceful
spring:
# 최신버전 Spring Cloud Config 에서 권장하는 방식
# config:
# import: "optional:configserver: http://localhost:8888"
cloud:
# 구버전 bootstrap 설정 방식
config:
# Config Server URI
uri: https://${CONFIG_URI:config.com}
# AppName
name: gateway-server
profile: prod
# true 일 경우 CONFIG_URI에 접속되지 않으면 서버가 기동되지 않는다
fail-fast: true
# Config Server 접속에 실패했을 때 재시도 정책
retry:
initial-interval: 10000 # 초기 간격: 초기 재시도 간격 ms 단위 10초
max-attempts: 20 # 최대 시도 횟수: 최대 시도 횟수 20회
max-interval: 15000 # 최대 간격: 최대 재시도 간격 ms 단위 15초
multiplier: 1.1 # 승수: 각 재시도 실패 후 재시도 간격의 배수
bus:
# Bus로 설정변경 새로고침 활성화
enabled: true
# bus-refresh EndPoint 활성화 호출시 전체 Config Bus Client 들에게 메세지 전송
refresh:
enabled: true
env:
enabled: true
# Actuator 허용 EndPoint 정의
management:
endpoints:
web:
exposure:
include: health,refresh,beans,httptrace,busrefresh
# gateway EndPoint 활성화
endpoint:
gateway:
enabled: true
3. 테스트
설정 값을 확인해볼 수 있는 API를 추가해서 테스트 하거나 Config Server 와 Gateway Server 의 로그를 tail 을 걸어 놓고 GitHub Hook 으로 Test Push Event 요청 후 Kafka 를 통해 메시지가 잘 전송되는지 확인
참조1: https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-%ED%81%B4%EB%9D%BC%EC%9A%B0%EB%93%9C-%EB%A7%88%EC%9D%B4%ED%81%AC%EB%A1%9C%EC%84%9C%EB%B9%84%EC%8A%A4
참조2: https://happycloud-lee.tistory.com/209
참조3: https://madplay.github.io/post/spring-cloud-config-using-git-webhook-to-auto-refresh
참조4: https://medium.com/@athulravindran/spring-cloud-config-server-auto-refresh-using-apache-kafka-in-kubernetes-86e3c427926e
참조5: https://cheese10yun.github.io/spring-cloud-config/
참조6: https://goateedev.tistory.com/167?category=874797
참조7: https://multifrontgarden.tistory.com/278
'SpringCloud' 카테고리의 다른 글
[Spring Cloud로 개발하는 마이크로서비스 애플리케이션] 2. Spring Cloud란? (0) | 2021.08.01 |
---|---|
[Spring Cloud로 개발하는 마이크로서비스 애플리케이션] 1. Microservice 소개 (0) | 2021.08.01 |
[11번가 Spring Cloud 기반 MSA로의 전환] 7. Appendix (0) | 2021.02.08 |
[11번가 Spring Cloud 기반 MSA로의 전환] 6. 모니터링 (0) | 2021.02.08 |
[11번가 Spring Cloud 기반 MSA로의 전환] 5. 11번가 MSA 환경 구성 (0) | 2021.02.08 |
댓글