본문 바로가기

Spring & Java

몽고디비를 활용한 주문 프로세스 관리: 스텝별 상태 저장 및 조회 모든 쇼핑몰에서 사용자 주문, 결제, 배송 과정은 거의 모두 동일 합니다. 이러한 트랜잭션의 각 단계를 효과적으로 관리하고 추적하는 것은 사용자 경험을 향상시키고 운영 효율성을 높이는데 도움을 줍니다. 이번 포스팅에서는 몽고디비를 활용하여 주문 아이디 기준으로 스텝별 상태를 저장하고 조회하는 방법을 소개합니다. 시퀀스 도커 컴포즈 설정 몽고디비 도커 컴포즈를 통해 구성합니다. version: '3.8' services: # MongoDB configuration mongodb: image: mongo:latest container_name: mongodb_container ports: - "27017:27017" volumes: - mongodb_data:/data/db environment: MONGO.. 더보기
스프링의 RedisLockRegistry를 활용한 제한된 상품의 동시성 제어 및 주문 처리 개요 이 포스팅에서는 스프링의 RedisLockRegistry를 활용하여 제한된 상품의 주문 처리에 대한 동시성 제어를 다루어 보겠습니다. 사용자가 동일한 제한된 상품을 구매할 때, 여러 주문 서비스 인스턴스에서 동시성 제어를 해야 할 필요가 있습니다. 이 때 RedisLockRegistry가 큰 역할을 합니다. 설정 의존성 추가 id 'org.springframework.boot' version '3.1.4' id 'io.spring.dependency-management' version '1.1.3' 먼저, 필요한 의존성을 추가합니다. implementation 'org.springframework.integration:spring-integration-redis' RedisLockRegistry 설.. 더보기
스프링 부트에서 외부 API 서비스를 병렬 처리하여 응답하는 방법 많은 서비스에서 다양한 자산 정보를 사용자에게 제공해야 하는 경우가 있습니다. 예를 들어, 사용자가 보유한 적립금, 쿠폰 개수, 코인 수량 등의 정보를 한번의 요청으로 빠르게 얻고 싶어합니다. 스프링 부트와 자바를 활용하여 이런 자산 정보들을 병렬로 처리하고, 그 결과를 사용자에게 효과적으로 제공하는 방법에 대해 살펴보겠습니다. 스프링 부트에서는 @Async 어노테이션과 CompletableFuture를 활용하여 쉽게 비동기 호출을 구현할 수 있습니다. 이를 통해 여러 API 호출을 동시에 수행하고, 모든 결과를 기다린 뒤 하나로 합치는 병렬 처리를 구현할 수 있습니다. 본 글에서는 적립금, 쿠폰 개수, 코인 수량과 같은 사용자의 다양한 자산 정보를 병렬로 조회 및 처리하여 응답하는 API 서비스 구현에.. 더보기
자바의 Supplier와 Consumer 인터페이스 이해와 활용 자바의 Supplier와 Consumer 인터페이스는 자바 8에서 소개된 함수형 인터페이스로, 각각 데이터를 제공하는 로직과 데이터를 소비하는 로직을 간단하게 표현하는데 사용됩니다. Supplier 인터페이스 Supplier 인터페이스는 파라미터를 받지 않고 값을 리턴하는 get() 메소드를 가지고 있습니다. @FunctionalInterface public interface Supplier { T get(); } Consumer 인터페이스 반면 Consumer 인터페이스는 파라미터를 하나 받아서 소비하고 (처리하고) 리턴 값이 없는 accept() 메소드를 가지고 있습니다 @FunctionalInterface public interface Consumer { void accept(T t); } 예제: .. 더보기
Spring Boot Config Server 1. Spring Boot Config Server 설정 build.gradle.kts plugins { kotlin("jvm") version "1.6.0" id("org.springframework.boot") version "2.6.2" id("io.spring.dependency-management") version "1.0.11.RELEASE" } group = "com.example" version = "0.0.1-SNAPSHOT" java.sourceCompatibility = JavaVersion.VERSION_11 repositories { mavenCentral() } dependencies { implementation("org.springframework.boot:spring-boo.. 더보기
Spring boot Private field and method Test ReflectionTestUtils를 이용하여 private field or method 를 테스트 할 수 있다. public class Student { private int id; private String getFirstNameAndId(){ return getFirstName() + " " + getId(); } } @SpringBootTest class StudentTest { @Autowired Student student; @BeforeEach void studentBeforeEach(){ student.setFirstName("Eric"); student.setLastName("Roby"); student.setEmailAddress("asdfa@asdf.com"); student.setS.. 더보기
Spring boot Throwing Exception test @SpringBootTest class StudentRepositoryTest { @Autowired Student student; @Autowired StudentGrades studentGrades; @MockBean private StudentRepository studentRepository; @Autowired private StudentService studentService; @Autowired private ApplicationContext context; @DisplayName("Throw runtime error") @Test void throwRuntimeError() { Student nullStudent = (Student)context.getBean("Student"); doTh.. 더보기
Spring boot @MockBean @MockBean - includes Mockito @Mock functionality - also adds mock bean to if existing bean is there, the mock bean will replace it - thus making the mock bean available for injection with @Autowired Using @Mock @SpringBootTest class StudentRepositoryTest { @Mock private StudentRepository studentRepository; @InjectMocks private StudentService studentService; } Using @MockBean @SpringBootTest clas.. 더보기