본문 바로가기

전자정부프레임워크

표준프레임워크) MSA_적용 개발 실습 -1

Spring Boot을 활용한 MSA 애플리케이션 제작

Request -> 화면서비스(8081) -> Restful Api ->  커스터머 서비스(8082)

Spring Cloud 기능 구성

 

 

프로젝트 생성

Next 클릭

Service URL : https://start.spring.io
Use default location : 체크 (기본 프로젝트 경로 변경을 원하면 해제 후 지정)
Type : Maven
Packaging : Jar
Java Version : 8
Language : Java
Group : egovframework.msa.sample
Artifact : Catalogs
Version : 1.0.0
Description : MSA Sample Project
Group Id : egovframework.msa.sample

 

 

finish

 

 

파일 구성

 

카탈로그(Catalogs)의 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>Catalogs</artifactId>
	<version>1.0.0</version>
	<name>Catalogs</name>
	<description>MSA Sample Project</description>

	<properties>
		<java.version>1.8</java.version>
		<maven-jar-plugin.version>3.1.1</maven-jar-plugin.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>

 

 

 

pom.xml에 오류가 뜰 경우 참고 해주세요.

https://kimbold.tistory.com/49

 

Spring) pom.xml 첫줄에러 <parent> 에러

pom.xml 첫줄 에러 & 에러 해결 방법 입니다. 원인 : Eclipse에서 나타는 버그로 maven이 jar을 다운 받으면서 도중에 꼬인 것이라고 한다. <?xml version="1.0" encoding="UTF-8"?> 4.0.0 org.springframework.bo..

kimbold.tistory.com

 

application.yml 파일 소스 내용

server:
  port: 5081
spring:
 application:
    name: catalog

properties -> yml 변경

port 번호는 상황에 맞춰 쓰세요 8081 ->5081 변경

복붙 하시다가 오류 뜨시면 직접 코드 입력 해보세요.

 

 

CatalogsApplication.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 CatalogsApplication {
public static void main(String[] args) {
SpringApplication.run(CatalogsApplication.class);
}
}

 

 

CatalogsController.java 파일 작성

package egovframework.msa.sample.controller;

import org.springframework.beans.factory.annotation.Autowired;
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;

import egovframework.msa.sample.service.CustomerApiService;

@RestController
@RequestMapping("/catalogs/customerinfo")
public class CatalogsController {
	@Autowired
	private CustomerApiService customerApiService;

	@GetMapping(path = "/{customerId}")
	public String getCustomerInfo(@PathVariable String customerId) {
		String customerInfo = customerApiService.getCustomerDetail(customerId);
		System.out.println("response customerInfo : " + customerInfo);
		return String.format("[Customer id = %s at %s %s ]", customerId, System.currentTimeMillis(), customerInfo);
	}
}

 

 

CustomerApiService.java 파일 작성

package egovframework.msa.sample.service;
public interface CustomerApiService {
	String getCustomerDetail(String customerId);
}

 

CustomerApiServiceImpl.java 파일 작성

package egovframework.msa.sample.serviceImpl;
import org.springframework.stereotype.Service;
import egovframework.msa.sample.service.CustomerApiService;
@Service
public class CustomerApiServiceImpl implements CustomerApiService {
	@Override
	public String getCustomerDetail(String customerId) {
		return customerId;
	}
}

 

 

결과

URL : http://localhost:8081/catalogs/customerinfo/1234