• 검색 결과가 없습니다.

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

4.1 S PRING B OOT 을 활용한 MSA 애플리케이션 제작

4.1.2 Customers 서비스

Customers 서비스는 고객정보를 조회할 수 있는 서비스로 요청에 따라 고객정보를 반환한다. 실제 데이터베이스를 활용한 데이터 입출력을 제외하며 서비스 프로세스 절차에 집중한 가이드를 제공한다. 이후 추가로 DAO 및 DataAccess 에 해당하는 로직을 별도로 구현한다.

4.1.2.1 Customers 서비스 프로젝트 생성

전자정부 표준프레임워크 개발환경을 활용하여 다음과 같이 생성한다. (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 : Customers

Version : 1.0.0

Description : MSA Sample Project

Group Id : egovframework.msa.sample

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

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

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

4.1.2.2 Customers 프로젝트 디렉터리 구조 설정 커스터머 서비스의 디렉터리 구조는 다음과 같다.

파일명 패키지 명 구분 비고

Pom.xml / 의존성 관리 파일

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

CustomersApplication.j ava

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

구동 파일 CustomersController.ja

va

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

Restful Api Controller

4.1.2.3 Customers 서비스의 의존성(Dependency) 설정

아래와 같이 의존성을 추가한다.

커스터머(Customers)의 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>Customers</artifactId>

<version>1.0.0</version>

<name>Customers</name>

<description>MSA Sample Project</description>

<properties>

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

<org.egovframe.rte.version>4.0.0</org.egovframe.rte.version>

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

</properties>

<repositories>

<repository>

<id>mvn2s</id>

<url>https://repo1.maven.org/maven2/</url>

<releases>

<enabled>true</enabled>

</releases>

<snapshots>

<enabled>true</enabled>

</snapshots>

</repository>

<repository>

<id>egovframe</id>

<url>http://maven.egovframe.go.kr/maven/</url>

<releases>

<enabled>true</enabled>

</releases>

<snapshots>

<enabled>false</enabled>

</snapshots>

</repository>

</repositories>

<dependencies>

<dependency>

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

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

<exclusions>

<exclusion>

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

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

</exclusion>

</exclusions>

</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.egovframe.rte</groupId>

<artifactId>org.egovframe.rte.fdl.cmmn</artifactId>

<version>${org.egovframe.rte.version}</version>

</dependency>

<dependency>

<groupId>org.egovframe.rte</groupId>

<artifactId>org.egovframe.rte.ptl.mvc</artifactId>

<version>${org.egovframe.rte.version}</version>

</dependency>

</dependencies>

<build>

<plugins>

<plugin>

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

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

</plugin>

</plugins>

</build>

</project>

4.1.2.4 애플리케이션 설정 파일 생성

스프링 부트 및 프레임워크의 의존성(dependency)를 추가하였으면, 애플리케이션의 설정을 위한 application.properties 파일을 생성한다. application.yml 파일은

src/main/resources 디렉터리에 위치하며, yml(yaml) 파일 대신 properties 형태의 파일을 사용할 수도 있다.

application.yml 파일 소스 내용

Customers 서비스의 이름과 contextPath 및 접속 포트를 설정한다.

server:

port: 8082

spring:

application:

name: customer

4.1.2.5 각 클래스 파일 작성

MSA 애플리케이션의 구조 파일들을 모두 작성하였으면, 다음으로 실제 로직을 담고 있는 각각의 클래스 파일들을 작성한다.

4.1.2.5.1 CustomersApplication.java 파일 작성

package egovframework.msa.sample;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.context.annotation.ComponentScan;

@ComponentScan("egovframework.*")

@SpringBootApplication

public class CustomersApplication {

public static void main(String[] args) {

SpringApplication.run(CustomersApplication.class, args);

} }

4.1.2.5.2 CustomerController.java 파일 작성

package egovframework.msa.sample.controller;

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

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

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

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

@RestController

@RequestMapping("/customers") public class CustomerController { @GetMapping("/{customerId}")

public String getCustomerDetail(@PathVariable String customerId) { System.out.println("request customerId :" + customerId);

return "[Customer id = " + customerId + " at " + System.currentTimeMillis() + "]";

} }

4.1.2.6 Customers 서비스 구동 및 테스트

Customers 의 CustomersApplcation.java 파일을 java application 으로 실행하면, 아래와 같이 Spring boot 를 통하여 embedded tomcat 으로 구동되는 것을 확인할 수 있다.

. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/

:: Spring Boot :: (v2.3.4.RELEASE)

2020-10-19 16:16:04.188 INFO 50318 --- [ main] e.msa.sample.CustomersApplication : Starting CustomersApplication on jeonghunhuiui-iMac.local with PID 50318

(/Users/EGOV3.9/workspace/Customers/target/classes started by hhjeong in /Users/EGOV3.9/workspace/Customers)

2020-10-19 16:16:04.190 INFO 50318 --- [ main] e.msa.sample.CustomersApplication : No active profile set, falling back to default profiles: default

2020-10-19 16:16:04.865 INFO 50318 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8082 (http)

2020-10-19 16:16:04.873 INFO 50318 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]

2020-10-19 16:16:04.873 INFO 50318 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.38]

2020-10-19 16:16:04.937 INFO 50318 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/]

: Initializing Spring embedded WebApplicationContext 2020-10-19 16:16:04.937 INFO 50318 --- [ main]

w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 713 ms

2020-10-19 16:16:05.077 INFO 50318 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'

2020-10-19 16:16:05.205 INFO 50318 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8082 (http) with context path ''

2020-10-19 16:16:05.213 INFO 50318 --- [ main] e.msa.sample.CustomersApplication : Started CustomersApplication in 1.532 seconds (JVM running for 1.782)

2020-10-19 16:16:10.795 INFO 50318 --- [nio-8082-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]

: Initializing Spring DispatcherServlet 'dispatcherServlet'

2020-10-19 16:16:10.796 INFO 50318 --- [nio-8082-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'

2020-10-19 16:16:10.799 INFO 50318 --- [nio-8082-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 3 ms

202x-0x-xx 22:49:26.150 INFO 10020 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 202x-0x-xx 22:49:26.157 INFO 10020 --- [nio-8080-exec-1]

o.s.web.servlet.DispatcherServlet : Completed initialization in 6 ms

그럼, 웹 브라우저를 실행하고 아래와 같이 URL 을 입력하면 customerid 가 1234 인 내용을 확인할 수 있다.

URL : http://localhost:8082/customers/1234