[Spring Boot] Spring Cloud Config(2) User MicroService와 연동하기 -MSA(6)

모지리 개발자·2022년 9월 27일
0

MSA

목록 보기
7/9

Spring Cloud로 개발하는 마이크로서비스 애플리케이션(MSA)
를 수강하면서 작성한 글입니다.

user-service는 이곳에서 clone 받으셔서 사용하셔도 됩니다.

Intro


지금까지 각각의 MicroService는 독립적인 application.yml 파일을 가지고 있었습니다. 하지만 이제는 이전 글에서 작성한 Spring Cloud Config 서버를 통해 설정정보 파일을 가져와 보겠습니다.

code

기존 User Service에서는 아래와 같이 설정정보를 관리하고있었습니다. 이제는 config server를 사용할 것이므로 주석처리를 하겠습니다.
application.yml

token:
  expiration-time: 86400000 # 하루짜리 토큰 생성
  secret: secret-key

각각의 서비스에는 아래와 같은 의존성을 추가하겠습니다.
build.gradle

implementation "org.springframework.cloud:spring-cloud-starter-config"
implementation "org.springframework.cloud:spring-cloud-starter-bootstrap"

그 후 bootstrap.yml file을 application.yml file과 같은 경로에 생성합니다. 아래 내용을 작성해줍니다.
bootstrap.yml

spring:
  cloud:
    config:
      uri: http://127.0.0.1:8888
      name: ecommerce # 파일명이라고 생각하면 된다. abc.yml이라고 생성했었다면 abc 로 작성

UserController.java

@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"));
    }

컨트롤러를 간단하게 작성하여 설정 값들을 잘 가져오는지 확인해보겠습니다.

그 후 서비스를 config server 가 실행되어있는 상태에서 각각의 서비스를 실행해보면

위와 같이 config 서버로부터 구성정보들을 받아 오는 것을 볼 수 있습니다.

결과


위와 같이 Config 서버로부터 설정값들을 가져와 잘 반영되어 있는 것을 알 수 있습니다.

결론

이렇게 Config Server를 따로 두어 사용하게 되면 유지보수에 큰 장점이 있는 것 같습니다. 이제 설정정보가 변경된다면 Config Server의 파일 값만 변경해주면 따로 서비스를 빌드, 배포할 필요가 없어지게 됩니다.

profile
항상 부족하다 생각하며 발전하겠습니다.

0개의 댓글