Spring Cloud Config

이재철·2021년 8월 21일
0

MSA

목록 보기
4/13
  • 분산 시스템에서 서버 클라이언트 구성에 필요한 설정 정보(application.yml)를 외부 시스텝에서 관리
  • 하나의 중앙화 된 저장소에서 구성요소 관리 가능
  • 각 서비스를 다시 빌드하지 않고 바로적용가능
  • 애플리케이션 배포 파이프라인을 통해 dev-uat-prod 환경에 맞는 구성 정보 사용
  • private git - 환경설정 정보를 관리하는게 일반적

지금까지 각 프로젝트에 application.yml을 만들었음
한곳에서 관리하도록 변경할 예정

config-service

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-config-server</artifactId>
</dependency>
  • application.yml
server:
  port: 8888

spring:
  application:
    name: config-service
  profiles:
    active: native # 로컬 파일 가져오겠다고 명시

  cloud:
    config:
      server:
        native:
          search-locations: file://${user.home}/Desktop/spring-config/git-local-repo
        git:
          uri: https://github.com/LEEJAECHEOL/spring-cloud-config
          # private 일 경우
          #username:
          #password:
#          uri: file:///Users/lee/Desktop/spring-config/git-local-repo

apigateway-service

  • pom.xml
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
  • bootstrap.yml
spring:
  cloud:
    config:
      uri: http://127.0.0.1:8888
      name: ecommerce
  profiles:
    active: dev

# 서버 정보가 바뀔때 마다 다시 가져오는 3가지 방법
# 서버 재기동
# Actuator refresh -> Spring Boot Actuator 사용
# Spring cloud bus 사용
  • application.yml
server:
  port: 8000
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:8761/eureka

spring:
  application:
    name: apigateway-service
  cloud:
    gateway:
      default-filters:
        - name: GlobalFilter
          args: # Global Filter 내 inner Class의 Config class 에 값이 들어감.
            baseMessage: Spring Cloud Gateway Global Filter
            preLogger: true
            postLogger: true
      routes:
#        - id: user-service
#          uri: lb://USER-SERVICE
#          predicates:
#            - Path=/user-service/**

        - id: user-service
          uri: lb://USER-SERVICE
          predicates:
            - Path=/user-service/login # 로그인
            - Method=POST
          filters:
            - RemoveRequestHeader=Cookie # 전달되는 데이터를 매번 새롭게 인식하기 위해서 삭제
            - RewritePath=/user-service/(?<segment>.*), /$\{segment} # /user-service/login -> /login 으로 변경
            
        - id: user-service
          uri: lb://USER-SERVICE
          predicates:
            - Path=/user-service/users # 회원가입
            - Method=POST
          filters:
            - RemoveRequestHeader=Cookie
            - RewritePath=/user-service/(?<segment>.*), /$\{segment}
            
        - id: user-service
          uri: lb://USER-SERVICE
          predicates: # 조건절
            - Path=/user-service/**
            - Method=GET
          filters:
            - RemoveRequestHeader=Cookie
            - RewritePath=/user-service/(?<segment>.*), /$\{segment}
            - AuthorizationHeaderFilter
            
        - id: user-service
          uri: lb://USER-SERVICE
          predicates: # 조건절
            - Path=/user-service/actuator/**
            - Method=GET,POST
          filters:
            - RemoveRequestHeader=Cookie
            - RewritePath=/user-service/(?<segment>.*), /$\{segment}

        - id: catalog-service
          uri: lb://CATALOG-SERVICE
          predicates:
            - Path=/catalog-service/**
        - id: order-service
          uri: lb://ORDER-SERVICE
          predicates:
            - Path=/order-service/**

        - id: first-service
          uri: lb://MY_FIRST-SERVICE
          predicates:
            - Path=/first-service/**
          filters:
#            - AddRequestHeader=first-request, first-request-header2
#            - AddResponseHeader=first-response, first-response-header2
            - name: CustomFilter
            - name: LoggingFilter # 만약에 args를 넣어야한다면 filters 내에 name: 을 앞에 추가해서 구분해줘야함.
              args:
                baseMessage: Hi, there
                preLogger: true
                postLogger: true
        - id: second-service
          uri: lb://MY-SECOND-SERVICE
          predicates:
            - Path=/second-service/**
          filters:
#            - AddRequestHeader=second-request, second-request-header2
#            - AddResponseHeader=second-response, second-response-header2
            - CustomFilter


management:
  endpoints:
    web:
      exposure:
        include: refresh, health, beans, httptrace

#token:
#  secret: user_token

user-service

  • pom.xml, bootstrap.yml 추가 동일
  • application.yml 아래 것만 추가하면됨. apigateway-service에서도 추가되어있음
management:
  endpoints:
    web:
      exposure:
        include: refresh, health, beans

0개의 댓글