• 검색 결과가 없습니다.

4. SPRING CLOUD 기반 마이크로 서비스 활용

4.2 S PRING C LOUD 의 컴포넌트 활용

4.2.5 Config 서버

defaultZone: http://localhost:8761/eureka

4.2.4.5 Zuul 서버의 구동 및 테스트

Zuul 서버의 구동 이전에 Catalogs, Customers, EurekaServer 모두를 실행하고 정상작동을 확인한다. 이 후 Zuul 서버를 기동하고 Eureka 페이지에서 Zuul 이 정상적으로 등록되었는지 확인한다.

정상적으로 구동이 확인되었으면 아래의 URL 통하여 결과를 확인한다. URL 모두 API Gateway 인 localhost:8080 을 통하여 테스트를 진행한다.

Customers 서비스 URL : http://localhost:8080/customer/customers/1234

Catalogs 서비스 URL :

http://localhost:8080/catalog/catalogs/customerinfo/1234

전체 마이크로서비스들을 다시 빌드해야 하는데, 마이크로서비스의 개수가 많으면

많을수록 변경사항을 적용하는데 많은 비용이 발생하게 된다.

이러한 문제를 해결하기 위해 Spring Cloud Config 컴포넌트가 제공된다. Config 서버를 구축하여 사용하면 마이크로서비스들의 환경설정파일을 중앙 서버 한 곳에 관리할 수 있게 된다.

(Config 서버 구성도)

위 그림과 같이 Config 서버가 구동될 때 Environment Repository 에서 설정 내용을 가져온다. Environment Repository 는 VCS, File System, DB 로 구성 가능하다. 보통 Git 이나 SVN 같은 VCS 를 많이 사용한다.

각 마이크로 서비스들은 서비스가 구동될 때 Config 서버에서 설정 내용을 내려 받아 애플리케이션이 초기화되고 구동된다.

만약 서비스 운영 중에 설정파일을 변경 해야 할 경우에는 Spring Cloud Bus 를

이용하여 모든 마이크로 서비스의 환경설정을 업데이트할 수 있다. Spring Cloud Bus 는 각 서비스들간의 실시간 메시지 통신 역할을 하는 RabbitMQ 또는 Kafka 같은 경량 메시지 브로커들을 사용할 수 있도록 해주며, 이 메시지 브로커들을 이용하여 각 서비스 들에게 Config 변경사항을 전파한다.

4.2.5.1 Config 서버 작성

4.2.5.1.1 Config 서버 프로젝트 생성

전자정부 표준프레임워크의 개발환경을 활용하여 Config 서버 애플리케이션을 아래와 같이 생성한다. (v3.10 기준)

New > Project > Spring Boot > Spring Starter Project 를 선택 후 아래와 같이

입력한 후 Next 를 선택한다.

Service URL : https://start.spring.io Use default location : 체크 (기본 프로젝트 경로 변경을 원하면 해제 후 지정)

Type : Maven Packaging : Jar Java Version : 8 Language : Java

Group : egovframework.msa.sample Artifact : ConfigServer

Version : 1.0.0

Description : MSA Sample Project

Group Id : egovframework.msa.sample

다음 단계는 프로젝트의 Dependency 를 추가하는 단계인데 여기서는 선택하지 않는다.

(이 가이드에서는 의존관계를 pom.xml 에 직접 등록하는 방법으로 진행한다.)

Next > Finish 또는 Finish 를 바로 선택하여 프로젝트를 생성한다.

Config 서버는 환경변수 외부화 역할 이외의 별도의 작업이 없으므로 모든

egovframework 라이브러리를 제거하고 아래와 같이 Config 서버, Security 그리고 웹 라이브러리를 등록한다.

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.2.6.RELEASE</version>

<relativePath />

</parent>

<groupId>egovframework.msa.sample</groupId>

<artifactId>ConfigServer</artifactId>

<version>1.0.0</version>

<name>ConfigServer</name>

<description>MSA Sample Project</description>

<properties>

<java.version>1.8</java.version>

<spring.cloud.version>2.2.5.RELEASE</spring.cloud.version>

</properties>

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

<exclusions>

<exclusion>

<groupId>org.junit.vintage</groupId>

<artifactId>junit-vintage-engine</artifactId>

</exclusion>

</exclusions>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-actuator</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-config-server</artifactId>

<version>${spring.cloud.version}</version>

</dependency>

</dependencies>

<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

</plugins>

</build>

</project>

4.2.5.1.2 ConfigServer 프로젝트 디렉터리 구조

ConfigServer 서비스의 디렉터리 구조는 다음과 같다.

파일명 패키지 명 구분 비고

pom.xml / 의존성 관리 파일

application.yml /src/main/resources Resource 파일 SpringBoot 설정

파일 templateSimple-dev.yml

templatePortal-dev.yml templateEnterprise-dev.yml

/src/main/resources/configuration-repository

Resource 환경 파일 애플리케이션 환경설정파일

ConfigServerApplication.java egovframework.msa.sample 클래스 파일 애플리케이션 구동 파일

