The developer’s tale: Building a microservice with Spring Boot and Java
2025. 3. 2. 22:32ㆍSpring Microservice
👨💻 개발자의 관점: Spring Boot와 Java로 마이크로서비스 구축하기
이 섹션에서는 O-stock 도메인 모델을 기반으로 라이선스 마이크로서비스를 구축하는 개발자의 주요 작업을 살펴봅니다.
개발자는 다음의 세 가지 핵심 작업을 수행해야 합니다.
1️⃣ Spring Boot 컨트롤러 클래스 구현
✅ 라이선스 서비스의 엔드포인트를 매핑하는 컨트롤러 클래스 구현
- Spring Boot의
@RestController
를 활용하여 HTTP 요청을 처리 - 엔드포인트를 정의하고, 클라이언트가 라이선스 정보를 요청할 수 있도록 함
📌 예시 코드 (기본 컨트롤러 구현)
@RestController
@RequestMapping("/licenses")
public class LicenseController {
@GetMapping("/{licenseId}")
public ResponseEntity<String> getLicense(@PathVariable String licenseId) {
return ResponseEntity.ok("License details for ID: " + licenseId);
}
}
📌 역할
@RequestMapping("/licenses")
→/licenses
경로를 처리하는 컨트롤러로 지정@GetMapping("/{licenseId}")
→ 특정 라이선스 ID에 대한 GET 요청 처리
💡 ➡ 컨트롤러는 마이크로서비스의 핵심 엔드포인트를 정의하고, 클라이언트가 API를 통해 데이터를 요청할 수 있도록 함
2️⃣ 다국어 지원 (Internationalization, i18n) 구현
✅ 국제화를 지원하여 다양한 언어로 메시지를 제공
- Spring의 메시지 번들(
messages.properties
)을 활용하여 다국어 지원 - 예를 들어,
messages_en.properties
(영어)와messages_ko.properties
(한국어)를 별도로 관리
📌 예시 코드 (국제화 메시지 설정)
📁 src/main/resources/messages_en.properties
license.message = License details for ID:
📁 src/main/resources/messages_ko.properties
license.message = 라이선스 상세 정보 (ID):
📌 국제화 적용 코드 (컨트롤러에서 메시지 가져오기)
@RestController
@RequestMapping("/licenses")
public class LicenseController {
@Autowired
private MessageSource messageSource;
@GetMapping("/{licenseId}")
public ResponseEntity<String> getLicense(
@PathVariable String licenseId,
@RequestHeader(name = "Accept-Language", required = false) Locale locale) {
String message = messageSource.getMessage("license.message", null, locale);
return ResponseEntity.ok(message + " " + licenseId);
}
}
📌 역할
MessageSource
를 이용하여 다국어 메시지를 가져옴Accept-Language
헤더에 따라 적절한 언어로 메시지 반환
💡 ➡ 클라이언트가 요청 시 언어를 지정하면, 해당 언어에 맞는 메시지를 제공하여 국제화 지원
3️⃣ Spring HATEOAS 적용
✅ HATEOAS(Hypermedia As The Engine Of Application State)로 API 응답에 추가 정보 포함
- 클라이언트가 API를 더 쉽게 탐색할 수 있도록 관련 링크를 포함
- Spring Boot의
spring-boot-starter-hateoas
를 활용
📌 예시 코드 (HATEOAS 적용 컨트롤러)
@RestController
@RequestMapping("/licenses")
public class LicenseController {
@Autowired
private EntityLinks entityLinks;
@GetMapping("/{licenseId}")
public EntityModel<License> getLicense(@PathVariable String licenseId) {
License license = new License(licenseId, "Software License");
EntityModel<License> resource = EntityModel.of(license);
resource.add(entityLinks.linkToItemResource(License.class, licenseId).withSelfRel());
resource.add(entityLinks.linkToCollectionResource(License.class).withRel("all-licenses"));
return resource;
}
}
📌 역할
EntityModel
을 사용하여 API 응답에 하이퍼링크 포함.add(entityLinks.linkToItemResource(...))
→ 현재 라이선스의 자체 링크 추가.add(entityLinks.linkToCollectionResource(...))
→ 전체 라이선스 목록으로 이동하는 링크 추가
💡 ➡ 클라이언트가 API 응답을 보고 추가적인 액션을 취할 수 있도록 관련 정보를 제공
📌 정리: 마이크로서비스 개발자가 해야 할 작업
작업 | 설명 |
---|---|
✅ Spring Boot 컨트롤러 구현 | HTTP 요청을 처리하는 엔드포인트 생성 |
✅ 다국어(i18n) 지원 | 메시지 번들 활용하여 여러 언어 지원 |
✅ Spring HATEOAS 적용 | API 응답에 추가 링크 포함하여 탐색성 향상 |
💡 ➡ 위의 3가지 요소를 적용하면, 보다 직관적이고 확장 가능한 마이크로서비스를 구축할 수 있음! 🚀
'Spring Microservice' 카테고리의 다른 글
Adding internationalization into the licensing service (0) | 2025.03.02 |
---|---|
Building the doorway into the microservice: The Spring Boot controller (0) | 2025.03.02 |
3.Building microserviceswith Spring Boot (0) | 2025.03.02 |
Getting started with the skeleton project (0) | 2025.03.02 |
Setting up the environment (0) | 2025.03.02 |