본문 바로가기
FrameWork/Spring Cloud

Configuration Service

by 태윤2 2021. 9. 3.

Spring Cloud Config

  • 분산 시스템에서 서버, 클라이언트 구성에 필요한 설정 정보(application.yml(properties))를 외부 시스템에서 관리
  • 하나의 중앙화 된 저장소에서 구성요소 관리 가능
  • 각 서비스를 다시 빌드하지 않고, 바로 적응 가능
  • 애플리케이션 배포 파이프라인을 통해 DEV(개발) - UAT(테스트) - PROD(프로덕션) 환경에 맞는 구성 정보 사용

 

구성 정보 저장 장소

  • Private Git Repository
  • Secure Vault
  • Secure File Storage

위와 같은 저장 장소에 저장한 설정 정보를 Spring Cloud Config Server가 정보를 가져와 Service에 등록된 MicroService에 데이터를 전달 해줌

 

 

 

Local Git Repository

  • git-local-repo-springcloud 디렉토리 생성

 

  • git init

 

  • ecommerce.yml 파일 생성

 

  • vim ecommerce.yml or open with  VsCode

  • git add ecommerce.yml

 

  • git commit -m "upload an application yaml file"

우선순위

application.yml -> application-[name].yml -> application-[name]-[profile].yml

 

처럼 파일을 분리해 서비스, 구동환경에 따라 상황에 맞춰 호출을 할 수 있다.

 

 

Spring Config-Service

  • Config-Serivce 프로젝트 생성

  • @EnableConfigServer 어노테이션 추가

  • application.yml
 

2. Spring Cloud Config Server

Where should you store the configuration data for the Config Server? The strategy that governs this behaviour is the EnvironmentRepository, serving Environment objects. This Environment is a shallow copy of the domain from the Spring Environment (including

cloud.spring.io

/c는 빼고 경로 입력

server:
  port: 8888
spring:
  application:
    name: config-service
  cloud:
    config:
      server:
        git:
          uri: file://${user.home}/Desktop/git-local-repo-springcloud
  • server on

 

 

Users Microservice

의존성 추가

  • spring-cloud-starter-config
  • spring-cloud-starter-bootstrap
    • or) spring.cloud.bootstrap.enabled=true

 

bootstrap.yml

  • 외부의 application.yml(설정파일) 파일을 등록 해주는 역할
  • 기존의 application.yml(설정파일) 보다 먼저 로드 됨
spring:
  cloud:
    config:
      uri: http://127.0.0.1:8888
      name: ecommerce

 

UserContorller in user-service

@GetMapping("/health_check")
    public String status() {
        return String.format("It's Working in User Service"
                + ", port(local.server.port)=" + env.getProperty("local.server.port")
                + ", port(server.port)=" + env.getProperty("server.port")
                + ", token secret=" + env.getProperty("token.secret")
                + ", token expiration_time=" + env.getProperty("token.expiration_time")
        );
    }

 

ecommerce yml 파일을 불러옴

 

Change configuration values

  • 서버 재가동
    • user-service를 재기동 -> 매번 재기동 하는 방법은 좋은 방법은 아님
  • Actuator refresh
    • Spring Boot Actuator
      • Application 상태, 모니터링
      • Metric 수집을 위한 Http End point 제공
  • Spring cloud bus 사용

 

spring boot actuator

 

 

Spring Boot Actuator: Production-ready Features

HTTP Tracing can be enabled by providing a bean of type HttpTraceRepository in your application’s configuration. For convenience, Spring Boot offers an InMemoryHttpTraceRepository that stores traces for the last 100 request-response exchanges, by default

docs.spring.io

  • 의존성 추가

  • SpringSecurity actuator permit 추가
http.authorizeRequests().antMatchers("/actuator/**").permitAll();
  • actuator endpoints

 

  • application.yml 추가 (user-service)

  • health

  • beans

일부부만 캡쳐

  • refresh (method=POST)

값 변경후 변경된 부분을 적용시키고 표시 해줌

 

두개를 변경
refrest
서버 재기동없이 변경적용

 

 

Spring Cloud Gateway에 config 적용

  • 의존성 주입

  • bootstrap.yml 추가
spring:
  cloud:
    config:
      uri: http://127.0.0.1:8888
      name: ecommerce
  • HttpTraceRepository를 리턴하는 httpTraceRepository메서드 생성

  • application.yml에 actuator 라우터 등록

 

Mulitple environments

  • 설정 정보에 해당하는 이름을 넣어 여러개의 application.yml 파일을 만들어보자

 

  • ecommerce-dev.yml -> service파일 호출용
  • ecommerce-uat.yml
  • ecommerce-prod.yml -> gateway파일 호출용

아무런 정보가 없으면 ecommerce.yml를 호출(default파일)

  • dev,prod yml 파일 생성

  • user-service, gateway profiles 추가

user-service
gateway

 

  • RunConfiguration에서 Active Profiles 설정가능

Remote Git Repository

  • $   git   remote   -v
  • $   git  remote   add   origin  [repository url]
  • $   git  push   --set-upstream   origin   master

config-service application.yml 변경

 

Native File Repository

  • local file system에 저장하기

 

config-service application.yml에 추가

 

 

'FrameWork > Spring Cloud' 카테고리의 다른 글

암호화(Encryption) - 대칭키  (0) 2021.09.05
Spring Cloud Bus  (0) 2021.09.04
E-Commerce 애플리케이션  (0) 2021.08.26
Spring Cloud Gateway - Load Balancer  (0) 2021.08.25
API Gateway Service  (0) 2021.08.25