4.2.5.1.3 ConfigServerApplication.java 작성

ConfigServerApplication 클래스에 @SpringBootApplication,

@EnableConfigServer 어노테이션을 추가한다.

package egovframework.msa.sample;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication

@EnableConfigServer

public class ConfigServerApplication { public static void main(String[] args) {

SpringApplication.run(ConfigServerApplication.class, args);

} }

4.2.5.1.4 ConfigServer 의 application.yml 파일 작성

Config 서버의 관련 내용을 아래와 같이 설정한다. Environment Repository 는 Git, SVN, File system, Database 등으로 구성이 가능하지만, 본 가이드에서는 File

system 을 이용하여 작성한다.

server:

port: 8888 spring:

application:

name:

-templateSimple -templatePortal -templateEnterprise profiles:

active: native cloud:

config:

server:

native:

search-locations: classpath:configuration-repository/ #검색할 설정파일 경로

4.2.5.1.5 환경파일 작성 (templateSimple-dev.yml, templatePortal-dev.yml,

templateEnterprise-dev.yml)

templateSimple-dev.yml

config:

profile: sht

message: templateSimple(dev) Globals:

DbType: mysql

DriverClassName: com.mysql.jdbc.Driver Url: jdbc:mysql://127.0.0.1:3306/sht UserName: com

Password: com01

templatePortal-dev.yml

config:

profile: pst

message: templatePortal(dev) Globals:

DbType: mysql

DriverClassName: com.mysql.jdbc.Driver Url: jdbc:mysql://127.0.0.1:3306/pst UserName: com

Password: com01

templateEnterprise-dev.yml

config:

profile: ebt

message: templateEnterprise(dev) Globals:

DbType: mysql

DriverClassName: com.mysql.jdbc.Driver Url: jdbc:mysql://127.0.0.1:3306/ebt UserName: com

Password: com01

4.2.5.1.6 Config 서버의 구동 및 테스트

ConfigServerApplication.java 를 Run As > Java Application 으로 실행한다.

정상적으로 구동이 확인되었으면 아래의 URL 통하여 결과를 확인한다.

http://localhost:8888/actuator 에 접속하여 서버가 정상으로 작동하는지 확인한다.

4.2.5.2 Config 클라이언트 작성

4.2.5.2.1 Config 클라이언트 프로젝트 생성

전자정부 표준프레임워크의 개발환경을 활용하여 Config 클라이언트 애플리케이션을 아래와 같이 생성한다. (v3.10 기준)

New > Project > Spring Boot > Spring Starter Project 를 선택 후 아래와 같이 입력한 후 Next 를 선택한다.

Service URL : https://start.spring.io Use default location : 체크 (기본 프로젝트 경로 변경을 원하면 해제 후 지정)

Type : Maven Packaging : Jar Java Version : 8 Language : Java

Group : egovframework.msa.sample Artifact : ConfigClient

Version : 1.0.0

Description : MSA Sample Project

Group Id : egovframework.msa.sample

다음 단계는 프로젝트의 Dependency 를 추가하는 단계인데 여기서는 선택하지 않는다.

(이 가이드에서는 의존관계를 pom.xml 에 직접 등록하는 방법으로 진행한다.)

Next > Finish 또는 Finish 를 바로 선택하여 프로젝트를 생성한다.

pom.xml 을 아래와 같이 설정한다.

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.2.6.RELEASE</version>

<relativePath />

</parent>

<groupId>egovframework.msa.sample</groupId>

<artifactId>ConfigClient</artifactId>

<version>1.0.0</version>

<name>ConfigClient</name>

<description>MSA Sample Project</description>

<properties>

<java.version>1.8</java.version>

<spring.cloud.version>2.2.5.RELEASE</spring.cloud.version>

</properties>

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

<exclusions>

<exclusion>

<groupId>org.junit.vintage</groupId>

<artifactId>junit-vintage-engine</artifactId>

</exclusion>

</exclusions>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-actuator</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-config</artifactId>

<version>${spring.cloud.version}</version>

</dependency>

</dependencies>

<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

</plugins>

</build>

</project>

4.2.5.2.2 ConfigClient 프로젝트 디렉터리 구조

ConfigClient 서비스의 디렉터리 구조는 다음과 같다.

파일명 패키지 명 구분 비고

pom.xml / 의존성 관리 파일

application.yml /src/main/resources Resource 파일 SpringBoot 설정 파일

bootstrap.yml /src/main/resources Resource 파일 SpringBoot 설정

파일

(application.yml 보다 우선순위) ConfigClientApplicatio

n.java

egovframework.msa.sample 클래스 파일 애플리케이션

구동 파일 ConfigClientController.

java

egovframework.msa.sample.controller 클래스 파일 테스트용 컨트롤러 파일

4.2.5.2.3 ConfigClientApplication.java 작성

ConfigClientApplication 클래스에 @SpringBootApplication 어노테이션을 추가한다.

