Spring Boot와 Spring Cloud 컴포넌트를 사용하여 Cloud-Native Application을 제작해 보자.
본 예제에 사용되는 세팅은 다음과 같다.
- OpenJDK 17.0.7
- spring-boot-starter
- spring-boot-starter-test
- spring-boot-test
- spring-boot-starter-web
- Spring Framework
이 예제에서는 두 가지 서비스를 활용하여 Cloud-Native한 애플리케이션을 제작한다.
첫 번째는 화면의 디스플레이를 담당하는 "화면 서비스"와
두 번째인 실제 정보를 담고 있는 "고객 서비스"를 제공한다.
Catalogs 서비스 생성
화면 서비스인 Catalogs 서비스를 만들어 보자.
카탈로그 서비스는 상품의 전시 및 화면에 대한 로직을 처리하는 서비스로서 별도의 데이터를 가지고 있지는 않지만 사용자의 입력 및 출력에 대한 서비스를 제공한다. 또한, 요청에 따라 각각의 서비스를 호출하여 요청에 응답한다.
카탈로그 서비스의 파일은 아래와 같이 생성한다.
//CatalogsController
@RestController
@RequestMapping("/catalogs/customerinfo")
@RequiredArgsConstructor
public class CatalogsController {
private final 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
@Service
public class CustomerApiService {
public String getCustomerDetail(String customerid){
return customerid;
}
}
//application.properties
server.port=8081
spring.application.name=catalog
작성 후 스프링부트 서버를 실행하고, 웹 브라우저를 실행 후 아래와 같이 URL을 입력하면 customerid가 1234인 내용을 확인할 수 있다.
http://localhost:8081/catalogs/customerinfo/1234
Customer 서비스 생성
Customers 서비스는 고객정보를 조회할 수 있는 서비스로 요청에 따라 고객정보를 반환한다. 실제 데이터베이스를 활용한 데이터 입출력을 제외하며 서비스 프로세스 절차에 집중한 가이드를 제공한다. 이후 추가로 DAO 및 DataAccess 에 해당하는 로직을 별도로 구현한다.
//CustomersController
@RestController
@RequestMapping("/customers")
public class CustomersController {
@GetMapping("/{customerId}")
public String getCustomerDetail(@PathVariable String customerId){
System.out.println("request customerId : "+customerId);
return "[Customer id = " + customerId + " at " + System.currentTimeMillis() + "]";
}
}
//application.properties
server.port=8082
spring.application.name=customer
다시, 아까랑 똑같이 스프링 서버를 실행시키고, 웹 브라우저에서 아래와 같이 URL을 입력하면 customerid가 1234인 내용을 확인할 수 있다.
http://localhost:8082/customers/1234
Catalogs & Customers 서비스 연동 및 테스트
각각의 서비스들의 구현 및 테스트가 끝났다면, 다음으로 두 서비스를 연동하도록 하겠다. Customer 서비스는 서비스가 호출되는 서비스로서 별도의 변경내용은 없다. 그러나 Catalogs 에서는 Customer 서비스를 호출하기 위하여 별도의 RestTemplate 을 적용하여 서비스를 호출하도록 변경한다.
서비스를 호출하여 JSON 형태의 결과를 받기 위하여 스프링에서 제공하는 RestTemplate 을 Catalogs 서비스에 아래와 같이 변경한다.
1. CatalogAppllication.java 파일 수정
//CatalogsApplication
@SpringBootApplication
public class CatalogsApplication {
////수정사항(추가)////
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
//////////////////
public static void main(String[] args) {
SpringApplication.run(CatalogsApplication.class, args);
}
}
2. CustomerApiService.java 파일 수정
//CustomerApiService 전체 수정
@Service
@RequiredArgsConstructor
public class CustomerApiService {
private final RestTemplate restTemplate;
public String getCustomerDetail(String customerid){
return restTemplate.getForObject("http://localhost:8082/customers/"+customerid,String.class);
}
}
연동 서비스 실행 및 테스트
연동을 위한 수정사항이 모두 반영되었으면, 두 서비스를 각각 실행하고 테스트를 통하여 정상작동을 확인한다. 각각의 서비스를 실행하고, Catalogs 서비스를 호출하여 두 서비스가 연동되었는지 확인한다.
- Customer 서비스 구동
- Catalogs 서비스 구동
- 테스트 Url 호출 : http://localhost:8081/catalogs/customerinfo/1234
이렇게 두 서비스를 연동하여 동작하는 것을 확인할 수 있다.
'DEV > MSA' 카테고리의 다른 글
MSA 개발 가이드(5) - Spring Cloud Eureka(Service Registry) (0) | 2023.06.19 |
---|---|
MSA 개발 가이드(4) - Resilience4j(Circuit Breaker) (0) | 2023.06.19 |
MSA 개발 가이드(2) - Spring Cloud 기반 마이크로서비스(MSA) (0) | 2023.06.16 |
MSA 개발 가이드(1) - MSA란 무엇인가 (0) | 2023.06.16 |
API Gateway의 이해 (0) | 2023.06.13 |