From 89ffa58448166fd1852b54d4393880ef70396c50 Mon Sep 17 00:00:00 2001 From: wjddn2165 Date: Mon, 19 Aug 2024 10:51:01 +0900 Subject: [PATCH 01/14] =?UTF-8?q?feat:=20info,=20warn,=20error=20=EB=A1=9C?= =?UTF-8?q?=EA=B7=B8=EB=A5=BC=20=ED=8C=8C=EC=9D=BC=EB=A1=9C=20=EC=A0=80?= =?UTF-8?q?=EC=9E=A5=ED=95=98=EA=B3=A0,=20=EA=B0=81=20=EB=A1=9C=EA=B7=B8?= =?UTF-8?q?=EB=A7=88=EB=8B=A4=20=EC=8A=A4=EB=A0=88=EB=93=9C=20=EA=B3=A0?= =?UTF-8?q?=EC=9C=A0=20UUID=EB=A5=BC=20=EB=B6=99=EC=9D=B4=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20logback.xml=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Server/src/main/resources/logback.xml | 67 +++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 Server/src/main/resources/logback.xml diff --git a/Server/src/main/resources/logback.xml b/Server/src/main/resources/logback.xml new file mode 100644 index 00000000..2dabe840 --- /dev/null +++ b/Server/src/main/resources/logback.xml @@ -0,0 +1,67 @@ + + + + + %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg [%X{requestId}]%n + + + + + + logs/info.log + + logs/info.%d{yyyy-MM-dd}.log + 30 + + + %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg [%X{requestId}]%n + + + INFO + ACCEPT + DENY + + + + + + logs/error.log + + logs/error.%d{yyyy-MM-dd}.log + 30 + + + %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg [%X{requestId}]%n + + + ERROR + ACCEPT + DENY + + + + + + logs/warn.log + + logs/warn.%d{yyyy-MM-dd}.log + 30 + + + %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg [%X{requestId}]%n + + + WARN + ACCEPT + DENY + + + + + + + + + + + From 53aae82161288481426d99f228e24a6a65ff3029 Mon Sep 17 00:00:00 2001 From: wjddn2165 Date: Mon, 19 Aug 2024 10:51:38 +0900 Subject: [PATCH 02/14] =?UTF-8?q?chore:=20prometheus,=20actuator=20?= =?UTF-8?q?=EC=9D=98=EC=A1=B4=EC=84=B1=20=EC=B6=94=EA=B0=80=20=EB=B0=8F=20?= =?UTF-8?q?swagger=20=EB=B2=84=EC=A0=84=20=EC=97=85=EA=B7=B8=EB=A0=88?= =?UTF-8?q?=EC=9D=B4=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Server/build.gradle | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Server/build.gradle b/Server/build.gradle index edfad680..151cd640 100644 --- a/Server/build.gradle +++ b/Server/build.gradle @@ -34,8 +34,7 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-aop' implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE' - implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0' - + implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0") runtimeOnly 'com.h2database:h2' testImplementation 'org.springframework.boot:spring-boot-starter-test' @@ -49,6 +48,10 @@ dependencies { annotationProcessor('org.projectlombok:lombok') implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.13.3' + // prometheus + implementation 'org.springframework.boot:spring-boot-starter-actuator' + implementation 'io.micrometer:micrometer-registry-prometheus' + } tasks.named('test') { From 7b0544a1d9f11febf7b007d9f7fa117f3faed28c Mon Sep 17 00:00:00 2001 From: wjddn2165 Date: Mon, 19 Aug 2024 11:24:58 +0900 Subject: [PATCH 03/14] =?UTF-8?q?chore:=20=EC=B6=A9=EB=8F=8C=20=ED=95=B4?= =?UTF-8?q?=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/JGS/CasperEvent/global/config/WebConfig.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Server/src/main/java/JGS/CasperEvent/global/config/WebConfig.java b/Server/src/main/java/JGS/CasperEvent/global/config/WebConfig.java index a3252e98..696f0f9f 100644 --- a/Server/src/main/java/JGS/CasperEvent/global/config/WebConfig.java +++ b/Server/src/main/java/JGS/CasperEvent/global/config/WebConfig.java @@ -1,6 +1,7 @@ package JGS.CasperEvent.global.config; import JGS.CasperEvent.domain.event.service.adminService.AdminService; +import JGS.CasperEvent.global.interceptor.RequestInterceptor; import JGS.CasperEvent.global.jwt.filter.JwtAuthorizationFilter; import JGS.CasperEvent.global.jwt.filter.JwtUserFilter; import JGS.CasperEvent.global.jwt.filter.VerifyAdminFilter; @@ -15,6 +16,7 @@ import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @@ -93,5 +95,10 @@ public FilterRegistrationBean jwtAuthorizationFilter(JwtProvider provider, Objec return filterRegistrationBean; } + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(new RequestInterceptor()) + .addPathPatterns("/**"); // 모든 경로에 대해 인터셉터 적용 + } } From 6dba62abbda79e7b205c54a5334060b44a1a0470 Mon Sep 17 00:00:00 2001 From: wjddn2165 Date: Mon, 19 Aug 2024 10:52:57 +0900 Subject: [PATCH 04/14] =?UTF-8?q?chore:=20swaggerConfig=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../global/config/SwaggerConfig.java | 24 ------------------- 1 file changed, 24 deletions(-) delete mode 100644 Server/src/main/java/JGS/CasperEvent/global/config/SwaggerConfig.java diff --git a/Server/src/main/java/JGS/CasperEvent/global/config/SwaggerConfig.java b/Server/src/main/java/JGS/CasperEvent/global/config/SwaggerConfig.java deleted file mode 100644 index 388701b2..00000000 --- a/Server/src/main/java/JGS/CasperEvent/global/config/SwaggerConfig.java +++ /dev/null @@ -1,24 +0,0 @@ -package JGS.CasperEvent.global.config; - -import io.swagger.v3.oas.models.Components; -import io.swagger.v3.oas.models.OpenAPI; -import io.swagger.v3.oas.models.info.Info; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration -public class SwaggerConfig { - @Bean - public OpenAPI openAPI() { - return new OpenAPI() - .components(new Components()) - .info(apiInfo()); - } - - private Info apiInfo() { - return new Info() - .title("API Test") // API의 제목 - .description("Let's practice Swagger UI") // API에 대한 설명 - .version("1.0.0"); // API의 버전 - } -} From 53725d17280b65314814721b539bc7ccc4cc3ad7 Mon Sep 17 00:00:00 2001 From: wjddn2165 Date: Mon, 19 Aug 2024 10:54:34 +0900 Subject: [PATCH 05/14] =?UTF-8?q?chore:=20=EA=B3=B5=ED=86=B5=20application?= =?UTF-8?q?.yml=20=EC=97=90=20application=20name=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Server/src/main/resources/application.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Server/src/main/resources/application.yml b/Server/src/main/resources/application.yml index 19292914..3e49e647 100644 --- a/Server/src/main/resources/application.yml +++ b/Server/src/main/resources/application.yml @@ -1,4 +1,6 @@ spring: + application: + name: hybrid-jgs jpa: - show-sql: true + show-sql: false # 데이터베이스 설정은 공통으로 지정하지 않음 From 71eb9ec05056828dd7fe4a95cd3431c6eb29ddf1 Mon Sep 17 00:00:00 2001 From: wjddn2165 Date: Mon, 19 Aug 2024 10:55:00 +0900 Subject: [PATCH 06/14] =?UTF-8?q?chore:=20=EB=B0=B0=ED=8F=AC=ED=99=98?= =?UTF-8?q?=EA=B2=BD=EC=97=90=20prometheus=20=EC=84=A4=EC=A0=95=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Server/src/main/resources/application-prod.yml | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Server/src/main/resources/application-prod.yml b/Server/src/main/resources/application-prod.yml index c1da7f6f..5de00a76 100644 --- a/Server/src/main/resources/application-prod.yml +++ b/Server/src/main/resources/application-prod.yml @@ -34,4 +34,13 @@ client: url: ${CLIENT_URL} localUrl: ${LOCAL_CLIENT_URL} shortenUrlService: - url: ${SPRING_SERVER_URL} \ No newline at end of file + url: ${SPRING_SERVER_URL} + +management: + endpoints: + web: + exposure: + include: prometheus, health, info + metrics: + tags: + application: ${spring.application.name} From be9849f4f9ad794d944ead6ce9785e266897ff9b Mon Sep 17 00:00:00 2001 From: wjddn2165 Date: Mon, 19 Aug 2024 10:55:30 +0900 Subject: [PATCH 07/14] =?UTF-8?q?chore:=20=EC=95=A0=ED=94=8C=EB=A6=AC?= =?UTF-8?q?=EC=BC=80=EC=9D=B4=EC=85=98=20=EC=8B=A4=ED=96=89=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=EC=97=90=20@EnableScheduling=20=EC=95=A0?= =?UTF-8?q?=EB=85=B8=ED=85=8C=EC=9D=B4=EC=85=98=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/JGS/CasperEvent/CasperEventApplication.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Server/src/main/java/JGS/CasperEvent/CasperEventApplication.java b/Server/src/main/java/JGS/CasperEvent/CasperEventApplication.java index b5910477..4005d90b 100644 --- a/Server/src/main/java/JGS/CasperEvent/CasperEventApplication.java +++ b/Server/src/main/java/JGS/CasperEvent/CasperEventApplication.java @@ -3,9 +3,11 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.EnableAspectJAutoProxy; +import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableAspectJAutoProxy +@EnableScheduling public class CasperEventApplication { public static void main(String[] args) { From 13438823ee7dedbb215d9d75f333e6b6a0b7839f Mon Sep 17 00:00:00 2001 From: wjddn2165 Date: Mon, 19 Aug 2024 10:55:43 +0900 Subject: [PATCH 08/14] =?UTF-8?q?chore:=20=EC=98=A4=ED=83=80=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/JGS/CasperEvent/global/enums/CustomErrorCode.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Server/src/main/java/JGS/CasperEvent/global/enums/CustomErrorCode.java b/Server/src/main/java/JGS/CasperEvent/global/enums/CustomErrorCode.java index 8f49dcd3..1530b86f 100644 --- a/Server/src/main/java/JGS/CasperEvent/global/enums/CustomErrorCode.java +++ b/Server/src/main/java/JGS/CasperEvent/global/enums/CustomErrorCode.java @@ -5,7 +5,7 @@ @Getter public enum CustomErrorCode { NO_RUSH_EVENT("선착순 이벤트를 찾을 수 없습니다.", 404), - NO_LOTTERY_EVENT("선착순 이벤트를 찾을 수 없습니다.", 404), + NO_LOTTERY_EVENT("추첨 이벤트를 찾을 수 없습니다.", 404), NO_RUSH_EVENT_OPTION("해당 밸런스 게임 선택지를 찾을 수 없습니다.", 404), INVALID_PARAMETER("잘못된 파라미터 입력입니다.", 400), CASPERBOT_NOT_FOUND("배지를 찾을 수 없습니다.", 404), From 67b10e64f1079853b60d354589ebb4532f719ab2 Mon Sep 17 00:00:00 2001 From: wjddn2165 Date: Mon, 19 Aug 2024 10:56:26 +0900 Subject: [PATCH 09/14] =?UTF-8?q?chore:=20whiteList=20=EC=97=90=20/actuato?= =?UTF-8?q?r=20=EA=B4=80=EB=A0=A8=20url=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CasperEvent/global/jwt/filter/JwtAuthorizationFilter.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Server/src/main/java/JGS/CasperEvent/global/jwt/filter/JwtAuthorizationFilter.java b/Server/src/main/java/JGS/CasperEvent/global/jwt/filter/JwtAuthorizationFilter.java index 186b5154..cc086358 100644 --- a/Server/src/main/java/JGS/CasperEvent/global/jwt/filter/JwtAuthorizationFilter.java +++ b/Server/src/main/java/JGS/CasperEvent/global/jwt/filter/JwtAuthorizationFilter.java @@ -32,7 +32,8 @@ public class JwtAuthorizationFilter implements Filter { "/event/rush", "/event/lottery/caspers", "/admin/join", "/admin/auth", "/h2", "/h2/*", "/swagger-ui/*", "/v3/api-docs", "/v3/api-docs/*", - "/event/lottery", "/link/*", "/event/total" + "/event/lottery", "/link/*", "/event/total", + "/actuator", "/actuator/*" }; private final String[] blackListUris = new String[]{ "/event/rush/*", "/event/lottery/casperBot" From 48319efc64421cd9c5c51711fe11eb5d6bce16e7 Mon Sep 17 00:00:00 2001 From: wjddn2165 Date: Mon, 19 Aug 2024 10:57:06 +0900 Subject: [PATCH 10/14] =?UTF-8?q?fix:=20=EC=8A=A4=EC=BC=80=EC=A4=84?= =?UTF-8?q?=EB=A7=81=20=EC=9E=91=EC=97=85=EC=9D=84=200=EC=8B=9C=EC=97=90?= =?UTF-8?q?=20=EB=8F=99=EC=9E=91=ED=95=98=EB=8F=84=EB=A1=9D=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD=20=EB=B0=8F=20=EB=A1=9C=EA=B9=85=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/eventService/RushEventScheduler.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/Server/src/main/java/JGS/CasperEvent/domain/event/service/eventService/RushEventScheduler.java b/Server/src/main/java/JGS/CasperEvent/domain/event/service/eventService/RushEventScheduler.java index 0150eac1..901e846c 100644 --- a/Server/src/main/java/JGS/CasperEvent/domain/event/service/eventService/RushEventScheduler.java +++ b/Server/src/main/java/JGS/CasperEvent/domain/event/service/eventService/RushEventScheduler.java @@ -2,32 +2,36 @@ import JGS.CasperEvent.domain.event.dto.ResponseDto.rushEventResponseDto.RushEventResponseDto; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import java.time.LocalDate; +import java.time.LocalDateTime; @Service @EnableScheduling @RequiredArgsConstructor +@Slf4j public class RushEventScheduler { private final RushEventService rushEventService; private final RedisTemplate rushEventRedisTemplate; - // 매일 12시에 스케줄된 작업을 실행합니다. - @Scheduled(cron = "0 0 12 * * ?") + // 매일 0시 1분에 스케줄된 작업을 실행합니다. + @Scheduled(cron = "0 1 0 * * ?") public void fetchDailyEvents() { - // 오늘의 날짜를 구합니다. - LocalDate today = LocalDate.now(); - // EventService를 통해 오늘의 이벤트를 가져옵니다. - RushEventResponseDto todayEvent = rushEventService.getTodayRushEvent(today); + RushEventResponseDto todayEvent = rushEventService.getTodayRushEventFromRDB(); // 가져온 이벤트에 대한 추가 작업을 수행합니다. // 예: 캐싱, 로그 기록, 알림 발송 등 rushEventRedisTemplate.opsForValue().set("todayEvent", todayEvent); + + // 로그 출력 + log.info("선착순 이벤트 스케줄러 실행: {}", LocalDateTime.now()); + log.info("가져온 이벤트 날짜: {}", todayEvent.startDateTime()); } } From c85cdfe72f59dbcc8a1586f568be82697b607e37 Mon Sep 17 00:00:00 2001 From: wjddn2165 Date: Mon, 19 Aug 2024 10:57:44 +0900 Subject: [PATCH 11/14] =?UTF-8?q?chore:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=9D=B4=EB=A6=84=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20=EB=B0=8F=20=ED=95=A8=EC=88=98=20=EC=9D=B4=EB=A6=84?= =?UTF-8?q?=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/eventService/RushEventServiceTest.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Server/src/test/java/JGS/CasperEvent/domain/event/service/eventService/RushEventServiceTest.java b/Server/src/test/java/JGS/CasperEvent/domain/event/service/eventService/RushEventServiceTest.java index 2b60c572..42ad9cbe 100644 --- a/Server/src/test/java/JGS/CasperEvent/domain/event/service/eventService/RushEventServiceTest.java +++ b/Server/src/test/java/JGS/CasperEvent/domain/event/service/eventService/RushEventServiceTest.java @@ -42,7 +42,6 @@ class RushEventServiceTest { private RedisTemplate rushEventRedisTemplate; @Mock private RushOptionRepository rushOptionRepository; - @Mock private ValueOperations valueOperations; @@ -375,7 +374,7 @@ void getTodayRushEvent() { given(rushEventRepository.findByEventDate(today)).willReturn(List.of(rushEvent)); // when - RushEventResponseDto result = rushEventService.getTodayRushEvent(today); + RushEventResponseDto result = rushEventService.getTodayRushEventFromRDB(); // then assertNotNull(result); @@ -383,7 +382,7 @@ void getTodayRushEvent() { } @Test - @DisplayName("오늘의 선착순 이벤트 DB에서 가져오기 테스트 (Redis에 선착순 이벤트가 없는 경우)") + @DisplayName("오늘의 선착순 이벤트 DB에서 가져오기 테스트 (RDB에 선착순 이벤트가 없는 경우)") void getTodayRushEvent2() { // given LocalDate today = LocalDate.now(); @@ -391,7 +390,7 @@ void getTodayRushEvent2() { // when & then CustomException exception = assertThrows(CustomException.class, () -> - rushEventService.getTodayRushEvent(today) + rushEventService.getTodayRushEventFromRDB() ); assertEquals(CustomErrorCode.NO_RUSH_EVENT, exception.getErrorCode()); @@ -399,7 +398,7 @@ void getTodayRushEvent2() { } @Test - @DisplayName("오늘의 선착순 이벤트 DB에서 가져오기 테스트 (Redis에 선착순 이벤트가 2개 이상인 경우)") + @DisplayName("오늘의 선착순 이벤트 DB에서 가져오기 테스트 (RDB에 선착순 이벤트가 2개 이상인 경우)") void getTodayRushEvent3() { // given LocalDate today = LocalDate.now(); @@ -411,7 +410,7 @@ void getTodayRushEvent3() { // when & then CustomException exception = assertThrows(CustomException.class, () -> - rushEventService.getTodayRushEvent(today) + rushEventService.getTodayRushEventFromRDB() ); assertEquals(CustomErrorCode.MULTIPLE_RUSH_EVENTS_FOUND, exception.getErrorCode()); @@ -419,6 +418,7 @@ void getTodayRushEvent3() { } @Test + @DisplayName("선착순 이벤트 테스트 API 테스트") void setTodayEventToRedis() { // given RushEvent rushEvent = new RushEvent(); @@ -438,6 +438,7 @@ void setTodayEventToRedis() { } @Test + @DisplayName("오늘의 선착순 이벤트의 선택지 조회 테스트") void getTodayRushEventOptions() { // given RushEventResponseDto todayEvent = new RushEventResponseDto( From 019f9faea69749ef8f5572dd54e9b6c21edb3918 Mon Sep 17 00:00:00 2001 From: wjddn2165 Date: Mon, 19 Aug 2024 10:58:16 +0900 Subject: [PATCH 12/14] =?UTF-8?q?fix:=20=ED=95=A8=EC=88=98=20=EC=9D=B4?= =?UTF-8?q?=EB=A6=84=20=EB=B3=80=EA=B2=BD=20=EB=B0=8F=20Redis=EC=97=90=20?= =?UTF-8?q?=EC=98=A4=EB=8A=98=EC=9D=98=20=EC=9D=B4=EB=B2=A4=ED=8A=B8?= =?UTF-8?q?=EA=B0=80=20=EC=97=86=EC=9D=84=20=EA=B2=BD=EC=9A=B0=20DB=20?= =?UTF-8?q?=EC=97=90=EC=84=9C=20=EA=B0=80=EC=A0=B8=EC=98=A4=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../eventService/RushEventService.java | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/Server/src/main/java/JGS/CasperEvent/domain/event/service/eventService/RushEventService.java b/Server/src/main/java/JGS/CasperEvent/domain/event/service/eventService/RushEventService.java index aba48962..5e375633 100644 --- a/Server/src/main/java/JGS/CasperEvent/domain/event/service/eventService/RushEventService.java +++ b/Server/src/main/java/JGS/CasperEvent/domain/event/service/eventService/RushEventService.java @@ -35,7 +35,7 @@ public class RushEventService { @Transactional public RushEventListResponseDto getAllRushEvents() { // 오늘의 선착순 이벤트 꺼내오기 - RushEventResponseDto todayEvent = getTodayRushEvent(); + RushEventResponseDto todayEvent = getTodayRushEventFromRedis(); // DB에서 모든 RushEvent 가져오기 List rushEventList = rushEventRepository.findAll(); @@ -67,13 +67,13 @@ public RushEventListResponseDto getAllRushEvents() { // 응모 여부 조회 public boolean isExists(String userId) { - Long todayEventId = getTodayRushEvent().rushEventId(); + Long todayEventId = getTodayRushEventFromRedis().rushEventId(); return rushParticipantsRepository.existsByRushEvent_RushEventIdAndBaseUser_Id(todayEventId, userId); } @Transactional public void apply(BaseUser user, int optionId) { - Long todayEventId = getTodayRushEvent().rushEventId(); + Long todayEventId = getTodayRushEventFromRedis().rushEventId(); // 이미 응모한 회원인지 검증 if (rushParticipantsRepository.existsByRushEvent_RushEventIdAndBaseUser_Id(todayEventId, user.getId())) { @@ -90,7 +90,7 @@ public void apply(BaseUser user, int optionId) { // 진행중인 게임의 응모 비율 반환 public RushEventRateResponseDto getRushEventRate(BaseUser user) { - Long todayEventId = getTodayRushEvent().rushEventId(); + Long todayEventId = getTodayRushEventFromRedis().rushEventId(); Optional optionId = rushParticipantsRepository.getOptionIdByUserId(user.getId()); long leftOptionCount = rushParticipantsRepository.countByRushEvent_RushEventIdAndOptionId(todayEventId, 1); long rightOptionCount = rushParticipantsRepository.countByRushEvent_RushEventIdAndOptionId(todayEventId, 2); @@ -104,7 +104,7 @@ public RushEventRateResponseDto getRushEventRate(BaseUser user) { // 해당 요청은 무조건 응모한 유저일 때만 요청 가능하다고 가정 @Transactional public RushEventResultResponseDto getRushEventResult(BaseUser user) { - RushEventResponseDto todayRushEvent = getTodayRushEvent(); + RushEventResponseDto todayRushEvent = getTodayRushEventFromRedis(); // 최종 선택 비율을 조회 // TODO: 레디스에 캐시 @@ -145,9 +145,10 @@ public RushEventResultResponseDto getRushEventResult(BaseUser user) { return new RushEventResultResponseDto(rushEventRateResponseDto, rank, totalParticipants, isWinner); } - @Transactional // 오늘의 이벤트를 DB에 꺼내서 반환 - public RushEventResponseDto getTodayRushEvent(LocalDate today) { + public RushEventResponseDto getTodayRushEventFromRDB() { + LocalDate today = LocalDate.now(); + // 오늘 날짜에 해당하는 모든 이벤트 꺼내옴 List rushEventList = rushEventRepository.findByEventDate(today); @@ -163,10 +164,13 @@ public RushEventResponseDto getTodayRushEvent(LocalDate today) { } // 오늘의 이벤트 꺼내오기 - private RushEventResponseDto getTodayRushEvent() { + private RushEventResponseDto getTodayRushEventFromRedis() { RushEventResponseDto todayEvent = rushEventRedisTemplate.opsForValue().get("todayEvent"); + + // Redis에 오늘의 이벤트가 없으면 DB에서 가져와서 Redis에 저장한 후 반환. if (todayEvent == null) { - throw new CustomException("오늘의 이벤트가 Redis에 없습니다.", CustomErrorCode.TODAY_RUSH_EVENT_NOT_FOUND); + todayEvent = getTodayRushEventFromRDB(); + rushEventRedisTemplate.opsForValue().set("todayEvent", todayEvent); } return todayEvent; @@ -234,7 +238,7 @@ public void setTodayEventToRedis() { // 오늘의 이벤트 옵션 정보를 반환 public MainRushEventOptionsResponseDto getTodayRushEventOptions() { - RushEventResponseDto todayEvent = getTodayRushEvent(); + RushEventResponseDto todayEvent = getTodayRushEventFromRedis(); Set options = todayEvent.options(); RushEventOptionResponseDto leftOption = options.stream() @@ -255,7 +259,7 @@ public MainRushEventOptionsResponseDto getTodayRushEventOptions() { public ResultRushEventOptionResponseDto getRushEventOptionResult(int optionId) { Position position = Position.of(optionId); - RushEventResponseDto todayEvent = getTodayRushEvent(); + RushEventResponseDto todayEvent = getTodayRushEventFromRedis(); Set options = todayEvent.options(); if (options.size() != 2) { From b0132e743011f0ba12b769bf9339a43c7492a574 Mon Sep 17 00:00:00 2001 From: wjddn2165 Date: Mon, 19 Aug 2024 10:58:30 +0900 Subject: [PATCH 13/14] =?UTF-8?q?chore:=20=ED=95=A8=EC=88=98=20=EC=9D=B4?= =?UTF-8?q?=EB=A6=84=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../event/service/eventService/RushEventSchedulerTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Server/src/test/java/JGS/CasperEvent/domain/event/service/eventService/RushEventSchedulerTest.java b/Server/src/test/java/JGS/CasperEvent/domain/event/service/eventService/RushEventSchedulerTest.java index f1b05513..1ef8b649 100644 --- a/Server/src/test/java/JGS/CasperEvent/domain/event/service/eventService/RushEventSchedulerTest.java +++ b/Server/src/test/java/JGS/CasperEvent/domain/event/service/eventService/RushEventSchedulerTest.java @@ -46,13 +46,13 @@ void fetchDailyEvents_ShouldCacheTodayEventInRedis() { ); given(rushEventRedisTemplate.opsForValue()).willReturn(valueOperations); - given(rushEventService.getTodayRushEvent(today)).willReturn(todayEvent); + given(rushEventService.getTodayRushEventFromRDB()).willReturn(todayEvent); // when rushEventScheduler.fetchDailyEvents(); // then - verify(rushEventService).getTodayRushEvent(today); + verify(rushEventService).getTodayRushEventFromRDB(); verify(rushEventRedisTemplate.opsForValue()).set("todayEvent", todayEvent); } } From f62d18cd1e73c63aab0ff7c761a41a0ba73fc0ad Mon Sep 17 00:00:00 2001 From: wjddn2165 Date: Mon, 19 Aug 2024 10:59:05 +0900 Subject: [PATCH 14/14] =?UTF-8?q?feat:=20=EB=AA=A8=EB=93=A0=20=EC=9A=94?= =?UTF-8?q?=EC=B2=AD=EC=97=90=20=EB=8C=80=ED=95=B4=EC=84=9C=20=EB=AA=A8?= =?UTF-8?q?=EB=93=A0=20=EC=8A=A4=EB=A0=88=EB=93=9C=EA=B0=80=20=EA=B3=A0?= =?UTF-8?q?=EC=9C=A0=ED=95=9C=20UUID=20=EB=A5=BC=20=EB=A1=9C=EA=B7=B8?= =?UTF-8?q?=EC=97=90=20=EC=B0=8D=EC=9D=84=20=EC=88=98=20=EC=9E=88=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20RequestInterceptor=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../interceptor/RequestInterceptor.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Server/src/main/java/JGS/CasperEvent/global/interceptor/RequestInterceptor.java diff --git a/Server/src/main/java/JGS/CasperEvent/global/interceptor/RequestInterceptor.java b/Server/src/main/java/JGS/CasperEvent/global/interceptor/RequestInterceptor.java new file mode 100644 index 00000000..1aef7a80 --- /dev/null +++ b/Server/src/main/java/JGS/CasperEvent/global/interceptor/RequestInterceptor.java @@ -0,0 +1,46 @@ +package JGS.CasperEvent.global.interceptor; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import lombok.NonNull; +import lombok.extern.slf4j.Slf4j; +import org.slf4j.MDC; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.HandlerInterceptor; + +import java.util.UUID; + +@Component +@Slf4j +public class RequestInterceptor implements HandlerInterceptor { + + private static final String REQUEST_ID = "requestId"; + + @Override + public boolean preHandle(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull Object handler) { + // UUID를 사용해 고유한 requestId 생성 + String requestId = UUID.randomUUID().toString(); + + // MDC에 requestId 추가하여 로깅 시 포함되도록 설정 + MDC.put(REQUEST_ID, requestId); + + String requestURI = request.getMethod() + " " + request.getRequestURL(); + + String queryString = request.getQueryString(); + + log.info("Request [{}{}]", requestURI, queryString); + + // 요청의 헤더에 requestId 추가 (선택 사항) + response.addHeader(REQUEST_ID, requestId); + + return true; // 다음 인터셉터나 컨트롤러로 요청 전달 + } + + @Override + public void afterCompletion(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull Object handler, Exception ex) { + log.info("Response {} [{}]", response.getStatus(), handler); + + // 요청이 완료된 후 MDC에서 requestId 제거 + MDC.remove(REQUEST_ID); + } +} \ No newline at end of file