package egovframework.msa.sample;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class ConfigClientApplication {

public static void main(String[] args) {

String profile = System.getProperty("spring.profiles.active");

if (profile == null) {

System.setProperty("spring.profiles.active", "dev");

}

SpringApplication.run(ConfigClientApplication.class, args);

} }

4.2.5.2.4 ConfigClientController.java 작성

ConfigClientController 클래스에 @RestController 와 @RefreshScope 어노테이션을 추가한다. @RefreshScope 어노테이션을 추가해주어야 Config 서버의 Config 정보를 갱신(refresh)할 수 있다.

package egovframework.msa.sample.controller;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.cloud.context.config.annotation.RefreshScope;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController

@RefreshScope

public class ConfigClientController { @Value("${config.profile}") private String profile;

@Value("${config.message}")

private String message;

@GetMapping("/config/profile") public String profile() { return profile;

}

@GetMapping("/config/message") public String message() { return message;

} }

4.2.5.2.5 ConfigClient 의 Bootstrap.yml 과 Application.yml 파일 작성 다음과 같이 bootstrap.yml 을 작성한다.

server:

port: 9265 spring:

application:

name: templateEnterprise #서비스ID (Config클라이언트가 어떤 서비스를 조회할 것인지 매핑) cloud:

config:

uri: http://localhost:8888 #Config서버 URL

다음과 같이 application.yml 을 작성한다.

management:

endpoints:

web:

exposure:

include: [‘env’, ‘refresh’] #엔드포인트, ex) http://localhost:9265/actuator/refresh (POST)

4.2.5.2.6 Config 클라이언트의 구동 및 테스트

ConfigClientApplication.java 를 Run As > Java Application 으로 실행한다.

정상적으로 구동이 확인되었으면 아래의 URL 통하여 결과를 확인한다.

http://localhost:9265/config/profile

설정파일 (config 서버의 templateEnterprise-dev.yml 파일의 config.profile 값)의 내용이 정상 출력되는지 확인한다.

http://localhost:9265/config/message

설정파일 (config 서버의 templateEnterprise-dev.yml 파일의 config.message 값)의 내용이 정상 출력되는지 확인한다.

http://localhost:9265/actuator/env

Config 서버의 모든 환경설정 값들이 json 형식의 문자열로 출력되는지 확인한다.

4.2.5.2.7 Config 클라이언트 환경설정값 업데이트 테스트

Config 서버의 다음의 환경파일에서 config.profile 속성정보를 'ebt'에서 'ebt2'로 변경한다.

templateEnterprise-dev.yml

config:

profile: ebt2 # ebt 에서 ebt2로 변경 message: templateEnterprise(dev) Globals:

DbType: mysql

DriverClassName: com.mysql.jdbc.Driver Url: jdbc:mysql://127.0.0.1:3306/ebt UserName: com

Password: com01

ConfigClient 의 설정(속성) 정보를 갱신하는 요청을 보내도록 한다. 아래의 URL 을 POST 로 요청하여 변경된 속성 정보가 출력되는지 확인 한다. (POST 로 요청)

Linux, Mac 의 경우 curl -X POST http://localhost:9265/actuator/refresh 로 요청하여 결과를 확인한다. 변경된 속성 정보가 json 문자열로 출력된다.

그 외 SoapUI, Postman, JMeter 등의 rest-api 테스트 도구를 이용하여 POST 요청을 할

수 있고, 요청결과를 확인할 수 있다. (아래 그림 예시 참조)

위 그림은 rest-api 테스트 도구 (Postman) 을 이용하여 설정(속성) 정보를 refresh 하도록 요청을 보내고, 결과를 확인하는 그림이다. (변경된 속성 정보가 json 문자열로 출력된다.)

위 요청을 보내고 정상적으로 refresh 가 된 것을 확인한 후

http://localhost:9265/config/profile 로 접속해보면 변경된 속성 정보가 출력되는 것을 확인할 수 있다.

4.2.5.3 Spring Cloud Bus 환경 구성

Config 서버의 설정 값이 변경될 경우, 마이크로서비스들이 변경된 설정 값을 갱신하기 위해서는 마이크로서비스를 재시작하거나, 위 Config Client 예제의 /actuator/refresh 를 호출해주어야 한다. 하지만 마이크로서비스의 개수가 많을 경우 각각의 서비스들을 모두 수동으로 재시작하거나 refresh 해주기는 쉽지 않다.

Config 서버에서는 마이크로 서비스 들에게 refresh 를 수행하라는 메시지를 전송해야 한다. 이 메시지를 전송해주는 컴포넌트를 메시지브로커라고 부른다. 대표적으로 많이 사용되는 메시지브로커로는 RabbitMQ, Kafka 등이 있다. 본 가이드에서는 RabbitMQ 를 기준으로 가이드 한다.

4.2.5.3.1 RabbitMQ 설치

RabbitMQ 를 설치하는 방법은 각 OS 환경에 따라 다양한 방법이 존재한다. 본 가이드에서는 각 환경에 따른 RabbitMQ 설치방법에 대해서는 생략한다.

RabbitMQ 의 자세한 설치방법은 아래의 공식 설치 가이드를 참조한다.