• 검색 결과가 없습니다.

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

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

4.2.4 API Gateway – Zuul

트래픽 드랍(Load Shedding) : 각 요청에 대해 용량을 할당하고, 제한된 이상의

요청이 발생한 경우 이를 제한할 수 있는 기능을 제공한다.

정적 응답 처리 (Static Response Handling) : 특정 요청에 대해서는 백엔드로 트래픽을 보내는 대신 즉시 API Gateway 에서 응답을 처리하는 기능을

제공한다.

4.2.4.1 Zuul 서비스 작성

전자정부 표준프레임워크의 개발환경을 활용하여 Zuul 서비스 애플리케이션을 아래와 같이 생성한다. (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 : ZuulServer

Version : 1.0.0

Description : MSA Sample Project

Group Id : egovframework.msa.sample

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

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

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

Zuul 서버는 API Gateway 역할 이외의 별도의 작업이 없으므로 모든 egovframework 라이브러리를 제거하고 아래와 같이 Zuul, Eureka 클라이언트, Spring-retry

라이브러리를 등록한다.

<?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>ZuulServer</artifactId>

<version>1.0.0</version>

<name>ZuulServer</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.cloud</groupId>

<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

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

</dependency>

<dependency>

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

<artifactId>spring-cloud-starter-netflix-zuul</artifactId>

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

</dependency>

<dependency>

<groupId>org.springframework.retry</groupId>

<artifactId>spring-retry</artifactId>

</dependency>

</dependencies>

<build>

<plugins>

<plugin>

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

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

</plugin>

</plugins>

</build>

</project>

4.2.4.2 ZuulServer 프로젝트 디렉터리 구조 ZuulServer 서비스의 디렉터리 구조는 다음과 같다.

파일명 패키지 명 구분 비고

Pom.xml / 의존성 관리 파일

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

파일 ZuulServerApplication.j

ava

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

4.2.4.3 ZuulServerApplication.java 작성

ZuulServerApplication 클래스에 @SpringBootApplication, @EnableZuulProxy 와

@EnableDiscoveryClient 어노테이션을 추가한다. (@EnableDiscoveryClient 는

@EnableEurekaClient 와 동일하게 작동하지만, @EnableEurekaClient 는 Eureka 서버일 경우만 작동하고, @EnableDiscoveryClient 는 Eureka 뿐 아니라 Consul, Zookeeper 도 지원한다. 여기서는 @EnableDiscoveryClient 를 사용하도록 한다.)

package egovframework.msa.sample;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication

@EnableZuulProxy

@EnableDiscoveryClient

public class ZuulServerApplication {

public static void main(String[] args) {

SpringApplication.run(ZuulServerApplication.class, args);

} }

4.2.4.4 ZuulServer 의 Application.yml 파일 작성 Zuul 서버의 관련 내용을 아래와 같이 설정한다.

spring:

application:

name: zuul

server:

port: 8080

zuul:

routes:

catalog:

path: /catalogs/**

serviceId: catalog stripPrefix: false customer:

path: /customers/**

serviceId: customer

eureka:

instance:

non-secure-port: ${server.port}

prefer-ip-address: true client:

service-url:

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