Spring Boot을 활용한 MSA 애플리케이션 제작
Request -> 화면서비스(8081) -> Restful Api -> 커스터머 서비스(8082)
프로젝트 생성
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 |
파일 구성
카탈로그(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
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
'전자정부프레임워크' 카테고리의 다른 글
표준프레임워크) MSA_적용 개발 실습 -3 (0) | 2022.10.20 |
---|---|
표준프레임워크) MSA_적용 개발 실습 -2 (0) | 2022.10.20 |
전자정부 lab104 공통컴포넌트 생성 (0) | 2022.10.06 |
lab102-code-generation 실습 (0) | 2022.10.06 |
Boot Web 프로젝트 실습 (0) | 2022.10.06 |